aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/test/filterlist.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-04-14 22:09:01 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-04-21 20:14:56 +0300
commit68da62a64cd65a8b28dcfe61b2174e55a47b7517 (patch)
treec7058ef19c30c86c0d1626c3fcb691b944f02e02 /staging/adblock/test/filterlist.cpp
parentAdd tests for MatcherRule and RegexRule (diff)
downloadsmolbote-68da62a64cd65a8b28dcfe61b2174e55a47b7517.tar.xz
Add some FilterList tests
Diffstat (limited to 'staging/adblock/test/filterlist.cpp')
-rw-r--r--staging/adblock/test/filterlist.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/staging/adblock/test/filterlist.cpp b/staging/adblock/test/filterlist.cpp
new file mode 100644
index 0000000..6b27904
--- /dev/null
+++ b/staging/adblock/test/filterlist.cpp
@@ -0,0 +1,89 @@
+#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 std::array<std::pair<QString, bool>, 5> testUrls = {
+ std::make_pair("http://ads.example.com/foo.gif", true),
+ std::make_pair("http://server1.ads.example.com/foo.gif", true),
+ std::make_pair("https://ads.example.com:8000/", true),
+ std::make_pair("http://ads.example.com.ua/foo.gif", false),
+ std::make_pair("http://example.com/redirect/http://ads.example.com/", false)
+ };
+
+ auto *rule = FilterList::parseRule("||ads.example.com^");
+ REQUIRE(rule->shouldBlock());
+ REQUIRE(!rule->shouldRedirect());
+
+ for(const auto &pair : testUrls) {
+ REQUIRE(rule->hasMatch(&pair.first) == pair.second);
+ }
+
+ delete rule;
+}
+
+TEST_CASE("string equals")
+{
+ const std::array<std::pair<QString, bool>, 3> testUrls = {
+ std::make_pair("http://example.com/", true),
+ std::make_pair("http://example.com/foo.gif", false),
+ std::make_pair("http://example.info/redirect/http://example.com/", false)
+ };
+
+ auto *rule = FilterList::parseRule("|http://example.com/|");
+ REQUIRE(rule->shouldBlock());
+ REQUIRE(!rule->shouldRedirect());
+
+ for(const auto &pair : testUrls) {
+ REQUIRE(rule->hasMatch(&pair.first) == pair.second);
+ }
+
+ delete rule;
+}
+
+TEST_CASE("string starts with")
+{
+ 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));
+ REQUIRE(!rule->hasMatch(&allows));
+ delete rule;
+}
+
+TEST_CASE("string ends with")
+{
+ 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));
+ REQUIRE(!rule->hasMatch(&allows));
+ delete rule;
+}