From 3d2ae07c455c0e423c64f19e445518427a5684fa Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 9 Jan 2019 19:38:58 +0100 Subject: Rewrite lib/urlfilter - Make HostList and AdBlockList implementations independent from each other - Move urlfilter tests to 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 insertions(+) create mode 100644 lib/urlfilter/test/adblock.cpp create mode 100644 lib/urlfilter/test/adblock.txt create mode 100644 lib/urlfilter/test/hostlist.cpp create mode 100644 lib/urlfilter/test/hostlist.txt create 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 new file mode 100644 index 0000000..ecb94ee --- /dev/null +++ b/lib/urlfilter/test/adblock.cpp @@ -0,0 +1,88 @@ +#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 new file mode 100644 index 0000000..635ce09 --- /dev/null +++ b/lib/urlfilter/test/adblock.txt @@ -0,0 +1,26 @@ +! 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 new file mode 100644 index 0000000..041cd5f --- /dev/null +++ b/lib/urlfilter/test/hostlist.cpp @@ -0,0 +1,34 @@ +#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 new file mode 100644 index 0000000..a0b4e5c --- /dev/null +++ b/lib/urlfilter/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/lib/urlfilter/test/matcher.cpp b/lib/urlfilter/test/matcher.cpp new file mode 100644 index 0000000..1c1efbf --- /dev/null +++ b/lib/urlfilter/test/matcher.cpp @@ -0,0 +1,42 @@ +#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