diff options
| author | radhitya <alif@radhitya.org> | 2026-06-14 14:36:32 +0700 |
|---|---|---|
| committer | radhitya <alif@radhitya.org> | 2026-06-14 14:36:32 +0700 |
| commit | 4e6a897a0b55ee533c05f89fa38dbe0704f2798d (patch) | |
| tree | 12d9700e53775503ad7ba2beb72bedfc64bdd70d /internal/blocklist/blocklist_test.go | |
| parent | 3e44adc94f32bfe500730fcbf1c02cedf65b0a30 (diff) | |
dns recursive resolver(iterative, root hints, delegfation, glue, fallback), adblocker, dns cache
Diffstat (limited to 'internal/blocklist/blocklist_test.go')
| -rw-r--r-- | internal/blocklist/blocklist_test.go | 78 |
1 files changed, 78 insertions, 0 deletions
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") + } +} |
