forked from benburkert/dns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_test.go
133 lines (111 loc) · 3.38 KB
/
cache_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
package dns
import (
"context"
"math/rand"
"net"
"testing"
"time"
)
func TestCache(t *testing.T) {
client := &Client{
Resolver: new(Cache),
}
localhost := net.IPv4(127, 0, 0, 1).To4()
ipc := make(chan net.IP, 1)
ipc <- localhost
srv := mustServer(HandlerFunc(func(ctx context.Context, w MessageWriter, r *Query) {
w.Answer("test.local.", time.Minute, &A{A: <-ipc})
}))
addrUDP, err := net.ResolveUDPAddr("udp", srv.Addr)
if err != nil {
t.Fatal(err)
}
query := &Query{
RemoteAddr: addrUDP,
Message: &Message{
Questions: []Question{
{Name: "test.local.", Type: TypeA},
},
},
}
msg, err := client.Do(context.Background(), query)
if err != nil {
t.Fatal(err)
}
if want, got := localhost, msg.Answers[0].Record.(*A).A.To4(); !want.Equal(got) {
t.Errorf("want A record %q, got %q", want, got)
}
ipc <- net.IPv4(255, 255, 255, 255).To4()
if msg, err = client.Do(context.Background(), query); err != nil {
t.Fatal(err)
}
if want, got := localhost, msg.Answers[0].Record.(*A).A.To4(); !want.Equal(got) {
t.Errorf("want A record %q, got %q", want, got)
}
}
func TestCacheMultiAnswer(t *testing.T) {
client := &Client{
Resolver: new(Cache),
}
var answered bool
srv := mustServer(HandlerFunc(func(ctx context.Context, w MessageWriter, r *Query) {
if answered {
return
}
w.Answer("test.local.", 30*time.Second, &CNAME{CNAME: "cname.test.local."})
w.Answer("cname.test.local.", time.Minute, &A{A: net.IPv4(127, 0, 1, 1).To4()})
w.Answer("cname.test.local.", time.Minute, &A{A: net.IPv4(127, 0, 2, 1).To4()})
w.Answer("cname.test.local.", time.Minute, &A{A: net.IPv4(127, 0, 3, 1).To4()})
answered = true
}))
addrUDP, err := net.ResolveUDPAddr("udp", srv.Addr)
if err != nil {
t.Fatal(err)
}
query := &Query{
RemoteAddr: addrUDP,
Message: &Message{
Questions: []Question{
{Name: "test.local.", Type: TypeA},
},
},
}
msg, err := client.Do(context.Background(), query)
if err != nil {
t.Fatal(err)
}
if want, got := 4, len(msg.Answers); want != got {
t.Fatalf("want %d answers, got %d", want, got)
}
if want, got := "cname.test.local.", msg.Answers[0].Record.(*CNAME).CNAME; want != got {
t.Errorf("want CNAME record %q, got %q", want, got)
}
if want, got := "127.0.1.1", msg.Answers[1].Record.(*A).A.String(); want != got {
t.Errorf("want A record %q, got %q", want, got)
}
if want, got := "127.0.2.1", msg.Answers[2].Record.(*A).A.String(); want != got {
t.Errorf("want A record %q, got %q", want, got)
}
if want, got := "127.0.3.1", msg.Answers[3].Record.(*A).A.String(); want != got {
t.Errorf("want A record %q, got %q", want, got)
}
rand.Seed(0) // record order: 4 2 3 1
if msg, err = client.Do(context.Background(), query); err != nil {
t.Fatal(err)
}
if want, got := 4, len(msg.Answers); want != got {
t.Fatalf("want %d answers, got %d", want, got)
}
if want, got := "cname.test.local.", msg.Answers[0].Record.(*CNAME).CNAME; want != got {
t.Errorf("want CNAME record %q, got %q", want, got)
}
if want, got := "127.0.1.1", msg.Answers[1].Record.(*A).A.String(); want != got {
t.Errorf("want A record %q, got %q", want, got)
}
if want, got := "127.0.2.1", msg.Answers[3].Record.(*A).A.String(); want != got {
t.Errorf("want A record %q, got %q", want, got)
}
if want, got := "127.0.3.1", msg.Answers[2].Record.(*A).A.String(); want != got {
t.Errorf("want A record %q, got %q", want, got)
}
}