~ubuntu-branches/ubuntu/trusty/libqapt/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/upstream_Prevent-crashes-on-source-entries-that-contain-a-lin.patch/tests/sourceslisttest.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2014-04-24 10:33:17 UTC
  • Revision ID: package-import@ubuntu.com-20140424103317-ybl7pw5qppsmorgl
Tags: 2.1.70-0ubuntu4.1
Add upstream_Prevent-crashes-on-source-entries-that-contain-a-lin.patch
from upstream git to resolve a crash when parsing source entries
denoting a comment but not actually containing anything other than hash
chracters.
LP: #1312043
(this commit is not part of upstream's 2.2.0 tarball and therefore is not
imported as part of the upstream_git patch set)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * sourceslisttest - This is a test program for libqapt
 
3
 * Copyright 2013  Michael D. Stemle <themanchicken@gmail.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License as
 
7
 * published by the Free Software Foundation; either version 2 of
 
8
 * the License or (at your option) version 3 or any later version
 
9
 * accepted by the membership of KDE e.V. (or its successor approved
 
10
 * by the membership of KDE e.V.), which shall act as a proxy
 
11
 * defined in Section 14 of version 3 of the license.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 *
 
21
 */
 
22
#include "../tests/sourceslisttest.h"
 
23
 
 
24
#include <QtTest/QtTest>
 
25
 
 
26
#include <apt-pkg/init.h>
 
27
 
 
28
QTEST_MAIN(SourcesListTest);
 
29
 
 
30
// Verification routines
 
31
void SourcesListTest::verifySourceEntry(
 
32
    const QString           &label,
 
33
    const QApt::SourceEntry &entry,
 
34
    const QString           &type,
 
35
    const QString           &uri,
 
36
    const QString           &dist,
 
37
    const QString           &components,
 
38
    const QString           &archs,
 
39
    const bool              isEnabled,
 
40
    const bool              isValid
 
41
)
 
42
{
 
43
    // Verify type...
 
44
    QVERIFY2(
 
45
        entry.type().compare(type) == 0,
 
46
        qPrintable(
 
47
            label
 
48
            + ": The type isn't \""+ type +"\", it's \""
 
49
            + entry.type()
 
50
            + "\"!"
 
51
        )
 
52
    );
 
53
    
 
54
    // Verify URI...
 
55
    QVERIFY2(
 
56
        entry.uri().compare(uri) == 0,
 
57
        qPrintable(
 
58
            label
 
59
            + ": The URI isn't \""+ uri +"\", it's \""
 
60
            + entry.uri()
 
61
            + "\"!"
 
62
        )
 
63
    );
 
64
    
 
65
    // Verify dist...
 
66
    QVERIFY2(
 
67
        entry.dist().compare(dist) == 0,
 
68
        qPrintable(
 
69
            label
 
70
            + ": The distro isn't \""+ dist +"\", it's \""
 
71
            + entry.dist()
 
72
            + "\"!"
 
73
        )
 
74
    );
 
75
    
 
76
    // Verify components...
 
77
    QVERIFY2(
 
78
        entry.components().join("!").compare(components) == 0,
 
79
        qPrintable(
 
80
            label
 
81
            + ": The components (joined with !) aren't \""+ components +"\", they're \""
 
82
            + entry.components().join("!")
 
83
            + "\"!"
 
84
        )
 
85
    );
 
86
    
 
87
    // Verify architectures...
 
88
    QVERIFY2(
 
89
        entry.architectures().join("!").compare(archs) == 0,
 
90
        qPrintable(
 
91
            label
 
92
            + ": The archs (joined with !) aren't \""+ archs +"\", they're \""
 
93
            + entry.architectures().join("!")
 
94
            + "\"!"
 
95
        )
 
96
    );
 
97
    
 
98
    // Verify isEnabled...
 
99
    const char *isEnabledMsg = (isEnabled)
 
100
        ? "I was expecting this entry to be enabled, but it wasn't."
 
101
        : "I was expecting this entry to be disabled, but it wasn't.";
 
102
    QVERIFY2(
 
103
        entry.isEnabled() == isEnabled,
 
104
        qPrintable(label+": "+isEnabledMsg)
 
105
    );
 
106
    
 
107
    // Verify isValid...
 
108
    const char *isValidMsg = (isValid)
 
109
        ? "I was expecting this entry to be valid, but it wasn't."
 
110
        : "I was expecting this entry to be invalid, but it wasn't.";
 
111
    QVERIFY2(
 
112
        entry.isValid() == isValid,
 
113
        qPrintable(label+": "+isValidMsg)
 
114
    );
 
115
}
 
116
 
 
117
void SourcesListTest::initTestCase()
 
118
{
 
119
    pkgInitConfig(*_config);
 
120
    
 
121
    // Called before the first testfunction is executed
 
122
    QString cwd = QCoreApplication::applicationDirPath();
 
123
    sampleSourcesHasOneFile = QStringList(QString(cwd+"/data/test1.list"));
 
124
    sampleSourcesHasTwoFiles = QStringList(
 
125
        {
 
126
            QString(cwd+"/data/test1.list"),
 
127
            QString(cwd+"/data/test2.list")
 
128
        }
 
129
    );
 
130
    sampleSourcesHasDuplicateFiles = QStringList(
 
131
        {
 
132
            QString(cwd+"/data/test1.list"),
 
133
            QString(cwd+"/data/test1.list"),
 
134
            QString(cwd+"/data/test2.list")
 
135
        }
 
136
    );
 
137
    
 
138
    
 
139
    outputFile = QString(cwd+"/data/write_test.list");
 
140
    dummyFile = QString(cwd+"/data/dummy_file.list");
 
141
}
 
142
 
 
143
void SourcesListTest::cleanupTestCase()
 
144
{
 
145
    // Called after the last testfunction was executed
 
146
    
 
147
    // Let's trash the written file.
 
148
    QVERIFY2(
 
149
        QFile::remove(outputFile),
 
150
        qPrintable(
 
151
            "Unable to remove file \""
 
152
            + outputFile
 
153
            + "\"."
 
154
        )
 
155
    );
 
156
}
 
157
 
 
158
void SourcesListTest::init()
 
159
{
 
160
    // Called before each testfunction is executed
 
161
}
 
162
 
 
163
void SourcesListTest::cleanup()
 
164
{
 
165
    // Called after every testfunction
 
166
}
 
167
 
 
168
void SourcesListTest::testConstructor()
 
169
{
 
170
    QObject parentObject;
 
171
    parentObject.setProperty("propertyName", QString("propertyValue"));
 
172
    
 
173
    QApt::SourcesList subjectDefaultConstructor;
 
174
    QStringList shouldntBeEmpty = subjectDefaultConstructor.sourceFiles();
 
175
    QVERIFY2(
 
176
        subjectDefaultConstructor.parent() == 0,
 
177
        "The default constructor doesn't have a zero parent?!"
 
178
    );
 
179
    QVERIFY2(
 
180
        shouldntBeEmpty.count() > 0,
 
181
        qPrintable(
 
182
            "The default constructor should have given us an empty file list, but it didn't. I got these: "
 
183
            + shouldntBeEmpty.join("!")
 
184
            + " which is "
 
185
            + QString::number(shouldntBeEmpty.count())
 
186
        )
 
187
    );
 
188
    int previousCount = subjectDefaultConstructor.entries().count();
 
189
    subjectDefaultConstructor.reload();
 
190
    QVERIFY2(
 
191
        previousCount == subjectDefaultConstructor.entries().count(),
 
192
        qPrintable(
 
193
            "The default constructor should have given us the same number of entries as the subsequent reload() call. I had "
 
194
            + QString::number(previousCount)
 
195
            + " and now I have "
 
196
            + QString::number(subjectDefaultConstructor.entries().count())
 
197
        )
 
198
    );
 
199
    
 
200
    QApt::SourcesList subjectParentOnly(&parentObject);
 
201
    shouldntBeEmpty = subjectParentOnly.sourceFiles();
 
202
    QVERIFY2(
 
203
        subjectParentOnly.parent() == &parentObject,
 
204
        "The parent-only constructor parent isn't the one we sent in?!"
 
205
        
 
206
    );
 
207
    QVERIFY2(
 
208
        shouldntBeEmpty.count() > 0,
 
209
        "The parent-only constructor should have given us sources anyway, using the default list."
 
210
    );
 
211
    
 
212
    QApt::SourcesList subjectListOnly(0, sampleSourcesHasTwoFiles);
 
213
    shouldntBeEmpty = subjectListOnly.sourceFiles();
 
214
    QVERIFY2(
 
215
        subjectListOnly.parent() == 0,
 
216
        "The list-only constructor doesn't have a zero parent?!"
 
217
    );
 
218
    QVERIFY2(
 
219
        shouldntBeEmpty.count() == 2,
 
220
        "The list-only constructor should have given us an empty file list, but it didn't."
 
221
    );
 
222
 
 
223
    QApt::SourcesList subjectDuplicateListOnly(0, sampleSourcesHasDuplicateFiles);
 
224
    shouldntBeEmpty = subjectDuplicateListOnly.sourceFiles();
 
225
    QVERIFY2(
 
226
        subjectDuplicateListOnly.parent() == 0,
 
227
        "The list-only constructor doesn't have a zero parent?!"
 
228
    );
 
229
    QVERIFY2(
 
230
        shouldntBeEmpty.count() == 2,
 
231
        "The list-only constructor should have given us an empty file list, but it didn't."
 
232
    );
 
233
    
 
234
    QApt::SourcesList subjectParentAndList(&parentObject, sampleSourcesHasTwoFiles);
 
235
    shouldntBeEmpty = subjectListOnly.sourceFiles();
 
236
    QVERIFY2(
 
237
        subjectParentOnly.parent() == &parentObject,
 
238
        "The parent-and-list constructor parent isn't the one we sent in?!"
 
239
    );
 
240
    QVERIFY2(
 
241
        shouldntBeEmpty.count() == 2,
 
242
        "The parent-and-list constructor should have given us an empty file list, but it didn't."
 
243
    );
 
244
}
 
245
 
 
246
/*
 
247
 * Test plan:
 
248
 * 1. Load the sample data file
 
249
 * 2. Verify that we loaded the file using entries()
 
250
 * 3. Verify that we loaded the file using entries(filename)
 
251
 */
 
252
#define SOURCES_COUNT 12
 
253
#define TEST1_MD5     "327eec1a9e826859e478258e75c07fbb"
 
254
void SourcesListTest::testLoadSourcesOneFile()
 
255
{
 
256
    QVERIFY2(sampleSourcesHasOneFile.count() == 1, "Verify we have only one source...");
 
257
 
 
258
    QApt::SourcesList subject (0, sampleSourcesHasOneFile);
 
259
    
 
260
    QApt::SourceEntryList entries = subject.entries();
 
261
    qDebug() << "I have " << entries.count() << " entries.";
 
262
    for ( const QApt::SourceEntry &one : entries ) {
 
263
        qDebug() << "Entry " << one.toString();
 
264
    }
 
265
    
 
266
    // Since this test depends so much on the test1.list file,
 
267
    // let's do everything we can to make sure people know what it
 
268
    // should look like!
 
269
    QVERIFY2(
 
270
        entries.count() == SOURCES_COUNT,
 
271
        qPrintable(
 
272
            "We don't have "
 
273
            + QString::number(SOURCES_COUNT)
 
274
            + " entries, we have "
 
275
            + QString::number(entries.count())
 
276
            + "! Please check the file, the MD5 should be \""
 
277
            + TEST1_MD5
 
278
            + "\""
 
279
        )
 
280
    );
 
281
    
 
282
    // Now we're going to go through each of the lines in the file and verify that
 
283
    // it loaded properly...
 
284
    QVERIFY2(
 
285
        entries[0].toString().compare("## First test") == 0,
 
286
        qPrintable(
 
287
            "I was expecting \"## First test\" as the first line, but I got "
 
288
            + entries[0].toString()
 
289
        )
 
290
    );
 
291
    // A commented line is inactive and invalid
 
292
    verifySourceEntry(
 
293
        "Line #0",
 
294
        entries[0],
 
295
        QString::null,
 
296
        QString::null,
 
297
        QString::null,
 
298
        QString::null,
 
299
        QString::null,
 
300
        false,
 
301
        false
 
302
    );
 
303
    verifySourceEntry(
 
304
        "Line #1",
 
305
        entries[1],
 
306
        "deb",
 
307
        "http://apttest/ubuntu",
 
308
        "saucy",
 
309
        "partner",
 
310
        "",
 
311
        true,
 
312
        true
 
313
    );
 
314
    verifySourceEntry(
 
315
        "Line #2",
 
316
        entries[2],
 
317
        "deb-src",
 
318
        "http://apttest/ubuntu",
 
319
        "saucy",
 
320
        "partner",
 
321
        "",
 
322
        true,
 
323
        true
 
324
    );
 
325
    verifySourceEntry(
 
326
        "Line #3",
 
327
        entries[3],
 
328
        "deb",
 
329
        "http://apttest/ubuntu",
 
330
        "saucy",
 
331
        "contrib!main!partner",
 
332
        "",
 
333
        true,
 
334
        true
 
335
    );
 
336
    verifySourceEntry(
 
337
        "Line #4",
 
338
        entries[4],
 
339
        "deb",
 
340
        "http://apttest/ubuntu",
 
341
        "saucy",
 
342
        "partner",
 
343
        "i386",
 
344
        true,
 
345
        true
 
346
    );
 
347
    verifySourceEntry(
 
348
        "Line #5",
 
349
        entries[5],
 
350
        "deb",
 
351
        "http://apttest/ubuntu",
 
352
        "saucy",
 
353
        "partner",
 
354
        "i386!ppc",
 
355
        true,
 
356
        true
 
357
    );
 
358
    verifySourceEntry(
 
359
        "Line #6",
 
360
        entries[6],
 
361
        "deb",
 
362
        "http://apttest/ubuntu",
 
363
        "saucy",
 
364
        "contrib!main!partner",
 
365
        "i386!ppc",
 
366
        true,
 
367
        true
 
368
    );
 
369
    verifySourceEntry(
 
370
        "Line #7",
 
371
        entries[7],
 
372
        "deb",
 
373
        "https://apttest/ubuntu", // <-- Note, https this time
 
374
        "saucy",
 
375
        "contrib!main!partner",
 
376
        "i386!ppc",
 
377
        true,
 
378
        true
 
379
    );
 
380
    QVERIFY2(
 
381
        entries[8].toString().compare("") == 0,
 
382
        qPrintable(
 
383
            "I was expecting an empty string as the ninth line (index 8), but I got "
 
384
            + entries[8].toString()
 
385
        )
 
386
    );
 
387
    // An empty line isn't invalid
 
388
    verifySourceEntry(
 
389
        "Line #8",
 
390
        entries[8],
 
391
        QString::null,
 
392
        QString::null,
 
393
        QString::null,
 
394
        QString::null,
 
395
        QString::null,
 
396
        true,
 
397
        false
 
398
    );
 
399
    verifySourceEntry(
 
400
        "Line #9",
 
401
        entries[9],
 
402
        "deb",
 
403
        "http://apttest/ubuntu",
 
404
        "saucy",
 
405
        "contrib!main!partner",
 
406
        "i386!ppc",
 
407
        false, // <-- Note, disabled!
 
408
        true
 
409
    );
 
410
    verifySourceEntry(
 
411
        "Line #10",
 
412
        entries[10],
 
413
        "deb",
 
414
        "http://apttest/ubuntu",
 
415
        "saucy",
 
416
        "contrib!main!partner",
 
417
        "i386!ppc",
 
418
        false, // <-- Note, disabled!
 
419
        true
 
420
    );
 
421
    verifySourceEntry(
 
422
        "Line #11",
 
423
        entries[11],
 
424
        "deb",
 
425
        "http://apttest/ubuntu",
 
426
        "saucy",
 
427
        "contrib!main!partner",
 
428
        "i386!ppc",
 
429
        false, // <-- Note, disabled!
 
430
        true
 
431
    );
 
432
}
 
433
 
 
434
#define SOURCES_COUNT_2   3
 
435
#define ALL_SOURCES_COUNT (SOURCES_COUNT + SOURCES_COUNT_2)
 
436
#define TEST2_MD5         "af99c3d972146a82c7af125c2022c581"
 
437
void SourcesListTest::testLoadSourcesManyFiles()
 
438
{
 
439
    QVERIFY2(sampleSourcesHasTwoFiles.count() == 2,
 
440
        qPrintable(
 
441
            "We don't have the 2 files I was expecting, we have "
 
442
            + QString::number(sampleSourcesHasTwoFiles.count())
 
443
        )
 
444
    );
 
445
 
 
446
    QApt::SourcesList subject (0, sampleSourcesHasTwoFiles);
 
447
    
 
448
    QApt::SourceEntryList entries = subject.entries();
 
449
    qDebug() << "I have " << entries.count() << " entries.";
 
450
    for ( const QApt::SourceEntry &one : entries ) {
 
451
        qDebug() << one.file() << ": Entry " << one.toString();
 
452
    }
 
453
    
 
454
    // Since this test depends so much on the test1.list file,
 
455
    // let's do everything we can to make sure people know what it
 
456
    // should look like!
 
457
    QVERIFY2(
 
458
        entries.count() == ALL_SOURCES_COUNT,
 
459
        qPrintable(
 
460
            "We don't have "
 
461
            + QString::number(ALL_SOURCES_COUNT)
 
462
            + " entries, we have "
 
463
            + QString::number(entries.count())
 
464
            + "! Please check the files test1.list and test2.list, the MD5's should be \""
 
465
            + TEST1_MD5
 
466
            + "\" and \""
 
467
            + TEST2_MD5
 
468
            + "\" respectively."
 
469
        )
 
470
    );
 
471
    
 
472
    // Verify that grabbing each source by file returns the correct number of records.
 
473
    QApt::SourceEntryList test1 = subject.entries(sampleSourcesHasTwoFiles[0]);
 
474
    QApt::SourceEntryList test2 = subject.entries(sampleSourcesHasTwoFiles[1]);    
 
475
    QVERIFY2(
 
476
        test1.count() == SOURCES_COUNT,
 
477
        qPrintable(
 
478
            sampleSourcesHasTwoFiles[0]
 
479
            + "\nLooks like test1.list doesn't have "
 
480
            + QString::number(SOURCES_COUNT)
 
481
            + " entries afterall; it has "
 
482
            + QString::number(test1.count())
 
483
            + " entries."
 
484
        )
 
485
    );
 
486
    QVERIFY2(
 
487
        test2.count() == SOURCES_COUNT_2,
 
488
        qPrintable(
 
489
            sampleSourcesHasTwoFiles[0]
 
490
            + "\nLooks like test2.list doesn't have "
 
491
            + QString::number(SOURCES_COUNT_2)
 
492
            + " entries afterall; it has "
 
493
            + QString::number(test1.count())
 
494
            + " entries."
 
495
        )
 
496
    );
 
497
}
 
498
 
 
499
void SourcesListTest::testAddSource()
 
500
{
 
501
    QStringList outfilesListJustOne (dummyFile);
 
502
    QApt::SourcesList subjectSingleEntry(0, outfilesListJustOne);
 
503
    
 
504
    QVERIFY2(
 
505
        subjectSingleEntry.entries().count() == 0,
 
506
        qPrintable(
 
507
            "I expected to have a new, empty file with zero entries. I found "
 
508
            + QString::number(subjectSingleEntry.entries().count())
 
509
            + " entries."
 
510
        )
 
511
    );
 
512
    
 
513
    QApt::SourceEntry entryOne;
 
514
    entryOne.setType("deb");
 
515
    entryOne.setUri("https://foo.com/bar");
 
516
    entryOne.setComponents({"main"});
 
517
    entryOne.setDist("saucy");
 
518
    entryOne.setEnabled(true);
 
519
    // Verify the item we just created...
 
520
    verifySourceEntry(
 
521
        "New Entry #0",
 
522
        entryOne,
 
523
        "deb",
 
524
        "https://foo.com/bar",
 
525
        "saucy",
 
526
        "main",
 
527
        QString::null,
 
528
        true,
 
529
        true
 
530
    );
 
531
    QApt::SourceEntry entryTwo;
 
532
    entryTwo.setType("deb");
 
533
    entryTwo.setUri("https://foo.com/bar2");
 
534
    entryTwo.setComponents({"main"});
 
535
    entryTwo.setDist("saucy");
 
536
    entryTwo.setEnabled(true);
 
537
    
 
538
    // Verify the item we just created...
 
539
    verifySourceEntry(
 
540
        "New Entry #1",
 
541
        entryTwo,
 
542
        "deb",
 
543
        "https://foo.com/bar2",
 
544
        "saucy",
 
545
        "main",
 
546
        QString::null,
 
547
        true,
 
548
        true
 
549
    );
 
550
    
 
551
    subjectSingleEntry.addEntry(entryOne);
 
552
    QVERIFY2(
 
553
        subjectSingleEntry.entries().count() == 1,
 
554
        qPrintable(
 
555
            "I expected to have a single entry now. I found "
 
556
            + QString::number(subjectSingleEntry.entries().count())
 
557
            + " entries."
 
558
        )
 
559
    );
 
560
    
 
561
    QVERIFY2(
 
562
        subjectSingleEntry.containsEntry(entryOne),
 
563
        qPrintable(
 
564
            "I totally thought I had an entry \""
 
565
            + entryOne.toString()
 
566
            + "\", but I don't."
 
567
        )
 
568
    );
 
569
    
 
570
    QApt::SourcesList subjectMultipleFiles(0, sampleSourcesHasTwoFiles);
 
571
    QVERIFY2(
 
572
        subjectMultipleFiles.entries().count() == ALL_SOURCES_COUNT,
 
573
        qPrintable(
 
574
            "I expected to have both files 1 and 2 loaded with "
 
575
            + QString::number(ALL_SOURCES_COUNT)
 
576
            + " entries. I found "
 
577
            + QString::number(subjectMultipleFiles.entries().count())
 
578
            + " entries."
 
579
        )
 
580
    );
 
581
    subjectMultipleFiles.addEntry(entryOne);
 
582
    QVERIFY2(
 
583
        subjectMultipleFiles.entries().count() == ALL_SOURCES_COUNT+1,
 
584
        qPrintable(
 
585
            "I expected to have a new, empty file with "
 
586
            + QString::number(ALL_SOURCES_COUNT+1)
 
587
            + " entries. I found "
 
588
            + QString::number(subjectMultipleFiles.entries().count())
 
589
            + " entries."
 
590
        )
 
591
    );
 
592
}
 
593
 
 
594
void SourcesListTest::testRemoveSource()
 
595
{
 
596
    QStringList outfilesListJustOne (dummyFile);
 
597
    QApt::SourcesList subject(0, outfilesListJustOne);
 
598
    QApt::SourceEntry entryOne;
 
599
    QApt::SourceEntry entryTwo;
 
600
 
 
601
    subject.reload();
 
602
    
 
603
    // Construct our two sources
 
604
    entryOne.setType("deb");
 
605
    entryOne.setUri("https://foo.com/bar");
 
606
    entryOne.setComponents({"main"});
 
607
    entryOne.setDist("saucy");
 
608
    entryOne.setEnabled(true);
 
609
    entryTwo.setType("deb");
 
610
    entryTwo.setUri("https://foo.com/bar2");
 
611
    entryTwo.setComponents({"main"});
 
612
    entryTwo.setDist("saucy");
 
613
    entryTwo.setEnabled(true);
 
614
    
 
615
    subject.addEntry(entryOne);
 
616
    subject.addEntry(entryTwo);
 
617
    
 
618
    QVERIFY2(
 
619
        subject.entries().count() == 2,
 
620
        qPrintable(
 
621
            "I expected to have 2 sources. I found "
 
622
            + QString::number(subject.entries().count())
 
623
            + " entries."
 
624
        )
 
625
    );
 
626
    
 
627
    subject.removeEntry(entryOne);
 
628
    QVERIFY2(
 
629
        subject.entries().count() == 1,
 
630
        qPrintable(
 
631
            "Now, I expect to have 1 sources. I found "
 
632
            + QString::number(subject.entries().count())
 
633
            + " entries."
 
634
        )
 
635
    );    
 
636
}
 
637
 
 
638
void SourcesListTest::testSaveSources()
 
639
{
 
640
    QStringList outfilesListJustOne (outputFile);
 
641
    QApt::SourcesList subject(0, outfilesListJustOne);
 
642
    QApt::SourceEntry entryOne;
 
643
    QApt::SourceEntry entryTwo;
 
644
 
 
645
    // Construct our two sources
 
646
    entryOne.setType("deb");
 
647
    entryOne.setUri("https://foo.com/bar");
 
648
    entryOne.setComponents({"main"});
 
649
    entryOne.setDist("saucy");
 
650
    entryOne.setEnabled(true);
 
651
    entryTwo.setType("deb");
 
652
    entryTwo.setUri("https://foo.com/bar2");
 
653
    entryTwo.setComponents({"main"});
 
654
    entryTwo.setDist("saucy");
 
655
    entryTwo.setEnabled(true);
 
656
    
 
657
    subject.addEntry(entryOne);
 
658
    
 
659
    qDebug() << "Here's your sources list:\n" << subject.toString();
 
660
    
 
661
    QVERIFY2(
 
662
                subject.containsEntry(entryOne),
 
663
                qPrintable(
 
664
                    "I expected to have the entry "
 
665
                    + entryOne.toString()
 
666
                )
 
667
    );
 
668
    QVERIFY2(
 
669
        subject.entries().count() == 1,
 
670
        qPrintable(
 
671
            "I expected to have 1 sources. I found "
 
672
            + QString::number(subject.entries().count())
 
673
            + " entries."
 
674
        )
 
675
    );
 
676
    
 
677
    subject.save();
 
678
    
 
679
    QApt::SourcesList loadingAfterSave(0, outfilesListJustOne);
 
680
    QVERIFY2(
 
681
        loadingAfterSave.entries().count() == 1,
 
682
        qPrintable(
 
683
            "I expected to have 1 sources after loading. I found "
 
684
            + QString::number(loadingAfterSave.entries().count())
 
685
            + " entries."
 
686
        )
 
687
    );
 
688
 
 
689
    loadingAfterSave.addEntry(entryTwo);
 
690
    loadingAfterSave.save();
 
691
    QVERIFY2(
 
692
        loadingAfterSave.entries().count() == 2,
 
693
        qPrintable(
 
694
            "I expected to have 2 sources after adding another. I found "
 
695
            + QString::number(loadingAfterSave.entries().count())
 
696
            + " entries."
 
697
        )
 
698
    );
 
699
 
 
700
    QApt::SourcesList loadingAfterSecondSave(0, outfilesListJustOne);
 
701
    QVERIFY2(
 
702
        loadingAfterSave.entries().count() == 2,
 
703
        qPrintable(
 
704
            "I expected to have 2 sources after loading again. I found "
 
705
            + QString::number(loadingAfterSecondSave.entries().count())
 
706
            + " entries."
 
707
        )
 
708
    );
 
709
 
 
710
    loadingAfterSecondSave.save();
 
711
    QVERIFY2(
 
712
        loadingAfterSecondSave.entries().count() == 2,
 
713
        qPrintable(
 
714
            "I expected to have 2 sources after adding another. I found "
 
715
            + QString::number(loadingAfterSecondSave.entries().count())
 
716
            + " entries."
 
717
        )
 
718
    );
 
719
 
 
720
    QApt::SourcesList loadingAfterSecondSaveB(0, outfilesListJustOne);
 
721
    QVERIFY2(
 
722
        loadingAfterSave.entries().count() == 2,
 
723
        qPrintable(
 
724
            "I expected to have 2 sources after loading again. I found "
 
725
            + QString::number(loadingAfterSecondSaveB.entries().count())
 
726
            + " entries."
 
727
        )
 
728
    );
 
729
    
 
730
    loadingAfterSecondSave.removeEntry(entryOne);
 
731
    loadingAfterSecondSave.removeEntry(entryTwo);
 
732
    
 
733
    QVERIFY2(
 
734
        loadingAfterSecondSave.entries().count() == 0,
 
735
        qPrintable(
 
736
            "I expected to have 0 sources after removing the two I had previously added. I found "
 
737
            + QString::number(loadingAfterSecondSave.entries().count())
 
738
            + " entries."
 
739
        )
 
740
    );
 
741
    
 
742
    loadingAfterSecondSave.save();
 
743
    
 
744
    QApt::SourcesList loadingAfterThirdSave(0, outfilesListJustOne);
 
745
    QVERIFY2(
 
746
        loadingAfterThirdSave.entries().count() == 3,
 
747
        qPrintable(
 
748
            "I expected to have 3 sources (all the default comments) after saving following the remove. I found "
 
749
            + QString::number(loadingAfterThirdSave.entries().count())
 
750
            + " entries."
 
751
        )
 
752
    );
 
753
    
 
754
    loadingAfterSecondSave.reload();
 
755
    QVERIFY2(
 
756
        loadingAfterSecondSave.entries().count() == 3,
 
757
        qPrintable(
 
758
            "I expected to have 3 sources (all the default comments) after saving following the remove (using reload()). I found "
 
759
            + QString::number(loadingAfterSecondSave.entries().count())
 
760
            + " entries."
 
761
        )
 
762
    );
 
763
}
 
764
 
 
765
// #include "../t/sourceslisttest.moc"