2
* sourceslisttest - This is a test program for libqapt
3
* Copyright 2013 Michael D. Stemle <themanchicken@gmail.com>
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.
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.
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/>.
22
#include "../tests/sourceslisttest.h"
24
#include <QtTest/QtTest>
26
#include <apt-pkg/init.h>
28
QTEST_MAIN(SourcesListTest);
30
// Verification routines
31
void SourcesListTest::verifySourceEntry(
33
const QApt::SourceEntry &entry,
37
const QString &components,
45
entry.type().compare(type) == 0,
48
+ ": The type isn't \""+ type +"\", it's \""
56
entry.uri().compare(uri) == 0,
59
+ ": The URI isn't \""+ uri +"\", it's \""
67
entry.dist().compare(dist) == 0,
70
+ ": The distro isn't \""+ dist +"\", it's \""
76
// Verify components...
78
entry.components().join("!").compare(components) == 0,
81
+ ": The components (joined with !) aren't \""+ components +"\", they're \""
82
+ entry.components().join("!")
87
// Verify architectures...
89
entry.architectures().join("!").compare(archs) == 0,
92
+ ": The archs (joined with !) aren't \""+ archs +"\", they're \""
93
+ entry.architectures().join("!")
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.";
103
entry.isEnabled() == isEnabled,
104
qPrintable(label+": "+isEnabledMsg)
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.";
112
entry.isValid() == isValid,
113
qPrintable(label+": "+isValidMsg)
117
void SourcesListTest::initTestCase()
119
pkgInitConfig(*_config);
121
// Called before the first testfunction is executed
122
QString cwd = QCoreApplication::applicationDirPath();
123
sampleSourcesHasOneFile = QStringList(QString(cwd+"/data/test1.list"));
124
sampleSourcesHasTwoFiles = QStringList(
126
QString(cwd+"/data/test1.list"),
127
QString(cwd+"/data/test2.list")
130
sampleSourcesHasDuplicateFiles = QStringList(
132
QString(cwd+"/data/test1.list"),
133
QString(cwd+"/data/test1.list"),
134
QString(cwd+"/data/test2.list")
139
outputFile = QString(cwd+"/data/write_test.list");
140
dummyFile = QString(cwd+"/data/dummy_file.list");
143
void SourcesListTest::cleanupTestCase()
145
// Called after the last testfunction was executed
147
// Let's trash the written file.
149
QFile::remove(outputFile),
151
"Unable to remove file \""
158
void SourcesListTest::init()
160
// Called before each testfunction is executed
163
void SourcesListTest::cleanup()
165
// Called after every testfunction
168
void SourcesListTest::testConstructor()
170
QObject parentObject;
171
parentObject.setProperty("propertyName", QString("propertyValue"));
173
QApt::SourcesList subjectDefaultConstructor;
174
QStringList shouldntBeEmpty = subjectDefaultConstructor.sourceFiles();
176
subjectDefaultConstructor.parent() == 0,
177
"The default constructor doesn't have a zero parent?!"
180
shouldntBeEmpty.count() > 0,
182
"The default constructor should have given us an empty file list, but it didn't. I got these: "
183
+ shouldntBeEmpty.join("!")
185
+ QString::number(shouldntBeEmpty.count())
188
int previousCount = subjectDefaultConstructor.entries().count();
189
subjectDefaultConstructor.reload();
191
previousCount == subjectDefaultConstructor.entries().count(),
193
"The default constructor should have given us the same number of entries as the subsequent reload() call. I had "
194
+ QString::number(previousCount)
196
+ QString::number(subjectDefaultConstructor.entries().count())
200
QApt::SourcesList subjectParentOnly(&parentObject);
201
shouldntBeEmpty = subjectParentOnly.sourceFiles();
203
subjectParentOnly.parent() == &parentObject,
204
"The parent-only constructor parent isn't the one we sent in?!"
208
shouldntBeEmpty.count() > 0,
209
"The parent-only constructor should have given us sources anyway, using the default list."
212
QApt::SourcesList subjectListOnly(0, sampleSourcesHasTwoFiles);
213
shouldntBeEmpty = subjectListOnly.sourceFiles();
215
subjectListOnly.parent() == 0,
216
"The list-only constructor doesn't have a zero parent?!"
219
shouldntBeEmpty.count() == 2,
220
"The list-only constructor should have given us an empty file list, but it didn't."
223
QApt::SourcesList subjectDuplicateListOnly(0, sampleSourcesHasDuplicateFiles);
224
shouldntBeEmpty = subjectDuplicateListOnly.sourceFiles();
226
subjectDuplicateListOnly.parent() == 0,
227
"The list-only constructor doesn't have a zero parent?!"
230
shouldntBeEmpty.count() == 2,
231
"The list-only constructor should have given us an empty file list, but it didn't."
234
QApt::SourcesList subjectParentAndList(&parentObject, sampleSourcesHasTwoFiles);
235
shouldntBeEmpty = subjectListOnly.sourceFiles();
237
subjectParentOnly.parent() == &parentObject,
238
"The parent-and-list constructor parent isn't the one we sent in?!"
241
shouldntBeEmpty.count() == 2,
242
"The parent-and-list constructor should have given us an empty file list, but it didn't."
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)
252
#define SOURCES_COUNT 12
253
#define TEST1_MD5 "327eec1a9e826859e478258e75c07fbb"
254
void SourcesListTest::testLoadSourcesOneFile()
256
QVERIFY2(sampleSourcesHasOneFile.count() == 1, "Verify we have only one source...");
258
QApt::SourcesList subject (0, sampleSourcesHasOneFile);
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();
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
270
entries.count() == SOURCES_COUNT,
273
+ QString::number(SOURCES_COUNT)
274
+ " entries, we have "
275
+ QString::number(entries.count())
276
+ "! Please check the file, the MD5 should be \""
282
// Now we're going to go through each of the lines in the file and verify that
283
// it loaded properly...
285
entries[0].toString().compare("## First test") == 0,
287
"I was expecting \"## First test\" as the first line, but I got "
288
+ entries[0].toString()
291
// A commented line is inactive and invalid
307
"http://apttest/ubuntu",
318
"http://apttest/ubuntu",
329
"http://apttest/ubuntu",
331
"contrib!main!partner",
340
"http://apttest/ubuntu",
351
"http://apttest/ubuntu",
362
"http://apttest/ubuntu",
364
"contrib!main!partner",
373
"https://apttest/ubuntu", // <-- Note, https this time
375
"contrib!main!partner",
381
entries[8].toString().compare("") == 0,
383
"I was expecting an empty string as the ninth line (index 8), but I got "
384
+ entries[8].toString()
387
// An empty line isn't invalid
403
"http://apttest/ubuntu",
405
"contrib!main!partner",
407
false, // <-- Note, disabled!
414
"http://apttest/ubuntu",
416
"contrib!main!partner",
418
false, // <-- Note, disabled!
425
"http://apttest/ubuntu",
427
"contrib!main!partner",
429
false, // <-- Note, disabled!
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()
439
QVERIFY2(sampleSourcesHasTwoFiles.count() == 2,
441
"We don't have the 2 files I was expecting, we have "
442
+ QString::number(sampleSourcesHasTwoFiles.count())
446
QApt::SourcesList subject (0, sampleSourcesHasTwoFiles);
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();
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
458
entries.count() == ALL_SOURCES_COUNT,
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 \""
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]);
476
test1.count() == SOURCES_COUNT,
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())
487
test2.count() == SOURCES_COUNT_2,
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())
499
void SourcesListTest::testAddSource()
501
QStringList outfilesListJustOne (dummyFile);
502
QApt::SourcesList subjectSingleEntry(0, outfilesListJustOne);
505
subjectSingleEntry.entries().count() == 0,
507
"I expected to have a new, empty file with zero entries. I found "
508
+ QString::number(subjectSingleEntry.entries().count())
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...
524
"https://foo.com/bar",
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);
538
// Verify the item we just created...
543
"https://foo.com/bar2",
551
subjectSingleEntry.addEntry(entryOne);
553
subjectSingleEntry.entries().count() == 1,
555
"I expected to have a single entry now. I found "
556
+ QString::number(subjectSingleEntry.entries().count())
562
subjectSingleEntry.containsEntry(entryOne),
564
"I totally thought I had an entry \""
565
+ entryOne.toString()
570
QApt::SourcesList subjectMultipleFiles(0, sampleSourcesHasTwoFiles);
572
subjectMultipleFiles.entries().count() == ALL_SOURCES_COUNT,
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())
581
subjectMultipleFiles.addEntry(entryOne);
583
subjectMultipleFiles.entries().count() == ALL_SOURCES_COUNT+1,
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())
594
void SourcesListTest::testRemoveSource()
596
QStringList outfilesListJustOne (dummyFile);
597
QApt::SourcesList subject(0, outfilesListJustOne);
598
QApt::SourceEntry entryOne;
599
QApt::SourceEntry entryTwo;
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);
615
subject.addEntry(entryOne);
616
subject.addEntry(entryTwo);
619
subject.entries().count() == 2,
621
"I expected to have 2 sources. I found "
622
+ QString::number(subject.entries().count())
627
subject.removeEntry(entryOne);
629
subject.entries().count() == 1,
631
"Now, I expect to have 1 sources. I found "
632
+ QString::number(subject.entries().count())
638
void SourcesListTest::testSaveSources()
640
QStringList outfilesListJustOne (outputFile);
641
QApt::SourcesList subject(0, outfilesListJustOne);
642
QApt::SourceEntry entryOne;
643
QApt::SourceEntry entryTwo;
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);
657
subject.addEntry(entryOne);
659
qDebug() << "Here's your sources list:\n" << subject.toString();
662
subject.containsEntry(entryOne),
664
"I expected to have the entry "
665
+ entryOne.toString()
669
subject.entries().count() == 1,
671
"I expected to have 1 sources. I found "
672
+ QString::number(subject.entries().count())
679
QApt::SourcesList loadingAfterSave(0, outfilesListJustOne);
681
loadingAfterSave.entries().count() == 1,
683
"I expected to have 1 sources after loading. I found "
684
+ QString::number(loadingAfterSave.entries().count())
689
loadingAfterSave.addEntry(entryTwo);
690
loadingAfterSave.save();
692
loadingAfterSave.entries().count() == 2,
694
"I expected to have 2 sources after adding another. I found "
695
+ QString::number(loadingAfterSave.entries().count())
700
QApt::SourcesList loadingAfterSecondSave(0, outfilesListJustOne);
702
loadingAfterSave.entries().count() == 2,
704
"I expected to have 2 sources after loading again. I found "
705
+ QString::number(loadingAfterSecondSave.entries().count())
710
loadingAfterSecondSave.save();
712
loadingAfterSecondSave.entries().count() == 2,
714
"I expected to have 2 sources after adding another. I found "
715
+ QString::number(loadingAfterSecondSave.entries().count())
720
QApt::SourcesList loadingAfterSecondSaveB(0, outfilesListJustOne);
722
loadingAfterSave.entries().count() == 2,
724
"I expected to have 2 sources after loading again. I found "
725
+ QString::number(loadingAfterSecondSaveB.entries().count())
730
loadingAfterSecondSave.removeEntry(entryOne);
731
loadingAfterSecondSave.removeEntry(entryTwo);
734
loadingAfterSecondSave.entries().count() == 0,
736
"I expected to have 0 sources after removing the two I had previously added. I found "
737
+ QString::number(loadingAfterSecondSave.entries().count())
742
loadingAfterSecondSave.save();
744
QApt::SourcesList loadingAfterThirdSave(0, outfilesListJustOne);
746
loadingAfterThirdSave.entries().count() == 3,
748
"I expected to have 3 sources (all the default comments) after saving following the remove. I found "
749
+ QString::number(loadingAfterThirdSave.entries().count())
754
loadingAfterSecondSave.reload();
756
loadingAfterSecondSave.entries().count() == 3,
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())
765
// #include "../t/sourceslisttest.moc"