-
Notifications
You must be signed in to change notification settings - Fork 11
/
client_test.go
142 lines (115 loc) · 2.55 KB
/
client_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
package dns
import (
"context"
"net"
"reflect"
"sort"
"strings"
"testing"
"time"
)
func TestLookupHost(t *testing.T) {
t.Parallel()
tests := []struct {
name string
host string
addrs []string
err error
}{
{
name: "dual-ipv-lookup",
host: "localhost.dev",
addrs: []string{"127.0.0.1", "::1"},
},
}
srv := mustServer(&Zone{
Origin: "dev.",
RRs: RRSet{
"localhost": {
TypeA: {
&A{A: net.IPv4(127, 0, 0, 1)},
},
TypeAAAA: {
&AAAA{AAAA: net.ParseIP("::1")},
},
},
},
})
addr, err := net.ResolveUDPAddr("udp", srv.Addr)
if err != nil {
t.Fatal(err)
}
client := &Client{
Transport: &Transport{
Proxy: func(_ context.Context, _ net.Addr) (net.Addr, error) {
return addr, nil
},
},
}
rlv := &net.Resolver{
PreferGo: true,
Dial: client.Dial,
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
addrs, err := rlv.LookupHost(context.Background(), test.host)
if err != nil {
t.Fatal(err)
}
sort.Strings(addrs)
if want, got := test.addrs, addrs; !reflect.DeepEqual(want, got) {
t.Errorf("want LookupHost addrs %q, got %q", want, got)
}
})
}
}
func TestClientResolver(t *testing.T) {
t.Parallel()
localhost := net.IPv4(127, 0, 0, 1).To4()
goog := net.IPv4(8, 8, 8, 8).To4()
client := &Client{
Resolver: HandlerFunc(func(ctx context.Context, w MessageWriter, r *Query) {
fqdn := r.Questions[0].Name
if !strings.HasSuffix(fqdn, ".local.") {
msg, err := w.Recur(ctx)
if err != nil {
w.Status(ServFail)
return
}
writeMessage(w, msg)
}
w.Answer(fqdn, time.Minute, &A{A: localhost})
}),
}
srv := mustServer(HandlerFunc(func(ctx context.Context, w MessageWriter, r *Query) {
fqdn := r.Questions[0].Name
w.Answer(fqdn, time.Minute, &A{A: goog})
}))
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)
}
query.Questions[0] = Question{Name: "test.goog.", Type: TypeA}
if msg, err = client.Do(context.Background(), query); err != nil {
t.Fatal(err)
}
if want, got := goog, msg.Answers[0].Record.(*A).A.To4(); !want.Equal(got) {
t.Errorf("want A record %q, got %q", want, got)
}
}