From 9100d9bb0fc30df1046e58064e0293b13dbeda89 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 19 Dec 2018 16:27:41 +0100 Subject: Add ContentsMatcher class --- test/adblock/adblocktest.cpp | 124 +++++++++++++++++++++++++------------------ test/adblock/adblocktest.h | 15 ------ test/meson.build | 6 ++- 3 files changed, 75 insertions(+), 70 deletions(-) delete mode 100644 test/adblock/adblocktest.h (limited to 'test') diff --git a/test/adblock/adblocktest.cpp b/test/adblock/adblocktest.cpp index 871b7ca..bbcaf0e 100644 --- a/test/adblock/adblocktest.cpp +++ b/test/adblock/adblocktest.cpp @@ -1,65 +1,83 @@ -#include "adblocktest.h" -#include #include "formats/adblockrule.h" #include "formats/adblockrule_parse.h" +#include -void AdBlockTest::parseRule() -{ - auto *rule = loadRule("/spamdomain/$domain=spamdomain.com,image"); - QCOMPARE(rule != nullptr, true); - QCOMPARE(rule->match(QUrl::fromUserInput("subdomain.spamdomain.com")), true); - QCOMPARE(rule->action().first == FilterLeaf::Block, true); - QCOMPARE(rule->option(QWebEngineUrlRequestInfo::ResourceTypeImage).value(), true); +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")); } -/*void AdBlockTest::parseList() -{ - std::vector rules; +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")); + +} - QFile list("adblock.txt"); - int ruleCount = 0; - QCOMPARE(list.open(QIODevice::ReadOnly | QIODevice::Text), true); - { - QTextStream l(&list); - QString line; - while(l.readLineInto(&line)) { - AdBlockRule rule(line); - if(rule.isEnabled()) { - rules.emplace_back(std::move(rule)); - ruleCount++; - qDebug("added rule: %s", qUtf8Printable(line)); - } - } - } - list.close(); +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); +} - // there should be 3 rules - QCOMPARE(rules.size(), ruleCount); +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"))); +} - // block by address part - QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/img")), true); - QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/bar/img?param")), true); - QCOMPARE(check(rules, QUrl("http://example.com/banner//img/foo")), true); - QCOMPARE(check(rules, QUrl("http://example.com/banner/img")), false); - QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/imgraph")), false); - QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/img.gif")), false); +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/"))); +} - // block by domain - QCOMPARE(check(rules, QUrl("http://ads.example.com/foo.gif")), true); - QCOMPARE(check(rules, QUrl("http://server1.ads.example.com/foo.gif")), true); - QCOMPARE(check(rules, QUrl("https://ads.example.com:8000/")), true); - QCOMPARE(check(rules, QUrl("http://ads.example.com.ua/foo.gif")), false); - QCOMPARE(check(rules, QUrl("http://example.com/redirect/http://ads.example.com/")), false); - // block exact address - QCOMPARE(check(rules, QUrl("http://example.com/")), true); - QCOMPARE(check(rules, QUrl("http://example.com/foo.gif")), false); - QCOMPARE(check(rules, QUrl("http://example.info/redirect/http://example.com/")), false); +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/"))); +} - // regular expression - QCOMPARE(check(rules, QUrl("http://another.com/banner123")), true); - QCOMPARE(check(rules, QUrl("http://another.com/banner321")), true); - QCOMPARE(check(rules, QUrl("http://another.com/banners")), false); -}*/ +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"))); +} -QTEST_GUILESS_MAIN(AdBlockTest) +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/adblock/adblocktest.h b/test/adblock/adblocktest.h deleted file mode 100644 index 7e58197..0000000 --- a/test/adblock/adblocktest.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef ADBLOCKTEST_H -#define ADBLOCKTEST_H - -#include - -class AdBlockTest : public QObject -{ - Q_OBJECT - -private slots: - void parseRule(); - //void parseList(); -}; - -#endif // ADBLOCKTEST_H diff --git a/test/meson.build b/test/meson.build index df6a9c9..e2e25f6 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,7 +1,9 @@ +dep_gtest = dependency('gtest') + # Adblock parsing test adblock = executable('AdblockTest', - dependencies: [dep_qt5, dep_urlfilter], - sources: ['adblock/adblocktest.cpp', qt5.preprocess(moc_headers: 'adblock/adblocktest.h', dependencies: dep_qt5)] + dependencies: [dep_gtest, dep_qt5, dep_urlfilter], + sources: ['adblock/adblocktest.cpp'] ) test('urlfilter-adblock', adblock, workdir: meson.current_source_dir()) -- cgit v1.2.1