aboutsummaryrefslogtreecommitdiff
path: root/test/urlfilter/urlfiltertest.cpp
blob: eb124216ff5d4202eb6c3df7f97a168a7370d6c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* ============================================================
* 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)