strawberry/tests/test_tracker_set.cpp
trvon 2e94ae1485
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Privacy: tracker list feeds (advisory only, never block)
First piece of tracker attribution (docs/tracker-attribution.md): which
programs reach advertising and tracking domains, built with the
measurement rules of the CarolinaCon 2026 privacy talk (bridgescope):
a listed domain is a circumstantial label, never proof.

- Feed kind "trackers": hosts lines, plain domains and adblock
  whole-domain rules (||host^, ||host^$third-party). Cosmetic rules,
  exceptions, paths and other options are skipped; the comment cut used
  to turn "site.example##.ad" into a domain.
- TrackerSet: suffix matching (a.b.t.example matches t.example, never
  nott.example, an IP literal or a bare TLD); first feed reported.
- List parsing moves from rule_updater.cpp to ioc_list.cpp (no network
  code) so the query-side core can use it.
- Source invariant R11: tracker lists never meet exec-guard or OpenSnitch
  code. They are not in the OpenSnitch blocklist sync.

Tests: T1-T3 (test_tracker_set), trackers in the list-parsing property
and the list fuzzer (60 s clean), R11 self-test fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 05:25:53 -06:00

88 lines
3.5 KiB
C++

// 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 <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
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<std::string> out;
const auto rejected = parse_ioc_list("trackers", list, 100, out);
std::sort(out.begin(), out.end());
const std::vector<std::string> 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<std::string> domains;
(void) parse_ioc_list("domains", "||ads.example^\nplain.example\n", 100, domains);
CHECK(domains == std::vector<std::string>{"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;
}