File indexing completed on 2024-09-15 04:26:46
0001 0002 #include "verifiertest.h" 0003 #include "../core/verifier.h" 0004 #include "../settings.h" 0005 0006 #include <QDebug> 0007 #include <QTemporaryDir> 0008 #include <QtTest> 0009 0010 typedef QPair<QString, QString> Checksum; 0011 typedef QPair<QString, PartialChecksums> Checksums; 0012 Q_DECLARE_METATYPE(Checksum) 0013 Q_DECLARE_METATYPE(QList<Checksum>) 0014 Q_DECLARE_METATYPE(Checksums) 0015 Q_DECLARE_METATYPE(QList<Checksums>) 0016 Q_DECLARE_METATYPE(Verifier::ChecksumStrength) 0017 Q_DECLARE_METATYPE(QList<KIO::fileoffset_t>) 0018 0019 VerfierTest::VerfierTest(QObject *parent) 0020 : QObject(parent) 0021 , m_supported(Verifier::supportedVerficationTypes()) 0022 { 0023 // create a file which will used in the test 0024 m_tempDir.reset(new QTemporaryDir(QDir::tempPath() + QStringLiteral("/kget_test"))); 0025 const QString path = m_tempDir->filePath("test.txt"); 0026 QFile file(path); 0027 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { 0028 qCritical() << "Creating file failed:" << path; 0029 abort(); 0030 } 0031 m_file = QUrl::fromLocalFile(path); 0032 0033 const QByteArray data("This is a test for the KGet Verifier class.\n"); 0034 const qint64 size = data.size(); 0035 for (int i = 0; i < 50000; ++i) { 0036 if (file.write(data) != size) { 0037 qCritical() << "Creating file failed:" << path; 0038 abort(); 0039 } 0040 } 0041 0042 qDebug() << "Supported types:" << m_supported; 0043 } 0044 0045 void VerfierTest::testChecksum() 0046 { 0047 QFETCH(QString, type); 0048 QFETCH(QString, checksum); 0049 0050 if (!m_supported.contains(type)) { 0051 return; 0052 } 0053 0054 QCOMPARE(Verifier::checksum(m_file, type, nullptr), checksum); 0055 } 0056 0057 void VerfierTest::testChecksum_data() 0058 { 0059 QTest::addColumn<QString>("type"); 0060 QTest::addColumn<QString>("checksum"); 0061 0062 QTest::newRow("md5 checksum") << "md5" 0063 << "1c3b1b627e4f236fdac8f6ab3ee160d1"; 0064 QTest::newRow("sha1 checksum") << "sha1" 0065 << "0a4886f5c6e45c5a879b0093a43cb06fc5568bc5"; 0066 QTest::newRow("sha256 checksum") << "sha256" 0067 << "9cf2cfe941569d8627dc3ede2897dea1c3949eb6bd124d3bca06a888492f955b"; 0068 QTest::newRow("sha384 checksum") << "sha384" 0069 << "a27d95917b3d3ff9145fd9a66cc9852f3a15e7ebac9e7112d4260e2747d45c89a949fe53d110ecc260e246f959ca30e9"; 0070 QTest::newRow("sha512 checksum") 0071 << "sha512" 0072 << "4c6d2020482ddb1c6f7275f40fb35084805f06a23e433420e46b794ef7ad9770d527c4a7f5dc50bdf7acbd3f2e0a34f00a1b7283a1b222da6835f93505db6641"; 0073 } 0074 0075 void VerfierTest::testPartialChecksums() 0076 { 0077 QFETCH(QString, type); 0078 QFETCH(KIO::filesize_t, length); 0079 QFETCH(QStringList, checksums); 0080 QFETCH(bool, result); 0081 0082 const PartialChecksums partial = Verifier::partialChecksums(m_file, type, length, nullptr); 0083 QCOMPARE((partial.checksums() == checksums), result); 0084 } 0085 0086 void VerfierTest::testPartialChecksums_data() 0087 { 0088 QTest::addColumn<QString>("type"); 0089 QTest::addColumn<KIO::filesize_t>("length"); 0090 QTest::addColumn<QStringList>("checksums"); 0091 QTest::addColumn<bool>("result"); 0092 0093 QTest::newRow("md5, empty checksums") << "md5" << KIO::filesize_t(512 * 1000) << QStringList() << expectedResult(false, "md5"); 0094 QTest::newRow("md5, one checksum is too long, 500 KiB") << "md5" << KIO::filesize_t(500 * 1024) 0095 << (QStringList() << "AAAAA8c53d0109e5222e2c57d187eec33ab71" 0096 << "f13b7c9978ac7edac8b1c2b9be55abb0" 0097 << "643086ed0d7e733629c848e94e29cdc4" 0098 << "6c349ee55737d9bc45ef1b1e75ec7bdf" 0099 << "40363ab59bbfa39f6faa4aa18ee75a6c") 0100 << expectedResult(false, "md5"); 0101 QTest::newRow("valid md5 checksums, 500 KiB") << "md5" << KIO::filesize_t(500 * 1024) 0102 << (QStringList() << "8c53d0109e5222e2c57d187eec33ab71" 0103 << "f13b7c9978ac7edac8b1c2b9be55abb0" 0104 << "643086ed0d7e733629c848e94e29cdc4" 0105 << "6c349ee55737d9bc45ef1b1e75ec7bdf" 0106 << "40363ab59bbfa39f6faa4aa18ee75a6c") 0107 << expectedResult(true, "md5"); 0108 QTest::newRow("valid md5 checksums, 400 KiB") << "md5" << KIO::filesize_t(400 * 1024) 0109 << (QStringList() << "ce7d795bd0b1499f18d2ba8f338302d3" 0110 << "e6681cc0049c6cae347039578eaf1117" 0111 << "e66499f43f930fb013092a2d66ecdfaf" 0112 << "b2930c55fea65c11813dff09447fbe92" 0113 << "77b79bd53b62accec6367c7d7b2fc85b" 0114 << "40363ab59bbfa39f6faa4aa18ee75a6c") 0115 << expectedResult(true, "md5"); 0116 QTest::newRow("valid md5 checksums, one part with file size") 0117 << "md5" << KIO::filesize_t(2200000) << (QStringList() << "1c3b1b627e4f236fdac8f6ab3ee160d1") << expectedResult(true, "md5"); 0118 QTest::newRow("valid sha1 checksums, 1000 KiB") << "sha1" << KIO::filesize_t(1000 * 1024) 0119 << (QStringList() << "5d76447e7655fd1d4dfba458c33340757d81eb95" 0120 << "0bc9428e3b39fc34ab457d58d62f1973a1183ac2" 0121 << "48a313a958ea1c55eb9527b0141ae30742fedfdb") 0122 << expectedResult(true, "sha1"); 0123 } 0124 0125 void VerfierTest::testIsChecksum() 0126 { 0127 QFETCH(QString, type); 0128 QFETCH(QString, checksum); 0129 QFETCH(bool, result); 0130 0131 QCOMPARE(Verifier::isChecksum(type, checksum), result); 0132 } 0133 0134 void VerfierTest::testIsChecksum_data() 0135 { 0136 QTest::addColumn<QString>("type"); 0137 QTest::addColumn<QString>("checksum"); 0138 QTest::addColumn<bool>("result"); 0139 0140 const QStringList supported = Verifier::supportedVerficationTypes(); 0141 0142 QTest::newRow("md5, too many chars") << "md5" 0143 << "1c3b1b627e4f236fdac8f6ab3ee160d1a" << expectedResult(false, "md5"); 0144 QTest::newRow("md5, correct length, one wrong char") << "md5" 0145 << "1c b1b627e4f236fdac8f6ab3ee160d1" << expectedResult(false, "md5"); 0146 QTest::newRow("md5, correct length, many wrong chars") << "md5" 0147 << "1c[b1b627e4f236fd)c8f6ab3_e160d1" << expectedResult(false, "md5"); 0148 QTest::newRow("valid md5 checksum") << "md5" 0149 << "1C3B1b627e4F236fdac8f6ab3ee160d1" << expectedResult(true, "md5"); 0150 QTest::newRow("valid sha1 checksum") << "sha1" 0151 << "0a4886f5c6e45c5a879b0093a43cb06fc5568bc5" << expectedResult(true, "sha1"); 0152 QTest::newRow("valid sha256 checksum") << "sha256" 0153 << "9cf2cfe941569d8627dc3ede2897dea1c3949eb6bd124d3bca06a888492f955b" << expectedResult(true, "sha256"); 0154 QTest::newRow("valid sha384 checksum") << "sha384" 0155 << "a27d95917b3d3ff9145fd9a66cc9852f3a15e7ebac9e7112d4260e2747d45c89a949fe53d110ecc260e246f959ca30e9" 0156 << expectedResult(true, "sha384"); 0157 QTest::newRow("valid sha512 checksum") 0158 << "sha512" 0159 << "4c6d2020482ddb1c6f7275f40fb35084805f06a23e433420e46b794ef7ad9770d527c4a7f5dc50bdf7acbd3f2e0a34f00a1b7283a1b222da6835f93505db6641" 0160 << expectedResult(true, "sha512"); 0161 } 0162 0163 void VerfierTest::testAvailableChecksum() 0164 { 0165 QFETCH(QList<Checksum>, checksums); 0166 QFETCH(Verifier::ChecksumStrength, strength); 0167 QFETCH(Checksum, result); 0168 0169 Verifier verifier(m_file); 0170 for (int i = 0; i < checksums.size(); ++i) { 0171 verifier.addChecksum(checksums[i].first, checksums[i].second); 0172 } 0173 0174 const Checksum returned = verifier.availableChecksum(strength); 0175 QCOMPARE(returned, result); 0176 } 0177 0178 void VerfierTest::testAvailableChecksum_data() 0179 { 0180 QTest::addColumn<QList<Checksum>>("checksums"); 0181 QTest::addColumn<Verifier::ChecksumStrength>("strength"); 0182 QTest::addColumn<Checksum>("result"); 0183 0184 const Checksum md5sum("md5", "1c3b1b627e4f236fdac8f6ab3ee160d1"); 0185 const Checksum md5sumInvalid("md5", "1c b1b627e4f236fdac8f6ab3ee160d1"); 0186 const Checksum sha1sum("sha1", "0a4886f5c6e45c5a879b0093a43cb06fc5568bc5"); 0187 const Checksum sha1sumInvalid("sha1", "0a4886f5c6e45c5a879b0093a43cb06fc5568bc"); 0188 const Checksum emptysum; 0189 0190 QTest::newRow("weak (md5), md5 should be returned") << (QList<Checksum>() << md5sum) << Verifier::Weak << md5sum; 0191 QTest::newRow("strong (md5), md5 should be returned") << (QList<Checksum>() << md5sum) << Verifier::Strong << md5sum; 0192 QTest::newRow("strongest (md5), md5 should be returned") << (QList<Checksum>() << md5sum) << Verifier::Strongest << md5sum; 0193 QTest::newRow("weak (md5), nothing should be returned (md5 invalid)") << (QList<Checksum>() << md5sumInvalid) << Verifier::Weak << emptysum; 0194 QTest::newRow("strong (md5), nothing should be returned (md5 invalid)") << (QList<Checksum>() << md5sumInvalid) << Verifier::Strong << emptysum; 0195 QTest::newRow("strongest (md5), nothing should be returned (md5 invalid)") << (QList<Checksum>() << md5sumInvalid) << Verifier::Strongest << emptysum; 0196 if (m_supported.contains("sha1")) { 0197 QTest::newRow("weak (md5, sha1), sha1 should be returned (md5 invalid)") 0198 << (QList<Checksum>() << md5sumInvalid << sha1sum) << Verifier::Weak << sha1sum; 0199 QTest::newRow("strong (md5, sha1), md5 should be returned (sha1 invalid)") 0200 << (QList<Checksum>() << md5sum << sha1sumInvalid) << Verifier::Strong << md5sum; 0201 QTest::newRow("strongest (md5, sha1), md5 should be returned (sha1 invalid)") 0202 << (QList<Checksum>() << md5sum << sha1sumInvalid) << Verifier::Strongest << md5sum; 0203 QTest::newRow("strong (md5, sha1), sha1 should be returned") << (QList<Checksum>() << md5sum << sha1sum) << Verifier::Strong << sha1sum; 0204 QTest::newRow("strong (md5, sha1), sha1 should be returned") << (QList<Checksum>() << md5sum << sha1sum) << Verifier::Strongest << sha1sum; 0205 QTest::newRow("strongest (md5, sha1), nothing should be returned (md5, sha1 invalid)") 0206 << (QList<Checksum>() << md5sumInvalid << sha1sumInvalid) << Verifier::Strongest << emptysum; 0207 } 0208 } 0209 0210 void VerfierTest::testAvailablePartialChecksum() 0211 { 0212 QFETCH(QList<Checksums>, data); 0213 QFETCH(Verifier::ChecksumStrength, strength); 0214 QFETCH(Checksums, result); 0215 0216 Verifier verifier(m_file); 0217 for (int i = 0; i < data.size(); ++i) { 0218 verifier.addPartialChecksums(data[i].first, data[i].second.length(), data[i].second.checksums()); 0219 } 0220 0221 const QPair<QString, PartialChecksums *> returned = verifier.availablePartialChecksum(strength); 0222 QVERIFY(!returned.first.isEmpty()); 0223 QVERIFY(returned.second); 0224 0225 if (returned.second) { 0226 QCOMPARE(returned.first, result.first); 0227 QCOMPARE(returned.second->checksums(), result.second.checksums()); 0228 QCOMPARE(returned.second->length(), result.second.length()); 0229 } 0230 } 0231 0232 void VerfierTest::testAvailablePartialChecksum_data() 0233 { 0234 QTest::addColumn<QList<Checksums>>("data"); 0235 QTest::addColumn<Verifier::ChecksumStrength>("strength"); 0236 QTest::addColumn<Checksums>("result"); 0237 0238 const Checksums md5Sums("md5", 0239 PartialChecksums(400 * 1024, 0240 (QStringList() << "ce7d795bd0b1499f18d2ba8f338302d3" 0241 << "e6681cc0049c6cae347039578eaf1117" 0242 << "e66499f43f930fb013092a2d66ecdfaf" 0243 << "b2930c55fea65c11813dff09447fbe92" 0244 << "77b79bd53b62accec6367c7d7b2fc85b" 0245 << "40363ab59bbfa39f6faa4aa18ee75a6c"))); 0246 const Checksums sha1Sums("sha1", 0247 PartialChecksums(1000 * 1024, 0248 (QStringList() << "5d76447e7655fd1d4dfba458c33340757d81eb95" 0249 << "0bc9428e3b39fc34ab457d58d62f1973a1183ac2" 0250 << "48a313a958ea1c55eb9527b0141ae30742fedfdb"))); 0251 const Checksums sha512Sums( 0252 "sha512", 0253 PartialChecksums( 0254 2000 * 1024, 0255 (QStringList() 0256 << "ba9c09d26d7ec7ff60671bc72ec9fed10dee851ae951fbb9e41061490fc10019076982b4c25723870bb5ff17401fdd6d21db43f6018a0604177197db384122d3" 0257 << "a63af512c40951216f20c01f5d5623af4c24b907f9a78ded98c14ab550e23764cd131961cbf45df7dfcb17b3cca7443db12de1e0540ed421579d15ccfc7863d0"))); 0258 0259 QTest::newRow("weak (md5), md5 should be returned") << (QList<Checksums>() << md5Sums) << Verifier::Weak << md5Sums; 0260 QTest::newRow("strong (md5), md5 should be returned") << (QList<Checksums>() << md5Sums) << Verifier::Strong << md5Sums; 0261 QTest::newRow("strongest (md5), md5 should be returned") << (QList<Checksums>() << md5Sums) << Verifier::Strongest << md5Sums; 0262 if (m_supported.contains("sha1")) { 0263 QTest::newRow("weak (md5, sha1), md5 should be returned") << (QList<Checksums>() << md5Sums << sha1Sums) << Verifier::Weak << md5Sums; 0264 QTest::newRow("strong (md5, sha1), sha1 should be returned") << (QList<Checksums>() << md5Sums << sha1Sums) << Verifier::Strong << sha1Sums; 0265 QTest::newRow("strongest (md5, sha1), sha1 should be returned") << (QList<Checksums>() << md5Sums << sha1Sums) << Verifier::Strongest << sha1Sums; 0266 if (m_supported.contains("sha512")) { 0267 QTest::newRow("weak (md5, sha1, sha512), md5 should be returned") 0268 << (QList<Checksums>() << md5Sums << sha1Sums << sha512Sums) << Verifier::Weak << md5Sums; 0269 QTest::newRow("strong (md5, sha1, sha512), sha1 should be returned") 0270 << (QList<Checksums>() << md5Sums << sha1Sums << sha512Sums) << Verifier::Strong << sha1Sums; 0271 QTest::newRow("strongest (md5, sha1, sha512), sha512 should be returned") 0272 << (QList<Checksums>() << md5Sums << sha1Sums << sha512Sums) << Verifier::Strongest << sha512Sums; 0273 } 0274 } 0275 } 0276 0277 bool VerfierTest::expectedResult(bool expected, const QString &type) 0278 { 0279 return (expected && m_supported.contains(type)); 0280 } 0281 0282 void VerfierTest::testVerify() 0283 { 0284 QFETCH(QList<Checksum>, checksums); 0285 QFETCH(Verifier::ChecksumStrength, strength); 0286 QFETCH(bool, result); 0287 0288 const Verifier::ChecksumStrength tempStrength = static_cast<Verifier::ChecksumStrength>(Settings::checksumStrength()); 0289 Settings::setChecksumStrength(strength); 0290 Verifier verifier(m_file); 0291 for (int i = 0; i < checksums.size(); ++i) { 0292 verifier.addChecksum(checksums[i].first, checksums[i].second); 0293 } 0294 0295 QSignalSpy stateSpy(&verifier, SIGNAL(verified(bool))); 0296 QVERIFY(stateSpy.isValid()); 0297 QCOMPARE(stateSpy.count(), 0); 0298 0299 verifier.verify(); 0300 0301 // wait for a maximum of 5 seconds, the slowest computer should be done by then 0302 for (int i = 0; !stateSpy.count() && (i < 50); ++i) { 0303 QTest::qWait(100); 0304 } 0305 0306 QCOMPARE(stateSpy.count(), 1); 0307 const QList<QVariant> argument = stateSpy.takeFirst(); 0308 QCOMPARE(argument.count(), 1); 0309 QCOMPARE(argument.first().toBool(), result); 0310 0311 Settings::setChecksumStrength(tempStrength); 0312 } 0313 0314 void VerfierTest::testVerify_data() 0315 { 0316 QTest::addColumn<QList<Checksum>>("checksums"); 0317 QTest::addColumn<Verifier::ChecksumStrength>("strength"); 0318 QTest::addColumn<bool>("result"); 0319 0320 const Checksum md5sumCorrect("md5", "1c3b1b627e4f236fdac8f6ab3ee160d1"); 0321 const Checksum md5sumWrong("md5", "ac3b1b627e4f236fdac8f6ab3ee160d1"); 0322 const Checksum md5sumInvalid("md5", "1c b1b627e4f236fdac8f6ab3ee160d1"); 0323 const Checksum sha1sumCorrect("sha1", "0a4886f5c6e45c5a879b0093a43cb06fc5568bc5"); 0324 const Checksum sha1sumWrong("sha1", "aa4886f5c6e45c5a879b0093a43cb06fc5568bc5"); 0325 const Checksum sha1sumInvalid("sha1", "0a4886f5c6e45c5a879b0093a43cb0"); 0326 0327 QTest::newRow("weak (md5), should be verified") << (QList<Checksum>() << md5sumCorrect) << Verifier::Weak << true; 0328 QTest::newRow("strong (md5), should be verified") << (QList<Checksum>() << md5sumCorrect) << Verifier::Strong << true; 0329 QTest::newRow("strongest (md5), should be verified") << (QList<Checksum>() << md5sumCorrect) << Verifier::Strongest << true; 0330 QTest::newRow("weak (md5), should not be verified (md5 wrong)") << (QList<Checksum>() << md5sumWrong) << Verifier::Weak << false; 0331 if (m_supported.contains("sha1")) { 0332 QTest::newRow("weak (md5, sha1), should not be verified (md5 wrong)") 0333 << (QList<Checksum>() << md5sumWrong << sha1sumCorrect) << Verifier::Weak << false; 0334 QTest::newRow("weak (md5, sha1), should be verified (md5 invalid)") << (QList<Checksum>() << md5sumInvalid << sha1sumCorrect) << Verifier::Weak << true; 0335 QTest::newRow("strong (md5, sha1), should not be verified (sha1 wrong)") 0336 << (QList<Checksum>() << md5sumCorrect << sha1sumWrong) << Verifier::Strong << false; 0337 QTest::newRow("strong (md5, sha1), should be verified (sha1 invalid)") 0338 << (QList<Checksum>() << md5sumCorrect << sha1sumInvalid) << Verifier::Strong << true; 0339 QTest::newRow("strong (md5, sha1), should be verified") << (QList<Checksum>() << md5sumCorrect << sha1sumCorrect) << Verifier::Strong << true; 0340 QTest::newRow("strongest (md5, sha1), should be verified (sha1 invalid)") 0341 << (QList<Checksum>() << md5sumCorrect << sha1sumInvalid) << Verifier::Strongest << true; 0342 } 0343 } 0344 0345 void VerfierTest::testBrokenPieces() 0346 { 0347 QFETCH(QList<Checksums>, data); 0348 QFETCH(Verifier::ChecksumStrength, strength); 0349 QFETCH(QList<KIO::fileoffset_t>, offsets); 0350 QFETCH(KIO::filesize_t, length); 0351 0352 const Verifier::ChecksumStrength tempStrength = static_cast<Verifier::ChecksumStrength>(Settings::checksumStrength()); 0353 Settings::setChecksumStrength(strength); 0354 Verifier verifier(m_file); 0355 for (int i = 0; i < data.size(); ++i) { 0356 verifier.addPartialChecksums(data[i].first, data[i].second.length(), data[i].second.checksums()); 0357 } 0358 0359 QSignalSpy stateSpy(&verifier, SIGNAL(brokenPieces(QList<KIO::fileoffset_t>, KIO::filesize_t))); 0360 QVERIFY(stateSpy.isValid()); 0361 QCOMPARE(stateSpy.count(), 0); 0362 0363 verifier.brokenPieces(); 0364 0365 // wait for a maximum of 5 seconds, the slowest computer should be done by then 0366 for (int i = 0; !stateSpy.count() && (i < 50); ++i) { 0367 QTest::qWait(100); 0368 } 0369 0370 QCOMPARE(stateSpy.count(), 1); 0371 const QList<QVariant> argument = stateSpy.takeFirst(); 0372 QCOMPARE(argument.count(), 2); 0373 const QList<KIO::fileoffset_t> returnedOffsets = qvariant_cast<QList<KIO::fileoffset_t>>(argument[0]); 0374 QCOMPARE(returnedOffsets, offsets); 0375 const KIO::filesize_t returnedLength = qvariant_cast<KIO::filesize_t>(argument[1]); 0376 QCOMPARE(returnedLength, length); 0377 0378 Settings::setChecksumStrength(tempStrength); 0379 } 0380 0381 void VerfierTest::testBrokenPieces_data() 0382 { 0383 QTest::addColumn<QList<Checksums>>("data"); 0384 QTest::addColumn<Verifier::ChecksumStrength>("strength"); 0385 QTest::addColumn<QList<KIO::fileoffset_t>>("offsets"); 0386 QTest::addColumn<KIO::filesize_t>("length"); 0387 0388 const QList<KIO::fileoffset_t> emptyOffsets; 0389 0390 const KIO::filesize_t md5Length = 400 * 1024; 0391 const Checksums md5sumsCorrect("md5", 0392 PartialChecksums(md5Length, 0393 (QStringList() << "ce7d795bd0b1499f18d2ba8f338302d3" 0394 << "e6681cc0049c6cae347039578eaf1117" 0395 << "e66499f43f930fb013092a2d66ecdfaf" 0396 << "b2930c55fea65c11813dff09447fbe92" 0397 << "77b79bd53b62accec6367c7d7b2fc85b" 0398 << "40363ab59bbfa39f6faa4aa18ee75a6c"))); 0399 const Checksums md5sumsWrong1("md5", 0400 PartialChecksums(md5Length, 0401 (QStringList() << "ae7d795bd0b1499f18d2ba8f338302d3" 0402 << "e6681cc0049c6cae347039578eaf1117" 0403 << "e66499f43f930fb013092a2d66ecdfaf" 0404 << "b2930c55fea65c11813dff09447fbe92" 0405 << "77b79bd53b62accec6367c7d7b2fc85b" 0406 << "40363ab59bbfa39f6faa4aa18ee75a6c"))); 0407 QList<KIO::fileoffset_t> md5sumsWrong1Offsets1 = (QList<KIO::fileoffset_t>() << 0); 0408 const Checksums md5sumsWrong2("md5", 0409 PartialChecksums(md5Length, 0410 (QStringList() << "ce7d795bd0b1499f18d2ba8f338302d3" 0411 << "e6681cc0049c6cae347039578eaf1117" 0412 << "d66499f43f930fb013092a2d66ecdfaf" 0413 << "b2930c55fea65c11813dff09447fbe92" 0414 << "77b79bd53b62accec6367c7d7b2fc85b" 0415 << "d0363ab59bbfa39f6faa4aa18ee75a6c"))); 0416 QList<KIO::fileoffset_t> md5sumsWrong1Offsets2 = (QList<KIO::fileoffset_t>() << 2 * md5Length << 5 * md5Length); 0417 0418 const KIO::filesize_t sha1Length = 1000 * 1024; 0419 const Checksums sha1sumsCorrect("sha1", 0420 PartialChecksums(sha1Length, 0421 (QStringList() << "5d76447e7655fd1d4dfba458c33340757d81eb95" 0422 << "0bc9428e3b39fc34ab457d58d62f1973a1183ac2" 0423 << "48a313a958ea1c55eb9527b0141ae30742fedfdb"))); 0424 const Checksums sha1sumsWrong1("sha1", 0425 PartialChecksums(sha1Length, 0426 (QStringList() << "5d76447e7655fd1d4dfba458c33340757d81eb95" 0427 << "abc9428e3b39fc34ab457d58d62f1973a1183ac2" 0428 << "48a313a958ea1c55eb9527b0141ae30742fedfdb"))); 0429 QList<KIO::fileoffset_t> sha1sumsWrong1Offsets1 = (QList<KIO::fileoffset_t>() << sha1Length); 0430 const Checksums sha1sumsWrong2("sha1", 0431 PartialChecksums(sha1Length, 0432 (QStringList() << "ad76447e7655fd1d4dfba458c33340757d81eb95" 0433 << "abc9428e3b39fc34ab457d58d62f1973a1183ac2" 0434 << "a8a313a958ea1c55eb9527b0141ae30742fedfdb"))); 0435 QList<KIO::fileoffset_t> sha1sumsWrong1Offsets2 = (QList<KIO::fileoffset_t>() << 0 << sha1Length << 2 * sha1Length); 0436 0437 QTest::newRow("weak (md5), no broken pieces") << (QList<Checksums>() << md5sumsCorrect) << Verifier::Weak << emptyOffsets << md5Length; 0438 QTest::newRow("weak (md5), 1 broken piece (first)") << (QList<Checksums>() << md5sumsWrong1) << Verifier::Weak << md5sumsWrong1Offsets1 << md5Length; 0439 QTest::newRow("weak (md5), 2 broken piece (third, sixth)") << (QList<Checksums>() << md5sumsWrong2) << Verifier::Weak << md5sumsWrong1Offsets2 << md5Length; 0440 if (m_supported.contains("sha1")) { 0441 QTest::newRow("weak (md5, sha1), no broken pieces (sha1 wrong)") 0442 << (QList<Checksums>() << md5sumsCorrect << sha1sumsWrong1) << Verifier::Weak << emptyOffsets << md5Length; 0443 QTest::newRow("strong (md5, sha1), no broken pieces (md5 wrong)") 0444 << (QList<Checksums>() << md5sumsWrong1 << sha1sumsCorrect) << Verifier::Strong << emptyOffsets << sha1Length; 0445 QTest::newRow("strong (md5, sha1), 3 broken pieces (first, second, third)") 0446 << (QList<Checksums>() << md5sumsCorrect << sha1sumsWrong2) << Verifier::Strong << sha1sumsWrong1Offsets2 << sha1Length; 0447 QTest::newRow("strongest (md5, sha1), 1 broken piece (second)") 0448 << (QList<Checksums>() << md5sumsCorrect << sha1sumsWrong1) << Verifier::Strongest << sha1sumsWrong1Offsets1 << sha1Length; 0449 } 0450 } 0451 0452 QTEST_MAIN(VerfierTest) 0453 0454 #include "moc_verifiertest.cpp"