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 "checkphishingurlcachetest.h"
0008 #include "../checkphishingurlcache.h"
0009 #include "../checkphishingurlutil.h"
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <QStandardPaths>
0013 #include <QTest>
0014 
0015 CheckPhishingUrlCacheTest::CheckPhishingUrlCacheTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018     QStandardPaths::setTestModeEnabled(true);
0019 }
0020 
0021 CheckPhishingUrlCacheTest::~CheckPhishingUrlCacheTest() = default;
0022 
0023 void CheckPhishingUrlCacheTest::shouldNotBeAMalware()
0024 {
0025     WebEngineViewer::CheckPhishingUrlCache cache;
0026     QCOMPARE(cache.urlStatus(QUrl(QStringLiteral("http://www.kde.org"))), WebEngineViewer::CheckPhishingUrlCache::Unknown);
0027 }
0028 
0029 void CheckPhishingUrlCacheTest::shouldAddValue_data()
0030 {
0031     QTest::addColumn<QUrl>("url");
0032     QTest::addColumn<bool>("validurl");
0033     QTest::addColumn<uint>("seconds");
0034     QTest::addColumn<WebEngineViewer::CheckPhishingUrlCache::UrlStatus>("status");
0035     uint currentValue = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
0036     QTest::newRow("valid") << QUrl(QStringLiteral("http://www.kde.org")) << true << currentValue << WebEngineViewer::CheckPhishingUrlCache::UrlOk;
0037     QTest::newRow("malware1validcache") << QUrl(QStringLiteral("http://www.kde.org")) << false << (currentValue + 2000)
0038                                         << WebEngineViewer::CheckPhishingUrlCache::MalWare;
0039     QTest::newRow("malware1invalidcache") << QUrl(QStringLiteral("http://www.kde.org")) << false << (currentValue - 2000)
0040                                           << WebEngineViewer::CheckPhishingUrlCache::Unknown;
0041 }
0042 
0043 void CheckPhishingUrlCacheTest::shouldAddValue()
0044 {
0045     QFETCH(QUrl, url);
0046     QFETCH(bool, validurl);
0047     QFETCH(uint, seconds);
0048     QFETCH(WebEngineViewer::CheckPhishingUrlCache::UrlStatus, status);
0049 
0050     WebEngineViewer::CheckPhishingUrlCache cache;
0051     cache.addCheckingUrlResult(url, validurl, seconds);
0052     QCOMPARE(cache.urlStatus(url), status);
0053     cache.clearCache();
0054 }
0055 
0056 void CheckPhishingUrlCacheTest::shouldStoreValues()
0057 {
0058     WebEngineViewer::CheckPhishingUrlCache cache;
0059     QUrl url = QUrl(QStringLiteral("http://www.kde.org"));
0060 
0061     uint currentValue = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
0062     cache.addCheckingUrlResult(url, false, currentValue + 2000);
0063 
0064     // Add malware
0065     KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
0066     KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("MalwareUrl"));
0067     QList<QUrl> listMalware = grp.readEntry("Url", QList<QUrl>());
0068     QList<double> listMalwareCachedTime = grp.readEntry("CachedTime", QList<double>());
0069 
0070     QCOMPARE(listMalware.count(), 1);
0071     QCOMPARE(listMalwareCachedTime.count(), 1);
0072 
0073     // Clear cache
0074     cache.clearCache();
0075     KConfig phishingurlKConfig2(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
0076     grp = phishingurlKConfig2.group(QStringLiteral("MalwareUrl"));
0077     listMalware = grp.readEntry("Url", QList<QUrl>());
0078     listMalwareCachedTime = grp.readEntry("CachedTime", QList<double>());
0079 
0080     QCOMPARE(listMalware.count(), 0);
0081     QCOMPARE(listMalwareCachedTime.count(), 0);
0082 
0083     // Add correct url
0084     cache.clearCache();
0085 
0086     cache.addCheckingUrlResult(url, true, 0);
0087 
0088     KConfig phishingurlKConfig3(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
0089     grp = phishingurlKConfig3.group(QStringLiteral("MalwareUrl"));
0090     listMalware = grp.readEntry("Url", QList<QUrl>());
0091     listMalwareCachedTime = grp.readEntry("CachedTime", QList<double>());
0092 
0093     QCOMPARE(listMalware.count(), 0);
0094     QCOMPARE(listMalwareCachedTime.count(), 0);
0095 
0096     cache.clearCache();
0097 
0098     // Load another instance => data were saved
0099     cache.addCheckingUrlResult(url, false, currentValue + 2000);
0100 
0101     WebEngineViewer::CheckPhishingUrlCache cache2;
0102     KConfig phishingurlKConfig4(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
0103     grp = phishingurlKConfig4.group(QStringLiteral("MalwareUrl"));
0104     listMalware = grp.readEntry("Url", QList<QUrl>());
0105     listMalwareCachedTime = grp.readEntry("CachedTime", QList<double>());
0106 
0107     QCOMPARE(listMalware.count(), 1);
0108     QCOMPARE(listMalwareCachedTime.count(), 1);
0109 
0110     // Need to clear to avoid problem when we relaunch this autotest
0111     cache2.clearCache();
0112     cache.clearCache();
0113 }
0114 
0115 QTEST_MAIN(CheckPhishingUrlCacheTest)
0116 
0117 #include "moc_checkphishingurlcachetest.cpp"