diff options
| author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-07-18 10:07:51 +0200 | 
|---|---|---|
| committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-07-18 10:07:51 +0200 | 
| commit | f507a7a5557c7cca9570c684920e055c5251b08e (patch) | |
| tree | 0fad83fc99053006b375608e2a2d04388776ad89 /test | |
| parent | AdBlockTest: loading subscription (diff) | |
| download | smolbote-f507a7a5557c7cca9570c684920e055c5251b08e.tar.xz | |
AdBlockRule: move matching logic to FilterRule
Diffstat (limited to 'test')
| -rw-r--r-- | test/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | test/urlfilter/adblocktest.cpp | 8 | ||||
| -rw-r--r-- | test/urlfilter/urlfiltertest.cpp | 81 | ||||
| -rw-r--r-- | test/urlfilter/urlfiltertest.h | 41 | 
4 files changed, 4 insertions, 137 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 414d616..a026ad6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,17 +2,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)  set(CMAKE_AUTOMOC ON)  set(CMAKE_AUTORCC ON) -add_executable(UrlFilterTest -    urlfilter/urlfiltertest.cpp -    urlfilter/urlfiltertest.h -) - -target_include_directories(UrlFilterTest -    PRIVATE ../lib/web -) - -target_link_libraries(UrlFilterTest Qt5::Test web) -  add_executable(AdBlockTest      urlfilter/adblocktest.cpp      urlfilter/adblocktest.h diff --git a/test/urlfilter/adblocktest.cpp b/test/urlfilter/adblocktest.cpp index 304a29e..416bc20 100644 --- a/test/urlfilter/adblocktest.cpp +++ b/test/urlfilter/adblocktest.cpp @@ -2,10 +2,10 @@  #include <QtTest/QtTest>  #include "urlfilter/adblockrule.h" -inline bool check(const std::vector<AdBlockRule> rules, const QUrl &url) +inline bool check(const std::vector<FilterRule> rules, const QUrl &url)  { -    for(const AdBlockRule &rule : rules) { -        if(rule.matchesUrl(url)) +    for(const FilterRule &rule : rules) { +        if(rule.matchesDomain(url.host()) && rule.matchesUrl(url))              return true;      }      return false; @@ -13,7 +13,7 @@ inline bool check(const std::vector<AdBlockRule> rules, const QUrl &url)  void AdBlockTest::parseList()  { -    std::vector<AdBlockRule> rules; +    std::vector<FilterRule> rules;      QFile list("adblock.txt");      QCOMPARE(list.open(QIODevice::ReadOnly | QIODevice::Text), true); diff --git a/test/urlfilter/urlfiltertest.cpp b/test/urlfilter/urlfiltertest.cpp deleted file mode 100644 index eb12421..0000000 --- a/test/urlfilter/urlfiltertest.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* ============================================================ -* Falkon - Qt web browser -* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program.  If not, see <http://www.gnu.org/licenses/>. -* ============================================================ */ - -#include "urlfiltertest.h" -#include <QtTest/QtTest> -#include <QUrl> - -#include <QJsonObject> - -void UrlFilterTest::matchingDomain_data() -{ -    filterDomain.addDomain("example.com"); -    filterDomain.addDomain("second-domain.org"); - -    // Test adapted from Falkon -    QTest::addColumn<QString>("domain"); -    QTest::addColumn<bool>("result"); - -    //            description           site domain                result -    QTest::newRow("missing tld")     << "example"               << false; -    QTest::newRow("different tld")   << "example.org"           << false; -    QTest::newRow("exact match")     << "example.com"           << true; -    QTest::newRow("exact match 2")   << "second-domain.org"     << true; -    QTest::newRow("subdomain match") << "www.example.com"       << true; -    QTest::newRow("subdomain match") << "www.test.example.com"  << true; -    QTest::newRow("similar domain")  << "anotherexample.com"    << false; -    QTest::newRow("empty domain")    << ""                      << false; -} - -void UrlFilterTest::matchingDomain() -{ -    QFETCH(QString, domain); -    QFETCH(bool, result); - -    QCOMPARE(filterDomain.hasMatch(domain), result); -} - -void UrlFilterTest::matchingType_data() -{ -    QJsonObject j; -    j.insert("action", "Blacklist"); -    j.insert("contains", "annoying-ad.banner"); - -    filterRule = new FilterRule(j); -    Q_ASSERT(filterRule->isValid()); - -    QTest::addColumn<QString>("requestUrl"); -    QTest::addColumn<int>("resourceType"); -    QTest::addColumn<bool>("result"); - -    QTest::newRow("contains 1") << "http://example.com/ads/annoying-ad.banner/something" << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeImage) << true; -    QTest::newRow("contains 2") << "http://example.com/ads/annoying-ad.banner/something" << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeMedia) << false; -    QTest::newRow("contains 3") << "http://example.com/ads/banner"                       << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeImage) << false; -    QTest::newRow("blank")      << ""                                                    << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeUnknown) << false; -} - -void UrlFilterTest::matchingType() -{ -    QFETCH(QString, requestUrl); -    QFETCH(int, resourceType); -    QFETCH(bool, result); - -    QCOMPARE(filterRule->matchRequestUrl(requestUrl, static_cast<QWebEngineUrlRequestInfo::ResourceType>(resourceType)), result); -} - -QTEST_GUILESS_MAIN(UrlFilterTest) diff --git a/test/urlfilter/urlfiltertest.h b/test/urlfilter/urlfiltertest.h deleted file mode 100644 index 1b158e8..0000000 --- a/test/urlfilter/urlfiltertest.h +++ /dev/null @@ -1,41 +0,0 @@ -/* ============================================================ -* Falkon - Qt web browser -* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program.  If not, see <http://www.gnu.org/licenses/>. -* ============================================================ */ -#ifndef URLFILTER_TEST_H -#define URLFILTER_TEST_H - -#include "urlfilter/filterrule.h" -#include "urlfilter/filterdomain.h" -#include <QObject> - -class UrlFilterTest : public QObject -{ -    Q_OBJECT - -private slots: -    void matchingDomain_data(); -    void matchingDomain(); - -    void matchingType_data(); -    void matchingType(); - -private: -    FilterDomain filterDomain; -    FilterRule *filterRule; -}; - -#endif // URLFILTER_TEST_H  | 
