File indexing completed on 2024-04-21 04:55:38

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "adblocktest.h"
0019 #include "adblockrule.h"
0020 #include "adblocksubscription.h"
0021 
0022 #include <QtTest/QtTest>
0023 
0024 class AdBlockRule_Test : public AdBlockRule
0025 {
0026 public:
0027     QStringList parseRegExpFilter(const QString &parsedFilter)
0028     {
0029         return AdBlockRule::parseRegExpFilter(parsedFilter);
0030     }
0031 
0032     bool isMatchingDomain(const QString &domain, const QString &filter) const
0033     {
0034         return AdBlockRule::isMatchingDomain(domain, filter);
0035     }
0036 };
0037 
0038 void AdBlockTest::isMatchingCookieTest_data()
0039 {
0040     // Test copied from CookiesTest
0041     QTest::addColumn<QString>("filterDomain");
0042     QTest::addColumn<QString>("siteDomain");
0043     QTest::addColumn<bool>("result");
0044 
0045     QTest::newRow("test1") << "example.com" << "www.example.com" << true;
0046     QTest::newRow("test2") << "example.com" << "example.com" << true;
0047     QTest::newRow("test3") << "example.com" << "anotherexample.com" << false;
0048     QTest::newRow("test4") << "test.example.com" << "example.com" << false;
0049     QTest::newRow("test5") << "www.example.com" << "example.com" << false;
0050     QTest::newRow("test_empty") << "www.example.com" << "" << false;
0051     QTest::newRow("test_empty2") << "" << "example.com" << false;
0052 }
0053 
0054 void AdBlockTest::isMatchingCookieTest()
0055 {
0056     AdBlockRule_Test rule_test;
0057 
0058     QFETCH(QString, filterDomain);
0059     QFETCH(QString, siteDomain);
0060     QFETCH(bool, result);
0061 
0062     QCOMPARE(rule_test.isMatchingDomain(siteDomain, filterDomain), result);
0063 }
0064 
0065 void AdBlockTest::parseRegExpFilterTest_data()
0066 {
0067     QTest::addColumn<QString>("parsedFilter");
0068     QTest::addColumn<QStringList>("result");
0069 
0070     QTest::newRow("test1") << QSL("||doubleclick.net/pfadx/tmg.telegraph.")
0071                            << (QStringList() << QSL("doubleclick.net/pfadx/tmg.telegraph."));
0072     QTest::newRow("test2") << QSL("||doubleclick.net/pfadx/*.mtvi")
0073                            << (QStringList() << QSL("doubleclick.net/pfadx/") << QSL(".mtvi"));
0074     QTest::newRow("test3") << QSL("&prvtof=*&poru=")
0075                            << (QStringList() << QSL("&prvtof=") << QSL("&poru="));
0076     QTest::newRow("test4") << QSL("/addyn|*;adtech;")
0077                            << (QStringList() << QSL("/addyn") << QSL(";adtech;"));
0078     QTest::newRow("test5") << QSL("/eas_fif.html^")
0079                            << (QStringList() << QSL("/eas_fif.html"));
0080     QTest::newRow("test6") << QSL("://findnsave.^.*/api/groupon.json?")
0081                            << (QStringList() << QSL("://findnsave.") << QSL("/api/groupon.json?"));
0082     QTest::newRow("test7") << QSL("^fp=*&prvtof=")
0083                            << (QStringList() << QSL("fp=") << QSL("&prvtof="));
0084     QTest::newRow("test8") << QSL("|http://ax-d.*/jstag^")
0085                            << (QStringList() << QSL("http://ax-d.") << QSL("/jstag"));
0086     QTest::newRow("test9") << QSL("||reuters.com^*/rcom-wt-mlt.js")
0087                            << (QStringList() << QSL("reuters.com") <<QSL("/rcom-wt-mlt.js"));
0088     QTest::newRow("test10") << QSL("||chip.de^*/tracking.js")
0089                            << (QStringList() << QSL("chip.de") << QSL("/tracking.js"));
0090     QTest::newRow("ignore1char") << QSL("/search.php?uid=*.*&src=")
0091                            << (QStringList() << QSL("/search.php?uid=") << QSL("&src="));
0092     QTest::newRow("ignoreDuplicates") << QSL("/search.*.dup.*.dup.*&src=")
0093                            << (QStringList() << QSL("/search.") << QSL(".dup.") << QSL("&src="));
0094     QTest::newRow("empty") << QString()
0095                            << (QStringList());
0096     QTest::newRow("justspaces") << QSL("       ")
0097                            << (QStringList() << QSL("       "));
0098     QTest::newRow("spacesWithMetachars") << QSL("   *    ?")
0099                            << (QStringList() << QSL("   ") << QSL("    ?"));
0100 }
0101 
0102 void AdBlockTest::parseRegExpFilterTest()
0103 {
0104     AdBlockRule_Test rule_test;
0105 
0106     QFETCH(QString, parsedFilter);
0107     QFETCH(QStringList, result);
0108 
0109     QCOMPARE(rule_test.parseRegExpFilter(parsedFilter), result);
0110 }
0111 
0112 void AdBlockTest::ignoreEmptyLinesInSubscriptionTest()
0113 {
0114     AdBlockSubscription subscription(QSL("test-subscription"));
0115     subscription.setFilePath(QSL(":autotests/data/adblock_empty_lines.txt"));
0116     subscription.loadSubscription({});
0117 
0118     QCOMPARE(subscription.allRules().count(), 3);
0119     QCOMPARE(subscription.allRules().at(0)->filter(), QSL("filter.com"));
0120     QCOMPARE(subscription.allRules().at(1)->filter(), QSL("test"));
0121     QCOMPARE(subscription.allRules().at(2)->isComment(), true);
0122 }
0123 
0124 QTEST_GUILESS_MAIN(AdBlockTest)