From aaed4ebc642d95dfb3cddab4fad196e870077bcf Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 5 Jul 2018 19:37:05 +0200 Subject: Add web/urlfilter --- test/CMakeLists.txt | 19 ++++----- test/HostlistTest.cpp | 25 ------------ test/HostlistTest.h | 16 -------- test/autotests.qrc | 5 --- test/data/hostlist.txt | 2 - test/urlfilter/urlfiltertest.cpp | 88 ++++++++++++++++++++++++++++++++++++++++ test/urlfilter/urlfiltertest.h | 41 +++++++++++++++++++ 7 files changed, 137 insertions(+), 59 deletions(-) delete mode 100644 test/HostlistTest.cpp delete mode 100644 test/HostlistTest.h delete mode 100644 test/autotests.qrc delete mode 100644 test/data/hostlist.txt create mode 100644 test/urlfilter/urlfiltertest.cpp create mode 100644 test/urlfilter/urlfiltertest.h (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ad2ca1f..a44da07 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,16 +2,13 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -macro(create_test testname) - add_executable(${testname} - ${testname}.cpp ${testname}.h - autotests.qrc - ${ARGN}) +add_executable(UrlFilterTest + urlfilter/urlfiltertest.cpp + urlfilter/urlfiltertest.h +) - target_include_directories(${testname} PRIVATE ../lib PRIVATE ../src) - target_link_libraries(${testname} Qt5::Test Qt5::Concurrent Qt5::WebEngineWidgets) +target_include_directories(UrlFilterTest + PRIVATE ../lib/web +) - add_test(NAME smolbote-${testname} COMMAND ${testname}) -endmacro() - -create_test(HostlistTest ../src/webengine/urlinterceptor.cpp ../src/webengine/urlinterceptor.h) +target_link_libraries(UrlFilterTest Qt5::Test web) diff --git a/test/HostlistTest.cpp b/test/HostlistTest.cpp deleted file mode 100644 index 31ae11c..0000000 --- a/test/HostlistTest.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "HostlistTest.h" - -void HostlistTest::initTestCase() -{ - rules = parse(":/autotests/data/hostlist.txt"); -} - -void HostlistTest::parse_ruleCount() -{ - QVERIFY(rules.count() == 3); -} - -void HostlistTest::parse_blockSomehost() -{ - QVERIFY(rules.contains("somehost.org")); - QVERIFY(rules.value("somehost.org").isBlocking); -} - -void HostlistTest::parse_blockHost2() -{ - QVERIFY(rules.contains("host2.org")); - QVERIFY(rules.value("host2.org").isBlocking); -} - -QTEST_MAIN(HostlistTest) diff --git a/test/HostlistTest.h b/test/HostlistTest.h deleted file mode 100644 index dcfd5a3..0000000 --- a/test/HostlistTest.h +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include "webengine/urlinterceptor.h" - -class HostlistTest : public QObject -{ - Q_OBJECT - -private slots: - void initTestCase(); - void parse_ruleCount(); - void parse_blockSomehost(); - void parse_blockHost2(); - -private: - QHash rules; -}; diff --git a/test/autotests.qrc b/test/autotests.qrc deleted file mode 100644 index 5817c00..0000000 --- a/test/autotests.qrc +++ /dev/null @@ -1,5 +0,0 @@ - - - data/hostlist.txt - - \ No newline at end of file diff --git a/test/data/hostlist.txt b/test/data/hostlist.txt deleted file mode 100644 index d228e1d..0000000 --- a/test/data/hostlist.txt +++ /dev/null @@ -1,2 +0,0 @@ -0.0.0.0 somehost.org -0.0.0.0 host1.org host2.org diff --git a/test/urlfilter/urlfiltertest.cpp b/test/urlfilter/urlfiltertest.cpp new file mode 100644 index 0000000..f7ae0fb --- /dev/null +++ b/test/urlfilter/urlfiltertest.cpp @@ -0,0 +1,88 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2013-2018 David Rosca +* +* 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 . +* ============================================================ */ + +#include "urlfiltertest.h" +#include +#include + +#include + +void UrlFilterTest::matchingDomain_data() +{ + filterDomain.addDomain("example.com"); + filterDomain.addDomain("second-domain.org"); + + // Test adapted from Falkon + QTest::addColumn("domain"); + QTest::addColumn("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); + + filterDomain.setType(FilterDomain::Whitelist); + QCOMPARE(filterDomain.hasMatch(domain), result); + filterDomain.setType(FilterDomain::Blacklist); + QCOMPARE(filterDomain.hasMatch(domain), !result); + filterDomain.setType(FilterDomain::WhitelistAll); + QCOMPARE(filterDomain.hasMatch(domain), true); + filterDomain.setType(FilterDomain::BlacklistAll); + QCOMPARE(filterDomain.hasMatch(domain), false); +} + +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("requestUrl"); + QTest::addColumn("resourceType"); + QTest::addColumn("result"); + + QTest::newRow("contains 1") << "http://example.com/ads/annoying-ad.banner/something" << static_cast(QWebEngineUrlRequestInfo::ResourceTypeImage) << true; + QTest::newRow("contains 2") << "http://example.com/ads/annoying-ad.banner/something" << static_cast(QWebEngineUrlRequestInfo::ResourceTypeMedia) << false; + QTest::newRow("contains 3") << "http://example.com/ads/banner" << static_cast(QWebEngineUrlRequestInfo::ResourceTypeImage) << false; + QTest::newRow("blank") << "" << static_cast(QWebEngineUrlRequestInfo::ResourceTypeUnknown) << false; +} + +void UrlFilterTest::matchingType() +{ + QFETCH(QString, requestUrl); + QFETCH(int, resourceType); + QFETCH(bool, result); + + QCOMPARE(filterRule->matchRequestUrl(requestUrl, static_cast(resourceType)), result); +} + +QTEST_GUILESS_MAIN(UrlFilterTest) diff --git a/test/urlfilter/urlfiltertest.h b/test/urlfilter/urlfiltertest.h new file mode 100644 index 0000000..1b158e8 --- /dev/null +++ b/test/urlfilter/urlfiltertest.h @@ -0,0 +1,41 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2013-2018 David Rosca +* +* 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 . +* ============================================================ */ +#ifndef URLFILTER_TEST_H +#define URLFILTER_TEST_H + +#include "urlfilter/filterrule.h" +#include "urlfilter/filterdomain.h" +#include + +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 -- cgit v1.2.1