/
blackhole_test.go
61 lines (49 loc) · 1.35 KB
/
blackhole_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
package coredns_blackhole
import (
"context"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
"testing"
)
func TestBlackhole_ServeDNS(t *testing.T) {
// Create a new Example Plugin. Use the test.ErrorHandler as the next plugin.
list := NewBlocklist()
list.Add("example.net.")
bl := Blackhole{Next: test.ErrorHandler(), Blocklist: list}
ctx := context.TODO()
tests := []struct {
reqDomain string
shouldErr bool
expectedRCode int
}{
{
"example.net",
false,
BlockedRcode,
},
{
"example.com",
false,
dns.RcodeServerFailure,
},
}
for i, testcase := range tests {
req := new(dns.Msg)
req.SetQuestion(dns.Fqdn(testcase.reqDomain), dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := bl.ServeDNS(ctx, rec, req)
if err == nil && testcase.shouldErr {
t.Fatalf("Test %d: expected errors, but got no error", i)
} else if err != nil && !testcase.shouldErr {
t.Fatalf("Test %d: expected no errors, but got '%v'", i, err)
continue
}
if code != testcase.expectedRCode {
t.Errorf("Test %d: Expected status code %d, but got %d", i, testcase.expectedRCode, code)
}
if rec.Rcode != testcase.expectedRCode {
t.Errorf("Test %d: Expected return code %d, but got %d", i, testcase.expectedRCode, rec.Rcode)
}
}
}