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 "checkphishingurljobtest.h"
0008 #include "../checkphishingurljob.h"
0009 #include "../checkphishingurlutil.h"
0010 
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 
0014 CheckPhishingUrlJobTest::CheckPhishingUrlJobTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 CheckPhishingUrlJobTest::~CheckPhishingUrlJobTest() = default;
0020 
0021 void CheckPhishingUrlJobTest::shouldNotBeAbleToStartWithEmptyUrl()
0022 {
0023     WebEngineViewer::CheckPhishingUrlJob job;
0024     QVERIFY(!job.canStart());
0025 }
0026 
0027 void CheckPhishingUrlJobTest::shouldCreateRequest_data()
0028 {
0029     QTest::addColumn<QUrl>("url");
0030     QTest::addColumn<QString>("request");
0031     QTest::addColumn<bool>("canStart");
0032     QTest::newRow("no url") << QUrl() << QString() << false;
0033     QTest::newRow("value") << QUrl(QStringLiteral("http://www.kde.org"))
0034                            << QStringLiteral(
0035                                   "{\"client\":{\"clientId\":\"KDE\",\"clientVersion\":\"%1\"},\"threatInfo\":{\"platformTypes\":[\"WINDOWS\"],"
0036                                   "\"threatEntries\":[{\"url\":\"http://www.kde.org\"}],\"threatEntryTypes\":[\"URL\"],\"threatTypes\":[\"MALWARE\"]}}")
0037                                   .arg(WebEngineViewer::CheckPhishingUrlUtil::versionApps())
0038                            << true;
0039 }
0040 
0041 void CheckPhishingUrlJobTest::shouldCreateRequest()
0042 {
0043     QFETCH(QUrl, url);
0044     QFETCH(QString, request);
0045     QFETCH(bool, canStart);
0046     WebEngineViewer::CheckPhishingUrlJob job;
0047     job.setUrl(url);
0048     QCOMPARE(job.canStart(), canStart);
0049     if (canStart) {
0050         QCOMPARE(job.jsonRequest(), request.toLatin1());
0051     }
0052 }
0053 
0054 void CheckPhishingUrlJobTest::shouldParseResult_data()
0055 {
0056     QTest::addColumn<QByteArray>("input");
0057     QTest::addColumn<QUrl>("checkedUrl");
0058     QTest::addColumn<uint>("verifyCacheAfterThisTime");
0059     QTest::addColumn<WebEngineViewer::CheckPhishingUrlUtil::UrlStatus>("urlStatus");
0060     uint val = 0;
0061     QTest::newRow("empty") << QByteArray() << QUrl() << val << WebEngineViewer::CheckPhishingUrlUtil::Unknown;
0062     QTest::newRow("empty1") << QByteArray() << QUrl(QStringLiteral("http://www.kde.org")) << val << WebEngineViewer::CheckPhishingUrlUtil::Unknown;
0063 
0064     QTest::newRow("urlOk") << QByteArrayLiteral("{}") << QUrl(QStringLiteral("http://www.kde.org")) << val << WebEngineViewer::CheckPhishingUrlUtil::Ok;
0065 
0066     val = WebEngineViewer::CheckPhishingUrlUtil::refreshingCacheAfterThisTime(WebEngineViewer::CheckPhishingUrlUtil::convertToSecond(QStringLiteral("300s")));
0067     QTest::newRow("malware") << QByteArrayLiteral(
0068         "{\"matches\":[{\"threatType\":\"MALWARE\",\"platformType\":\"WINDOWS\",\"threat\":{\"url\":\"http://malware.testing.google.test/testing/malware/"
0069         "\"},\"cacheDuration\":\"300s\",\"threatEntryType\":\"URL\"}]}")
0070                              << QUrl(QStringLiteral("http://malware.testing.google.test/testing/malware/")) << val
0071                              << WebEngineViewer::CheckPhishingUrlUtil::MalWare;
0072 }
0073 
0074 void CheckPhishingUrlJobTest::shouldParseResult()
0075 {
0076     QFETCH(QByteArray, input);
0077     QFETCH(QUrl, checkedUrl);
0078     QFETCH(uint, verifyCacheAfterThisTime);
0079     QFETCH(WebEngineViewer::CheckPhishingUrlUtil::UrlStatus, urlStatus);
0080 
0081     WebEngineViewer::CheckPhishingUrlJob job;
0082     job.setUrl(checkedUrl);
0083     QSignalSpy spy1(&job, &WebEngineViewer::CheckPhishingUrlJob::result);
0084     job.parse(input);
0085     QCOMPARE(spy1.count(), 1);
0086     QCOMPARE(spy1.at(0).at(0).value<WebEngineViewer::CheckPhishingUrlUtil::UrlStatus>(), urlStatus);
0087     QCOMPARE(spy1.at(0).at(1).toUrl(), checkedUrl);
0088     QCOMPARE(spy1.at(0).at(2).value<uint>(), verifyCacheAfterThisTime);
0089 }
0090 
0091 QTEST_MAIN(CheckPhishingUrlJobTest)
0092 
0093 #include "moc_checkphishingurljobtest.cpp"