aboutsummaryrefslogtreecommitdiff
path: root/test/urlfilter/adblocktest.cpp
blob: 304a29ed7dccec5383b1e4649cc29cffbfd0f3b7 (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
#include "adblocktest.h"
#include <QtTest/QtTest>
#include "urlfilter/adblockrule.h"

inline bool check(const std::vector<AdBlockRule> rules, const QUrl &url)
{
    for(const AdBlockRule &rule : rules) {
        if(rule.matchesUrl(url))
            return true;
    }
    return false;
}

void AdBlockTest::parseList()
{
    std::vector<AdBlockRule> rules;

    QFile list("adblock.txt");
    QCOMPARE(list.open(QIODevice::ReadOnly | QIODevice::Text), true);
    {
        QTextStream l(&list);
        QString line;
        while(l.readLineInto(&line)) {
            AdBlockRule rule(line);
            if(rule.isEnabled()) {
                rules.emplace_back(std::move(rule));
                qDebug("added rule: %s", qUtf8Printable(line));
            }
        }
    }
    list.close();

    // there should be 3 rules
    QCOMPARE(rules.size(), 3);

    // block by address part
    QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/img")), true);
    QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/bar/img?param")), true);
    QCOMPARE(check(rules, QUrl("http://example.com/banner//img/foo")), true);
    QCOMPARE(check(rules, QUrl("http://example.com/banner/img")), false);
    QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/imgraph")), false);
    QCOMPARE(check(rules, QUrl("http://example.com/banner/foo/img.gif")), false);

    // block by domain
    QCOMPARE(check(rules, QUrl("http://ads.example.com/foo.gif")), true);
    QCOMPARE(check(rules, QUrl("http://server1.ads.example.com/foo.gif")), true);
    QCOMPARE(check(rules, QUrl("https://ads.example.com:8000/")), true);
    QCOMPARE(check(rules, QUrl("http://ads.example.com.ua/foo.gif")), false);
    QCOMPARE(check(rules, QUrl("http://example.com/redirect/http://ads.example.com/")), false);


    // block exact address
    QCOMPARE(check(rules, QUrl("http://example.com/")), true);
    QCOMPARE(check(rules, QUrl("http://example.com/foo.gif")), false);
    QCOMPARE(check(rules, QUrl("http://example.info/redirect/http://example.com/")), false);
}

QTEST_GUILESS_MAIN(AdBlockTest)