From d35124d74bde12bceffe98c446470368ef831e53 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 17 Jul 2018 17:51:36 +0200 Subject: AdBlockTest: loading subscription --- test/urlfilter/adblocktest.cpp | 81 ++++++++++++++++++++++++------------------ test/urlfilter/adblocktest.h | 6 +--- 2 files changed, 48 insertions(+), 39 deletions(-) (limited to 'test/urlfilter') diff --git a/test/urlfilter/adblocktest.cpp b/test/urlfilter/adblocktest.cpp index 58bafc5..304a29e 100644 --- a/test/urlfilter/adblocktest.cpp +++ b/test/urlfilter/adblocktest.cpp @@ -2,44 +2,57 @@ #include #include "urlfilter/adblockrule.h" -void AdBlockTest::blockByAddressPart() +inline bool check(const std::vector rules, const QUrl &url) { - 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); + for(const AdBlockRule &rule : rules) { + if(rule.matchesUrl(url)) + return true; + } + return false; } -void AdBlockTest::parseOptions() +void AdBlockTest::parseList() { - AdBlockRule rule("annoying_banners/ads$image,script"); + std::vector rules; + + QFile list("adblock.txt"); + QCOMPARE(list.open(QIODevice::ReadOnly | QIODevice::Text), true); + { + QTextStream l(&list); + QString line; + while(l.readLineInto(&line)) { + AdBlockRule rule(line); + if(rule.isEnabled()) { + rules.emplace_back(std::move(rule)); + qDebug("added rule: %s", qUtf8Printable(line)); + } + } + } + list.close(); + + // there should be 3 rules + QCOMPARE(rules.size(), 3); + + // block by address part + QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/img")), true); + QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/bar/img?param")), true); + QCOMPARE(check(rules, QUrl("http://example.com/banner//img/foo")), true); + QCOMPARE(check(rules, QUrl("http://example.com/banner/img")), false); + QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/imgraph")), false); + QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/img.gif")), false); + + // block by domain + QCOMPARE(check(rules, QUrl("http://ads.example.com/foo.gif")), true); + QCOMPARE(check(rules, QUrl("http://server1.ads.example.com/foo.gif")), true); + QCOMPARE(check(rules, QUrl("https://ads.example.com:8000/")), true); + QCOMPARE(check(rules, QUrl("http://ads.example.com.ua/foo.gif")), false); + QCOMPARE(check(rules, QUrl("http://example.com/redirect/http://ads.example.com/")), false); + + + // block exact address + QCOMPARE(check(rules, QUrl("http://example.com/")), true); + QCOMPARE(check(rules, QUrl("http://example.com/foo.gif")), false); + QCOMPARE(check(rules, 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 index 00ad118..95cb7e2 100644 --- a/test/urlfilter/adblocktest.h +++ b/test/urlfilter/adblocktest.h @@ -7,11 +7,7 @@ class AdBlockTest : public QObject Q_OBJECT private slots: - void blockByAddressPart(); - void blockByDomain(); - void blockExactAddress(); - - void parseOptions(); + void parseList(); }; #endif // ADBLOCKTEST_H -- cgit v1.2.1