File indexing completed on 2025-01-26 04:57:21
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "verifydatabaseupdatetest.h" 0008 #include "../updatedatabaseinfo.h" 0009 #include <QCryptographicHash> 0010 #include <QTest> 0011 0012 Q_DECLARE_METATYPE(QList<WebEngineViewer::Addition>) 0013 0014 VerifyDataBaseUpdateTest::VerifyDataBaseUpdateTest(QObject *parent) 0015 : QObject(parent) 0016 { 0017 } 0018 0019 VerifyDataBaseUpdateTest::~VerifyDataBaseUpdateTest() = default; 0020 0021 void VerifyDataBaseUpdateTest::shouldVerifyCheckSums_data() 0022 { 0023 QTest::addColumn<QList<WebEngineViewer::Addition>>("additionList"); 0024 QTest::addColumn<int>("numberOfItems"); 0025 QTest::addColumn<QByteArray>("calculateCheckSums"); 0026 0027 WebEngineViewer::Addition a; 0028 a.hashString = QByteArray("----1111bbbb"); 0029 a.prefixSize = 4; 0030 0031 WebEngineViewer::Addition b; 0032 b.hashString = QByteArray("abcdefgh"); 0033 b.prefixSize = 4; 0034 0035 WebEngineViewer::Addition c; 0036 c.hashString = QByteArray("54321abcde"); 0037 c.prefixSize = 5; 0038 0039 WebEngineViewer::Addition d; 0040 d.hashString = QByteArray("22222bcdef"); 0041 d.prefixSize = 5; 0042 0043 QList<WebEngineViewer::Addition> lst; 0044 lst << a << b << c << d; 0045 0046 QByteArray calculateCheckSums = QByteArrayLiteral( 0047 "\xBC\xB3\xEDk\xE3x\xD1(\xA9\xEDz7]x\x18\xBDn]" 0048 "\xA5\xA8R\xF7\xAB\xCF\xC1\xA3\xA3\xC5Z,\xA6o"); 0049 0050 QTest::newRow("checksum1") << lst << 9 << calculateCheckSums; 0051 0052 lst.clear(); 0053 0054 WebEngineViewer::Addition a1; 0055 a1.hashString = QByteArray("----1111bbbbabcdefgh"); 0056 a1.prefixSize = 4; 0057 WebEngineViewer::Addition c1; 0058 c1.hashString = QByteArray("54321abcde22222bcdef"); 0059 c1.prefixSize = 5; 0060 0061 lst << a1 << c1; 0062 QTest::newRow("checksum2") << lst << 9 << calculateCheckSums; 0063 0064 lst.clear(); 0065 0066 WebEngineViewer::Addition c2; 0067 c2.hashString = QByteArray("54321abcde22222bcdef"); 0068 c2.prefixSize = 5; 0069 0070 WebEngineViewer::Addition a2; 0071 a2.hashString = QByteArray("----1111bbbbabcdefgh"); 0072 a2.prefixSize = 4; 0073 0074 lst << c2 << a2; 0075 QTest::newRow("checksum3") << lst << 9 << calculateCheckSums; 0076 } 0077 0078 void VerifyDataBaseUpdateTest::shouldVerifyCheckSums() 0079 { 0080 QFETCH(QList<WebEngineViewer::Addition>, additionList); 0081 QFETCH(int, numberOfItems); 0082 QFETCH(QByteArray, calculateCheckSums); 0083 0084 // Proof of checksum validity using python: 0085 // >>> import hashlib 0086 // >>> m = hashlib.sha256() 0087 // >>> m.update("----11112222254321abcdabcdebbbbbcdefefgh") 0088 // >>> m.digest() 0089 // "\xbc\xb3\xedk\xe3x\xd1(\xa9\xedz7]" 0090 // "x\x18\xbdn]\xa5\xa8R\xf7\xab\xcf\xc1\xa3\xa3\xc5Z,\xa6o" 0091 0092 QList<WebEngineViewer::Addition> itemToStore; 0093 for (const WebEngineViewer::Addition &add : additionList) { 0094 const QByteArray uncompressed = add.hashString; 0095 for (int i = 0; i < uncompressed.size();) { 0096 const QByteArray m = uncompressed.mid(i, add.prefixSize); 0097 i += add.prefixSize; 0098 0099 WebEngineViewer::Addition tmp; 0100 tmp.hashString = m; 0101 tmp.prefixSize = add.prefixSize; 0102 itemToStore << tmp; 0103 0104 if (m.size() != add.prefixSize) { 0105 qDebug() << "hashstring: " << m << " hash size: " << m.size(); 0106 } 0107 } 0108 } 0109 QCOMPARE(itemToStore.count(), numberOfItems); 0110 0111 QByteArray newSsha256; 0112 std::sort(itemToStore.begin(), itemToStore.end(), WebEngineViewer::Addition::lessThan); 0113 for (const WebEngineViewer::Addition &add : std::as_const(itemToStore)) { 0114 QByteArray ba = add.hashString; 0115 newSsha256 += ba; 0116 } 0117 QCOMPARE(newSsha256, QByteArrayLiteral("----11112222254321abcdabcdebbbbbcdefefgh")); 0118 0119 const QByteArray newSsha256Value = QCryptographicHash::hash(newSsha256, QCryptographicHash::Sha256); 0120 QCOMPARE(newSsha256Value, calculateCheckSums); 0121 QCOMPARE(newSsha256Value.toBase64(), calculateCheckSums.toBase64()); 0122 } 0123 0124 QTEST_GUILESS_MAIN(VerifyDataBaseUpdateTest) 0125 0126 #include "moc_verifydatabaseupdatetest.cpp"