-
Notifications
You must be signed in to change notification settings - Fork 62
/
blocklist_test.go
84 lines (71 loc) · 2.11 KB
/
blocklist_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
package rdns
import (
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestBlocklistRegexp(t *testing.T) {
var ci ClientInfo
q := new(dns.Msg)
r := new(TestResolver)
loader := NewStaticLoader([]string{
`(^|\.)block\.test`,
`(^|\.)evil\.test`,
})
m, err := NewRegexpDB("testlist", loader)
require.NoError(t, err)
opt := BlocklistOptions{
BlocklistDB: m,
}
b, err := NewBlocklist("test-bl", r, opt)
require.NoError(t, err)
// First query a domain not blocked. Should be passed through to the resolver
q.SetQuestion("test.com.", dns.TypeA)
_, err = b.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
// One domain from the blocklist should come back with NXDOMAIN
q.SetQuestion("x.evil.test.", dns.TypeA)
a, err := b.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
require.Equal(t, dns.RcodeNameError, a.Rcode)
}
func TestBlocklistAllow(t *testing.T) {
var ci ClientInfo
q := new(dns.Msg)
r := new(TestResolver)
blockloader := NewStaticLoader([]string{
`(^|\.)block\.test`,
`(^|\.)evil\.test`,
})
allowloader := NewStaticLoader([]string{
`(^|\.)good\.evil\.test`,
})
blockDB, err := NewRegexpDB("testlist", blockloader)
require.NoError(t, err)
allowDB, err := NewRegexpDB("testlist", allowloader)
require.NoError(t, err)
opt := BlocklistOptions{
BlocklistDB: blockDB,
AllowlistDB: allowDB,
}
b, err := NewBlocklist("test-bl", r, opt)
require.NoError(t, err)
// First query a domain not blocked. Should be passed through to the resolver
q.SetQuestion("test.com.", dns.TypeA)
_, err = b.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
// One domain from the blocklist should come back with NXDOMAIN
q.SetQuestion("x.evil.test.", dns.TypeA)
a, err := b.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
require.Equal(t, dns.RcodeNameError, a.Rcode)
// One domain blocklist that also matches the allowlist should go through
q.SetQuestion("good.evil.test.", dns.TypeA)
_, err = b.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 2, r.HitCount())
}