diff options
-rw-r--r-- | lib/web/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/web/urlfilter/adblockrule.cpp | 68 | ||||
-rw-r--r-- | lib/web/urlfilter/adblockrule.h | 35 | ||||
-rw-r--r-- | lib/web/urlfilter/filterdomain.cpp | 68 | ||||
-rw-r--r-- | lib/web/urlfilter/filterdomain.h | 37 | ||||
-rw-r--r-- | lib/web/urlfilter/filterrule.cpp | 168 | ||||
-rw-r--r-- | lib/web/urlfilter/filterrule.h | 78 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 21 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.h | 4 | ||||
-rw-r--r-- | test/CMakeLists.txt | 11 | ||||
-rw-r--r-- | test/urlfilter/adblocktest.cpp | 8 | ||||
-rw-r--r-- | test/urlfilter/urlfiltertest.cpp | 81 | ||||
-rw-r--r-- | test/urlfilter/urlfiltertest.h | 41 |
13 files changed, 108 insertions, 514 deletions
diff --git a/lib/web/CMakeLists.txt b/lib/web/CMakeLists.txt index 9515566..ed969cd 100644 --- a/lib/web/CMakeLists.txt +++ b/lib/web/CMakeLists.txt @@ -8,8 +8,6 @@ add_library(web webprofile.cpp webprofile.h - urlfilter/filterdomain.cpp - urlfilter/filterdomain.h urlfilter/filterrule.cpp urlfilter/filterrule.h diff --git a/lib/web/urlfilter/adblockrule.cpp b/lib/web/urlfilter/adblockrule.cpp index b20ed8f..60262b7 100644 --- a/lib/web/urlfilter/adblockrule.cpp +++ b/lib/web/urlfilter/adblockrule.cpp @@ -1,26 +1,5 @@ #include "adblockrule.h" -bool isMatchingDomain(const QString &domain, const QString &filter) -{ - // domain and filter are the same - if(domain == filter) { - return true; - } - - // domain can't be matched by filter if it doesn't end with filter - // ex. example2.com isn't matched by example.com - if(!domain.endsWith(filter)) { - return false; - } - - // match with subdomains - // ex. subdomain.example.com is matched by example.com - int index = domain.indexOf(filter); - - // match if (domain ends with filter) && (filter has been found) and (character before filter is '.') - return index > 0 && domain[index - 1] == QLatin1Char('.'); -} - inline std::pair<QWebEngineUrlRequestInfo::ResourceType, bool> parseOption(const QString &option) { if(option.endsWith(QLatin1Literal("script"))) { @@ -105,14 +84,14 @@ AdBlockRule::AdBlockRule(const QString &filter) if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) { parsedLine = parsedLine.mid(1, parsedLine.length() - 2); - matchType = RegularExpressionMatch; + urlMatchType = RegularExpressionMatch; regexp.setPattern(parsedLine); return; } // basic filter rules if(parsedLine.startsWith(QLatin1Literal("|")) && parsedLine.endsWith(QLatin1Literal("|"))) { - matchType = StringEquals; + urlMatchType = StringEquals; match = parsedLine.mid(1, parsedLine.length() - 2); return; } @@ -128,7 +107,7 @@ AdBlockRule::AdBlockRule(const QString &filter) parsedLine.chop(1); if(parsedLine.startsWith(QLatin1Literal("||")) && parsedLine.endsWith(QLatin1Literal("^"))) { - matchType = DomainMatch; + urlMatchType = DomainMatch; match = parsedLine.mid(2, parsedLine.length() - 3); return; } @@ -137,49 +116,12 @@ AdBlockRule::AdBlockRule(const QString &filter) // wildcard "*" - any number of characters // separator "^" - end, ? or / if(parsedLine.contains(QLatin1Literal("*")) || parsedLine.contains(QLatin1Literal("^"))) { - matchType = RegularExpressionMatch; + urlMatchType = RegularExpressionMatch; parsedLine.replace(QLatin1Literal("*"), QLatin1Literal(".*")); parsedLine.replace(QLatin1Literal("^"), QLatin1Literal("($|\\?|\\/)")); regexp.setPattern(parsedLine); return; } - matcher.setPattern(parsedLine); -} - -bool AdBlockRule::isEnabled() const -{ - return m_isEnabled; -} - -bool AdBlockRule::matchesType(QWebEngineUrlRequestInfo::ResourceType type) const -{ - // no options have been specified -> match all resource types - if(m_resourceTypeOptions.isEmpty()) - return true; - - // this resource type has not been specified -> reject it - if(!m_resourceTypeOptions.contains(type)) - return false; - - // resource type has been specified; true to match, false to exception - return m_resourceTypeOptions.value(type); -} - -bool AdBlockRule::matchesUrl(const QUrl &url) const -{ - switch (matchType) { - case RegularExpressionMatch: - if(regexp.match(url.toString()).hasMatch()) - return !m_isException; - - case StringEquals: - return url.toString() == match; - - case DomainMatch: - return isMatchingDomain(url.host(), match); - - default: - return false; - } + match = parsedLine; } diff --git a/lib/web/urlfilter/adblockrule.h b/lib/web/urlfilter/adblockrule.h index 76484c3..aeabf4f 100644 --- a/lib/web/urlfilter/adblockrule.h +++ b/lib/web/urlfilter/adblockrule.h @@ -1,43 +1,18 @@ -#ifndef ADBLOCKRULE_H -#define ADBLOCKRULE_H +#ifndef SMOLBOTE_ADBLOCKRULE_H +#define SMOLBOTE_ADBLOCKRULE_H #include <QObject> #include <QString> #include <QRegularExpression> #include <QUrl> #include <QWebEngineUrlRequestInfo> +#include "filterrule.h" -class AdBlockRule +class AdBlockRule : public FilterRule { public: - - enum MatchType { - InvalidMatch, - RegularExpressionMatch, - StringContains, - StringStartsWith, - StringEndsWith, - StringEquals, - DomainMatch - }; - AdBlockRule(const QString &filter); - bool isEnabled() const; - bool matchesType(QWebEngineUrlRequestInfo::ResourceType type) const; - bool matchesUrl(const QUrl &url) const; - -private: - bool m_isEnabled = false; - bool m_isException = false; - - MatchType matchType = InvalidMatch; - QHash<QWebEngineUrlRequestInfo::ResourceType, bool> m_resourceTypeOptions; - QStringList allowedDomains, blockedDomains; - - QString match; - QRegularExpression regexp; - QStringMatcher matcher; }; -#endif // ADBLOCKRULE_H +#endif // SMOLBOTE_ADBLOCKRULE_H diff --git a/lib/web/urlfilter/filterdomain.cpp b/lib/web/urlfilter/filterdomain.cpp deleted file mode 100644 index 99cc71c..0000000 --- a/lib/web/urlfilter/filterdomain.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "filterdomain.h" -#include <QVector> - -bool isMatchingDomain(const QString &domain, const QString &filter) -{ - // domain and filter are the same - if(domain == filter) { - return true; - } - - // domain can't be matched by filter if it doesn't end with filter - // ex. example2.com isn't matched by example.com - if(!domain.endsWith(filter)) { - return false; - } - - // match with subdomains - // ex. subdomain.example.com is matched by example.com - int index = domain.indexOf(filter); - - // match if (domain ends with filter) && (filter has been found) and (character before filter is '.') - return index > 0 && domain[index - 1] == QLatin1Char('.'); -} - -FilterDomain::FilterDomain(QObject *parent) - : QObject(parent) -{ -} - -void FilterDomain::addDomain(const QString &domain, bool isException) -{ - if(domain.isEmpty()) - return; - - if(!isException) - m_allowedOnDomains.append(domain); - else - m_blockedOnDomains.append(domain); -} - -void FilterDomain::addRule(FilterRule_ptr &rule) -{ - Q_ASSERT(rule); - m_rules.emplace_back(std::move(rule)); -} - -bool FilterDomain::hasMatch(const QString &host) const -{ - for(const auto &domain : qAsConst(m_blockedOnDomains)) { - if(isMatchingDomain(host, domain)) - return false; - } - - for(const auto &domain : qAsConst(m_allowedOnDomains)) { - if(isMatchingDomain(host, domain)) - return true; - } - - return false; -} - -void FilterDomain::process(QWebEngineUrlRequestInfo &info) const -{ - for(const FilterRule_ptr &rule : m_rules) { - if(rule->matchRequestUrl(info.requestUrl().toString(), info.resourceType())) - rule->process(info); - } -} diff --git a/lib/web/urlfilter/filterdomain.h b/lib/web/urlfilter/filterdomain.h deleted file mode 100644 index b356a32..0000000 --- a/lib/web/urlfilter/filterdomain.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef FILTERDOMAIN_H -#define FILTERDOMAIN_H - -#include <QObject> -#include <QVector> -#include <memory> -#include "filterrule.h" - -typedef std::unique_ptr<FilterRule> FilterRule_ptr; -typedef QVector<QString> QStringVector; - -class FilterDomain : public QObject -{ - Q_OBJECT -public: - explicit FilterDomain(QObject *parent = nullptr); - - void addDomain(const QString &domain, bool isException = false); - void addRule(FilterRule_ptr &rule); - - bool hasMatch(const QString &host) const; - void process(QWebEngineUrlRequestInfo &info) const; - -private: - Q_DISABLE_COPY(FilterDomain) - - // lists of domains this rule group? is allowed on and blocked on - QStringVector m_allowedOnDomains; - QStringVector m_blockedOnDomains; - - std::vector<FilterRule_ptr> m_rules; -}; - -// function taken from KDE/Falkon -bool isMatchingDomain(const QString &domain, const QString &filter); - -#endif // FILTERDOMAIN_H diff --git a/lib/web/urlfilter/filterrule.cpp b/lib/web/urlfilter/filterrule.cpp index 73b357b..ba2181f 100644 --- a/lib/web/urlfilter/filterrule.cpp +++ b/lib/web/urlfilter/filterrule.cpp @@ -1,139 +1,89 @@ #include "filterrule.h" -#include <QWebEngineUrlRequestInfo> -#include <QJsonArray> -void parseJson(std::unique_ptr<FilterRule> &rule, const QJsonObject &filter) +inline bool isMatchingDomain(const QString &domain, const QString &filter) { - // set action - { - if(!filter.value("whitelist").isUndefined()) { - rule->setActionType(FilterRule::Whitelist); - - } else if(!filter.value("blacklist").isUndefined()) { - rule->setActionType(FilterRule::Blacklist); - - } else if(!filter.value("redirect").isUndefined()) { - rule->setActionType(FilterRule::Redirect); - rule->setRedirectUrl(QUrl::fromUserInput(filter.value("redirect").toString())); - - } else if(!filter.value("setheader").isUndefined()) { - rule->setActionType(FilterRule::SetHeader); - for(const QJsonValue v : filter.value("setheader").toArray()) { - QStringList h = v.toString().split(':'); - rule->addHeaderRule(h.at(0).toLatin1(), h.at(1).toLatin1()); - } - } - + // domain and filter are the same + if(domain == filter) { + return true; } - // set match type - { - const QJsonValue regexpValue = filter.value("regexp"); - const QJsonValue endswithValue = filter.value("endswith"); - const QJsonValue containsValue = filter.value("contains"); - - if(!regexpValue.isUndefined()) { - rule->setMatchType(FilterRule::RegExpMatchRule, regexpValue.toString()); - - } else if(!endswithValue.isUndefined()) { - rule->setMatchType(FilterRule::StringEndsMatchRule, endswithValue.toString()); - - } else if(!containsValue.isUndefined()) { - rule->setMatchType(FilterRule::StringContainsMatchRule, containsValue.toString()); - - } else - rule->setMatchType(FilterRule::MatchAllUrlsRule); + // domain can't be matched by filter if it doesn't end with filter + // ex. example2.com isn't matched by example.com + if(!domain.endsWith(filter)) { + return false; } -} + // match with subdomains + // ex. subdomain.example.com is matched by example.com + int index = domain.indexOf(filter); -FilterRule::FilterRule(const QJsonObject &filter) -{ - m_matcher.setCaseSensitivity(Qt::CaseInsensitive); + // match if (domain ends with filter) && (filter has been found) and (character before filter is '.') + return index > 0 && domain[index - 1] == QLatin1Char('.'); } -void FilterRule::setActionType(ActionType type) +bool FilterRule::isEnabled() const { - m_actionType = type; + return m_isEnabled; } -void FilterRule::setMatchType(MatchType type, const QString &pattern) +bool FilterRule::matchesDomain(const QString &domain) const { - m_matchType = type; - switch (type) { - case RegExpMatchRule: - m_regexp.setPattern(pattern); - break; - case StringEndsMatchRule: - m_pattern = pattern; - break; - case StringContainsMatchRule: - m_matcher.setPattern(pattern); - default: - break; + // no domains have been allowed or blocked -> allow on all domains + if(allowedDomains.isEmpty() && blockedDomains.isEmpty()) + return true; + + if(!blockedDomains.isEmpty()) { + // do not match rule if the domain has been blocked + if(blockedDomains.contains(domain)) + return false; } -} -void FilterRule::setRedirectUrl(const QUrl &url) -{ - m_redirectUrl = url; -} + if(!allowedDomains.isEmpty()) { + if(allowedDomains.contains(domain)) + return true; + } -void FilterRule::addHeaderRule(const QByteArray &header, const QByteArray &value) -{ - m_headers.insert(header, value); + return false; } -bool FilterRule::isValid() const +bool FilterRule::matchesType(QWebEngineUrlRequestInfo::ResourceType type) const { - return (m_matchType != MatchType::InvalidMatch) && (m_actionType != ActionType::InvalidAction); + // no options have been specified -> match all resource types + if(m_resourceTypeOptions.isEmpty()) + return true; + + // this resource type has not been specified -> reject it + if(!m_resourceTypeOptions.contains(type)) + return false; + + // resource type has been specified; true to match, false to exception + return m_resourceTypeOptions.value(type); } -bool FilterRule::process(QWebEngineUrlRequestInfo &info) const +bool FilterRule::matchesUrl(const QUrl &url) const { - Q_ASSERT(m_actionType != ActionType::InvalidAction); + switch (urlMatchType) { + case InvalidMatch: + return false; - if(matchRequestUrl(info.requestUrl().toString(), info.resourceType())) { - switch (m_actionType) { - case ActionType::Whitelist: - info.block(false); - return true; - case ActionType::Blacklist: - info.block(true); - return true; - case ActionType::Redirect: - info.redirect(m_redirectUrl); - return true; - case ActionType::SetHeader: - for(auto it = m_headers.constBegin(); it != m_headers.constEnd(); ++it) { - info.setHttpHeader(it.key(), it.value()); - } - return true; - case ActionType::InvalidAction: - break; - } - } + case RegularExpressionMatch: + if(regexp.match(url.toString()).hasMatch()) + return !m_isException; - return false; -} + case StringContains: + return url.toString().contains(match); -bool FilterRule::matchRequestUrl(const QString &requestUrl, const QWebEngineUrlRequestInfo::ResourceType type) const -{ - Q_ASSERT(m_matchType != MatchType::InvalidMatch); + case StringStartsWith: + return url.toString().startsWith(match); - if(!m_resourceTypeOptions.isEmpty() && !m_resourceTypeOptions.contains(type)) - return false; + case StringEndsWith: + return url.toString().endsWith(match); + + case StringEquals: + return url.toString() == match; + + case DomainMatch: + return isMatchingDomain(url.host(), match); - switch (m_matchType) { - case MatchType::RegExpMatchRule: - return m_regexp.match(requestUrl).hasMatch(); - case MatchType::StringEndsMatchRule: - return requestUrl.endsWith(m_pattern); - case MatchType::StringContainsMatchRule: - return m_matcher.indexIn(requestUrl) != -1; - case MatchType::MatchAllUrlsRule: - return true; - default: - return false; } } diff --git a/lib/web/urlfilter/filterrule.h b/lib/web/urlfilter/filterrule.h index f20ab15..8a622fe 100644 --- a/lib/web/urlfilter/filterrule.h +++ b/lib/web/urlfilter/filterrule.h @@ -1,70 +1,62 @@ #ifndef SMOLBOTE_FILTERRULE_H #define SMOLBOTE_FILTERRULE_H -#include <QObject> #include <QRegularExpression> #include <QStringList> #include <QStringMatcher> -#include <QJsonObject> #include <QUrl> #include <QWebEngineUrlRequestInfo> #include <memory> -class QUrl; class FilterRule { public: - enum ActionType { - Whitelist, - Blacklist, - Redirect, - SetHeader, - InvalidAction + enum UrlMatchType { + InvalidMatch, + RegularExpressionMatch, + StringContains, + StringStartsWith, + StringEndsWith, + StringEquals, + DomainMatch }; - enum MatchType { -// CssRule = 0, // -// DomainMatchRule = 1, // - RegExpMatchRule = 2, // match request url with regexp - StringEndsMatchRule = 3, // request url ends with string - StringContainsMatchRule = 4, // request url contains string - MatchAllUrlsRule = 5, // - InvalidMatch = 6, - }; - - FilterRule(const QJsonObject &filter); - ~FilterRule() = default; + FilterRule() = default; - void setActionType(ActionType type); - void setMatchType(MatchType type, const QString &pattern = QString()); - void setRedirectUrl(const QUrl &url); - void addHeaderRule(const QByteArray &header, const QByteArray &value); + bool isEnabled() const; - bool isValid() const; - bool process(QWebEngineUrlRequestInfo &info) const; - bool matchRequestUrl(const QString &requestUrl, const QWebEngineUrlRequestInfo::ResourceType type) const; + /** + * @brief matchesDomain + * @param domain + * @return + */ + bool matchesDomain(const QString &domain) const; -private: - Q_DISABLE_COPY(FilterRule) + /** + * @brief matchesType + * @param type + * @return true if type matches, false otherwise + */ + bool matchesType(QWebEngineUrlRequestInfo::ResourceType type) const; + /** + * @brief matchesUrl + * @param url + * @return + */ + bool matchesUrl(const QUrl &url) const; - ActionType m_actionType = ActionType::InvalidAction; - MatchType m_matchType = MatchType::InvalidMatch; +protected: + bool m_isEnabled = false; + bool m_isException = false; + UrlMatchType urlMatchType = InvalidMatch; QHash<QWebEngineUrlRequestInfo::ResourceType, bool> m_resourceTypeOptions; - QHash<QByteArray, QByteArray> m_headers; + QStringList allowedDomains, blockedDomains; - // Parsed rule for string matching (CSS Selector for CSS rules) - QString m_matchString; - // Case sensitivity for string matching - Qt::CaseSensitivity m_caseSensitivity = Qt::CaseInsensitive; + QString match; + QRegularExpression regexp; - QUrl m_redirectUrl; - QRegularExpression m_regexp; - QStringMatcher m_matcher; - QString m_pattern; }; -void parseJson(std::unique_ptr<FilterRule> &rule, const QJsonObject &filter); - #endif // SMOLBOTE_FILTERRULE_H diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 1b44c47..a66ab57 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -39,24 +39,7 @@ UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptr<Configuration } } - QFile rules(config->value<QString>("filter.json-path").value()); - if(rules.open(QIODevice::ReadOnly | QIODevice::Text)) { - auto doc = QJsonDocument::fromJson(rules.readAll()).object(); - Q_ASSERT(doc.value("domains").isArray()); - for(QJsonValue d : doc.value("domains").toArray()) { - domain.addDomain(d.toString()); - } - - Q_ASSERT(doc.value("rules").isArray()); - for(QJsonValue rule : doc.value("rules").toArray()) { - auto p = std::make_unique<FilterRule>(rule.toObject()); - parseJson(p, rule.toObject()); - domain.addRule(p); - } - - rules.close(); - } } @@ -72,10 +55,6 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) return; } - if(domain.hasMatch(info.requestUrl().host())) { - domain.process(info); - } - #ifdef QT_DEBUG // qDebug("request>>>"); // qDebug("firstParty url=%s", qUtf8Printable(info.firstPartyUrl().toString())); diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index 06a4b97..2f91e30 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -14,8 +14,6 @@ #include <QVector> #include <QByteArray> -#include "web/urlfilter/filterdomain.h" - typedef std::pair<std::string, std::string> Header; class Configuration; @@ -35,8 +33,6 @@ public: private: QHash<QString, HostRule> rules; std::vector<Header> m_headers; - - FilterDomain domain; }; QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 414d616..a026ad6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,17 +2,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -add_executable(UrlFilterTest - urlfilter/urlfiltertest.cpp - urlfilter/urlfiltertest.h -) - -target_include_directories(UrlFilterTest - PRIVATE ../lib/web -) - -target_link_libraries(UrlFilterTest Qt5::Test web) - add_executable(AdBlockTest urlfilter/adblocktest.cpp urlfilter/adblocktest.h diff --git a/test/urlfilter/adblocktest.cpp b/test/urlfilter/adblocktest.cpp index 304a29e..416bc20 100644 --- a/test/urlfilter/adblocktest.cpp +++ b/test/urlfilter/adblocktest.cpp @@ -2,10 +2,10 @@ #include <QtTest/QtTest> #include "urlfilter/adblockrule.h" -inline bool check(const std::vector<AdBlockRule> rules, const QUrl &url) +inline bool check(const std::vector<FilterRule> rules, const QUrl &url) { - for(const AdBlockRule &rule : rules) { - if(rule.matchesUrl(url)) + for(const FilterRule &rule : rules) { + if(rule.matchesDomain(url.host()) && rule.matchesUrl(url)) return true; } return false; @@ -13,7 +13,7 @@ inline bool check(const std::vector<AdBlockRule> rules, const QUrl &url) void AdBlockTest::parseList() { - std::vector<AdBlockRule> rules; + std::vector<FilterRule> rules; QFile list("adblock.txt"); QCOMPARE(list.open(QIODevice::ReadOnly | QIODevice::Text), true); diff --git a/test/urlfilter/urlfiltertest.cpp b/test/urlfilter/urlfiltertest.cpp deleted file mode 100644 index eb12421..0000000 --- a/test/urlfilter/urlfiltertest.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* ============================================================ -* Falkon - Qt web browser -* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see <http://www.gnu.org/licenses/>. -* ============================================================ */ - -#include "urlfiltertest.h" -#include <QtTest/QtTest> -#include <QUrl> - -#include <QJsonObject> - -void UrlFilterTest::matchingDomain_data() -{ - filterDomain.addDomain("example.com"); - filterDomain.addDomain("second-domain.org"); - - // Test adapted from Falkon - QTest::addColumn<QString>("domain"); - QTest::addColumn<bool>("result"); - - // description site domain result - QTest::newRow("missing tld") << "example" << false; - QTest::newRow("different tld") << "example.org" << false; - QTest::newRow("exact match") << "example.com" << true; - QTest::newRow("exact match 2") << "second-domain.org" << true; - QTest::newRow("subdomain match") << "www.example.com" << true; - QTest::newRow("subdomain match") << "www.test.example.com" << true; - QTest::newRow("similar domain") << "anotherexample.com" << false; - QTest::newRow("empty domain") << "" << false; -} - -void UrlFilterTest::matchingDomain() -{ - QFETCH(QString, domain); - QFETCH(bool, result); - - QCOMPARE(filterDomain.hasMatch(domain), result); -} - -void UrlFilterTest::matchingType_data() -{ - QJsonObject j; - j.insert("action", "Blacklist"); - j.insert("contains", "annoying-ad.banner"); - - filterRule = new FilterRule(j); - Q_ASSERT(filterRule->isValid()); - - QTest::addColumn<QString>("requestUrl"); - QTest::addColumn<int>("resourceType"); - QTest::addColumn<bool>("result"); - - QTest::newRow("contains 1") << "http://example.com/ads/annoying-ad.banner/something" << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeImage) << true; - QTest::newRow("contains 2") << "http://example.com/ads/annoying-ad.banner/something" << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeMedia) << false; - QTest::newRow("contains 3") << "http://example.com/ads/banner" << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeImage) << false; - QTest::newRow("blank") << "" << static_cast<int>(QWebEngineUrlRequestInfo::ResourceTypeUnknown) << false; -} - -void UrlFilterTest::matchingType() -{ - QFETCH(QString, requestUrl); - QFETCH(int, resourceType); - QFETCH(bool, result); - - QCOMPARE(filterRule->matchRequestUrl(requestUrl, static_cast<QWebEngineUrlRequestInfo::ResourceType>(resourceType)), result); -} - -QTEST_GUILESS_MAIN(UrlFilterTest) diff --git a/test/urlfilter/urlfiltertest.h b/test/urlfilter/urlfiltertest.h deleted file mode 100644 index 1b158e8..0000000 --- a/test/urlfilter/urlfiltertest.h +++ /dev/null @@ -1,41 +0,0 @@ -/* ============================================================ -* Falkon - Qt web browser -* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see <http://www.gnu.org/licenses/>. -* ============================================================ */ -#ifndef URLFILTER_TEST_H -#define URLFILTER_TEST_H - -#include "urlfilter/filterrule.h" -#include "urlfilter/filterdomain.h" -#include <QObject> - -class UrlFilterTest : public QObject -{ - Q_OBJECT - -private slots: - void matchingDomain_data(); - void matchingDomain(); - - void matchingType_data(); - void matchingType(); - -private: - FilterDomain filterDomain; - FilterRule *filterRule; -}; - -#endif // URLFILTER_TEST_H |