From 4e6a897a0b55ee533c05f89fa38dbe0704f2798d Mon Sep 17 00:00:00 2001 From: radhitya Date: Sun, 14 Jun 2026 14:36:32 +0700 Subject: dns recursive resolver(iterative, root hints, delegfation, glue, fallback), adblocker, dns cache --- internal/blocklist/blocklist_test.go | 78 ++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 internal/blocklist/blocklist_test.go (limited to 'internal/blocklist/blocklist_test.go') diff --git a/internal/blocklist/blocklist_test.go b/internal/blocklist/blocklist_test.go new file mode 100644 index 0000000..9ae1749 --- /dev/null +++ b/internal/blocklist/blocklist_test.go @@ -0,0 +1,78 @@ +package blocklist + +import ( + "os" + "path/filepath" + "testing" +) + +func TestBlockZeroIP(t *testing.T) { + b := New(ResponseZeroIP) + b.addRule("||example.com^") + b.addRule("0.0.0.0 doubleclick.net") + + cases := []struct { + domain string + block bool + }{ + {"example.com.", true}, + {"sub.example.com.", true}, + {"notexample.com.", false}, + {"doubleclick.net.", true}, + {"ads.doubleclick.net.", true}, + {"google.com.", false}, + } + for _, c := range cases { + got := b.IsBlocked(c.domain) + if got != c.block { + t.Errorf("IsBlocked(%q) = %v, want %v", c.domain, got, c.block) + } + } +} + +func TestException(t *testing.T) { + b := New(ResponseZeroIP) + b.addRule("||example.com^") + b.addRule("@@||whitelist.example.com^") + + if !b.IsBlocked("example.com.") { + t.Error("expected blocked") + } + if b.IsBlocked("whitelist.example.com.") { + t.Error("expected NOT blocked (exception)") + } + if b.IsBlocked("sub.whitelist.example.com.") { + t.Error("expected NOT blocked (exception subdomain)") + } +} + +func TestLoadFile(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "block.txt") + os.WriteFile(p, []byte("0.0.0.0 ads.com\n||tracker.net^\n"), 0644) + + b := New(ResponseZeroIP) + if err := b.LoadFile(p); err != nil { + t.Fatal(err) + } + if !b.IsBlocked("ads.com.") { + t.Error("expected blocked") + } + if !b.IsBlocked("sub.tracker.net.") { + t.Error("expected blocked") + } + if b.IsBlocked("safe.org.") { + t.Error("expected NOT blocked") + } +} + +func TestResponseType(t *testing.T) { + b := New(ResponseNXDOMAIN) + if b.Response() != ResponseNXDOMAIN { + t.Error("expected NXDOMAIN") + } + b2 := New(ResponseZeroIP) + if b2.Response() != ResponseZeroIP { + t.Error("expected ZeroIP") + } +} -- cgit v1.2.3