-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter_test.go
131 lines (111 loc) · 2.79 KB
/
filter_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
package filter
import (
"context"
"testing"
"github.com/coredns/caddy"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestFilter(t *testing.T) {
c := caddy.NewTestController("dns", `filter {
allow ./testdata/allowlist.list
block ./testdata/denylist.list
}`)
f, err := parseFilter(c)
if err != nil {
t.Fatal(err)
}
f.Next = test.NextHandler(dns.RcodeSuccess, nil)
if err = f.Load(); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
block bool
}{
{"example.com.", false},
{"ads.example.com.", false},
{"facebook.com.", false},
{"ads.facebook.com", true},
{"adservice.google.com.", true},
{"mipcwtf.lan.", true},
{"taboola.com.", true},
{"example.taboola.com.", true},
{"cdn.outbrain.com", true},
{"example.tracker.global", true},
{"malware.com.co", true},
{".", false},
}
for i, tt := range tests {
block := f.Match(tt.name)
if block != tt.block {
t.Errorf("Test %d: expected '%s' to be blocked", i, tt.name)
}
}
}
func TestServeDNS(t *testing.T) {
c := caddy.NewTestController("dns", `filter {
allow ./testdata/allowlist.list
block ./testdata/denylist.list
}`)
f, err := parseFilter(c)
if err != nil {
t.Fatal(err)
}
f.Next = test.NextHandler(dns.RcodeSuccess, nil)
if err = f.Load(); err != nil {
t.Fatal(err)
}
rec := dnstest.NewRecorder(&test.ResponseWriter{})
tests := []struct {
name string
dnsType uint16
}{
{"example.com.", dns.TypeA},
{"facebook.com.", dns.TypeAAAA},
{"adservice.google.com.", dns.TypeA},
{"amazon-adsystem.com.", dns.TypeAAAA},
{".", dns.TypeA},
}
for i, tt := range tests {
req := new(dns.Msg)
req.SetQuestion(tt.name, tt.dnsType)
rcode, err := f.ServeDNS(context.TODO(), rec, req)
if err != nil {
t.Fatal(err)
}
if rcode != dns.RcodeSuccess && rcode != dns.RcodeNameError {
t.Errorf("Test %d: expected other rcode but got %s", i, dns.RcodeToString[rcode])
}
}
}
func TestUncloak(t *testing.T) {
c := caddy.NewTestController("dns", `filter {
allow ./testdata/allowlist.list
block ./testdata/denylist.list
}`)
f, err := parseFilter(c)
if err != nil {
t.Fatal(err)
}
f.Next = test.NextHandler(dns.RcodeSuccess, nil)
if err = f.Load(); err != nil {
t.Fatal(err)
}
req := new(dns.Msg)
req.SetQuestion("notracker.example.com.", dns.TypeCNAME)
m := new(dns.Msg)
m.SetReply(req)
m.Response, m.RecursionAvailable = true, true
m.Answer = []dns.RR{test.CNAME("notracker.example.com. 3600 IN CNAME ads.tracker.com.")}
state := request.Request{W: &test.ResponseWriter{}, Req: req}
rw := &ResponseWriter{
ResponseWriter: &test.ResponseWriter{},
state: state,
server: "test",
Filter: f,
}
rw.WriteMsg(m) //nolint
}