aboutsummaryrefslogtreecommitdiff
path: root/plugins/smolblok_hostlist/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 /plugins/smolblok_hostlist/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 'plugins/smolblok_hostlist/test')
-rw-r--r--plugins/smolblok_hostlist/test/filterlist.cpp29
-rw-r--r--plugins/smolblok_hostlist/test/hostlist.txt6
-rw-r--r--plugins/smolblok_hostlist/test/plugin.cpp27
-rw-r--r--plugins/smolblok_hostlist/test/rule.cpp57
4 files changed, 119 insertions, 0 deletions
diff --git a/plugins/smolblok_hostlist/test/filterlist.cpp b/plugins/smolblok_hostlist/test/filterlist.cpp
new file mode 100644
index 0000000..4aa532b
--- /dev/null
+++ b/plugins/smolblok_hostlist/test/filterlist.cpp
@@ -0,0 +1,29 @@
+#define CATCH_CONFIG_MAIN
+#include "filterlist.h"
+#include <QFile>
+#include <catch2/catch.hpp>
+
+using namespace Hostlist;
+
+TEST_CASE("Hostlist")
+{
+ Filterlist list;
+
+ const QString filename(qgetenv("HOSTLIST_TXT"));
+ REQUIRE(!filename.isEmpty());
+
+ QFile f(filename);
+ REQUIRE(f.open(QIODevice::ReadOnly | QIODevice::Text));
+
+ REQUIRE(list.load(f));
+ f.close();
+
+ REQUIRE(list.count() == 4);
+
+ REQUIRE(list.findMatch("blockeddomain.first"));
+ REQUIRE(list.findMatch("blockeddomain.second"));
+
+ REQUIRE(list.findMatch("localhost.localdomain"));
+
+ REQUIRE(!list.findMatch("other.domain"));
+}
diff --git a/plugins/smolblok_hostlist/test/hostlist.txt b/plugins/smolblok_hostlist/test/hostlist.txt
new file mode 100644
index 0000000..a0b4e5c
--- /dev/null
+++ b/plugins/smolblok_hostlist/test/hostlist.txt
@@ -0,0 +1,6 @@
+# This is a comment, and after it comes a blank line
+
+127.0.0.1 localhost.localdomain
+
+0.0.0.0 blockeddomain.com
+0.0.0.0 blockeddomain.first blockeddomain.second
diff --git a/plugins/smolblok_hostlist/test/plugin.cpp b/plugins/smolblok_hostlist/test/plugin.cpp
new file mode 100644
index 0000000..fad34f2
--- /dev/null
+++ b/plugins/smolblok_hostlist/test/plugin.cpp
@@ -0,0 +1,27 @@
+#define CATCH_CONFIG_MAIN
+#include "plugin/plugin.h"
+#include <QFile>
+#include <catch2/catch.hpp>
+
+TEST_CASE("Hostlist")
+{
+ HostlistFilterPlugin plugin;
+
+ const QString filename(qgetenv("HOSTLIST_TXT"));
+ REQUIRE(!filename.isEmpty());
+ QFile f(filename);
+
+ // shouldn't be able to load an unopened QIODevice
+ REQUIRE(plugin.load(f) == nullptr);
+
+ REQUIRE(f.open(QIODevice::ReadOnly | QIODevice::Text));
+
+ auto *list = plugin.load(f);
+ REQUIRE(list != nullptr);
+ f.seek(0);
+ REQUIRE_FALSE(plugin.parse(nullptr, f));
+ REQUIRE(plugin.parse(list, f));
+ f.close();
+ REQUIRE_FALSE(plugin.parse(list, f));
+}
+
diff --git a/plugins/smolblok_hostlist/test/rule.cpp b/plugins/smolblok_hostlist/test/rule.cpp
new file mode 100644
index 0000000..b5ba6e0
--- /dev/null
+++ b/plugins/smolblok_hostlist/test/rule.cpp
@@ -0,0 +1,57 @@
+#define CATCH_CONFIG_MAIN
+#include "filterlist.h"
+#include <catch2/catch.hpp>
+
+using namespace Hostlist;
+
+SCENARIO("Hostlist::Rule")
+{
+ GIVEN("an invalid rule")
+ {
+ const auto rule = Filterlist::parseRule("0.0.0.0 ");
+ REQUIRE(rule.empty());
+ }
+ GIVEN("127.0.0.1 localhost.localdomain")
+ {
+ auto rule = Filterlist::parseRule("127.0.0.1 localhost.localdomain");
+
+ REQUIRE(!rule.empty());
+ REQUIRE(rule.size() == 1);
+
+ // note: you need to force it to hash a string, rather than the address itself
+ const auto index = qHash(QString("localhost.localdomain"), 0);
+ REQUIRE(rule[index].domain == "localhost.localdomain");
+ REQUIRE(rule[index].redirect == "127.0.0.1");
+ }
+
+ GIVEN("0.0.0.0 blockeddomain.com")
+ {
+ auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.com");
+
+ REQUIRE(!rule.empty());
+ REQUIRE(rule.size() == 1);
+
+ const auto index = qHash(QString("blockeddomain.com"), 0);
+ REQUIRE(rule[index].domain == "blockeddomain.com");
+ REQUIRE(rule[index].redirect.isEmpty());
+ ;
+ }
+
+ GIVEN("0.0.0.0 blockeddomain.first blockeddomain.second")
+ {
+ auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.first blockeddomain.second");
+
+ REQUIRE(!rule.empty());
+ REQUIRE(rule.size() == 2);
+ {
+ const auto index = qHash(QString("blockeddomain.first"), 0);
+ REQUIRE(rule[index].domain == "blockeddomain.first");
+ REQUIRE(rule[index].redirect.isEmpty());
+ }
+ {
+ const auto index = qHash(QString("blockeddomain.second"), 0);
+ REQUIRE(rule[index].domain == "blockeddomain.second");
+ REQUIRE(rule[index].redirect.isEmpty());
+ }
+ }
+}