From d1287f43964633035938f4f4d4133bb6d9da7b3e Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 10 Feb 2020 20:58:39 +0200 Subject: 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 --- lib/urlfilter/test/adblock.cpp | 88 ----------------------------------------- lib/urlfilter/test/adblock.txt | 26 ------------ lib/urlfilter/test/hostlist.cpp | 34 ---------------- lib/urlfilter/test/hostlist.txt | 6 --- lib/urlfilter/test/matcher.cpp | 42 -------------------- 5 files changed, 196 deletions(-) delete mode 100644 lib/urlfilter/test/adblock.cpp delete mode 100644 lib/urlfilter/test/adblock.txt delete mode 100644 lib/urlfilter/test/hostlist.cpp delete mode 100644 lib/urlfilter/test/hostlist.txt delete mode 100644 lib/urlfilter/test/matcher.cpp (limited to 'lib/urlfilter/test') diff --git a/lib/urlfilter/test/adblock.cpp b/lib/urlfilter/test/adblock.cpp deleted file mode 100644 index ecb94ee..0000000 --- a/lib/urlfilter/test/adblock.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "urlfilter.h" -#include "adblock/adblocklist.h" -#include -#include - -AdBlockList *list = nullptr; - -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, BasicFilter) { - // Rule: /banner/*/img^ - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/foo/img"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/foo/bar/img?param"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner//img/foo"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::Block); - - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/foo.png"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::NotMatched); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/img"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::NotMatched); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/foo/imgraph"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::NotMatched); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/foo/img.gif"), QWebEngineUrlRequestInfo::ResourceTypeImage).first, UrlFilter::NotMatched); - - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner/ads/img.png"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -TEST(AdBlockList, MatchBeginningEnd) { - // Rule: |http://beginning-pattern.com - EXPECT_EQ(list->match(QUrl(), QUrl("http://beginning-pattern.com"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("https://beginning-pattern.com"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); - // Rule: end-pattern| - EXPECT_EQ(list->match(QUrl(), QUrl("https://endpattern.com/end-pattern"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("https://endpattern.com/end-pattern/foo"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -TEST(AdBlockList, Domain) { - // Rule: ||ads.example.com^ - EXPECT_EQ(list->match(QUrl(), QUrl("http://ads.example.com/foo.gif"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("http://server1.ads.example.com/foo.gif"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("https://ads.example.com:8000/"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - - EXPECT_EQ(list->match(QUrl(), QUrl("http://ads.example.com.ua/foo.gif"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/redirect/http://ads.example.com/"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -TEST(AdBlockList, RegularExpression) { - // Rule: /banner\d+/ - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner123"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banner321"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("http://example.com/banners"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -TEST(AdBlockList, MatchCase) { - // Rule: matchThisCase$match-case - EXPECT_EQ(list->match(QUrl(), QUrl("http://matchcase.com/matchThisCase"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl("http://matchcase.com/MatchThisCase"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -TEST(AdBlockList, DomainOption) { - // Rule: domain-limited-string$domain=example.com - EXPECT_EQ(list->match(QUrl("https://example.com"), QUrl("https://example.com/domain-limited-string/foo"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl("https://example.com"), QUrl("https://example.com/another-domain-string/foo"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); - EXPECT_EQ(list->match(QUrl("https://another.com"), QUrl("https://example.com/domain-limited-string/foo"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); - - //Rule: exception-limited-string$domain=~example.com - EXPECT_EQ(list->match(QUrl("https://another.com"), QUrl("https://example.com/exception-limited-string/foo"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl("https://example.com"), QUrl("https://example.com/exception-limited-string/foo"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -int main(int argc, char **argv) { - QFile f("adblock.txt"); - if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug("Could not open list"); - return -1; - } - - list = new AdBlockList(&f); - f.close(); - - qDebug("Parsed %i rules", list->ruleCount()); - - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - diff --git a/lib/urlfilter/test/adblock.txt b/lib/urlfilter/test/adblock.txt deleted file mode 100644 index 635ce09..0000000 --- a/lib/urlfilter/test/adblock.txt +++ /dev/null @@ -1,26 +0,0 @@ -! Homepage: http://example.com/ -! Title: FooList -! Expires: 5 days -! Redirect: http://example.com/list.txt -! Version: 1234 - -/banner/*/img^ -||ads.example.com^ -|http://example.com/| -/banner\d+/ - -! match beginning -||http://beginning-pattern.com -! match end -end-pattern| - -! options -! match-case -matchThisCase$match-case - -! domain limiting -! only apply this filter on this domain -domain-limited-string$domain=example.com -! apply this filter to all domains but the listed one -exception-limited-string$domain=~example.com - diff --git a/lib/urlfilter/test/hostlist.cpp b/lib/urlfilter/test/hostlist.cpp deleted file mode 100644 index 041cd5f..0000000 --- a/lib/urlfilter/test/hostlist.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include "hostlist/hostlist.h" -#include - -HostList *list = nullptr; - -TEST(AdBlockList, Block) { - EXPECT_EQ(list->match(QUrl(), QUrl::fromUserInput("blockeddomain.com"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl::fromUserInput("blockeddomain.first"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - EXPECT_EQ(list->match(QUrl(), QUrl::fromUserInput("blockeddomain.second"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::Block); - - const auto r = list->match(QUrl(), QUrl::fromUserInput("localhost.localdomain"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame); - EXPECT_EQ(r.first, UrlFilter::Redirect); - EXPECT_EQ(r.second, QString("127.0.0.1")); - - EXPECT_EQ(list->match(QUrl(), QUrl::fromUserInput("other.domain"), QWebEngineUrlRequestInfo::ResourceTypeMainFrame).first, UrlFilter::NotMatched); -} - -int main(int argc, char **argv) { - QFile f("hostlist.txt"); - if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug("Could not open list"); - return -1; - } - - list = new HostList(&f); - f.close(); - - qDebug("Parsed %i rules", list->ruleCount()); - - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - diff --git a/lib/urlfilter/test/hostlist.txt b/lib/urlfilter/test/hostlist.txt deleted file mode 100644 index a0b4e5c..0000000 --- a/lib/urlfilter/test/hostlist.txt +++ /dev/null @@ -1,6 +0,0 @@ -# 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/lib/urlfilter/test/matcher.cpp b/lib/urlfilter/test/matcher.cpp deleted file mode 100644 index 1c1efbf..0000000 --- a/lib/urlfilter/test/matcher.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "urlfilter.h" -#include "matcher.h" -#include - -TEST(Matcher, StringContains) { - ContentsMatcher matcher("spam-pattern", UrlFilter::StringContains); - EXPECT_TRUE(matcher.hasMatch("this string contains a spam-pattern")); - EXPECT_FALSE(matcher.hasMatch("this string does not contain the pattern")); -} - -TEST(Matcher, StringStartsWith) { - ContentsMatcher matcher("beginning", UrlFilter::StringStartsWith); - EXPECT_TRUE(matcher.hasMatch("beginning this string is the pattern")); - EXPECT_FALSE(matcher.hasMatch("ending this string is the pattern, the word beginning")); - EXPECT_FALSE(matcher.hasMatch("this would be a string where the pattern cannot be found")); -} - -TEST(Matcher, StringEndsWith) { - ContentsMatcher matcher("ending", UrlFilter::StringEndsWith); - EXPECT_TRUE(matcher.hasMatch("this string has the proper ending")); - EXPECT_FALSE(matcher.hasMatch("and this string doesn't")); -} - -TEST(Matcher, StringEquals) { - ContentsMatcher matcher("string-to-match", UrlFilter::StringEquals); - EXPECT_TRUE(matcher.hasMatch("string-to-match")); - EXPECT_FALSE(matcher.hasMatch("same-len-string")); - EXPECT_FALSE(matcher.hasMatch("not the string-to-match")); -} - -TEST(Matcher, RegularExpression) { - ContentsMatcher matcher("banner\\d+", UrlFilter::RegularExpressionMatch); - EXPECT_TRUE(matcher.hasMatch("http://another.com/banner123")); - EXPECT_TRUE(matcher.hasMatch("http://another.com/banner321")); - EXPECT_FALSE(matcher.hasMatch("http://another.com/banners")); - -} - -int main(int argc, char **argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} -- cgit v1.2.1