forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewrite_test.go
114 lines (97 loc) · 2.79 KB
/
rewrite_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
package test
import (
"bytes"
"testing"
"github.com/miekg/dns"
)
func TestRewriteFailure(t *testing.T) {
t.Parallel()
i, udp, _, err := CoreDNSServerAndPorts(`.:0 {
rewrite name regex (.*)\.test\.$ {1}. answer auto
# no next plugin to induce SERVFAIL
}`)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer i.Stop()
m := new(dns.Msg)
m.SetQuestion("example.test.", dns.TypeMX)
r, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
if len(r.Question) == 0 {
t.Error("Invalid empty question section")
}
if r.Question[0].Name != "example.test." {
t.Errorf("Question section mismatch. expected \"example.test.\" got %q", r.Question[0].Name)
}
}
func TestRewrite(t *testing.T) {
t.Parallel()
corefile := `.:0 {
rewrite type MX a
rewrite edns0 local set 0xffee hello-world
erratic . {
drop 0
}
}`
i, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer i.Stop()
testMX(t, udp)
testEdns0(t, udp)
}
func testMX(t *testing.T, server string) {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeMX)
r, err := dns.Exchange(m, server)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
// expect answer section with A record in it
if len(r.Answer) == 0 {
t.Error("Expected to at least one RR in the answer section, got none")
}
if r.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected RR to A, got: %d", r.Answer[0].Header().Rrtype)
}
if r.Answer[0].(*dns.A).A.String() != "192.0.2.53" {
t.Errorf("Expected 192.0.2.53, got: %s", r.Answer[0].(*dns.A).A.String())
}
}
func testEdns0(t *testing.T, server string) {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
r, err := dns.Exchange(m, server)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
// expect answer section with A record in it
if len(r.Answer) == 0 {
t.Error("Expected to at least one RR in the answer section, got none")
}
if r.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected RR to A, got: %d", r.Answer[0].Header().Rrtype)
}
if r.Answer[0].(*dns.A).A.String() != "192.0.2.53" {
t.Errorf("Expected 192.0.2.53, got: %s", r.Answer[0].(*dns.A).A.String())
}
o := r.IsEdns0()
if o == nil || len(o.Option) == 0 {
t.Error("Expected EDNS0 options but got none")
} else {
if e, ok := o.Option[0].(*dns.EDNS0_LOCAL); ok {
if e.Code != 0xffee {
t.Errorf("Expected EDNS_LOCAL code 0xffee but got %x", e.Code)
}
if !bytes.Equal(e.Data, []byte("hello-world")) {
t.Errorf("Expected EDNS_LOCAL data 'hello-world' but got %q", e.Data)
}
} else {
t.Errorf("Expected EDNS0_LOCAL but got %v", o.Option[0])
}
}
}