diff options
| author | radhitya <alif@radhitya.org> | 2026-07-05 18:37:52 +0700 |
|---|---|---|
| committer | radhitya <alif@radhitya.org> | 2026-07-05 18:37:52 +0700 |
| commit | 01437d0a0afc3533baba26e1f36e66e32ae86ac9 (patch) | |
| tree | 526ea1dc76e602007fe86eddc819ffae63b7b441 /internal/blocklist/blocklist_test.go | |
| parent | 1799ef47cedcfed8c979e145783fff7c9d1944cc (diff) | |
finalize the blocklist feature
Diffstat (limited to 'internal/blocklist/blocklist_test.go')
| -rw-r--r-- | internal/blocklist/blocklist_test.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/internal/blocklist/blocklist_test.go b/internal/blocklist/blocklist_test.go new file mode 100644 index 0000000..0104bc9 --- /dev/null +++ b/internal/blocklist/blocklist_test.go @@ -0,0 +1,59 @@ +package blocklist + +import ( + "os" + "testing" +) + +func TestParseLine(t *testing.T) { + tests := []struct{in,out string}{ + {"0.0.0.0 domain.com", "domain.com"}, + {"0.0.0.0 domain.com # foo", "domain.com"}, + {"0.0.0.0 *.domain.com", "*.domain.com"}, + {"||domain.com^", "domain.com"}, + {"||domain.com^$script", "domain.com"}, + {"",""}, + {"# comment", ""}, + {"||*.domain.com^", "*.domain.com"}, + } + for _, tt := range tests { + if got := parseLine(tt.in); got != tt.out { + t.Errorf("parseLine(%q) = %q, want %q", tt.in, got, tt.out) + } + } +} + +func TestBlocked(t *testing.T) { + tmp, _ := os.CreateTemp("", "blocklist-*.txt") + tmp.WriteString("radhitya.org\n") + tmp.WriteString("0.0.0.0 ads.test.com\n") + tmp.WriteString("||example.net^\n") + tmp.WriteString("||*.domain.com^\n") + tmp.Close() + defer os.Remove(tmp.Name()) + + bl, err := Load(tmp.Name()) + if err != nil { + t.Fatal(err) + } + + if !bl.Blocked("radhitya.org") { + t.Error("radhitya.org shall be blocked") + } + if bl.Blocked("alif.radhitya.org") { + t.Error("alif.radhitya.org shall NOT be blocked") + } + if !bl.Blocked("ads.test.com") { + t.Error("ads.test.com shall be blocked (hosts format)") + } + if !bl.Blocked("example.net") { + t.Error("example.net shall be blocked (adguard format)") + } + + if bl.Blocked("domain.com") { + t.Error("domain.com shall NOT be blocked (wildcard skipped)") + } + if bl.Blocked("sub.domain.com") { + t.Error("sub.domain.com shall NOT be blocked (wildcard skipped)") + } +} |
