From c74367d82c1c7bec393548d2e5014c794333822f Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 16 Oct 2018 17:25:40 +0200 Subject: urlfilter: Add FilterTree class FilterTree is a class that holds filter rules, sorted by the domain they are to be applied on. The rules are to follow FilterLeaf as interface. - Add a hostlist rule format to FilterTree. - Add a test for hostlist format. --- test/adblock/adblocktest.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/adblock/adblocktest.cpp (limited to 'test/adblock/adblocktest.cpp') diff --git a/test/adblock/adblocktest.cpp b/test/adblock/adblocktest.cpp new file mode 100644 index 0000000..b31d965 --- /dev/null +++ b/test/adblock/adblocktest.cpp @@ -0,0 +1,65 @@ +#include "adblocktest.h" +#include +#include "filterrule.h" +#include "formats/adblockrule.h" + +inline bool check(const std::vector rules, const QUrl &url) +{ + for(const AdBlockRule &rule : rules) { + if(rule.matchesDomain(qHash(url.host())) && rule.matchesUrl(url)) + return true; + } + return false; +} + +void AdBlockTest::parseList() +{ + std::vector rules; + + QFile list("adblock.txt"); + int ruleCount = 0; + 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)); + ruleCount++; + qDebug("added rule: %s", qUtf8Printable(line)); + } + } + } + list.close(); + + // there should be 3 rules + QCOMPARE(rules.size(), ruleCount); + + // 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); + + // regular expression + QCOMPARE(check(rules, QUrl("http://another.com/banner123")), true); + QCOMPARE(check(rules, QUrl("http://another.com/banner321")), true); + QCOMPARE(check(rules, QUrl("http://another.com/banners")), false); +} + +QTEST_GUILESS_MAIN(AdBlockTest) -- cgit v1.2.1