File indexing completed on 2025-01-26 04:57:20

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 "createphishingurldatabasejobtest.h"
0008 #include "../checkphishingurlutil.h"
0009 #include "../createphishingurldatabasejob.h"
0010 #include "../updatedatabaseinfo.h"
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 
0014 extern WEBENGINEVIEWER_EXPORT bool webengineview_useCompactJson_CreatePhishingUrlDataBaseJob;
0015 
0016 QByteArray readJsonFile(const QString &jsonFile)
0017 {
0018     QFile file(QLatin1StringView(CHECKPHISHINGURL_DATA_DIR) + QLatin1Char('/') + jsonFile);
0019     file.open(QIODevice::ReadOnly);
0020     Q_ASSERT(file.isOpen());
0021     const QByteArray data = file.readAll();
0022     // Q_ASSERT(!data.isEmpty());
0023     return data;
0024 }
0025 
0026 QByteArray createHash(const QByteArray &ba)
0027 {
0028     QByteArray b = QCryptographicHash::hash(ba, QCryptographicHash::Sha256);
0029     return b.toBase64();
0030 }
0031 
0032 CreatePhishingUrlDataBaseJobTest::CreatePhishingUrlDataBaseJobTest(QObject *parent)
0033     : QObject(parent)
0034 {
0035     webengineview_useCompactJson_CreatePhishingUrlDataBaseJob = true;
0036 }
0037 
0038 CreatePhishingUrlDataBaseJobTest::~CreatePhishingUrlDataBaseJobTest() = default;
0039 
0040 void CreatePhishingUrlDataBaseJobTest::initTestCase()
0041 {
0042     qRegisterMetaType<WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult>();
0043     qRegisterMetaType<WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType>();
0044     qRegisterMetaType<WebEngineViewer::UpdateDataBaseInfo>();
0045 }
0046 
0047 void CreatePhishingUrlDataBaseJobTest::shouldClearUpdateDataBaseInfo()
0048 {
0049     WebEngineViewer::UpdateDataBaseInfo info;
0050     WebEngineViewer::UpdateDataBaseInfo info2;
0051     QCOMPARE(info, info2);
0052     info.clear();
0053     QCOMPARE(info, info2);
0054 
0055     WebEngineViewer::UpdateDataBaseInfo value;
0056     QList<WebEngineViewer::Addition> additionList;
0057     WebEngineViewer::Addition tmp;
0058     tmp.prefixSize = 4;
0059     tmp.hashString = QByteArrayLiteral("rnGLoQ==");
0060     additionList.append(tmp);
0061     QList<WebEngineViewer::Removal> removalList;
0062     WebEngineViewer::Removal tmpRemoval;
0063     tmpRemoval.indexes = QList<quint32>() << 0 << 2 << 4;
0064     removalList.append(tmpRemoval);
0065     value.minimumWaitDuration = QStringLiteral("593.440s");
0066     value.threatType = QStringLiteral("MALWARE");
0067     value.threatEntryType = QStringLiteral("URL");
0068     value.responseType = WebEngineViewer::UpdateDataBaseInfo::PartialUpdate;
0069     value.platformType = QStringLiteral("WINDOWS");
0070     value.newClientState = QStringLiteral("ChAIBRADGAEiAzAwMSiAEDABEAFGpqhd");
0071     value.sha256 = QByteArrayLiteral("YSgoRtsRlgHDqDA3LAhM1gegEpEzs1TjzU33vqsR8iM=");
0072     value.additionList = additionList;
0073     value.removalList = removalList;
0074 
0075     info = value;
0076     QCOMPARE(info, value);
0077     info2 = info;
0078     QCOMPARE(info, info2);
0079     info2.clear();
0080     info.clear();
0081     QCOMPARE(info, info2);
0082 
0083     WebEngineViewer::UpdateDataBaseInfo defaultValue;
0084     QCOMPARE(info, defaultValue);
0085     QCOMPARE(info2, defaultValue);
0086 }
0087 
0088 void CreatePhishingUrlDataBaseJobTest::shouldCreateRequest_data()
0089 {
0090     QTest::addColumn<QString>("databasestate");
0091     QTest::addColumn<WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadType>("downloadtype");
0092     QTest::addColumn<WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType>("contraintsCompressionType");
0093     QTest::addColumn<QString>("request");
0094     QTest::newRow("fulldownload")
0095         << QString() << WebEngineViewer::CreatePhishingUrlDataBaseJob::FullDataBase << WebEngineViewer::CreatePhishingUrlDataBaseJob::RawCompression
0096         << QStringLiteral(
0097                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RAW\"]},"
0098                "\"platformType\":\"WINDOWS\",\"state\":\"\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0099                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0100     QTest::newRow("fulldownloadwithdatabasestate")
0101         << QStringLiteral("foo") << WebEngineViewer::CreatePhishingUrlDataBaseJob::FullDataBase << WebEngineViewer::CreatePhishingUrlDataBaseJob::RawCompression
0102         << QStringLiteral(
0103                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RAW\"]},"
0104                "\"platformType\":\"WINDOWS\",\"state\":\"\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0105                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0106     QTest::newRow("partialdownloadwithdatabasestate")
0107         << QStringLiteral("foo") << WebEngineViewer::CreatePhishingUrlDataBaseJob::UpdateDataBase
0108         << WebEngineViewer::CreatePhishingUrlDataBaseJob::RawCompression
0109         << QStringLiteral(
0110                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RAW\"]},"
0111                "\"platformType\":\"WINDOWS\",\"state\":\"foo\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0112                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0113     QTest::newRow("partialdownloadwithoutdatabasestate")
0114         << QString() << WebEngineViewer::CreatePhishingUrlDataBaseJob::UpdateDataBase << WebEngineViewer::CreatePhishingUrlDataBaseJob::RawCompression
0115         << QStringLiteral(
0116                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RAW\"]},"
0117                "\"platformType\":\"WINDOWS\",\"state\":\"\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0118                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0119 
0120     QTest::newRow("fulldownload-rice")
0121         << QString() << WebEngineViewer::CreatePhishingUrlDataBaseJob::FullDataBase << WebEngineViewer::CreatePhishingUrlDataBaseJob::RiceCompression
0122         << QStringLiteral(
0123                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RICE\"]},"
0124                "\"platformType\":\"WINDOWS\",\"state\":\"\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0125                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0126     QTest::newRow("fulldownloadwithdatabasestate-rice")
0127         << QStringLiteral("foo") << WebEngineViewer::CreatePhishingUrlDataBaseJob::FullDataBase
0128         << WebEngineViewer::CreatePhishingUrlDataBaseJob::RiceCompression
0129         << QStringLiteral(
0130                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RICE\"]},"
0131                "\"platformType\":\"WINDOWS\",\"state\":\"\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0132                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0133     QTest::newRow("partialdownloadwithdatabasestate-rice")
0134         << QStringLiteral("foo") << WebEngineViewer::CreatePhishingUrlDataBaseJob::UpdateDataBase
0135         << WebEngineViewer::CreatePhishingUrlDataBaseJob::RiceCompression
0136         << QStringLiteral(
0137                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RICE\"]},"
0138                "\"platformType\":\"WINDOWS\",\"state\":\"foo\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0139                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0140     QTest::newRow("partialdownloadwithoutdatabasestate-rice")
0141         << QString() << WebEngineViewer::CreatePhishingUrlDataBaseJob::UpdateDataBase << WebEngineViewer::CreatePhishingUrlDataBaseJob::RiceCompression
0142         << QStringLiteral(
0143                "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"listUpdateRequests\":[{\"constraints\":{\"supportedCompressions\":[\"RICE\"]},"
0144                "\"platformType\":\"WINDOWS\",\"state\":\"\",\"threatEntryType\":\"URL\",\"threatType\":\"MALWARE\"}]}")
0145                .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps());
0146 }
0147 
0148 void CreatePhishingUrlDataBaseJobTest::shouldCreateRequest()
0149 {
0150     QFETCH(QString, databasestate);
0151     QFETCH(WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadType, downloadtype);
0152     QFETCH(WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType, contraintsCompressionType);
0153     QFETCH(QString, request);
0154 
0155     WebEngineViewer::CreatePhishingUrlDataBaseJob job;
0156     job.setDataBaseState(databasestate);
0157     job.setDataBaseDownloadNeeded(downloadtype);
0158     job.setContraintsCompressionType(contraintsCompressionType);
0159     QCOMPARE(job.jsonRequest(), request.toLatin1());
0160 }
0161 
0162 void CreatePhishingUrlDataBaseJobTest::checkRiceDeltaEncoding_data()
0163 {
0164     QTest::addColumn<QByteArray>("encodingData");
0165     QTest::addColumn<QByteArray>("firstValue");
0166     QTest::addColumn<int>("numberEntries");
0167     QTest::addColumn<int>("riceParameter");
0168     QTest::addColumn<bool>("valid");
0169     QTest::newRow("valid") << QByteArrayLiteral("ff") << QByteArrayLiteral("AAAA") << 15 << 18 << true;
0170     QTest::newRow("valid1") << QByteArrayLiteral("ff") << QByteArrayLiteral("AAAA") << 0 << 0 << true;
0171     QTest::newRow("invalid") << QByteArrayLiteral("ff") << QByteArrayLiteral("AAAA") << 99 << 200 << false;
0172     QTest::newRow("invalid1") << QByteArray() << QByteArrayLiteral("AAAA") << 15 << 18 << false;
0173     QTest::newRow("invalid2") << QByteArrayLiteral("AAAA") << QByteArray() << 15 << 18 << false;
0174     QTest::newRow("invalid3") << QByteArray() << QByteArray() << 15 << 18 << false;
0175 }
0176 
0177 void CreatePhishingUrlDataBaseJobTest::checkRiceDeltaEncoding()
0178 {
0179     QFETCH(QByteArray, encodingData);
0180     QFETCH(QByteArray, firstValue);
0181     QFETCH(int, numberEntries);
0182     QFETCH(int, riceParameter);
0183     QFETCH(bool, valid);
0184 
0185     WebEngineViewer::RiceDeltaEncoding a;
0186     a.encodingData = encodingData;
0187     a.firstValue = firstValue;
0188     a.numberEntries = numberEntries;
0189     a.riceParameter = riceParameter;
0190     QCOMPARE(a.isValid(), valid);
0191     WebEngineViewer::RiceDeltaEncoding b;
0192     b = a;
0193     QCOMPARE(b.isValid(), valid);
0194     QCOMPARE(a, b);
0195 }
0196 
0197 void CreatePhishingUrlDataBaseJobTest::checkAdditionElements_data()
0198 {
0199     QTest::addColumn<QByteArray>("hashString");
0200     QTest::addColumn<int>("prefixSize");
0201     QTest::addColumn<WebEngineViewer::UpdateDataBaseInfo::CompressionType>("compression");
0202     QTest::addColumn<bool>("isValid");
0203     QTest::newRow("invalid") << QByteArray() << 4 << WebEngineViewer::UpdateDataBaseInfo::RawCompression << false;
0204     QTest::newRow("notcorrectsize") << QByteArrayLiteral("IL5HqwT2c6bltw==") << 2 << WebEngineViewer::UpdateDataBaseInfo::RawCompression << false;
0205     QTest::newRow("valid") << QByteArrayLiteral("IL5HqwT2c6bltw=") << 5 << WebEngineViewer::UpdateDataBaseInfo::RawCompression << true;
0206     QTest::newRow("invalid1") << QByteArrayLiteral("foossso") << 4 << WebEngineViewer::UpdateDataBaseInfo::RawCompression << false;
0207     // QByteArray b = createHash(QByteArrayLiteral("abcde"));
0208     // QTest::newRow("valid1") << b << 5 << true;
0209 }
0210 
0211 void CreatePhishingUrlDataBaseJobTest::checkAdditionElements()
0212 {
0213     QFETCH(QByteArray, hashString);
0214     QFETCH(int, prefixSize);
0215     QFETCH(WebEngineViewer::UpdateDataBaseInfo::CompressionType, compression);
0216     QFETCH(bool, isValid);
0217 
0218     WebEngineViewer::Addition a;
0219     a.hashString = hashString;
0220     a.prefixSize = prefixSize;
0221     a.compressionType = compression;
0222     QCOMPARE(a.isValid(), isValid);
0223 }
0224 
0225 void CreatePhishingUrlDataBaseJobTest::checkRemovalElements_data()
0226 {
0227     QTest::addColumn<QList<quint32>>("lst");
0228     QTest::addColumn<WebEngineViewer::UpdateDataBaseInfo::CompressionType>("compression");
0229     QTest::addColumn<bool>("isValid");
0230     QList<quint32> lst;
0231     QTest::newRow("invalid") << lst << WebEngineViewer::UpdateDataBaseInfo::RawCompression << false;
0232     lst << 1 << 2 << 3;
0233     QTest::newRow("valid") << lst << WebEngineViewer::UpdateDataBaseInfo::RawCompression << true;
0234 }
0235 
0236 void CreatePhishingUrlDataBaseJobTest::checkRemovalElements()
0237 {
0238     QFETCH(QList<quint32>, lst);
0239     QFETCH(WebEngineViewer::UpdateDataBaseInfo::CompressionType, compression);
0240     QFETCH(bool, isValid);
0241     WebEngineViewer::Removal a;
0242     a.indexes = lst;
0243     a.compressionType = compression;
0244     QCOMPARE(a.isValid(), isValid);
0245 }
0246 
0247 void CreatePhishingUrlDataBaseJobTest::shouldParseResult_data()
0248 {
0249     QTest::addColumn<QString>("filename");
0250     QTest::addColumn<WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult>("parseResult");
0251     QTest::addColumn<WebEngineViewer::UpdateDataBaseInfo>("parseInfo");
0252     QTest::newRow("emptydocument") << QStringLiteral("empty.json") << WebEngineViewer::CreatePhishingUrlDataBaseJob::InvalidData
0253                                    << WebEngineViewer::UpdateDataBaseInfo();
0254     QTest::newRow("emptydocument2") << QStringLiteral("empty2.json") << WebEngineViewer::CreatePhishingUrlDataBaseJob::InvalidData
0255                                     << WebEngineViewer::UpdateDataBaseInfo();
0256 
0257     WebEngineViewer::UpdateDataBaseInfo value;
0258     QList<WebEngineViewer::Addition> additionList;
0259     WebEngineViewer::Addition tmp;
0260     tmp.prefixSize = 4;
0261     QByteArray hash = QByteArrayLiteral("rnGLoQ==");
0262     hash = QByteArray::fromBase64(hash);
0263     tmp.hashString = hash;
0264     tmp.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression;
0265     additionList.append(tmp);
0266     QList<WebEngineViewer::Removal> removalList;
0267     WebEngineViewer::Removal tmpRemoval;
0268     tmpRemoval.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression;
0269     tmpRemoval.indexes = QList<quint32>() << 0 << 2 << 4;
0270     removalList.append(tmpRemoval);
0271     value.minimumWaitDuration = QStringLiteral("593.440s");
0272     value.threatType = QStringLiteral("MALWARE");
0273     value.threatEntryType = QStringLiteral("URL");
0274     value.responseType = WebEngineViewer::UpdateDataBaseInfo::PartialUpdate;
0275     value.platformType = QStringLiteral("WINDOWS");
0276     value.newClientState = QStringLiteral("ChAIBRADGAEiAzAwMSiAEDABEAFGpqhd");
0277     value.sha256 = QByteArrayLiteral("YSgoRtsRlgHDqDA3LAhM1gegEpEzs1TjzU33vqsR8iM=");
0278     value.additionList = additionList;
0279     value.removalList = removalList;
0280 
0281     QTest::newRow("test1") << QStringLiteral("test1.json") << WebEngineViewer::CreatePhishingUrlDataBaseJob::ValidData << value;
0282 
0283     value.clear();
0284     QList<WebEngineViewer::Addition> additionList2;
0285     QByteArray hash1 = QByteArrayLiteral("AAAaxAAAG3QAACdhAAA");
0286     hash1 = QByteArray::fromBase64(hash1);
0287     tmp.hashString = hash1;
0288     tmp.prefixSize = 4;
0289     tmp.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression;
0290     additionList2.append(tmp);
0291     QByteArray hash2 = QByteArrayLiteral("IL5HqwT2c6bltw==");
0292     hash2 = QByteArray::fromBase64(hash2);
0293     tmp.hashString = hash2;
0294     tmp.prefixSize = 5;
0295     tmp.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression;
0296     additionList2.append(tmp);
0297 
0298     value.minimumWaitDuration = QStringLiteral("1786.932s");
0299     value.threatType = QStringLiteral("MALWARE");
0300     value.threatEntryType = QStringLiteral("URL");
0301     value.responseType = WebEngineViewer::UpdateDataBaseInfo::FullUpdate;
0302     value.platformType = QStringLiteral("WINDOWS");
0303     value.newClientState = QStringLiteral("Cg0IARAGGAEiAzAwMTABELmwARoCGAUmgN3G");
0304     value.sha256 = QByteArrayLiteral("ANcYWR8Umuoir+uNs1AhfxqW0iXEPDkxN6Pp2QF8dSs=");
0305     value.additionList = additionList2;
0306 
0307     QTest::newRow("test2") << QStringLiteral("test2.json") << WebEngineViewer::CreatePhishingUrlDataBaseJob::ValidData << value;
0308 }
0309 
0310 void CreatePhishingUrlDataBaseJobTest::shouldParseResult()
0311 {
0312     QFETCH(QString, filename);
0313     QFETCH(WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult, parseResult);
0314     QFETCH(WebEngineViewer::UpdateDataBaseInfo, parseInfo);
0315     const QByteArray ba = readJsonFile(filename);
0316     WebEngineViewer::CreatePhishingUrlDataBaseJob job;
0317     QSignalSpy spy1(&job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::finished);
0318     job.parseResult(ba);
0319     QCOMPARE(spy1.count(), 1);
0320     QCOMPARE(spy1.at(0).at(1).value<WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult>(), parseResult);
0321     QEXPECT_FAIL("test2", "Need to Investigate it", Continue);
0322     QCOMPARE(spy1.at(0).at(0).value<WebEngineViewer::UpdateDataBaseInfo>(), parseInfo);
0323 }
0324 
0325 QTEST_MAIN(CreatePhishingUrlDataBaseJobTest)
0326 
0327 #include "moc_createphishingurldatabasejobtest.cpp"