-
Notifications
You must be signed in to change notification settings - Fork 2
/
warnlist_test.go
111 lines (100 loc) · 2.04 KB
/
warnlist_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package warnlist
import (
"strconv"
"testing"
"github.com/google/go-cmp/cmp"
)
var testWarnlist = []string{
"example.org",
"something.evil",
"evil.com",
"something.wicked.test",
"coredns.io",
}
func Test_warnlistHits(t *testing.T) {
var testCases = []struct {
domain string
hit bool
name string
}{
{
name: "case 0: a domain in the list is matched",
domain: "example.org",
hit: true,
},
{
name: "case 1: a domain not in the list is not matched",
domain: "this-is-ok.org",
hit: false,
},
}
// Create our testing list
list := NewWarnlist()
for _, d := range testWarnlist {
list.Add(d)
}
list.Close()
// Run the test cases
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Log(tc.name)
hit := list.Contains(tc.domain)
if !cmp.Equal(tc.hit, hit) {
t.Fatalf("\n\n%s\n", cmp.Diff(tc.hit, hit))
}
})
}
}
func Test_radixContains(t *testing.T) {
var testCases = []struct {
domain string
hit bool
name string
}{
{
name: "case 0: a domain in the list is matched",
domain: "example.org",
hit: true,
},
{
name: "case 1: a domain not in the list is not matched",
domain: "this-is-ok.org",
hit: false,
},
{
name: "case 2: a subdomain of a domain in the list is matched",
domain: "very.evil.com",
hit: true,
},
{
name: "case 3: multiple subdomains of a domain in the list are matched",
domain: "oh.so.very.evil.com",
hit: true,
},
{
name: "case 4: a similar suffix domain is not matched",
domain: "devil.com",
hit: false,
},
{
name: "case 5: a similar substring is not matched",
domain: "evil.com.org",
hit: false,
},
}
list := NewRadixWarnlist()
for _, d := range testWarnlist {
list.Add(d)
}
list.Close()
// Run the test cases
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Log(tc.name)
hit := list.Contains(tc.domain)
if !cmp.Equal(tc.hit, hit) {
t.Fatalf("\n\n%s\n", cmp.Diff(tc.hit, hit))
}
})
}
}