-
Notifications
You must be signed in to change notification settings - Fork 62
/
blocklistdb-domain_test.go
67 lines (57 loc) · 1.49 KB
/
blocklistdb-domain_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
package rdns
import (
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestDomainDB(t *testing.T) {
loader := NewStaticLoader([]string{
"domain1.com.", // exact match
".domain2.com.", // exact match and subdomains
"x.domain2.com", // above rule should take precendence
"*.domain3.com", // subdomains only
"x.x.domain3.com", // more general wildcard above should take precedence
"domain4.com", // the more general rule below wins
".domain4.com",
})
m, err := NewDomainDB("testlist", loader)
require.NoError(t, err)
tests := []struct {
q string
match bool
}{
// exact
{"domain1.com.", true},
{"x.domain1.com.", false},
// exact and subdomains
{"domain2.com.", true},
{"sub.domain2.com.", true},
// wildcard (match only on subdomains)
{"domain3.com.", false},
{"sub.domain3.com.", true},
// two rules for this, the generic one wins
{"domain4.com.", true},
{"sub.domain4.com.", true},
// not matching
{"unblocked.test.", false},
{"com.", false},
}
for _, test := range tests {
q := dns.Question{Name: test.q, Qtype: dns.TypeA, Qclass: dns.ClassINET}
_, _, _, ok := m.Match(q)
require.Equal(t, test.match, ok, "query: %s", test.q)
}
}
func TestDomainDBError(t *testing.T) {
tests := []struct {
name string
}{
{"sub.*.com"},
{"*domain.com"},
}
for _, test := range tests {
loader := NewStaticLoader([]string{test.name})
_, err := NewDomainDB("testlist", loader)
require.Error(t, err)
}
}