aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-04-18 22:34:49 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-04-21 20:14:57 +0300
commitf76ef20b832428aa1090d7f5df749909828f9bd8 (patch)
tree61fb6147cf772d3e70e6d21d8c7b43ec74e0cdf8
parentAdd staging/hostlist (diff)
downloadsmolbote-f76ef20b832428aa1090d7f5df749909828f9bd8.tar.xz
Hostlist: test list parsing
-rw-r--r--staging/hostlist/filterlist.cpp20
-rw-r--r--staging/hostlist/filterlist.hpp27
-rw-r--r--staging/hostlist/meson.build48
-rw-r--r--staging/hostlist/test/filterlist.cpp29
-rw-r--r--staging/hostlist/test/hostlist.txt6
-rw-r--r--staging/hostlist/test/rule.cpp2
6 files changed, 93 insertions, 39 deletions
diff --git a/staging/hostlist/filterlist.cpp b/staging/hostlist/filterlist.cpp
index 483ba7d..42be349 100644
--- a/staging/hostlist/filterlist.cpp
+++ b/staging/hostlist/filterlist.cpp
@@ -27,3 +27,23 @@ std::map<Filterlist::DomainHash, Filterlist::Rule> Filterlist::parseRule(const Q
}
return r;
}
+
+bool Filterlist::load(QIODevice &from)
+{
+ if(!from.isReadable() || !from.isTextModeEnabled()) {
+ return false;
+ }
+
+ while(from.bytesAvailable() > 0) {
+ const auto line = from.readLine(512).trimmed();
+ if(!line.isEmpty() && line.at(0) != '#') {
+ auto r = parseRule(line);
+ if(!r.empty()) {
+ qDebug("merging in %lu rules", r.size());
+ rules.merge(r);
+ }
+ }
+ }
+ return true;
+}
+
diff --git a/staging/hostlist/filterlist.hpp b/staging/hostlist/filterlist.hpp
index 24243e5..6edc19d 100644
--- a/staging/hostlist/filterlist.hpp
+++ b/staging/hostlist/filterlist.hpp
@@ -17,9 +17,29 @@ namespace Hostlist
class Filterlist final : public Filter
{
public:
+ typedef uint DomainHash;
+ struct Rule {
+ QString domain;
+ QString redirect;
+ };
+
Filterlist() = default;
~Filterlist() = default;
+ [[nodiscard]] bool findMatch(const QString &domain) const
+ {
+ const auto hash = qHash(domain, 0);
+ const auto found = rules.find(hash);
+ if(found != rules.end()) {
+ return true;
+ }
+ return false;
+ }
+ int count() const
+ {
+ return rules.size();
+ }
+
[[nodiscard]] bool filter(QWebEngineUrlRequestInfo &info) const
{
return false;
@@ -29,12 +49,7 @@ public:
return true;
}
- typedef uint DomainHash;
- struct Rule {
- QString domain;
- QString redirect;
- };
-
+ bool load(QIODevice &device);
[[nodiscard]] static std::map<DomainHash, Rule> parseRule(const QString &line);
private:
diff --git a/staging/hostlist/meson.build b/staging/hostlist/meson.build
index d37db1b..c5f3499 100644
--- a/staging/hostlist/meson.build
+++ b/staging/hostlist/meson.build
@@ -1,36 +1,20 @@
lib_hostlistfilter = static_library('hostlistfilter',
- ['filterlist.cpp'],
- include_directories
- : smolbote_interfaces,
- dependencies
- : [dep_qt5])
+ [ 'filterlist.cpp' ],
+ include_directories: smolbote_interfaces,
+ dependencies: [dep_qt5]
+)
- dep_hostlistfilter
- = declare_dependency(
- include_directories
- : ['.', smolbote_interfaces],
- link_with
- : lib_hostlistfilter)
+dep_hostlistfilter = declare_dependency(
+ include_directories: ['.', smolbote_interfaces],
+ link_with : lib_hostlistfilter
+)
-#AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin',
-#['plugin/plugin.cpp',
-#mod_qt5.preprocess(include_directories \
- : smolbote_interfaces,
-#moc_headers : 'plugin/plugin.h', dependencies : dep_qt5)
-#],
-#include_directories : smolbote_interfaces,
-#link_with : lib_adblockfilter,
-#dependencies : dep_qt5,
-#install : true,
-#install_dir : get_option('libdir') / 'smolbote/plugins'
-#)
+test('hostlist: rule parsing', executable('rule',
+ sources: 'test/rule.cpp',
+ dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]))
- test('hostlist: rule parsing', executable('rule', sources
- : 'test/rule.cpp', dependencies
- : [dep_qt5, dep_catch, dep_hostlistfilter]))
-
- subdir_done()
-
- test('hostlist: filterlist', executable('filterlist', sources
- : 'test/filterlist.cpp', dependencies
- : [dep_qt5, dep_catch, dep_hostlistfilter]))
+test('hostlist: filterlist', executable('filterlist',
+ sources: 'test/filterlist.cpp',
+ dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]),
+ env: 'HOSTLIST_TXT='+meson.current_source_dir()/'test/hostlist.txt'
+)
diff --git a/staging/hostlist/test/filterlist.cpp b/staging/hostlist/test/filterlist.cpp
new file mode 100644
index 0000000..fb71068
--- /dev/null
+++ b/staging/hostlist/test/filterlist.cpp
@@ -0,0 +1,29 @@
+#define CATCH_CONFIG_MAIN
+#include "filterlist.hpp"
+#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/staging/hostlist/test/hostlist.txt b/staging/hostlist/test/hostlist.txt
new file mode 100644
index 0000000..a0b4e5c
--- /dev/null
+++ b/staging/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/staging/hostlist/test/rule.cpp b/staging/hostlist/test/rule.cpp
index 6a16f86..6b1ea70 100644
--- a/staging/hostlist/test/rule.cpp
+++ b/staging/hostlist/test/rule.cpp
@@ -4,7 +4,7 @@
using namespace Hostlist;
-SCENARIO("MatcherRule")
+SCENARIO("Hostlist::Rule")
{
GIVEN("an invalid rule")
{