-
Notifications
You must be signed in to change notification settings - Fork 0
/
spoof_test.go
82 lines (66 loc) · 2.39 KB
/
spoof_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
package cache
import (
"context"
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
func TestSpoof(t *testing.T) {
// Send query for example.org, get reply for example.net; should not be cached.
c := newTestK8sCache(false)
c.Next = spoofHandler(true)
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
c.ServeDNS(context.TODO(), rec, req)
qname := rec.Msg.Question[0].Name
if c.pcache.Len() != 0 {
t.Errorf("Cached %s, while reply had %s", "example.org.", qname)
}
// qtype
c.Next = spoofHandlerType()
req.SetQuestion("example.org.", dns.TypeMX)
c.ServeDNS(context.TODO(), rec, req)
qtype := rec.Msg.Question[0].Qtype
if c.pcache.Len() != 0 {
t.Errorf("Cached %s type %d, while reply had %d", "example.org.", dns.TypeMX, qtype)
}
}
func TestResponse(t *testing.T) {
// Send query for example.org, get reply for example.net; should not be cached.
c := newTestK8sCache(false)
c.Next = spoofHandler(false)
req := new(dns.Msg)
req.SetQuestion("example.net.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
c.ServeDNS(context.TODO(), rec, req)
if c.pcache.Len() != 0 {
t.Errorf("Cached %s, while reply had response set to %t", "example.net.", rec.Msg.Response)
}
}
// spoofHandler is a fake plugin implementation which returns a single A records for example.org. The qname in the
// question section is set to example.NET (i.e. they *don't* match).
func spoofHandler(response bool) plugin.Handler {
return plugin.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
m := new(dns.Msg)
m.SetQuestion("example.net.", dns.TypeA)
m.Response = response
m.Answer = []dns.RR{test.A("example.org. IN A 127.0.0.53")}
w.WriteMsg(m)
return dns.RcodeSuccess, nil
})
}
// spoofHandlerType is a fake plugin implementation which returns a single MX records for example.org. The qtype in the
// question section is set to A.
func spoofHandlerType() plugin.Handler {
return plugin.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
m.Response = true
m.Answer = []dns.RR{test.MX("example.org. IN MX 10 mail.example.org.")}
w.WriteMsg(m)
return dns.RcodeSuccess, nil
})
}