-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
resolvers_test.go
509 lines (432 loc) · 12.6 KB
/
resolvers_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Copyright © by Jeff Foley 2017-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"context"
"io"
"net"
"sync"
"testing"
"time"
"github.com/miekg/dns"
)
func TestInitializeResolver(t *testing.T) {
r := NewResolvers()
if res := r.initializeResolver(100, "192.168.1.1"); res == nil ||
res.address.IP.String() != "192.168.1.1" || res.address.Port != 53 {
t.Errorf("failed to add the port to the provided address")
}
if res := r.initializeResolver(100, "300.300.300.300"); res != nil {
t.Errorf("failed to detect the invalid IP address provided")
}
}
func TestSetTimeout(t *testing.T) {
r := NewResolvers()
_ = r.AddResolvers(maxQPSPerNameserver, "8.8.8.8")
defer r.Stop()
timeout := 2 * time.Second
r.SetTimeout(timeout)
if r.timeout != timeout || r.pool.GetResolver().xchgs.timeout != timeout {
t.Errorf("failed to set the new timeout value throughout the resolver pool")
}
}
func TestPoolQuery(t *testing.T) {
dns.HandleFunc("pool.net.", typeAHandler)
defer dns.HandleRemove("pool.net.")
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(100, addrstr)
defer r.Stop()
num := 1000
var failures int
ch := make(chan *dns.Msg, num)
for i := 0; i < num; i++ {
r.Query(context.Background(), QueryMsg("pool.net", 1), ch)
}
for i := 0; i < num; i++ {
if ans := ExtractAnswers(<-ch); len(ans) == 0 || ans[0].Data != "192.168.1.1" {
failures++
}
}
if failures > 50 {
t.Errorf("too many incorrect addresses returned")
}
}
func TestLen(t *testing.T) {
r := NewResolvers()
defer r.Stop()
// Test that the length is zero before adding DNS resolvers
if r.Len() > 0 {
t.Errorf("the length was greater than zero before adding DNS resolvers")
}
// Test that the length equals one after adding a single resolver
_ = r.AddResolvers(10, "8.8.8.8")
if r.Len() != 1 {
t.Errorf("the length did not equal one after adding the first resolver")
}
}
func TestAddLogger(t *testing.T) {
r := NewResolvers()
defer r.Stop()
r.SetLogger(nil)
if r.log != nil {
t.Errorf("failed to set the resolver pool logger")
}
}
func TestQPS(t *testing.T) {
r := NewResolvers()
defer r.Stop()
// Test that the value returned by QPS equals the value of the qps field
if r.QPS() != r.qps {
t.Errorf("the value returned by QPS did not equal the qps field")
}
qps := 100
// Test that the value returned by QPS equals the qps provided
r.SetMaxQPS(qps)
if r.QPS() != qps {
t.Errorf("the value returned by QPS did not equal the qps provided to AddMaxQPS")
}
// A rate limiter should now be setup, since a QPS greater than zero has been provided
if !r.maxSet || r.rate == nil {
t.Errorf("the rate limiter was not setup after providing a qps greater than zero")
}
// The rate limiter should be removed once we zero out the qps
r.SetMaxQPS(0)
if r.maxSet || r.rate != nil {
t.Errorf("the rate limiter was not removed after providing a qps of zero")
}
}
func TestAddResolvers(t *testing.T) {
r := NewResolvers()
defer r.Stop()
// Test that resolvers are not added when a zero QPS is provided
if err := r.AddResolvers(0, "8.8.8.8"); err == nil || r.Len() > 0 {
t.Errorf("the resolver was added with a QPS of value zero")
}
// Test that the resolver is added with a QPS greater than zero
if err := r.AddResolvers(10, "8.8.8.8"); err != nil || r.Len() == 0 {
t.Errorf("the resolver was not added with a QPS greater than zero")
}
}
func TestStopped(t *testing.T) {
r := NewResolvers()
_ = r.AddResolvers(10, "8.8.8.8")
// The resolver should not be considered stopped
select {
case <-r.done:
t.Errorf("resolvers should not be considered stopped")
default:
}
r.Stop()
// The resolver should be considered stopped
select {
case <-r.done:
default:
t.Errorf("resolvers should be stopped")
}
// It should be safe to stop the resolver pool more than once
r.Stop()
}
func TestQuery(t *testing.T) {
dns.HandleFunc("caffix.net.", typeAHandler)
defer dns.HandleRemove("caffix.net.")
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
r.SetDetectionResolver(10, "8.8.4.4")
ch := make(chan *dns.Msg, 1)
r.Query(context.Background(), nil, ch)
if resp := <-ch; resp != nil {
t.Errorf("the query did not return the expected nil response message")
}
var success bool
for i := 0; i < 5; i++ {
r.Query(context.Background(), QueryMsg("caffix.net", 1), ch)
if ans := ExtractAnswers(<-ch); len(ans) > 0 && ans[0].Data == "192.168.1.1" {
success = true
break
}
}
if !success {
t.Errorf("the query did not return the expected IP address")
}
}
func TestQueryChan(t *testing.T) {
dns.HandleFunc("caffix.net.", typeAHandler)
defer dns.HandleRemove("caffix.net.")
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
var success bool
for i := 0; i < 5; i++ {
ch := r.QueryChan(context.Background(), QueryMsg("caffix.net", 1))
if ans := ExtractAnswers(<-ch); len(ans) > 0 && ans[0].Data == "192.168.1.1" {
success = true
break
}
}
if !success {
t.Errorf("the query did not return the expected IP address")
}
}
func TestQueryBlocking(t *testing.T) {
name := "caffix.net."
dns.HandleFunc(name, typeAHandler)
defer dns.HandleRemove(name)
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
var success bool
ctx, cancel := context.WithCancel(context.Background())
for i := 0; i < 5; i++ {
if resp, err := r.QueryBlocking(ctx, QueryMsg(name, 1)); err == nil {
if ans := ExtractAnswers(resp); len(ans) > 0 && ans[0].Data == "192.168.1.1" {
success = true
break
}
}
}
if !success {
t.Errorf("the query did not return the expected IP address")
}
cancel()
// The query should fail since the context has expired
_, err = r.QueryBlocking(ctx, QueryMsg(name, 1))
if err == nil {
t.Errorf("the query did not fail as expected")
}
}
func TestEnforceMaxQPS(t *testing.T) {
r := NewResolvers()
r.SetMaxQPS(20)
// The query should fail since no DNS resolver has been added to the pool
resp, _ := r.QueryBlocking(context.Background(), QueryMsg("caffix.net", 1))
if resp.Rcode != RcodeNoResponse {
t.Errorf("the query did not fail as expected")
}
}
func TestQueryTimeout(t *testing.T) {
dns.HandleFunc("timeout.org.", timeoutHandler)
defer dns.HandleRemove("timeout.org.")
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
resp, err := r.QueryBlocking(context.Background(), QueryMsg("timeout.org", 1))
if err == nil && len(ExtractAnswers(resp)) != 0 {
t.Errorf("the query did not fail as expected")
}
}
func TestEdgeCases(t *testing.T) {
dns.HandleFunc("google.com.", typeAHandler)
defer dns.HandleRemove("google.com.")
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := r.QueryBlocking(ctx, QueryMsg("google.com", 1)); err == nil {
t.Errorf("query was successful when provided an expired context")
}
r.Stop()
if resp, err := r.QueryBlocking(context.Background(), QueryMsg("google.com", 1)); err == nil && len(ExtractAnswers(resp)) > 0 {
t.Errorf("query was successful when provided a stopped Resolver")
}
}
func TestBadWriteNextMsg(t *testing.T) {
name := "caffix.net."
dns.HandleFunc(name, typeAHandler)
defer dns.HandleRemove(name)
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
r.conns.Close()
resp, err := r.QueryBlocking(context.Background(), QueryMsg(name, 1))
if err == nil && resp.Rcode != RcodeNoResponse {
t.Errorf("the query did not fail as expected")
}
}
func TestTruncatedMsgs(t *testing.T) {
name := "caffix.net."
dns.HandleFunc(name, truncatedHandler)
defer dns.HandleRemove(name)
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
// Perform the query to call the TCP exchange
_, _ = r.QueryBlocking(context.Background(), QueryMsg(name, 1))
}
func TestTCPExchange(t *testing.T) {
name := "caffix.net."
dns.HandleFunc(name, typeAHandler)
defer dns.HandleRemove(name)
s, addrstr, _, err := RunLocalTCPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
res := r.pool.GetResolver()
ch := make(chan *dns.Msg, 2)
msg := QueryMsg(name, 1)
res.tcpExchange(&request{
Res: res,
Msg: msg,
Result: ch,
})
if resp := <-ch; resp.Rcode == dns.RcodeSuccess && len(resp.Answer) > 0 {
if ans := ExtractAnswers(resp); len(ans) == 0 || ans[0].Data != "192.168.1.1" {
t.Errorf("the query did not return the expected IP address")
}
} else {
t.Errorf("The TCP exchange process failed to handle the query for: %s", name)
}
}
func TestBadTCPExchange(t *testing.T) {
name := "caffix.net."
dns.HandleFunc(name, typeAHandler)
defer dns.HandleRemove(name)
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
res := r.pool.GetResolver()
ch := make(chan *dns.Msg, 2)
msg := QueryMsg(name, 1)
res.tcpExchange(&request{
Res: res,
Msg: msg,
Result: ch,
})
if resp := <-ch; resp.Rcode != RcodeNoResponse {
t.Errorf("The TCP exchange process did not fail as expected")
}
}
func truncatedHandler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Truncated = true
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: net.ParseIP("192.168.1.1"),
}
_ = w.WriteMsg(m)
}
func typeAHandler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: net.ParseIP("192.168.1.1"),
}
_ = w.WriteMsg(m)
}
func timeoutHandler(w dns.ResponseWriter, req *dns.Msg) {
time.Sleep(DefaultTimeout + time.Second)
typeAHandler(w, req)
}
func RunLocalUDPServer(laddr string, opts ...func(*dns.Server)) (*dns.Server, string, chan error, error) {
pc, err := net.ListenPacket("udp", laddr)
if err != nil {
return nil, "", nil, err
}
return RunLocalServer(pc, nil, opts...)
}
func RunLocalTCPServer(laddr string, opts ...func(*dns.Server)) (*dns.Server, string, chan error, error) {
l, err := net.Listen("tcp", laddr)
if err != nil {
return nil, "", nil, err
}
return RunLocalServer(nil, l, opts...)
}
func RunLocalServer(pc net.PacketConn, l net.Listener, opts ...func(*dns.Server)) (*dns.Server, string, chan error, error) {
server := &dns.Server{
PacketConn: pc,
Listener: l,
ReadTimeout: time.Hour,
WriteTimeout: time.Hour,
}
waitLock := sync.Mutex{}
waitLock.Lock()
server.NotifyStartedFunc = waitLock.Unlock
for _, opt := range opts {
opt(server)
}
var (
addr string
closer io.Closer
)
if l != nil {
addr = l.Addr().String()
closer = l
} else {
addr = pc.LocalAddr().String()
closer = pc
}
// fin must be buffered so the goroutine below won't block
// forever if fin is never read from. This always happens
// if the channel is discarded and can happen in TestShutdownUDP.
fin := make(chan error, 1)
go func() {
fin <- server.ActivateAndServe()
closer.Close()
}()
waitLock.Lock()
return server, addr, fin, nil
}