aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/test/filterlist.cpp
blob: ca122acc846509ad2f5a7097ac3f20300e35d116 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#define CATCH_CONFIG_MAIN
#include "filterlist.h"
#include <QBuffer>
#include <catch2/catch.hpp>

using namespace AdblockPlus;

QByteArray sampleList =
    R"(! comment on line
! Last modified: 1 Jan 2000 00:00 UTC
! Expires: 4 days (update frequency)
)";

TEST_CASE("placeholder")
{
    QBuffer buffer(&sampleList);
    buffer.open(QIODevice::ReadOnly | QIODevice::Text);

    AdblockPlus::FilterList list(buffer);
    REQUIRE(!list.isUpToDate());
}

TEST_CASE("domain match")
{
    const QString defaultUrl = "";

    const QString block1 = "http://ads.example.com/foo.gif";
    const QString block2 = "http://server1.ads.example.com/foo.gif";
    const QString block3 = "https://ads.example.com:8000/";
    const QString block4 = "https://ads.example.com";

    const QString allow1 = "http://ads.example.com.ua/foo.gif";
    const QString allow2 = "http://example.com/redirect/http://ads.example.com/";

    auto *rule = FilterList::parseRule("||ads.example.com^");
    REQUIRE(rule->shouldBlock());
    REQUIRE(!rule->shouldRedirect());

    REQUIRE(rule->hasMatch(&block1, &defaultUrl, &defaultUrl));
    REQUIRE(rule->hasMatch(&block2, &defaultUrl, &defaultUrl));
    REQUIRE(rule->hasMatch(&block3, &defaultUrl, &defaultUrl));
    REQUIRE(rule->hasMatch(&block4, &defaultUrl, &defaultUrl));

    REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl));
    REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl));

    delete rule;
}

TEST_CASE("string equals")
{
    const QString defaultUrl = "";

    const QString block = "http://example.com/";

    const QString allow1 = "http://example.com/foo.gif";
    const QString allow2 = "http://example.info/redirect/http://example.com/";

    auto *rule = FilterList::parseRule("|http://example.com/|");
    REQUIRE(rule->shouldBlock());
    REQUIRE(!rule->shouldRedirect());

    REQUIRE(rule->hasMatch(&block, &defaultUrl, &defaultUrl));

    REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl));
    REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl));

    delete rule;
}

TEST_CASE("string starts with")
{
    const QString defaultUrl = "";

    auto *rule = FilterList::parseRule("|http://baddomain.example/");
    REQUIRE(rule->shouldBlock());
    REQUIRE(!rule->shouldRedirect());

    const QString blocks = "http://baddomain.example/banner.gif";
    const QString allows = "http://gooddomain.example/analyze?http://baddomain.example";

    REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl));
    REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl));
    delete rule;
}

TEST_CASE("string ends with")
{
    const QString defaultUrl = "";

    auto *rule = FilterList::parseRule("swf|");
    REQUIRE(rule->shouldBlock());
    REQUIRE(!rule->shouldRedirect());

    const QString blocks = "http://example.com/annoyingflash.swf";
    const QString allows = "http://example.com/swf/index.html";

    REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl));
    REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl));
    delete rule;
}

TEST_CASE("regular expressions")
{
    const QString defaultUrl = "";

    auto *rule = FilterList::parseRule("/banner\\d+/");
    const QString matches1 = "banner123";
    const QString matches2 = "banner321";

    const QString ignores = "banners";

    REQUIRE(rule->hasMatch(&matches1, &defaultUrl, &defaultUrl));
    REQUIRE(rule->hasMatch(&matches2, &defaultUrl, &defaultUrl));
    REQUIRE(!rule->hasMatch(&ignores, &defaultUrl, &defaultUrl));
    delete rule;
}