1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)")
}
}
|