File indexing completed on 2025-02-23 05:00:16
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 "searchfullhashjobtest.h" 0008 #include "../checkphishingurlutil.h" 0009 #include "../searchfullhashjob.h" 0010 #include <QTest> 0011 0012 SearchFullHashJobTest::SearchFullHashJobTest(QObject *parent) 0013 : QObject(parent) 0014 { 0015 } 0016 0017 SearchFullHashJobTest::~SearchFullHashJobTest() = default; 0018 0019 void SearchFullHashJobTest::shouldNotBeAbleToStartWithEmptyUrl() 0020 { 0021 WebEngineViewer::SearchFullHashJob job; 0022 QVERIFY(!job.canStart()); 0023 } 0024 0025 void SearchFullHashJobTest::shouldCreateRequest_data() 0026 { 0027 QTest::addColumn<QHash<QByteArray, QByteArray>>("hash"); 0028 QTest::addColumn<QStringList>("databaseHash"); 0029 QTest::addColumn<QString>("request"); 0030 QTest::addColumn<QUrl>("url"); 0031 QTest::addColumn<bool>("canStart"); 0032 QTest::newRow("no hash") << QHash<QByteArray, QByteArray>() << QStringList() << QString() << QUrl() << false; 0033 QTest::newRow("database hash but not hash and not url") 0034 << QHash<QByteArray, QByteArray>() << QStringList{QStringLiteral("boo")} << QString() << QUrl() << false; 0035 QHash<QByteArray, QByteArray> hashes; 0036 hashes.insert(QByteArrayLiteral("bla"), QByteArrayLiteral("bla")); 0037 QTest::newRow("database hash but hash and not url") << hashes << QStringList{QStringLiteral("boo")} << QString() << QUrl() << false; 0038 QTest::newRow("database hash and hash") 0039 << hashes << QStringList{QStringLiteral("boo")} 0040 << QStringLiteral( 0041 "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"clientStates\":[\"boo\"],\"threatInfo\":{\"platformTypes\":[\"WINDOWS\"]," 0042 "\"threatEntries\":[{\"hash\":\"bla\"}],\"threatEntryTypes\":[\"URL\"],\"threatTypes\":[\"MALWARE\"]}}") 0043 .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps()) 0044 << QUrl(QStringLiteral("http://www.kde.org")) << true; 0045 QTest::newRow("multi database hash and hash") 0046 << hashes << (QStringList() << QStringLiteral("boo") << QStringLiteral("bli")) 0047 << QStringLiteral( 0048 "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"clientStates\":[\"boo\",\"bli\"],\"threatInfo\":{\"platformTypes\":[\"WINDOWS\"]," 0049 "\"threatEntries\":[{\"hash\":\"bla\"}],\"threatEntryTypes\":[\"URL\"],\"threatTypes\":[\"MALWARE\"]}}") 0050 .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps()) 0051 << QUrl(QStringLiteral("http://www.kde.org")) << true; 0052 } 0053 0054 void SearchFullHashJobTest::shouldCreateRequest() 0055 { 0056 using hashdef = QHash<QByteArray, QByteArray>; 0057 QFETCH(hashdef, hash); 0058 QFETCH(QStringList, databaseHash); 0059 QFETCH(QString, request); 0060 QFETCH(QUrl, url); 0061 QFETCH(bool, canStart); 0062 WebEngineViewer::SearchFullHashJob job; 0063 job.setDatabaseState(databaseHash); 0064 job.setSearchFullHashForUrl(url); 0065 if (!hash.isEmpty()) { 0066 job.setSearchHashs(hash); 0067 } 0068 QCOMPARE(job.canStart(), canStart); 0069 if (canStart) { 0070 QCOMPARE(job.jsonRequest(), request.toLatin1()); 0071 } 0072 } 0073 0074 QTEST_GUILESS_MAIN(SearchFullHashJobTest) 0075 0076 #include "moc_searchfullhashjobtest.cpp"