-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnstrie_test.go
143 lines (130 loc) · 3.82 KB
/
dnstrie_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package dnstrie
import (
"reflect"
"testing"
)
func TestCheckAndRemoveWildcard(t *testing.T) {
type testCase struct {
domain string
domainParsed string
wildcard string
}
testCases := []testCase{
testCase{"*.google.com", "google.com", "*"},
testCase{"+.google.com", "google.com", "+"},
testCase{"google.com", "google.com", ""},
testCase{"foo.*.google.com", "foo.*.google.com", ""},
testCase{"foo.+.google.com", "foo.+.google.com", ""},
testCase{"*google.com", "*google.com", ""},
testCase{"+google.com", "+google.com", ""},
}
for _, tc := range testCases {
parsed, wild := checkAndRemoveWildcard(tc.domain)
if parsed != tc.domainParsed || wild != tc.wildcard {
t.Fatalf("Failed with %+v. Got %v, %v.", tc, parsed, wild)
}
}
}
func TestReverseLabelSlice(t *testing.T) {
type testCase struct {
domain string
reversedLabels []string
}
testCases := []testCase{
testCase{"www.google.com", []string{"com", "google", "www"}},
testCase{"www.google.co.uk", []string{"uk", "co", "google", "www"}},
testCase{"not.a.real.domain.asdashfkjah", []string{"asdashfkjah", "domain", "real", "a", "not"}},
testCase{"foo.com.gza.com", []string{"com", "gza", "com", "foo"}},
testCase{"com", []string{"com"}},
testCase{"", []string{""}},
testCase{"*.foo.com", []string{"com", "foo", "*"}},
}
for _, tc := range testCases {
reversedLabels, _ := reverseLabelSlice(tc.domain)
if !reflect.DeepEqual(reversedLabels, tc.reversedLabels) {
t.Fatalf("Failed to reverse labels. Got %+v expected %+v.", reversedLabels, tc.reversedLabels)
}
}
}
func TestMakeTrie(t *testing.T) {
type testCase struct {
domains []string
root *DomainTrie
}
testCases := []testCase{
{[]string{"www.google.com", "+.google.com"}, &DomainTrie{
label: ".",
others: domainTrieSlice{
&DomainTrie{
label: "com",
others: domainTrieSlice{
&DomainTrie{
label: "google",
others: domainTrieSlice{
&DomainTrie{"www", domainTrieSlice{}, true},
&DomainTrie{"+", domainTrieSlice{}, true},
},
},
},
},
},
},
},
}
for _, tc := range testCases {
root, _ := MakeTrie(tc.domains)
if !reflect.DeepEqual(root, tc.root) {
t.Fatalf("Failed to MakeTrie. Got:\n%+v\nExpected:\n%+v\n", root, tc.root)
}
}
}
func TestMatch(t *testing.T) {
type testCase struct {
domain string
match bool
}
root, err := MakeTrie([]string{"*.google.com", "www.google.org", "*.biz", "notarealdomain", "*nadji.us", "onizuka.homelinux.org", "+.yahoo.com"})
if err != nil {
t.Fatalf("Failed to MakeTrie: %v", err)
}
root, err = MakeTrie([]string{"+.google.com", "www.google.org", "+.biz", "onizuka.homelinux.org", "*.yahoo.com"})
if err != nil {
t.Fatalf("Failed to MakeTrie: %v", err)
}
testCases := []testCase{
testCase{"www.google.org", true},
testCase{"www.google.com", true},
testCase{"google.com", false},
testCase{"google.biz", true},
testCase{"foo.google.biz", true},
testCase{"bar.foo.google.biz", true},
testCase{"notarealdomain", false},
testCase{"foo.nadji.us", false},
testCase{"nadji.us", false},
testCase{"*.biz", true},
testCase{"onizuka.homelinux.org", true},
testCase{"www.yahoo.com", true},
testCase{"yahoo.com", true},
testCase{"lots.of.children.yahoo.com", true},
}
for _, tc := range testCases {
actual := root.Match(tc.domain)
if tc.match != actual {
t.Fatalf("Failed for %v (got %v expected %v): tree %+v", tc.domain, actual, tc.match, root)
}
}
}
func TestEmpty(t *testing.T) {
root := &DomainTrie{}
if !root.Empty() {
t.Fatalf("Empty() failed for initialized trie: %+v", root)
}
root, _ = MakeTrie([]string{})
if !root.Empty() {
t.Fatalf("Empty() failed for initialized trie: %+v", root)
}
root, _ = MakeTrie([]string{"google.com"})
if root.Empty() {
t.Fatalf("Empty() failed for initialized trie: %+v", root)
}
}