diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 9f65ff49..78d8b625 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -249,14 +249,9 @@ char _FTL_new_query(const unsigned int flags, const char *name, const struct all match_regex(domainString, client, REGEX_BLACKLIST) && !in_whitelist(domainString, client)) { - // Mark domain as regex match + // Mark domain as regex match (note that this might not apply for all clients!) domain->regexmatch = REGEX_BLOCKED; - } - // Status can be REGEX_BLOCKED either due to being set above - // or by having been set before for this domain - if(domain->regexmatch == REGEX_BLOCKED) - { // We have to block this domain blockDomain = 1; query->status = QUERY_WILDCARD; diff --git a/test/gravity.db.sql b/test/gravity.db.sql index 4a1b5af3..f141d7c2 100644 --- a/test/gravity.db.sql +++ b/test/gravity.db.sql @@ -216,20 +216,15 @@ INSERT INTO domain_audit VALUES(1,'google.com',1559928803); INSERT INTO client VALUES(1,"127.0.0.1"); -# A group associated with client 127.0.0.2 -# We do connect one adlist, and one regex blacklist INSERT INTO client VALUES(2,"127.0.0.2"); INSERT INTO "group" VALUES(2,1,"Second test group","A group associated with client 127.0.0.2"); INSERT INTO client_by_group VALUES(2,2); INSERT INTO adlist_by_group VALUES(1,2); INSERT INTO regex_blacklist_by_group VALUES(1,2); -# A group associated with client 127.0.0.3 -# We do not connect any domains so we expect no queries to be blocked at all INSERT INTO client VALUES(3,"127.0.0.3"); INSERT INTO "group" VALUES(3,1,"Third test group","A group associated with client 127.0.0.3"); INSERT INTO client_by_group VALUES(3,3); -INSERT INTO adlist_by_group VALUES(1,3); INSERT INTO info VALUES("version","4"); diff --git a/test/test_suite.bats b/test/test_suite.bats index c9446ad8..839b199e 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -43,6 +43,13 @@ [[ ${lines[1]} == "" ]] } +@test "Gravity domain is blocked (TCP)" { + run bash -c "dig gravity-blocked.test.pi-hole.net @127.0.0.1 +tcp +short" + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} == "0.0.0.0" ]] + [[ ${lines[1]} == "" ]] +} + @test "Gravity domain + whitelist exact match is not blocked" { run bash -c "dig whitelisted.test.pi-hole.net @127.0.0.1 +short" printf "%s\n" "${lines[@]}" @@ -80,24 +87,42 @@ [[ ${lines[0]} != "0.0.0.0" ]] } -@test "Per-client: Gravity match matching unassociated whitelist is blocked" { +@test "Client 2: Gravity match matching unassociated whitelist is blocked" { run bash -c "dig whitelisted.test.pi-hole.net -b 127.0.0.2 @127.0.0.1 +short" printf "%s\n" "${lines[@]}" [[ ${lines[0]} == "0.0.0.0" ]] } -@test "Per-client: Regex blacklist match matching unassociated whitelist is blocked" { +@test "Client 2: Regex blacklist match matching unassociated whitelist is blocked" { run bash -c "dig regex1.test.pi-hole.net -b 127.0.0.2 @127.0.0.1 +short" printf "%s\n" "${lines[@]}" [[ ${lines[0]} == "0.0.0.0" ]] } -@test "Per-client: Unassociated blacklist match is not blocked" { +@test "Client 2: Unassociated blacklist match is not blocked" { run bash -c "dig blacklist-blocked.test.pi-hole.net -b 127.0.0.2 @127.0.0.1 +short" printf "%s\n" "${lines[@]}" [[ ${lines[0]} != "0.0.0.0" ]] } +@test "Client 3: Exact blacklist domain is not blocked" { + run bash -c "dig blacklist-blocked.test.pi-hole.net -b 127.0.0.3 @127.0.0.1 +short" + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} != "0.0.0.0" ]] +} + +@test "Client 3: Regex blacklist domain is not blocked" { + run bash -c "dig regex1.test.pi-hole.net -b 127.0.0.3 @127.0.0.1 +short" + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} != "0.0.0.0" ]] +} + +@test "Client 3: Gravity domain is not blocked" { + run bash -c "dig discourse.pi-hole.net -b 127.0.0.3 @127.0.0.1 +short" + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} != "0.0.0.0" ]] +} + @test "Google.com (A) is not blocked" { run bash -c "dig A google.com @127.0.0.1 +short" printf "%s\n" "${lines[@]}" @@ -122,92 +147,60 @@ run bash -c 'echo ">stats >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" [[ ${lines[1]} == "domains_being_blocked 3" ]] - [[ ${lines[2]} == "dns_queries_today 16" ]] - [[ ${lines[3]} == "ads_blocked_today 5" ]] - [[ ${lines[4]} == "ads_percentage_today 31.250000" ]] + [[ ${lines[2]} == "dns_queries_today 20" ]] + [[ ${lines[3]} == "ads_blocked_today 6" ]] + [[ ${lines[4]} == "ads_percentage_today 30.000000" ]] [[ ${lines[5]} == "unique_domains 12" ]] [[ ${lines[6]} == "queries_forwarded 9" ]] - [[ ${lines[7]} == "queries_cached 4" ]] - [[ ${lines[8]} == "clients_ever_seen 2" ]] - [[ ${lines[9]} == "unique_clients 2" ]] - [[ ${lines[10]} == "dns_queries_all_types 16" ]] + [[ ${lines[7]} == "queries_cached 5" ]] + [[ ${lines[8]} == "clients_ever_seen 3" ]] + [[ ${lines[9]} == "unique_clients 3" ]] + [[ ${lines[10]} == "dns_queries_all_types 20" ]] [[ ${lines[11]} == "reply_NODATA 0" ]] [[ ${lines[12]} == "reply_NXDOMAIN 0" ]] [[ ${lines[13]} == "reply_CNAME 0" ]] - [[ ${lines[14]} == "reply_IP 16" ]] + [[ ${lines[14]} == "reply_IP 18" ]] [[ ${lines[15]} == "privacy_level 0" ]] [[ ${lines[16]} == "status enabled" ]] [[ ${lines[17]} == "" ]] } -@test "Top Clients (descending, default)" { - run bash -c 'echo ">top-clients >quit" | nc -v 127.0.0.1 4711' - printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "0 13 127.0.0.1 "* ]] - [[ ${lines[2]} == "1 3 127.0.0.2 "* ]] - [[ ${lines[3]} == "" ]] -} - -@test "Top Clients (ascending)" { - run bash -c 'echo ">top-clients asc >quit" | nc -v 127.0.0.1 4711' - printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "0 3 127.0.0.2 "* ]] - [[ ${lines[2]} == "1 13 127.0.0.1 "* ]] - [[ ${lines[3]} == "" ]] -} - # Here and below: It is not meaningful to assume a particular order # here as the values are sorted before output. It is unpredictable in # which order they may come out. While this has always been the same # when compiling for glibc, the new musl build reveals that another # library may have a different interpretation here. -@test "Top Domains (descending, default)" { +@test "Top Clients" { + run bash -c 'echo ">top-clients >quit" | nc -v 127.0.0.1 4711' + printf "%s\n" "${lines[@]}" + [[ ${lines[1]} == "0 14 127.0.0.1 "* ]] + [[ ${lines[2]} == "1 3 127.0.0.2 "* ]] + [[ ${lines[3]} == "2 3 127.0.0.3 "* ]] + [[ ${lines[4]} == "" ]] +} + +@test "Top Domains" { run bash -c 'echo ">top-domains (20) >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" - [[ "${lines[1]}" == *" 2 google.com"* ]] + [[ "${lines[@]}" == *" 2 google.com"* ]] + [[ "${lines[@]}" == *" 2 blacklist-blocked.test.pi-hole.net"* ]] + [[ "${lines[@]}" == *" 2 discourse.pi-hole.net"* ]] + [[ "${lines[@]}" == *" 2 regex1.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 version.ftl"* ]] [[ "${lines[@]}" == *" 1 version.bind"* ]] - [[ "${lines[@]}" == *" 1 blacklist-blocked.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 whitelisted.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 regexa.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 regex1.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 regex2.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 ftl.pi-hole.net"* ]] [[ "${lines[11]}" == "" ]] } -@test "Top Domains (ascending)" { - run bash -c 'echo ">top-domains asc (20) >quit" | nc -v 127.0.0.1 4711' - printf "%s\n" "${lines[@]}" - [[ "${lines[@]}" == *" 1 version.ftl"* ]] - [[ "${lines[@]}" == *" 1 version.bind"* ]] - [[ "${lines[@]}" == *" 1 blacklist-blocked.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 whitelisted.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 regexa.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 regex1.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 regex2.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 ftl.pi-hole.net"* ]] - [[ "${lines[10]}" == *" 2 google.com"* ]] - [[ "${lines[11]}" == "" ]] -} - -@test "Top Ads (descending, default)" { +@test "Top Ads" { run bash -c 'echo ">top-ads (20) >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" + [[ "${lines[@]}" == *" 2 gravity-blocked.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 blacklist-blocked.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 gravity-blocked.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 whitelisted.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 regex5.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 regex1.test.pi-hole.net"* ]] - [[ ${lines[6]} == "" ]] -} - -@test "Top Ads (ascending)" { - run bash -c 'echo ">top-ads asc (20) >quit" | nc -v 127.0.0.1 4711' - printf "%s\n" "${lines[@]}" - [[ "${lines[@]}" == *" 1 blacklist-blocked.test.pi-hole.net"* ]] - [[ "${lines[@]}" == *" 1 gravity-blocked.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 whitelisted.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 regex5.test.pi-hole.net"* ]] [[ "${lines[@]}" == *" 1 regex1.test.pi-hole.net"* ]] @@ -223,31 +216,22 @@ @test "Forward Destinations" { run bash -c 'echo ">forward-dest >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "-2 27.78 blocklist blocklist" ]] - [[ ${lines[2]} == "-1 22.22 cache cache" ]] - [[ ${lines[3]} == "0 50.00 "* ]] - [[ ${lines[4]} == "" ]] -} - -@test "Forward Destinations (unsorted)" { - run bash -c 'echo ">forward-dest unsorted >quit" | nc -v 127.0.0.1 4711' - printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "-2 27.78 blocklist blocklist" ]] - [[ ${lines[2]} == "-1 22.22 cache cache" ]] - [[ ${lines[3]} == "0 50.00 "* ]] + [[ ${lines[1]} == "-2 30.00 blocklist blocklist" ]] + [[ ${lines[2]} == "-1 25.00 cache cache" ]] + [[ ${lines[3]} == "0 45.00 "* ]] [[ ${lines[4]} == "" ]] } @test "Query Types" { run bash -c 'echo ">querytypes >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "A (IPv4): 81.25" ]] - [[ ${lines[2]} == "AAAA (IPv6): 6.25" ]] + [[ ${lines[1]} == "A (IPv4): 85.00" ]] + [[ ${lines[2]} == "AAAA (IPv6): 5.00" ]] [[ ${lines[3]} == "ANY: 0.00" ]] [[ ${lines[4]} == "SRV: 0.00" ]] [[ ${lines[5]} == "SOA: 0.00" ]] [[ ${lines[6]} == "PTR: 0.00" ]] - [[ ${lines[7]} == "TXT: 12.50" ]] + [[ ${lines[7]} == "TXT: 10.00" ]] [[ ${lines[8]} == "" ]] } @@ -261,19 +245,23 @@ [[ ${lines[2]} == *"TXT version.bind "?*" 3 0 6"* ]] [[ ${lines[3]} == *"A blacklist-blocked.test.pi-hole.net "?*" 5 0 4"* ]] [[ ${lines[4]} == *"A gravity-blocked.test.pi-hole.net "?*" 1 0 4"* ]] - [[ ${lines[5]} == *"A whitelisted.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[6]} == *"A discourse.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[7]} == *"A regex5.test.pi-hole.net "?*" 4 0 4"* ]] - [[ ${lines[8]} == *"A regexa.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[9]} == *"A regex1.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[10]} == *"A regex2.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[11]} == *"A whitelisted.test.pi-hole.net 127.0.0.2 3 0 4"* ]] - [[ ${lines[12]} == *"A regex1.test.pi-hole.net 127.0.0.2 3 0 4"* ]] - [[ ${lines[13]} == *"A blacklist-blocked.test.pi-hole.net 127.0.0.2 2 0 4"* ]] - [[ ${lines[14]} == *"A google.com "?*" 2 0 4"* ]] - [[ ${lines[15]} == *"AAAA google.com "?*" 2 0 4"* ]] - [[ ${lines[16]} == *"A ftl.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[17]} == "" ]] + [[ ${lines[5]} == *"A gravity-blocked.test.pi-hole.net "?*" 1 0 4"* ]] + [[ ${lines[6]} == *"A whitelisted.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[7]} == *"A discourse.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[8]} == *"A regex5.test.pi-hole.net "?*" 4 0 4"* ]] + [[ ${lines[9]} == *"A regexa.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[10]} == *"A regex1.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[11]} == *"A regex2.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[12]} == *"A whitelisted.test.pi-hole.net 127.0.0.2 1 0 4"* ]] + [[ ${lines[13]} == *"A regex1.test.pi-hole.net 127.0.0.2 4 0 4"* ]] + [[ ${lines[14]} == *"A blacklist-blocked.test.pi-hole.net 127.0.0.2 2 0 4"* ]] + [[ ${lines[15]} == *"A blacklist-blocked.test.pi-hole.net 127.0.0.3 3 0 4"* ]] + [[ ${lines[16]} == *"A regex1.test.pi-hole.net 127.0.0.3 3 0 4"* ]] + [[ ${lines[17]} == *"A discourse.pi-hole.net 127.0.0.3 3 0 4"* ]] + [[ ${lines[18]} == *"A google.com "?*" 2 0 4"* ]] + [[ ${lines[19]} == *"AAAA google.com "?*" 2 0 4"* ]] + [[ ${lines[20]} == *"A ftl.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[21]} == "" ]] } @test "Get all queries (domain filtered)" { @@ -284,7 +272,7 @@ } @test "Get all queries (domain + number filtered)" { - run bash -c 'echo ">getallqueries-domain regexa.test.pi-hole.net (9) >quit" | nc -v 127.0.0.1 4711' + run bash -c 'echo ">getallqueries-domain regexa.test.pi-hole.net (20) >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" [[ ${lines[1]} == *"A regexa.test.pi-hole.net "?*" 2 0 4"* ]] [[ ${lines[2]} == "" ]] @@ -297,16 +285,17 @@ [[ ${lines[2]} == *"TXT version.bind "?*" 3 0 6"* ]] [[ ${lines[3]} == *"A blacklist-blocked.test.pi-hole.net "?*" 5 0 4"* ]] [[ ${lines[4]} == *"A gravity-blocked.test.pi-hole.net "?*" 1 0 4"* ]] - [[ ${lines[5]} == *"A whitelisted.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[6]} == *"A discourse.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[7]} == *"A regex5.test.pi-hole.net "?*" 4 0 4"* ]] - [[ ${lines[8]} == *"A regexa.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[9]} == *"A regex1.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[10]} == *"A regex2.test.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[11]} == *"A google.com "?*" 2 0 4"* ]] - [[ ${lines[12]} == *"AAAA google.com "?*" 2 0 4"* ]] - [[ ${lines[13]} == *"A ftl.pi-hole.net "?*" 2 0 4"* ]] - [[ ${lines[14]} == "" ]] + [[ ${lines[5]} == *"A gravity-blocked.test.pi-hole.net "?*" 1 0 4"* ]] + [[ ${lines[6]} == *"A whitelisted.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[7]} == *"A discourse.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[8]} == *"A regex5.test.pi-hole.net "?*" 4 0 4"* ]] + [[ ${lines[9]} == *"A regexa.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[10]} == *"A regex1.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[11]} == *"A regex2.test.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[12]} == *"A google.com "?*" 2 0 4"* ]] + [[ ${lines[13]} == *"AAAA google.com "?*" 2 0 4"* ]] + [[ ${lines[14]} == *"A ftl.pi-hole.net "?*" 2 0 4"* ]] + [[ ${lines[15]} == "" ]] } @test "Get all queries (client + number filtered)" { @@ -320,7 +309,7 @@ @test "Recent blocked" { run bash -c 'echo ">recentBlocked >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "regex5.test.pi-hole.net" ]] + [[ ${lines[1]} == "regex1.test.pi-hole.net" ]] [[ ${lines[2]} == "" ]] }