aboutsummaryrefslogtreecommitdiff
path: root/test/urlfilter/adblocktest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/urlfilter/adblocktest.cpp')
-rw-r--r--test/urlfilter/adblocktest.cpp81
1 files changed, 47 insertions, 34 deletions
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 <QtTest/QtTest>
#include "urlfilter/adblockrule.h"
-void AdBlockTest::blockByAddressPart()
+inline bool check(const std::vector<AdBlockRule> 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<AdBlockRule> 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)