From 9808d18fa6cd19400f08897014a9948f168927df Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 9 Jul 2018 20:19:30 +0200 Subject: AdBlock rule testing --- test/CMakeLists.txt | 9 +++++++++ test/urlfilter/adblocktest.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ test/urlfilter/adblocktest.h | 15 +++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 test/urlfilter/adblocktest.cpp create mode 100644 test/urlfilter/adblocktest.h (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a44da07..414d616 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,3 +12,12 @@ target_include_directories(UrlFilterTest ) target_link_libraries(UrlFilterTest Qt5::Test web) + +add_executable(AdBlockTest + urlfilter/adblocktest.cpp + urlfilter/adblocktest.h +) + +target_include_directories(AdBlockTest PRIVATE ../lib/web) + +target_link_libraries(AdBlockTest Qt5::Test web) diff --git a/test/urlfilter/adblocktest.cpp b/test/urlfilter/adblocktest.cpp new file mode 100644 index 0000000..d1060f1 --- /dev/null +++ b/test/urlfilter/adblocktest.cpp @@ -0,0 +1,40 @@ +#include "adblocktest.h" +#include +#include "urlfilter/adblockrule.h" + +void AdBlockTest::blockByAddressPart() +{ + AdBlockRule rule("/banner/*/img^"); + + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/banner/foo/img")), true); + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/banner/foo/bar/img?param")), true); + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/banner//img/foo")), true); + + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/banner/img")), false); + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/banner/foo/imgraph")), false); + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/banner/foo/img.gif")), false); +} + +void AdBlockTest::blockByDomain() +{ + AdBlockRule rule("||ads.example.com^"); + + QCOMPARE(rule.shouldBlock(QUrl("http://ads.example.com/foo.gif")), true); + QCOMPARE(rule.shouldBlock(QUrl("http://server1.ads.example.com/foo.gif")), true); + QCOMPARE(rule.shouldBlock(QUrl("https://ads.example.com:8000/")), true); + + QCOMPARE(rule.shouldBlock(QUrl("http://ads.example.com.ua/foo.gif")), false); + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/redirect/http://ads.example.com/")), false); +} + +void AdBlockTest::blockExactAddress() +{ + AdBlockRule rule("|http://example.com/|"); + + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/")), true); + + QCOMPARE(rule.shouldBlock(QUrl("http://example.com/foo.gif")), false); + QCOMPARE(rule.shouldBlock(QUrl("http://example.info/redirect/http://example.com/")), false); +} + +QTEST_GUILESS_MAIN(AdBlockTest) diff --git a/test/urlfilter/adblocktest.h b/test/urlfilter/adblocktest.h new file mode 100644 index 0000000..a7b9f12 --- /dev/null +++ b/test/urlfilter/adblocktest.h @@ -0,0 +1,15 @@ +#ifndef ADBLOCKTEST_H +#define ADBLOCKTEST_H + +#include +class AdBlockTest : public QObject +{ + Q_OBJECT + +private slots: + void blockByAddressPart(); + void blockByDomain(); + void blockExactAddress(); +}; + +#endif // ADBLOCKTEST_H -- cgit v1.2.1