aboutsummaryrefslogtreecommitdiff
path: root/plugins/HostlistFilter/test/rule.cpp
blob: b5ba6e047290faf2db92e1a49c344eb5deb7e5ae (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
#define CATCH_CONFIG_MAIN
#include "filterlist.h"
#include <catch2/catch.hpp>

using namespace Hostlist;

SCENARIO("Hostlist::Rule")
{
    GIVEN("an invalid rule")
    {
        const auto rule = Filterlist::parseRule("0.0.0.0 ");
        REQUIRE(rule.empty());
    }
    GIVEN("127.0.0.1 localhost.localdomain")
    {
        auto rule = Filterlist::parseRule("127.0.0.1 localhost.localdomain");

        REQUIRE(!rule.empty());
        REQUIRE(rule.size() == 1);

        // note: you need to force it to hash a string, rather than the address itself
        const auto index = qHash(QString("localhost.localdomain"), 0);
        REQUIRE(rule[index].domain == "localhost.localdomain");
        REQUIRE(rule[index].redirect == "127.0.0.1");
    }

    GIVEN("0.0.0.0 blockeddomain.com")
    {
        auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.com");

        REQUIRE(!rule.empty());
        REQUIRE(rule.size() == 1);

        const auto index = qHash(QString("blockeddomain.com"), 0);
        REQUIRE(rule[index].domain == "blockeddomain.com");
        REQUIRE(rule[index].redirect.isEmpty());
        ;
    }

    GIVEN("0.0.0.0 blockeddomain.first blockeddomain.second")
    {
        auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.first blockeddomain.second");

        REQUIRE(!rule.empty());
        REQUIRE(rule.size() == 2);
        {
            const auto index = qHash(QString("blockeddomain.first"), 0);
            REQUIRE(rule[index].domain == "blockeddomain.first");
            REQUIRE(rule[index].redirect.isEmpty());
        }
        {
            const auto index = qHash(QString("blockeddomain.second"), 0);
            REQUIRE(rule[index].domain == "blockeddomain.second");
            REQUIRE(rule[index].redirect.isEmpty());
        }
    }
}