// Privacy tracker lists (docs/tracker-attribution.md). // // T1 "trackers" lists take hosts lines, plain domains and adblock // whole-domain rules; cosmetic rules, exceptions, paths and rules with // other options are skipped // T2 a host matches its listed domain or any parent of it, never a // look-alike that only ends with the same letters, an IP literal or a // bare top-level label // T3 the first feed listing a domain is reported #include "strawberry/ioc_list.hpp" #include "strawberry/tracker_set.hpp" #include #include #include #include #include using namespace strawberry; #define CHECK(cond) \ do { \ if (!(cond)) { \ std::cerr << "CHECK FAILED: " #cond " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \ std::abort(); \ } \ } while (0) namespace { void test_parse() { const std::string list = "[Adblock Plus 2.0]\n" "! Title: test list\n" "||ads.example^\n" "||metrics.example^$third-party\n" "||scripts.example^$script,third-party\n" // other options: part of a site "||cdn.example/pixel.gif^\n" // a path, not a domain "@@||allowed.example^\n" // exception "news.example##.banner\n" // cosmetic rule "shop.example#@#.ad\n" "0.0.0.0 hosts-style.example\n" "plain.example\n" "*.wild.example\n" "# comment\n"; std::vector out; const auto rejected = parse_ioc_list("trackers", list, 100, out); std::sort(out.begin(), out.end()); const std::vector want{"ads.example", "hosts-style.example", "metrics.example", "plain.example", "wild.example"}; if (out != want) { for (const auto& o : out) std::cerr << " got " << o << std::endl; } CHECK(out == want); // T1 CHECK(rejected >= 3); // The threat-intel "domains" kind is unchanged: no adblock syntax. std::vector domains; (void) parse_ioc_list("domains", "||ads.example^\nplain.example\n", 100, domains); CHECK(domains == std::vector{"plain.example"}); std::cout << "T1 tracker list parsing: PASSED" << std::endl; } void test_match() { TrackerSet set; set.add("easyprivacy", {"tracker.example", "ads.example"}); set.add("second", {"tracker.example", "other.example"}); const auto m = set.find("a.b.Tracker.Example."); CHECK(m && m->domain == "tracker.example" && m->feed == "easyprivacy"); // T2, T3 CHECK(set.find("tracker.example")); CHECK(!set.find("nottracker.example")); CHECK(!set.find("tracker.example.evil")); CHECK(!set.find("example")); CHECK(!set.find("")); CHECK(!set.find("192.0.2.1")); CHECK(!set.find("2001:db8::1")); CHECK(set.find("x.other.example")->feed == "second"); CHECK(set.size() == 3); std::cout << "T2 suffix matching, T3 first feed: PASSED" << std::endl; } } // namespace int main() { test_parse(); test_match(); return 0; }