From e2b1cc628b304e3f153abc17fb350aa781e26b36 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 18 Dec 2017 14:32:45 +0100 Subject: Basic adblock FilterRule --- lib/adblock/test/filtertest.cpp | 63 +++++++++++++++++++++++++++++++++++++++++ lib/adblock/test/filtertest.h | 28 ++++++++++++++++++ lib/adblock/test/main.cpp | 4 +++ 3 files changed, 95 insertions(+) create mode 100644 lib/adblock/test/filtertest.cpp create mode 100644 lib/adblock/test/filtertest.h create mode 100644 lib/adblock/test/main.cpp (limited to 'lib/adblock/test') diff --git a/lib/adblock/test/filtertest.cpp b/lib/adblock/test/filtertest.cpp new file mode 100644 index 0000000..c778bc7 --- /dev/null +++ b/lib/adblock/test/filtertest.cpp @@ -0,0 +1,63 @@ +#include "filtertest.h" +#include +#include "../filterrule.h" +#include + +FilterTest::FilterTest(QObject *parent) : QObject(parent) +{ +} + +void FilterTest::initTestCase() +{ + addressRule = new FilterRule("/banner/*/img^"); + QVERIFY(addressRule->isValid() == true); + QVERIFY(addressRule->isException() == false); + + domainRule = new FilterRule("||ads.example.com^"); + QVERIFY(domainRule->isValid() == true); + QVERIFY(domainRule->isException() == false); + + exactAddressRule = new FilterRule("|http://example.com/|"); + QVERIFY(exactAddressRule->isValid() == true); + QVERIFY(exactAddressRule->isException() == false); +} + +void FilterTest::testAddressBlock() +{ + // This rule blocks: + QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/img")) == true); + QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/bar/img?param")) == true); + QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner//img/foo")) == true); + + // This rule doesn't block: + QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/img")) == false); + QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/imgraph")) == false); + QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/img.gif")) == false); +} + +void FilterTest::testDomainBlock() +{ + // This rule blocks: + QVERIFY(domainRule->shouldBlock(QUrl("http://ads.example.com/foo.gif")) == true); + QVERIFY(domainRule->shouldBlock(QUrl("http://server1.ads.example.com/foo.gif")) == true); + QVERIFY(domainRule->shouldBlock(QUrl("https://ads.example.com:8000/")) == true); + + // This rule doesn't block: + QVERIFY(domainRule->shouldBlock(QUrl("http://ads.example.com.ua/foo.gif")) == false); + QVERIFY(domainRule->shouldBlock(QUrl("http://example.com/redirect/http://ads.example.com/")) == false); +} + +void FilterTest::testExactAddressBlock() +{ + // This rule blocks: + QVERIFY(exactAddressRule->shouldBlock(QUrl("http://example.com/")) == true); + + // This rule doesn't block: + QVERIFY(exactAddressRule->shouldBlock(QUrl("http://example.com/foo.gif")) == false); + QVERIFY(exactAddressRule->shouldBlock(QUrl("http://example.info/redirect/http://example.com/")) == false); +} + +void FilterTest::cleanupTestCase() +{ + delete addressRule; +} diff --git a/lib/adblock/test/filtertest.h b/lib/adblock/test/filtertest.h new file mode 100644 index 0000000..45cdde1 --- /dev/null +++ b/lib/adblock/test/filtertest.h @@ -0,0 +1,28 @@ +#ifndef FILTERTEST_H +#define FILTERTEST_H + +#include + +class FilterRule; +class FilterTest : public QObject +{ + Q_OBJECT +public: + explicit FilterTest(QObject *parent = nullptr); + +signals: + +private slots: + void initTestCase(); + void testAddressBlock(); + void testDomainBlock(); + void testExactAddressBlock(); + void cleanupTestCase(); + +private: + FilterRule *addressRule; + FilterRule *domainRule; + FilterRule *exactAddressRule; +}; + +#endif // FILTERTEST_H diff --git a/lib/adblock/test/main.cpp b/lib/adblock/test/main.cpp new file mode 100644 index 0000000..3284e47 --- /dev/null +++ b/lib/adblock/test/main.cpp @@ -0,0 +1,4 @@ +#include +#include "filtertest.h" + +QTEST_APPLESS_MAIN(FilterTest) -- cgit v1.2.1