diff --git a/test/gravity.db.sql b/test/gravity.db.sql index 57926216..7fefac3c 100644 --- a/test/gravity.db.sql +++ b/test/gravity.db.sql @@ -134,7 +134,7 @@ CREATE VIEW vw_gravity AS SELECT domain, adlist_by_group.group_id AS group_id LEFT JOIN "group" ON "group".id = adlist_by_group.group_id WHERE adlist.enabled = 1 AND (adlist_by_group.group_id IS NULL OR "group".enabled = 1); -CREATE VIEW vw_whitelist AS SELECT domain, whitelist_by_group.group_id AS group_id +CREATE VIEW vw_whitelist AS SELECT domain, whitelist.id AS id, whitelist_by_group.group_id AS group_id FROM whitelist LEFT JOIN whitelist_by_group ON whitelist_by_group.whitelist_id = whitelist.id LEFT JOIN "group" ON "group".id = whitelist_by_group.group_id @@ -146,7 +146,7 @@ CREATE TRIGGER tr_whitelist_update AFTER UPDATE ON whitelist UPDATE whitelist SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE domain = NEW.domain; END; -CREATE VIEW vw_blacklist AS SELECT domain, blacklist_by_group.group_id AS group_id +CREATE VIEW vw_blacklist AS SELECT domain, blacklist.id AS id, blacklist_by_group.group_id AS group_id FROM blacklist LEFT JOIN blacklist_by_group ON blacklist_by_group.blacklist_id = blacklist.id LEFT JOIN "group" ON "group".id = blacklist_by_group.group_id @@ -158,7 +158,7 @@ CREATE TRIGGER tr_blacklist_update AFTER UPDATE ON blacklist UPDATE blacklist SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE domain = NEW.domain; END; -CREATE VIEW vw_regex_blacklist AS SELECT DISTINCT domain +CREATE VIEW vw_regex_blacklist AS SELECT DISTINCT domain, regex_blacklist.id AS id, regex_blacklist_by_group.group_id AS group_id FROM regex_blacklist LEFT JOIN regex_blacklist_by_group ON regex_blacklist_by_group.regex_blacklist_id = regex_blacklist.id LEFT JOIN "group" ON "group".id = regex_blacklist_by_group.group_id @@ -170,7 +170,7 @@ CREATE TRIGGER tr_regex_blacklist_update AFTER UPDATE ON regex_blacklist UPDATE regex_blacklist SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE domain = NEW.domain; END; -CREATE VIEW vw_regex_whitelist AS SELECT DISTINCT domain +CREATE VIEW vw_regex_whitelist AS SELECT DISTINCT domain, regex_whitelist.id AS id, regex_whitelist_by_group.group_id AS group_id FROM regex_whitelist LEFT JOIN regex_whitelist_by_group ON regex_whitelist_by_group.regex_whitelist_id = regex_whitelist.id LEFT JOIN "group" ON "group".id = regex_whitelist_by_group.group_id @@ -214,6 +214,15 @@ INSERT INTO blacklist_by_group VALUES(2,1); INSERT INTO domain_audit VALUES(1,'google.com',1559928803); +INSERT INTO client VALUES(1,"127.0.0.1"); +INSERT INTO client VALUES(2,"127.0.0.2"); +INSERT INTO client VALUES(3,"127.0.0.3"); + +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); + INSERT INTO info VALUES("version","4"); COMMIT; diff --git a/test/test_suite.bats b/test/test_suite.bats index 95f9ee43..c9446ad8 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -80,6 +80,24 @@ [[ ${lines[0]} != "0.0.0.0" ]] } +@test "Per-client: 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" { + 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" { + 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 "Google.com (A) is not blocked" { run bash -c "dig A google.com @127.0.0.1 +short" printf "%s\n" "${lines[@]}" @@ -104,19 +122,19 @@ 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 13" ]] - [[ ${lines[3]} == "ads_blocked_today 3" ]] - [[ ${lines[4]} == "ads_percentage_today 23.076923" ]] + [[ ${lines[2]} == "dns_queries_today 16" ]] + [[ ${lines[3]} == "ads_blocked_today 5" ]] + [[ ${lines[4]} == "ads_percentage_today 31.250000" ]] [[ ${lines[5]} == "unique_domains 12" ]] - [[ ${lines[6]} == "queries_forwarded 8" ]] - [[ ${lines[7]} == "queries_cached 2" ]] - [[ ${lines[8]} == "clients_ever_seen 1" ]] - [[ ${lines[9]} == "unique_clients 1" ]] - [[ ${lines[10]} == "dns_queries_all_types 13" ]] + [[ ${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[11]} == "reply_NODATA 0" ]] [[ ${lines[12]} == "reply_NXDOMAIN 0" ]] [[ ${lines[13]} == "reply_CNAME 0" ]] - [[ ${lines[14]} == "reply_IP 11" ]] + [[ ${lines[14]} == "reply_IP 16" ]] [[ ${lines[15]} == "privacy_level 0" ]] [[ ${lines[16]} == "status enabled" ]] [[ ${lines[17]} == "" ]] @@ -126,14 +144,16 @@ 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]} == "" ]] + [[ ${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 13 127.0.0.1 "* ]] - [[ ${lines[2]} == "" ]] + [[ ${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 @@ -148,12 +168,13 @@ [[ "${lines[1]}" == *" 2 google.com"* ]] [[ "${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]}" == "" ]] + [[ "${lines[11]}" == "" ]] } @test "Top Domains (ascending)" { @@ -161,13 +182,14 @@ 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[9]}" == *" 2 google.com"* ]] - [[ "${lines[10]}" == "" ]] + [[ "${lines[10]}" == *" 2 google.com"* ]] + [[ "${lines[11]}" == "" ]] } @test "Top Ads (descending, default)" { @@ -175,8 +197,10 @@ 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[4]} == "" ]] + [[ "${lines[@]}" == *" 1 regex1.test.pi-hole.net"* ]] + [[ ${lines[6]} == "" ]] } @test "Top Ads (ascending)" { @@ -184,8 +208,10 @@ 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[4]} == "" ]] + [[ "${lines[@]}" == *" 1 regex1.test.pi-hole.net"* ]] + [[ ${lines[6]} == "" ]] } @test "Domain auditing, approved domains are not shown" { @@ -197,31 +223,31 @@ @test "Forward Destinations" { run bash -c 'echo ">forward-dest >quit" | nc -v 127.0.0.1 4711' printf "%s\n" "${lines[@]}" - [[ ${lines[1]} == "-2 23.08 blocklist blocklist" ]] - [[ ${lines[2]} == "-1 15.38 cache cache" ]] - [[ ${lines[3]} == "0 61.54 "* ]] + [[ ${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 23.08 blocklist blocklist" ]] - [[ ${lines[2]} == "-1 15.38 cache cache" ]] - [[ ${lines[3]} == "0 61.54 "* ]] + [[ ${lines[1]} == "-2 27.78 blocklist blocklist" ]] + [[ ${lines[2]} == "-1 22.22 cache cache" ]] + [[ ${lines[3]} == "0 50.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): 76.92" ]] - [[ ${lines[2]} == "AAAA (IPv6): 7.69" ]] + [[ ${lines[1]} == "A (IPv4): 81.25" ]] + [[ ${lines[2]} == "AAAA (IPv6): 6.25" ]] [[ ${lines[3]} == "ANY: 0.00" ]] [[ ${lines[4]} == "SRV: 0.00" ]] [[ ${lines[5]} == "SOA: 0.00" ]] [[ ${lines[6]} == "PTR: 0.00" ]] - [[ ${lines[7]} == "TXT: 15.38" ]] + [[ ${lines[7]} == "TXT: 12.50" ]] [[ ${lines[8]} == "" ]] } @@ -241,10 +267,13 @@ [[ ${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[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]} == "" ]] } @test "Get all queries (domain filtered)" { @@ -255,7 +284,7 @@ } @test "Get all queries (domain + number filtered)" { - run bash -c 'echo ">getallqueries-domain regexa.test.pi-hole.net (6) >quit" | nc -v 127.0.0.1 4711' + run bash -c 'echo ">getallqueries-domain regexa.test.pi-hole.net (9) >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]} == "" ]]