aboutsummaryrefslogtreecommitdiff
path: root/test/hostlist/hostlisttest.cpp
blob: 7f5c9544616254a3a9e0a8a578ee593aff495eaf (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
#include "hostlisttest.h"
#include <QtTest/QtTest>

void HostlistTest::parseList()
{
    // load filters
    QFile hostlist("hostlist.txt");
    QCOMPARE(hostlist.open(QIODevice::ReadOnly | QIODevice::Text), true);
    QCOMPARE(loadHostlist(hostlist, &tree), true);

    // hostlist filters are applied to all domains, so there should only be one branch
    QCOMPARE(tree.branches().length(), 1);
}

void HostlistTest::checkRules_data()
{
    QTest::addColumn<QUrl>("domain");
    QTest::addColumn<QUrl>("request");
    QTest::addColumn<int>("matches");
    QTest::addColumn<FilterLeaf::Action>("action");

    const QVector<QUrl> domains{ QUrl(), QUrl::fromUserInput("testdomain.host") };
    for(const QUrl &domain : domains) {
        QTest::newRow("block (1 domain per line)") << domain << QUrl::fromUserInput("blockeddomain.com") << 1 << FilterLeaf::Block;
        QTest::newRow("block (2 domains per line #1)") << domain << QUrl::fromUserInput("blockeddomain.first") << 1 << FilterLeaf::Block;
        QTest::newRow("block (2 domains per line #2)") << domain << QUrl::fromUserInput("blockeddomain.second") << 1 << FilterLeaf::Block;
        QTest::newRow("redirect") << domain << QUrl::fromUserInput("localhost.localdomain") << 1 << FilterLeaf::Redirect;
        QTest::newRow("domain not in hostlist") << domain << QUrl::fromUserInput("other.domain") << 0 << FilterLeaf::NotMatched;
    }
}

void HostlistTest::checkRules()
{
    QFETCH(QUrl, domain);
    QFETCH(QUrl, request);
    QFETCH(int, matches);
    QFETCH(FilterLeaf::Action, action);

    auto result = tree.match(domain, request);
    QCOMPARE(result.length(), matches);
    if(matches > 0)
        QCOMPARE(result.constFirst()->action().first, action);
    if(action == FilterLeaf::Redirect)
        QCOMPARE(result.constFirst()->action().second, QLatin1Literal("127.0.0.1"));
}

void HostlistTest::benchmark_parse()
{
    QFile hostlist("hostlist-benchmark.txt");
    if(hostlist.open(QIODevice::ReadOnly | QIODevice::Text)) {
        FilterTree benchmarkTree;
        bool loaded;
        QBENCHMARK {
            loaded = loadHostlist(hostlist, &benchmarkTree);
        }
        QCOMPARE(loaded, true);
        hostlist.close();
    }
}

QTEST_GUILESS_MAIN(HostlistTest)