aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-07-17 17:51:36 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-07-17 17:51:36 +0200
commitd35124d74bde12bceffe98c446470368ef831e53 (patch)
tree910d4053ee7915263dfc3a2f772c9f249cb1de9c /test
parentUpdate .gitignore (diff)
downloadsmolbote-d35124d74bde12bceffe98c446470368ef831e53.tar.xz
AdBlockTest: loading subscription
Diffstat (limited to 'test')
-rw-r--r--test/adblock.txt4
-rw-r--r--test/urlfilter/adblocktest.cpp81
-rw-r--r--test/urlfilter/adblocktest.h6
3 files changed, 52 insertions, 39 deletions
diff --git a/test/adblock.txt b/test/adblock.txt
new file mode 100644
index 0000000..38d4688
--- /dev/null
+++ b/test/adblock.txt
@@ -0,0 +1,4 @@
+/banner/*/img^
+||ads.example.com^
+|http://example.com/|
+
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)
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