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 --- test/adblock.txt | 4 -- test/adblock/adblocktest.cpp | 83 --------------------------- test/hostlist.txt | 6 -- test/hostlist/hostlisttest.cpp | 61 -------------------- test/hostlist/hostlisttest.h | 23 -------- test/matcherbenchmark/matcherbenchmark.cpp | 91 ------------------------------ test/matcherbenchmark/matcherbenchmark.h | 20 ------- test/meson.build | 34 ----------- test/urlfilter/urlfiltertest.cpp | 61 -------------------- 9 files changed, 383 deletions(-) delete mode 100644 test/adblock.txt delete mode 100644 test/adblock/adblocktest.cpp delete mode 100644 test/hostlist.txt delete mode 100644 test/hostlist/hostlisttest.cpp delete mode 100644 test/hostlist/hostlisttest.h delete mode 100644 test/matcherbenchmark/matcherbenchmark.cpp delete mode 100644 test/matcherbenchmark/matcherbenchmark.h delete mode 100644 test/meson.build delete mode 100644 test/urlfilter/urlfiltertest.cpp (limited to 'test') diff --git a/test/adblock.txt b/test/adblock.txt deleted file mode 100644 index cd284e8..0000000 --- a/test/adblock.txt +++ /dev/null @@ -1,4 +0,0 @@ -/banner/*/img^ -||ads.example.com^ -|http://example.com/| -/banner\d+/ diff --git a/test/adblock/adblocktest.cpp b/test/adblock/adblocktest.cpp deleted file mode 100644 index bbcaf0e..0000000 --- a/test/adblock/adblocktest.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "formats/adblockrule.h" -#include "formats/adblockrule_parse.h" -#include - -TEST(Matcher, StringContains) { - ContentsMatcher matcher("spam-pattern", FilterLeaf::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", FilterLeaf::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", FilterLeaf::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", FilterLeaf::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+", FilterLeaf::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")); - -} - -TEST(AdBlockRule, SimpleRule) { - AdBlockRule *rule = parseRule_adblock("/spamdomain/$domain=spamdomain.com,image"); - EXPECT_TRUE(rule->match(QUrl("subdomain.spamdomain.com"))); -// QCOMPARE(rule->action().first == FilterLeaf::Block, true); -// QCOMPARE(rule->option(QWebEngineUrlRequestInfo::ResourceTypeImage).value(), true); -} - -TEST(AdBlockRule, AddressPart) { - 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(AdBlockRule, Domain){ - AdBlockRule *rule = parseRule_adblock("||ads.example.com^"); - EXPECT_TRUE(rule->match(QUrl("http://ads.example.com/foo.gif"))); - EXPECT_TRUE(rule->match(QUrl("http://server1.ads.example.com/foo.gif"))); - EXPECT_TRUE(rule->match(QUrl("https://ads.example.com:8000/"))); - EXPECT_FALSE(rule->match(QUrl("http://ads.example.com.ua/foo.gif"))); - EXPECT_FALSE(rule->match(QUrl("http://example.com/redirect/http://ads.example.com/"))); -} - - -TEST(AdBlockRule, ExactAddress){ - AdBlockRule *rule = parseRule_adblock("|http://example.com/|"); - EXPECT_TRUE(rule->match(QUrl("http://example.com/"))); - EXPECT_FALSE(rule->match(QUrl("http://example.com/foo.gif"))); - EXPECT_FALSE(rule->match(QUrl("http://example.info/redirect/http://example.com/"))); -} - -TEST(AdBlockRule, RegularExpression) { - AdBlockRule *rule = parseRule_adblock("/banner\\d+/"); - EXPECT_TRUE(rule->match(QUrl("http://another.com/banner123"))); - EXPECT_TRUE(rule->match(QUrl("http://another.com/banner321"))); - EXPECT_FALSE(rule->match(QUrl("http://another.com/banners"))); -} - -int main(int argc, char **argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/hostlist.txt b/test/hostlist.txt deleted file mode 100644 index a0b4e5c..0000000 --- a/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/test/hostlist/hostlisttest.cpp b/test/hostlist/hostlisttest.cpp deleted file mode 100644 index 7f5c954..0000000 --- a/test/hostlist/hostlisttest.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "hostlisttest.h" -#include - -void HostlistTest::parseList() -{ - // load filters - QFile hostlist("hostlist.txt"); - QCOMPARE(hostlist.open(QIODevice::ReadOnly | QIODevice::Text), true); - QCOMPARE(loadHostlist(hostlist, &tree), true); - - // hostlist filters are applied to all domains, so there should only be one branch - QCOMPARE(tree.branches().length(), 1); -} - -void HostlistTest::checkRules_data() -{ - QTest::addColumn("domain"); - QTest::addColumn("request"); - QTest::addColumn("matches"); - QTest::addColumn("action"); - - const QVector domains{ QUrl(), QUrl::fromUserInput("testdomain.host") }; - for(const QUrl &domain : domains) { - QTest::newRow("block (1 domain per line)") << domain << QUrl::fromUserInput("blockeddomain.com") << 1 << FilterLeaf::Block; - QTest::newRow("block (2 domains per line #1)") << domain << QUrl::fromUserInput("blockeddomain.first") << 1 << FilterLeaf::Block; - QTest::newRow("block (2 domains per line #2)") << domain << QUrl::fromUserInput("blockeddomain.second") << 1 << FilterLeaf::Block; - QTest::newRow("redirect") << domain << QUrl::fromUserInput("localhost.localdomain") << 1 << FilterLeaf::Redirect; - QTest::newRow("domain not in hostlist") << domain << QUrl::fromUserInput("other.domain") << 0 << FilterLeaf::NotMatched; - } -} - -void HostlistTest::checkRules() -{ - QFETCH(QUrl, domain); - QFETCH(QUrl, request); - QFETCH(int, matches); - QFETCH(FilterLeaf::Action, action); - - auto result = tree.match(domain, request); - QCOMPARE(result.length(), matches); - if(matches > 0) - QCOMPARE(result.constFirst()->action().first, action); - if(action == FilterLeaf::Redirect) - QCOMPARE(result.constFirst()->action().second, QLatin1Literal("127.0.0.1")); -} - -void HostlistTest::benchmark_parse() -{ - QFile hostlist("hostlist-benchmark.txt"); - if(hostlist.open(QIODevice::ReadOnly | QIODevice::Text)) { - FilterTree benchmarkTree; - bool loaded; - QBENCHMARK { - loaded = loadHostlist(hostlist, &benchmarkTree); - } - QCOMPARE(loaded, true); - hostlist.close(); - } -} - -QTEST_GUILESS_MAIN(HostlistTest) diff --git a/test/hostlist/hostlisttest.h b/test/hostlist/hostlisttest.h deleted file mode 100644 index 96051a9..0000000 --- a/test/hostlist/hostlisttest.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef HOSTLIST_TEST -#define HOSTLIST_TEST - -#include "filtertree.h" -#include - -class HostlistTest : public QObject -{ - Q_OBJECT - -private slots: - void parseList(); - - void checkRules_data(); - void checkRules(); - - void benchmark_parse(); - -private: - FilterTree tree; -}; - -#endif diff --git a/test/matcherbenchmark/matcherbenchmark.cpp b/test/matcherbenchmark/matcherbenchmark.cpp deleted file mode 100644 index 84406d5..0000000 --- a/test/matcherbenchmark/matcherbenchmark.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "matcherbenchmark.h" -#include -#include -#include -#include -#include -#include -#include -#include - -void MatcherBenchmark::qstringcontains() -{ - const QString pattern("spamdomain"); - const QString request("subdomain.spamdomain.com"); - - QCOMPARE(request.contains(pattern), true); - QBENCHMARK { - request.contains(pattern); - } -} - -void MatcherBenchmark::qstringmatcher() -{ - const QStringMatcher pattern("spamdomain"); - const QString request("subdomain.spamdomain.com"); - - QCOMPARE(pattern.indexIn(request) != -1, true); - QBENCHMARK { - pattern.indexIn(request); - } -} - -void MatcherBenchmark::qregexp() -{ - const QRegExp pattern("spamdomain"); - const QString request("subdomain.spamdomain.com"); - - QCOMPARE(pattern.indexIn(request) != -1, true); - QBENCHMARK { - pattern.indexIn(request); - } -} - -void MatcherBenchmark::qregularexpressionmatch() -{ - const QRegularExpression pattern("spamdomain"); - const QString request("subdomain.spamdomain.com"); - - QCOMPARE(pattern.match(request).hasMatch(), true); - QBENCHMARK { - pattern.match(request).hasMatch(); - } -} - -void MatcherBenchmark::stdregex() -{ - const std::regex pattern("spamdomain"); - const std::string request("subdomain.spamdomain.com"); - - QCOMPARE(std::regex_search(request, pattern), true); - QBENCHMARK { - std::regex_search(request, pattern); - } -} - -void MatcherBenchmark::cregex() -{ - regex_t pattern; - QCOMPARE(regcomp(&pattern, "spamdomain", 0), 0); - const std::string request("subdomain.spamdomain.com"); - - QCOMPARE(regexec(&pattern, request.c_str(), 0, NULL, 0), false); - QBENCHMARK { - regexec(&pattern, request.c_str(), 0, NULL, 0); - } - - regfree(&pattern); -} - -void MatcherBenchmark::boostregex() -{ - const boost::regex pattern("spamdomain"); - const std::string request("subdomain.spamdomain.com"); - - QCOMPARE(boost::regex_search(request, pattern), true); - QBENCHMARK { - boost::regex_search(request, pattern); - } -} - -QTEST_GUILESS_MAIN(MatcherBenchmark) diff --git a/test/matcherbenchmark/matcherbenchmark.h b/test/matcherbenchmark/matcherbenchmark.h deleted file mode 100644 index deb4495..0000000 --- a/test/matcherbenchmark/matcherbenchmark.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef MATCHER_BENCHMARK -#define MATCHER_BENCHMARK - -#include - -class MatcherBenchmark : public QObject -{ - Q_OBJECT - -private slots: - void qstringcontains(); - void qstringmatcher(); - void qregexp(); - void qregularexpressionmatch(); - void stdregex(); - void cregex(); - void boostregex(); -}; - -#endif diff --git a/test/meson.build b/test/meson.build deleted file mode 100644 index 75e38ed..0000000 --- a/test/meson.build +++ /dev/null @@ -1,34 +0,0 @@ -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], - sources: ['adblock/adblocktest.cpp'] -) -test('urlfilter-adblock', adblock, workdir: meson.current_source_dir()) - -# Hostlist parsing test -hostlist = executable('HostlistTest', - dependencies: [dep_qt5, dep_urlfilter], - sources: ['hostlist/hostlisttest.cpp', qt5.preprocess(moc_headers: 'hostlist/hostlisttest.h', dependencies: dep_qt5)] -) -test('urlfilter-hostlist', hostlist, workdir: meson.current_source_dir()) - -# matching algorithms benchmark -matcherbenchmark = executable('MatcherBenchmark', - dependencies: [dep_qt5, dependency('boost', modules: 'regex')], - sources: ['matcherbenchmark/matcherbenchmark.cpp', qt5.preprocess(moc_headers: 'matcherbenchmark/matcherbenchmark.h', dependencies: dep_qt5)] -) - -# SingleApplication issue#40 test app -singleapp = executable('SingleApplication', - cpp_args: ['-DQAPPLICATION_CLASS=QApplication'], - dependencies: [dep_qt5, dep_SingleApplication], - sources: ['singleapplication-40/main.cpp'] -) - diff --git a/test/urlfilter/urlfiltertest.cpp b/test/urlfilter/urlfiltertest.cpp deleted file mode 100644 index f6cdbd4..0000000 --- a/test/urlfilter/urlfiltertest.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "formats/adblockrule.h" -#include "formats/adblockrule_parse.h" -#include "formats/adblocklist.h" -#include - -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(); -} -- cgit v1.2.1