aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/meson.build5
-rw-r--r--test/urlfilter/urlfiltertest.cpp61
2 files changed, 66 insertions, 0 deletions
diff --git a/test/meson.build b/test/meson.build
index e2e25f6..75e38ed 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -1,5 +1,10 @@
dep_gtest = dependency('gtest')
+test('urlfilter-AdBlockList', executable('AdBlockList',
+ dependencies: [dep_gtest, dep_qt5, dep_urlfilter],
+ sources: ['urlfilter/urlfiltertest.cpp']
+))
+
# Adblock parsing test
adblock = executable('AdblockTest',
dependencies: [dep_gtest, dep_qt5, dep_urlfilter],
diff --git a/test/urlfilter/urlfiltertest.cpp b/test/urlfilter/urlfiltertest.cpp
new file mode 100644
index 0000000..f6cdbd4
--- /dev/null
+++ b/test/urlfilter/urlfiltertest.cpp
@@ -0,0 +1,61 @@
+#include "formats/adblockrule.h"
+#include "formats/adblockrule_parse.h"
+#include "formats/adblocklist.h"
+#include <gtest/gtest.h>
+
+AdBlockList list;
+
+TEST(AdBlockList, MetaData) {
+ EXPECT_STREQ(qUtf8Printable(list.metadata("Homepage")), "http://example.com/");
+ EXPECT_STREQ(qUtf8Printable(list.metadata("Title")), "FooList");
+ EXPECT_STREQ(qUtf8Printable(list.metadata("Expires")), "5 days");
+ EXPECT_STREQ(qUtf8Printable(list.metadata("Redirect")), "http://example.com/list.txt");
+ EXPECT_STREQ(qUtf8Printable(list.metadata("Version")), "1234");
+}
+
+TEST(AdBlockList, Contains) {
+ EXPECT_TRUE(list.match(QUrl(), QUrl("http://example.com/banner/foo.png")));
+ EXPECT_FALSE(list.match(QUrl(), QUrl("http://example.com/banner/foo/img")));
+
+// AdBlockRule *rule = parseRule_adblock("/banner/*/img^");
+// EXPECT_TRUE(rule->match(QUrl("http://example.com/banner/foo/img")));
+// EXPECT_TRUE(rule->match(QUrl("http://example.com/banner/foo/bar/img?param")));
+// EXPECT_TRUE(rule->match(QUrl("http://example.com/banner//img/foo")));
+// EXPECT_FALSE(rule->match(QUrl("http://example.com/banner/img")));
+// EXPECT_FALSE(rule->match(QUrl("http://example.com/banner/foo/imgraph")));
+// EXPECT_FALSE(rule->match(QUrl("http://example.com/banner/foo/img.gif")));
+}
+
+TEST(AdBlockList, ContainsWildcard) {
+ EXPECT_TRUE(list.match(QUrl(), QUrl("http://example.com/banner/ads/img.png")));
+}
+
+TEST(AdBlockList, Domain) {
+ EXPECT_TRUE(list.match(QUrl(), QUrl("http://ads.example.com/foo.gif")));
+ EXPECT_TRUE(list.match(QUrl(), QUrl("http://server1.ads.example.com/foo.gif")));
+ EXPECT_TRUE(list.match(QUrl(), QUrl("https://ads.example.com:8000/")));
+ EXPECT_FALSE(list.match(QUrl(), QUrl("http://ads.example.com.ua/foo.gif")));
+ EXPECT_FALSE(list.match(QUrl(), QUrl("http://example.com/redirect/http://ads.example.com/")));
+}
+
+TEST(AdBlockList, RegularExpression) {
+ EXPECT_TRUE(list.match(QUrl(), QUrl("http://example.com/banner123")));
+ EXPECT_TRUE(list.match(QUrl(), QUrl("http://example.com/banner321")));
+ EXPECT_FALSE(list.match(QUrl(), QUrl("http://example.com/banners")));
+}
+
+int main(int argc, char **argv) {
+ list.parseLine("! Homepage: http://example.com/");
+ list.parseLine("! Title: FooList");
+ list.parseLine("! Expires: 5 days");
+ list.parseLine("! Redirect: http://example.com/list.txt");
+ list.parseLine("! Version: 1234");
+
+ EXPECT_TRUE(list.parseLine("/banner/foo.png"));
+ EXPECT_TRUE(list.parseLine("/banner/*/img.png"));
+ EXPECT_TRUE(list.parseLine("||ads.example.com^"));
+ EXPECT_TRUE(list.parseLine("/banner\\d+/"));
+
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}