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 "localdatabasefiletest.h" 0008 #include "../createdatabasefilejob.h" 0009 #include "../localdatabasefile.h" 0010 #include <QSignalSpy> 0011 #include <QStandardPaths> 0012 #include <QTest> 0013 0014 LocalDataBaseFileTest::LocalDataBaseFileTest(QObject *parent) 0015 : QObject(parent) 0016 { 0017 QStandardPaths::setTestModeEnabled(true); 0018 QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/phishingurl")); 0019 } 0020 0021 LocalDataBaseFileTest::~LocalDataBaseFileTest() = default; 0022 0023 void LocalDataBaseFileTest::shouldBeInvalidWithUnExistingFile() 0024 { 0025 WebEngineViewer::LocalDataBaseFile f(QStringLiteral("foo")); 0026 QVERIFY(!f.isValid()); 0027 QVERIFY(!f.fileExists()); 0028 } 0029 0030 void LocalDataBaseFileTest::shouldCheckHashBinaryFile_data() 0031 { 0032 QTest::addColumn<QByteArray>("hash"); 0033 QTest::addColumn<QByteArray>("resultHash"); 0034 QTest::addColumn<bool>("found"); 0035 QTest::newRow("nohash") << QByteArrayLiteral("foo") << QByteArrayLiteral("efgh") << true; 0036 QTest::newRow("foundhash") << QByteArrayLiteral("1111") << QByteArrayLiteral("1111") << true; 0037 QTest::newRow("foundhash1") << QByteArrayLiteral("11111") << QByteArrayLiteral("1111") << true; 0038 QTest::newRow("foundhash2") << QByteArrayLiteral("HGsse") << QByteArrayLiteral("54321") << true; 0039 QTest::newRow("foundhash3") << QByteArrayLiteral("1112") << QByteArrayLiteral("1111") << true; 0040 } 0041 0042 void LocalDataBaseFileTest::shouldCheckHashBinaryFile() 0043 { 0044 QFETCH(QByteArray, hash); 0045 QFETCH(QByteArray, resultHash); 0046 QFETCH(bool, found); 0047 WebEngineViewer::CreateDatabaseFileJob databasejob; 0048 const QString createDataBaseName = 0049 QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1StringView("/phishingurl") + QLatin1StringView("/correctBinary.db"); 0050 qDebug() << " new filename " << createDataBaseName; 0051 databasejob.setFileName(createDataBaseName); 0052 0053 WebEngineViewer::UpdateDataBaseInfo info; 0054 0055 WebEngineViewer::Addition a; 0056 a.hashString = QByteArray("----1111bbbb"); 0057 a.prefixSize = 4; 0058 a.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression; 0059 0060 WebEngineViewer::Addition b; 0061 b.hashString = QByteArray("abcdefgh"); 0062 b.prefixSize = 4; 0063 b.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression; 0064 0065 WebEngineViewer::Addition c; 0066 c.hashString = QByteArray("54321abcde"); 0067 c.prefixSize = 5; 0068 c.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression; 0069 0070 WebEngineViewer::Addition d; 0071 d.hashString = QByteArray("22222bcdef"); 0072 d.prefixSize = 5; 0073 d.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression; 0074 0075 QList<WebEngineViewer::Addition> lst; 0076 lst << a << b << c << d; 0077 info.additionList = lst; 0078 info.minimumWaitDuration = QStringLiteral("593.440s"); 0079 info.threatType = QStringLiteral("MALWARE"); 0080 info.threatEntryType = QStringLiteral("URL"); 0081 info.responseType = WebEngineViewer::UpdateDataBaseInfo::FullUpdate; 0082 info.platformType = QStringLiteral("WINDOWS"); 0083 info.newClientState = QStringLiteral("ChAIBRADGAEiAzAwMSiAEDABEAFGpqhd"); 0084 info.sha256 = QByteArrayLiteral("vLPta+N40Sip7Xo3XXgYvW5dpahS96vPwaOjxVospm8="); 0085 0086 databasejob.setUpdateDataBaseInfo(info); 0087 0088 QSignalSpy spy2(&databasejob, &WebEngineViewer::CreateDatabaseFileJob::finished); 0089 databasejob.start(); 0090 QCOMPARE(spy2.count(), 1); 0091 bool successCreateDataBase = spy2.at(0).at(0).toBool(); 0092 QVERIFY(successCreateDataBase); 0093 0094 WebEngineViewer::LocalDataBaseFile newFile(createDataBaseName); 0095 QVERIFY(newFile.isValid()); 0096 QCOMPARE(newFile.getUint16(0), static_cast<quint16>(1)); 0097 QCOMPARE(newFile.getUint16(2), static_cast<quint16>(0)); 0098 quint64 number = newFile.getUint64(4); 0099 QCOMPARE(number, static_cast<quint64>(9)); 0100 int index = 4 + sizeof(quint64); 0101 QCOMPARE(index, 12); 0102 const QByteArray val = newFile.searchHash(hash); 0103 qDebug() << "result : " << val; 0104 QCOMPARE(!val.isEmpty(), found); 0105 if (!val.isEmpty()) { 0106 QCOMPARE(resultHash, val); 0107 } 0108 } 0109 0110 QTEST_GUILESS_MAIN(LocalDataBaseFileTest) 0111 0112 #include "moc_localdatabasefiletest.cpp"