-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecondary_test.go
146 lines (126 loc) · 3.59 KB
/
secondary_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package file
import (
"fmt"
"testing"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestLess(t *testing.T) {
const (
min = 0
max = 4294967295
low = 12345
high = 4000000000
)
if less(min, max) {
t.Fatalf("Less: should be false")
}
if !less(max, min) {
t.Fatalf("Less: should be true")
}
if !less(high, low) {
t.Fatalf("Less: should be true")
}
if !less(7, 9) {
t.Fatalf("Less; should be true")
}
}
type soa struct {
serial uint32
}
func (s *soa) Handler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
switch req.Question[0].Qtype {
case dns.TypeSOA:
m.Answer = make([]dns.RR, 1)
m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial))
w.WriteMsg(m)
case dns.TypeAXFR:
m.Answer = make([]dns.RR, 4)
m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial))
m.Answer[1] = test.A(fmt.Sprintf("%s IN A 127.0.0.1", testZone))
m.Answer[2] = test.A(fmt.Sprintf("%s IN A 127.0.0.1", testZone))
m.Answer[3] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial))
w.WriteMsg(m)
}
}
func (s *soa) TransferHandler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Answer = make([]dns.RR, 1)
m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial))
w.WriteMsg(m)
}
const testZone = "secondary.miek.nl."
func TestShouldTransfer(t *testing.T) {
soa := soa{250}
s := dnstest.NewServer(soa.Handler)
defer s.Close()
z := NewZone("testzone", "test")
z.origin = testZone
z.TransferFrom = []string{s.Addr}
// when we have a nil SOA (initial state)
should, err := z.shouldTransfer()
if err != nil {
t.Fatalf("Unable to run shouldTransfer: %v", err)
}
if !should {
t.Fatalf("ShouldTransfer should return true for serial: %d", soa.serial)
}
// Serial smaller
z.Apex.SOA = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, soa.serial-1))
should, err = z.shouldTransfer()
if err != nil {
t.Fatalf("Unable to run shouldTransfer: %v", err)
}
if !should {
t.Fatalf("ShouldTransfer should return true for serial: %q", soa.serial-1)
}
// Serial equal
z.Apex.SOA = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, soa.serial))
should, err = z.shouldTransfer()
if err != nil {
t.Fatalf("Unable to run shouldTransfer: %v", err)
}
if should {
t.Fatalf("ShouldTransfer should return false for serial: %d", soa.serial)
}
}
func TestTransferIn(t *testing.T) {
soa := soa{250}
s := dnstest.NewServer(soa.Handler)
defer s.Close()
z := new(Zone)
z.origin = testZone
z.TransferFrom = []string{s.Addr}
if err := z.TransferIn(); err != nil {
t.Fatalf("Unable to run TransferIn: %v", err)
}
if z.Apex.SOA.String() != fmt.Sprintf("%s 3600 IN SOA bla. bla. 250 0 0 0 0", testZone) {
t.Fatalf("Unknown SOA transferred")
}
}
func TestIsNotify(t *testing.T) {
z := new(Zone)
z.origin = testZone
state := newRequest(testZone, dns.TypeSOA)
// need to set opcode
state.Req.Opcode = dns.OpcodeNotify
z.TransferFrom = []string{"10.240.0.1:53"} // IP from testing/responseWriter
if !z.isNotify(state) {
t.Fatal("Should have been valid notify")
}
z.TransferFrom = []string{"10.240.0.2:53"}
if z.isNotify(state) {
t.Fatal("Should have been invalid notify")
}
}
func newRequest(zone string, qtype uint16) request.Request {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
m.SetEdns0(4097, true)
return request.Request{W: &test.ResponseWriter{}, Req: m}
}