forked from miekg/dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfr_test.go
129 lines (104 loc) · 2.72 KB
/
xfr_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
package dns
import "testing"
var (
tsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
xfrSoa = testRR(`miek.nl. 0 IN SOA linode.atoom.net. miek.miek.nl. 2009032802 21600 7200 604800 3600`)
xfrA = testRR(`x.miek.nl. 1792 IN A 10.0.0.1`)
xfrMX = testRR(`miek.nl. 1800 IN MX 1 x.miek.nl.`)
xfrTestData = []RR{xfrSoa, xfrA, xfrMX, xfrSoa}
)
func InvalidXfrServer(w ResponseWriter, req *Msg) {
ch := make(chan *Envelope)
tr := new(Transfer)
go tr.Out(w, req, ch)
ch <- &Envelope{RR: []RR{}}
close(ch)
w.Hijack()
}
func SingleEnvelopeXfrServer(w ResponseWriter, req *Msg) {
ch := make(chan *Envelope)
tr := new(Transfer)
go tr.Out(w, req, ch)
ch <- &Envelope{RR: xfrTestData}
close(ch)
w.Hijack()
}
func MultipleEnvelopeXfrServer(w ResponseWriter, req *Msg) {
ch := make(chan *Envelope)
tr := new(Transfer)
go tr.Out(w, req, ch)
for _, rr := range xfrTestData {
ch <- &Envelope{RR: []RR{rr}}
}
close(ch)
w.Hijack()
}
func TestInvalidXfr(t *testing.T) {
HandleFunc("miek.nl.", InvalidXfrServer)
defer HandleRemove("miek.nl.")
s, addrstr, _, err := RunLocalTCPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %s", err)
}
defer s.Shutdown()
tr := new(Transfer)
m := new(Msg)
m.SetAxfr("miek.nl.")
c, err := tr.In(m, addrstr)
if err != nil {
t.Fatal("failed to zone transfer in", err)
}
for msg := range c {
if msg.Error == nil {
t.Fatal("failed to catch 'no SOA' error")
}
}
}
func TestSingleEnvelopeXfr(t *testing.T) {
HandleFunc("miek.nl.", SingleEnvelopeXfrServer)
defer HandleRemove("miek.nl.")
s, addrstr, _, err := RunLocalTCPServer(":0", func(srv *Server) {
srv.TsigSecret = tsigSecret
})
if err != nil {
t.Fatalf("unable to run test server: %s", err)
}
defer s.Shutdown()
axfrTestingSuite(t, addrstr)
}
func TestMultiEnvelopeXfr(t *testing.T) {
HandleFunc("miek.nl.", MultipleEnvelopeXfrServer)
defer HandleRemove("miek.nl.")
s, addrstr, _, err := RunLocalTCPServer(":0", func(srv *Server) {
srv.TsigSecret = tsigSecret
})
if err != nil {
t.Fatalf("unable to run test server: %s", err)
}
defer s.Shutdown()
axfrTestingSuite(t, addrstr)
}
func axfrTestingSuite(t *testing.T, addrstr string) {
tr := new(Transfer)
m := new(Msg)
m.SetAxfr("miek.nl.")
c, err := tr.In(m, addrstr)
if err != nil {
t.Fatal("failed to zone transfer in", err)
}
var records []RR
for msg := range c {
if msg.Error != nil {
t.Fatal(msg.Error)
}
records = append(records, msg.RR...)
}
if len(records) != len(xfrTestData) {
t.Fatalf("bad axfr: expected %v, got %v", records, xfrTestData)
}
for i, rr := range records {
if !IsDuplicate(rr, xfrTestData[i]) {
t.Fatalf("bad axfr: expected %v, got %v", records, xfrTestData)
}
}
}