summaryrefslogtreecommitdiff
path: root/internal/blocklist/blocklist_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/blocklist/blocklist_test.go')
-rw-r--r--internal/blocklist/blocklist_test.go78
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")
+ }
+}