aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-04-15 17:23:48 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-04-21 20:14:57 +0300
commitf5a067e505c442618dc59d962fd6fa5d6cf16ab8 (patch)
treef401b839240659b558c84643d977d10c7a3f0d4e
parentAdd tests for regex rules (diff)
downloadsmolbote-f5a067e505c442618dc59d962fd6fa5d6cf16ab8.tar.xz
Add some Options tests
-rw-r--r--staging/adblock/filterlist.cpp6
-rw-r--r--staging/adblock/meson.build6
-rw-r--r--staging/adblock/options.cpp49
-rw-r--r--staging/adblock/options.h7
-rw-r--r--staging/adblock/test/options.cpp43
5 files changed, 81 insertions, 30 deletions
diff --git a/staging/adblock/filterlist.cpp b/staging/adblock/filterlist.cpp
index 5566c87..03db642 100644
--- a/staging/adblock/filterlist.cpp
+++ b/staging/adblock/filterlist.cpp
@@ -76,10 +76,10 @@ Rule *FilterList::parseRule(const QByteArray &line)
if(pattern.contains('$')) {
const auto list = pattern.split('$');
pattern = list.at(0);
- const auto options = list.at(1).split(',');
+ const auto options = list.at(1);
- for(const auto &option : options) {
- if(!opt.set(option)) {
+ for(auto &option : splitOptions(&options)) {
+ if(!opt.parseAbp(option)) {
return nullptr;
}
}
diff --git a/staging/adblock/meson.build b/staging/adblock/meson.build
index f0a0e07..fb92481 100644
--- a/staging/adblock/meson.build
+++ b/staging/adblock/meson.build
@@ -22,6 +22,12 @@ test('adblock: rule', executable('libadblockfilter_rule',
dependencies: [ dep_qt5, dep_catch ]
))
+test('adblock: options', executable('libadblockfilter_options',
+ sources: 'test/options.cpp',
+ link_with: lib_adblockfilter,
+ dependencies: [ dep_qt5, dep_catch ]
+))
+
test('adblock: filterlist', executable('libadblockfilter_filterlist',
sources: 'test/filterlist.cpp',
include_directories: smolbote_interfaces,
diff --git a/staging/adblock/options.cpp b/staging/adblock/options.cpp
index 0f70570..a748a68 100644
--- a/staging/adblock/options.cpp
+++ b/staging/adblock/options.cpp
@@ -10,67 +10,67 @@
using namespace AdblockPlus;
-bool Options::set(const QString &opt)
+bool Options::parseAbp(QStringRef &option)
{
- const bool exception = opt.startsWith("~");
- const QString option = [exception, opt]() {
- if(exception)
- return opt.mid(1);
- else
- return opt;
- }();
-
+ // pattern options
if(option == "match-case") {
- matchcase = exception;
+ matchcase = true;
return true;
}
+ // Possible inverse type options include ~script, ~image, ~stylesheet, ~object,
+ // ~xmlhttprequest, ~subdocument, ~ping, ~websocket, ~webrtc, ~document, ~elemhide, ~other
+ const bool exception = option.startsWith('~');
+ if(exception) {
+ option = option.mid(1);
+ }
+
// TODO: map all ResourceType's to their respective strings
// TODO: websocket, webrtc, elemhide, generichide, genericblock, popup
- else if(option == "document") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeMainFrame] = exception;
+ if(option == "document") {
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeMainFrame] = !exception;
return true;
}
else if(option == "subdocument") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeSubFrame] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeSubFrame] = !exception;
return true;
}
else if(option == "stylesheet") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeStylesheet] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeStylesheet] = !exception;
return true;
}
else if(option == "script") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeScript] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeScript] = !exception;
return true;
}
else if(option == "image") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeImage] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeImage] = !exception;
return true;
}
else if(option == "font") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeFontResource] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeFontResource] = !exception;
return true;
}
else if(option == "other") {
// An "other" subresource.
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeSubResource] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeSubResource] = !exception;
return true;
}
else if(option == "object") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeObject] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeObject] = !exception;
return true;
}
else if(option == "media") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeMedia] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeMedia] = !exception;
return true;
}
/*
@@ -80,12 +80,12 @@ QWebEngineUrlRequestInfo::ResourceTypePrefetch 11 An explicitly requested prefet
QWebEngineUrlRequestInfo::ResourceTypeFavicon 12 A favicon.
*/
else if(option == "xmlhttprequest") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypeXhr] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypeXhr] = !exception;
return true;
}
else if(option == "ping") {
- resource_options[QWebEngineUrlRequestInfo::ResourceTypePing] = exception;
+ resource_options[QWebEngineUrlRequestInfo::ResourceTypePing] = !exception;
return true;
}
@@ -100,10 +100,7 @@ QWebEngineUrlRequestInfo::ResourceTypeUnknown 255 Unknown request type.
// Restriction to third-party/first-party requests
else if(option == "third-party") {
- if(exception)
- thirdparty = false;
- else
- firstparty = false;
+ thirdparty = !exception;
return true;
}
diff --git a/staging/adblock/options.h b/staging/adblock/options.h
index ed28502..642ea0c 100644
--- a/staging/adblock/options.h
+++ b/staging/adblock/options.h
@@ -35,9 +35,14 @@ struct Options {
bool thirdparty = true;
QHash<QWebEngineUrlRequestInfo::ResourceType, bool> resource_options;
- bool set(const QString &option);
+ bool parseAbp(QStringRef &opt);
};
+inline auto splitOptions(const QStringRef &options)
+{
+ return options.split(',');
+}
+
}
#endif // SMOLBOTE_ADBLOCK_OPTIONS_H
diff --git a/staging/adblock/test/options.cpp b/staging/adblock/test/options.cpp
new file mode 100644
index 0000000..d0ad4a0
--- /dev/null
+++ b/staging/adblock/test/options.cpp
@@ -0,0 +1,43 @@
+#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("various options in a QString")
+ {
+ const QString options = "match-case,document,~subdocument";
+
+ for(auto &i : splitOptions(&options)) {
+ REQUIRE(opt.parseAbp(i));
+ }
+
+ WHEN("match-case")
+ {
+ REQUIRE(opt.matchcase);
+ }
+
+ WHEN("document")
+ {
+ REQUIRE(opt.resource_options.value(QWebEngineUrlRequestInfo::ResourceTypeMainFrame));
+ }
+ WHEN("~subdocument")
+ {
+ REQUIRE(!opt.resource_options.value(QWebEngineUrlRequestInfo::ResourceTypeSubFrame));
+ }
+ }
+}