-
Notifications
You must be signed in to change notification settings - Fork 2
/
discover_test.go
225 lines (199 loc) · 6.47 KB
/
discover_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"math/rand"
"net"
"strings"
"testing"
"github.com/markdingo/autoreverse/log"
"github.com/markdingo/autoreverse/mock"
"github.com/markdingo/autoreverse/mock/resolver"
)
type tdCase struct {
forward string
reverses string
}
// Remember, these tests are intended to exercise the code inside this
// directory. Exercising the delegation logic happens on over in its directory.
func TestDiscoverForward(t *testing.T) {
rand.Seed(0) // Make PRNG predictable for probe generation
out := &mock.IOWriter{}
log.SetOut(out)
log.SetLevel(log.MajorLevel)
res := resolver.NewResolver("./testdata/discover")
ar := newAutoReverse(&config{TTLAsSecs: 61}, res) // Needed by zone parser
ar.cfg.listen = append(ar.cfg.listen, "127.0.0.1:6366") // Exercise mutables setting
ar.startServers()
srv := ar.servers[0]
mutsBefore := srv.getMutables()
defer ar.stopServers()
// Start off with a nice easy one that works. The delegation is
// ns.autoreverse.example.net which is returned in the NS query to the parent.
ar.cfg.delegatedForward = "autoreverse.example.net."
ar.forward = ar.cfg.delegatedForward
err := ar.discover()
if err != nil {
t.Error(err)
}
// Each successful discover should sets mutables afresh, so check that it did
mutsAfter := srv.getMutables()
if mutsBefore.ptrSuffix == mutsAfter.ptrSuffix {
t.Log("Mutables didn't change", mutsBefore.ptrSuffix, mutsAfter.ptrSuffix)
}
if mutsBefore.authorities.len() == mutsAfter.authorities.len() {
t.Log("Mutables didn't change",
mutsBefore.authorities.len(), mutsAfter.authorities.len())
}
// Forward Error paths
// Have parent, but not delegation
ar.cfg.delegatedForward = "noautoreverse.example.net."
err = ar.discover()
if err == nil {
t.Error("Expected discover to fail")
} else if !strings.Contains(err.Error(), "no delegation") {
t.Error("Wrong error returned", err)
}
// No probe response
ar.cfg.delegatedForward = "noprobe.example.net."
err = ar.discover()
if err == nil {
t.Error("Expected discover to fail")
} else if !strings.Contains(err.Error(), "failed to self-identify") {
t.Error("Wrong error returned", err)
}
// No delegation
ar.cfg.delegatedForward = "lameparents.example.org."
err = ar.discover()
if err == nil {
t.Error("Expected discover to fail")
} else if !strings.Contains(err.Error(), "no delegation") {
t.Error("Wrong error returned", err)
}
// No Parent
ar.cfg.delegatedForward = "does.not.exist."
err = ar.discover()
if err == nil {
t.Error("Expected discover to fail")
} else if !strings.Contains(err.Error(), "No Delegation") {
t.Error("Wrong error returned", err)
}
}
// A nice easy test with no error cases. A forward and two reverses. Very typical.
func TestDiscoverReverseGood(t *testing.T) {
rand.Seed(0) // Make PRNG predictable for probe generation
out := &mock.IOWriter{}
log.SetOut(out)
log.SetLevel(log.DebugLevel)
res := resolver.NewResolver("./testdata/discover")
ar := newAutoReverse(&config{TTLAsSecs: 61}, res) // Needed by zone parser
ar.cfg.delegatedForward = "autoreverse.example.net."
ar.forward = ar.cfg.delegatedForward
ar.cfg.listen = append(ar.cfg.listen, "127.0.0.1:6366") // Exercise mutables setting
ar.startServers()
srv := ar.servers[0]
mutsBefore := srv.getMutables()
defer ar.stopServers()
_, v4Net, e4 := net.ParseCIDR("192.0.2.0/24")
if e4 != nil {
t.Fatal("Setup", e4)
}
_, v6Net, e6 := net.ParseCIDR("2001:db8::/64")
if e6 != nil {
t.Fatal("Setup", e6)
}
ar.delegatedReverses = append(ar.delegatedReverses, v4Net)
ar.delegatedReverses = append(ar.delegatedReverses, v6Net)
err := ar.discover()
if err != nil {
t.Error(err)
}
mutsAfter := srv.getMutables()
if mutsBefore.ptrSuffix == mutsAfter.ptrSuffix {
t.Log("Mutables didn't change", mutsBefore.ptrSuffix, mutsAfter.ptrSuffix)
}
if mutsBefore.authorities.len() == mutsAfter.authorities.len() {
t.Log("Mutables didn't change",
mutsBefore.authorities.len(), mutsAfter.authorities.len())
}
if ar.authorities.len() != 3 {
t.Error("Authority count wrong. Want 3, got", ar.authorities.len())
t.Log(out.String())
}
}
type tdreCase struct {
reverse string
name, contains string
success bool
}
func TestDiscoverReverseErrors(t *testing.T) {
testCases := []tdreCase{
{"10.0.0.0/8", "10.in-addr.arpa.", "No Delegation", false},
{"192.0.3.0/24", "192.in-addr.arpa.", "has no delegation for", false},
{"192.0.4.0/24", "192.in-addr.arpa.", "Probe failed", false},
}
for ix, tc := range testCases {
t.Run(tc.reverse, func(t *testing.T) {
testDREOne(t, ix, tc)
})
}
}
func testDREOne(t *testing.T, ix int, tc tdreCase) {
rand.Seed(0) // Make PRNG predictable for probe generation
out := &mock.IOWriter{}
log.SetOut(out)
log.SetLevel(log.DebugLevel)
res := resolver.NewResolver("./testdata/discover")
ar := newAutoReverse(&config{TTLAsSecs: 61}, res) // Needed by zone parser
ar.cfg.localForward = "example.org." // set forward to a known
ar.forward = ar.cfg.localForward // good value
_, ipNet, err := net.ParseCIDR(tc.reverse)
if err != nil {
t.Fatal("Setup", err)
}
ar.delegatedReverses = append(ar.delegatedReverses, ipNet)
err = ar.discover()
if err != nil {
if len(tc.contains) > 0 {
if !strings.Contains(err.Error(), tc.contains) {
t.Error(ix, tc.reverse, "Got wrong error. Wanted",
tc.contains, "got", err.Error())
t.Log(out.String())
out.Reset()
}
} else {
t.Error(ix, tc.reverse, "Unexpected error", err.Error())
t.Log(out.String())
out.Reset()
}
return
}
if len(tc.contains) > 0 {
t.Error(ix, tc.reverse, "Did not get error with: ", tc.contains)
t.Log(out.String())
out.Reset()
return
}
}
func TestDiscoverReverseDupes(t *testing.T) {
rand.Seed(0) // Make PRNG predictable for probe generation
out := &mock.IOWriter{}
log.SetOut(out)
log.SetLevel(log.DebugLevel)
res := resolver.NewResolver("./testdata/discover")
ar := newAutoReverse(&config{TTLAsSecs: 61}, res) // Needed by zone parser
ar.cfg.localForward = "example.org." // set forward to a known
ar.forward = ar.cfg.localForward // good value
_, ipNet, err := net.ParseCIDR("2001:db8::/64")
if err != nil {
t.Fatal("Setup", err)
}
ar.delegatedReverses = append(ar.delegatedReverses, ipNet)
ar.delegatedReverses = append(ar.delegatedReverses, ipNet)
err = ar.discover()
if err == nil {
t.Error("Expected Error from duplicate reverses")
t.Log(out.String())
} else if !strings.Contains(err.Error(), "is duplicate") {
t.Error("Got wrong error. Want 'is duplicate', got", err.Error())
t.Log(out.String())
}
}