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.go59
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)")
+ }
+}