aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/test
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-02-10 20:58:39 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-05-30 11:01:35 +0300
commitd1287f43964633035938f4f4d4133bb6d9da7b3e (patch)
treed09efa4074815c20be9bd6348203fe4336dfe716 /staging/adblock/test
parentFix segfault in release build (diff)
downloadsmolbote-d1287f43964633035938f4f4d4133bb6d9da7b3e.tar.xz
staging: smolblok
smolblok is a replacement for the current lib/urlfilter AdBlockPlus and hostlist format filter parser. It is a library that uses plugins to provide support for different filter formats. staging/adblock: AdBlockPlus parser plugin plugins/smolblok_hostlist: hostlist format parser plugin Headers will be installed to include/smolbote/ Remove lib/urlfilter
Diffstat (limited to 'staging/adblock/test')
-rw-r--r--staging/adblock/test/filterlist.cpp118
-rw-r--r--staging/adblock/test/options.cpp42
-rw-r--r--staging/adblock/test/rule.cpp86
3 files changed, 246 insertions, 0 deletions
diff --git a/staging/adblock/test/filterlist.cpp b/staging/adblock/test/filterlist.cpp
new file mode 100644
index 0000000..ca122ac
--- /dev/null
+++ b/staging/adblock/test/filterlist.cpp
@@ -0,0 +1,118 @@
+#define CATCH_CONFIG_MAIN
+#include "filterlist.h"
+#include <QBuffer>
+#include <catch2/catch.hpp>
+
+using namespace AdblockPlus;
+
+QByteArray sampleList =
+ R"(! comment on line
+! Last modified: 1 Jan 2000 00:00 UTC
+! Expires: 4 days (update frequency)
+)";
+
+TEST_CASE("placeholder")
+{
+ QBuffer buffer(&sampleList);
+ buffer.open(QIODevice::ReadOnly | QIODevice::Text);
+
+ AdblockPlus::FilterList list(buffer);
+ REQUIRE(!list.isUpToDate());
+}
+
+TEST_CASE("domain match")
+{
+ const QString defaultUrl = "";
+
+ const QString block1 = "http://ads.example.com/foo.gif";
+ const QString block2 = "http://server1.ads.example.com/foo.gif";
+ const QString block3 = "https://ads.example.com:8000/";
+ const QString block4 = "https://ads.example.com";
+
+ const QString allow1 = "http://ads.example.com.ua/foo.gif";
+ const QString allow2 = "http://example.com/redirect/http://ads.example.com/";
+
+ auto *rule = FilterList::parseRule("||ads.example.com^");
+ REQUIRE(rule->shouldBlock());
+ REQUIRE(!rule->shouldRedirect());
+
+ REQUIRE(rule->hasMatch(&block1, &defaultUrl, &defaultUrl));
+ REQUIRE(rule->hasMatch(&block2, &defaultUrl, &defaultUrl));
+ REQUIRE(rule->hasMatch(&block3, &defaultUrl, &defaultUrl));
+ REQUIRE(rule->hasMatch(&block4, &defaultUrl, &defaultUrl));
+
+ REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl));
+
+ delete rule;
+}
+
+TEST_CASE("string equals")
+{
+ const QString defaultUrl = "";
+
+ const QString block = "http://example.com/";
+
+ const QString allow1 = "http://example.com/foo.gif";
+ const QString allow2 = "http://example.info/redirect/http://example.com/";
+
+ auto *rule = FilterList::parseRule("|http://example.com/|");
+ REQUIRE(rule->shouldBlock());
+ REQUIRE(!rule->shouldRedirect());
+
+ REQUIRE(rule->hasMatch(&block, &defaultUrl, &defaultUrl));
+
+ REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl));
+
+ delete rule;
+}
+
+TEST_CASE("string starts with")
+{
+ const QString defaultUrl = "";
+
+ auto *rule = FilterList::parseRule("|http://baddomain.example/");
+ REQUIRE(rule->shouldBlock());
+ REQUIRE(!rule->shouldRedirect());
+
+ const QString blocks = "http://baddomain.example/banner.gif";
+ const QString allows = "http://gooddomain.example/analyze?http://baddomain.example";
+
+ REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl));
+ delete rule;
+}
+
+TEST_CASE("string ends with")
+{
+ const QString defaultUrl = "";
+
+ auto *rule = FilterList::parseRule("swf|");
+ REQUIRE(rule->shouldBlock());
+ REQUIRE(!rule->shouldRedirect());
+
+ const QString blocks = "http://example.com/annoyingflash.swf";
+ const QString allows = "http://example.com/swf/index.html";
+
+ REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl));
+ delete rule;
+}
+
+TEST_CASE("regular expressions")
+{
+ const QString defaultUrl = "";
+
+ auto *rule = FilterList::parseRule("/banner\\d+/");
+ const QString matches1 = "banner123";
+ const QString matches2 = "banner321";
+
+ const QString ignores = "banners";
+
+ REQUIRE(rule->hasMatch(&matches1, &defaultUrl, &defaultUrl));
+ REQUIRE(rule->hasMatch(&matches2, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule->hasMatch(&ignores, &defaultUrl, &defaultUrl));
+ delete rule;
+}
+
diff --git a/staging/adblock/test/options.cpp b/staging/adblock/test/options.cpp
new file mode 100644
index 0000000..67dc143
--- /dev/null
+++ b/staging/adblock/test/options.cpp
@@ -0,0 +1,42 @@
+#define CATCH_CONFIG_MAIN
+#include "options.h"
+#include <catch2/catch.hpp>
+
+using namespace AdblockPlus;
+
+SCENARIO("parsing adblock options")
+{
+ Options opt;
+
+ GIVEN("an unknown option")
+ {
+ const QString unknown = "unknown";
+ THEN("the option is not parsed")
+ {
+ QStringRef unknown_ref(&unknown);
+ REQUIRE(!opt.parseAbp(unknown_ref));
+ }
+ }
+
+ GIVEN("match-case,document,~subdocument")
+ {
+ const QString options = "match-case,document,~subdocument";
+ REQUIRE(opt.parseAbp(&options));
+
+ WHEN("match-case")
+ {
+ REQUIRE(opt.matchcase);
+ }
+
+ WHEN("testing set/unset options")
+ {
+ REQUIRE(opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeMainFrame));
+ REQUIRE(!opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeSubFrame));
+ }
+
+ WHEN("testing other options")
+ {
+ REQUIRE(opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeStylesheet));
+ }
+ }
+}
diff --git a/staging/adblock/test/rule.cpp b/staging/adblock/test/rule.cpp
new file mode 100644
index 0000000..07186b9
--- /dev/null
+++ b/staging/adblock/test/rule.cpp
@@ -0,0 +1,86 @@
+#define CATCH_CONFIG_MAIN
+#include "rule.h"
+#include <catch2/catch.hpp>
+
+using namespace AdblockPlus;
+
+SCENARIO("MatcherRule")
+{
+ GIVEN("options with case sensitive pattern")
+ {
+ const QString defaultUrl = "";
+
+ const Options opt { .matchcase=true };
+ const QString patternContains("this string contains the pattern in it");
+ const QString patternBegins("pattern starts this string");
+ const QString patternEnds("this string ends with pattern");
+ const QString patternMissing("and this one does not");
+
+ WHEN("contains")
+ {
+ MatcherRule rule("pattern", opt);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched anywhere in the URL")
+ {
+ REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
+ REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl));
+ REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
+ }
+ }
+
+ WHEN("startsWith")
+ {
+ MatcherRule rule("pattern", opt, MatcherRule::UrlStartsWith);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched if at the start of the URL")
+ {
+ REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
+ REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
+ }
+ }
+
+ WHEN("endsWith")
+ {
+ MatcherRule rule("pattern", opt, MatcherRule::UrlEndsWith);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched if at the end of the URL")
+ {
+ REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl));
+ REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
+ }
+ }
+ }
+}
+
+SCENARIO("RegexRule")
+{
+ GIVEN("options with case sensitive pattern")
+ {
+ const QString defaultUrl;
+
+ const Options opt { .matchcase=true };
+ const QString patternContains("this string contains the pattern in it");
+ const QString patternMissing("and this one does not");
+
+ WHEN("contains")
+ {
+ RegexRule rule("pattern", opt);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched anywhere in the URL")
+ {
+ REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
+ REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
+ }
+ }
+ }
+}
+