-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpacketconn_test.go
More file actions
266 lines (233 loc) · 7.7 KB
/
Copy pathpacketconn_test.go
File metadata and controls
266 lines (233 loc) · 7.7 KB
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
package multidns
import (
"context"
"net"
"sync/atomic"
"testing"
"time"
"github.com/miekg/dns"
)
// TestPacketConn_RoundTrip verifies the full async path: WriteTo → pool
// dispatch → ReadFrom returns the wire-format response. This is the
// integration shape vaydns/dnstt expects.
func TestPacketConn_RoundTrip(t *testing.T) {
up := startUDPUpstream(t, func(q *dns.Msg) *dns.Msg { return reply(q, "192.0.2.200") })
mgr := New(Options{DefaultDeadline: time.Second})
defer mgr.Close()
if _, err := mgr.AddResolver(ResolverConfig{Protocol: ProtoUDP, Address: up.Address(), Timeout: 200 * time.Millisecond}); err != nil {
t.Fatal(err)
}
pc := mgr.PacketConn()
defer pc.Close()
q := newQ("a.example.")
q.Id = 0xBEEF
wire, err := q.Pack()
if err != nil {
t.Fatal(err)
}
n, err := pc.WriteTo(wire, nil)
if err != nil {
t.Fatalf("WriteTo: %v", err)
}
if n != len(wire) {
t.Fatalf("WriteTo wrote %d, want %d", n, len(wire))
}
_ = pc.SetReadDeadline(time.Now().Add(2 * time.Second))
buf := make([]byte, 4096)
rn, _, err := pc.ReadFrom(buf)
if err != nil {
t.Fatalf("ReadFrom: %v", err)
}
resp := new(dns.Msg)
if err := resp.Unpack(buf[:rn]); err != nil {
t.Fatalf("unpack: %v", err)
}
if resp.Rcode != dns.RcodeSuccess || len(resp.Answer) != 1 {
t.Fatalf("bad response: %#v", resp)
}
if resp.Id != 0xBEEF {
t.Fatalf("response Id = %#x, want 0xBEEF", resp.Id)
}
}
// TestPacketConn_FailoverPreserved verifies the smart pool's failover still
// applies when traffic goes through the PacketConn surface — this is the
// reason we built this surface (vaydns/dnstt's resolver becomes resilient).
func TestPacketConn_FailoverPreserved(t *testing.T) {
dropper := startUDPUpstream(t, func(q *dns.Msg) *dns.Msg { return nil })
good := startUDPUpstream(t, func(q *dns.Msg) *dns.Msg { return reply(q, "192.0.2.201") })
mgr := New(Options{DefaultDeadline: 1500 * time.Millisecond, DefaultResolverTimeout: 200 * time.Millisecond})
defer mgr.Close()
if _, err := mgr.AddResolver(ResolverConfig{Protocol: ProtoUDP, Address: dropper.Address()}); err != nil {
t.Fatal(err)
}
if _, err := mgr.AddResolver(ResolverConfig{Protocol: ProtoUDP, Address: good.Address()}); err != nil {
t.Fatal(err)
}
pc := mgr.PacketConn()
defer pc.Close()
q := newQ("ok.example.")
wire, _ := q.Pack()
got := 0
for i := 0; i < 4; i++ {
if _, err := pc.WriteTo(wire, nil); err != nil {
t.Fatal(err)
}
_ = pc.SetReadDeadline(time.Now().Add(1500 * time.Millisecond))
buf := make([]byte, 4096)
n, _, err := pc.ReadFrom(buf)
if err != nil {
continue
}
resp := new(dns.Msg)
if err := resp.Unpack(buf[:n]); err == nil && resp.Rcode == dns.RcodeSuccess {
got++
}
}
if got == 0 {
t.Fatalf("expected at least one successful response across 4 writes — failover not working through PacketConn")
}
}
// TestPacketConn_RateLimitProgression verifies that signals from PacketConn
// traffic still drive the AIMD throttle on the underlying resolverState —
// proving the smart-pool semantics are not bypassed by the wire-format
// surface.
func TestPacketConn_RateLimitProgression(t *testing.T) {
up := startUDPUpstream(t, func(q *dns.Msg) *dns.Msg { return rcodeOnly(q, dns.RcodeRefused) })
mgr := New(Options{
DefaultDeadline: time.Second,
DefaultResolverTimeout: 200 * time.Millisecond,
RateLimitFloorRPM: 6,
})
defer mgr.Close()
id, err := mgr.AddResolver(ResolverConfig{Protocol: ProtoUDP, Address: up.Address()})
if err != nil {
t.Fatal(err)
}
pc := mgr.PacketConn()
defer pc.Close()
q := newQ("rl.example.")
wire, _ := q.Pack()
for i := 0; i < 5; i++ {
_, _ = pc.WriteTo(wire, nil)
_ = pc.SetReadDeadline(time.Now().Add(400 * time.Millisecond))
buf := make([]byte, 4096)
_, _, _ = pc.ReadFrom(buf)
}
// Brief grace period — ReadFrom returns once the response is dispatched,
// and record() runs inside Resolve. Just be patient about scheduling.
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
if statByID(t, mgr, id).State == "rate_limited" {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("expected rate_limited via PacketConn traffic, got %q", statByID(t, mgr, id).State)
}
// TestPacketConn_ReadDeadline verifies SetReadDeadline triggers a timeout
// when no responses arrive — important so vaydns's tunnel can detect a
// stalled resolver and react.
func TestPacketConn_ReadDeadline(t *testing.T) {
mgr := New(Options{DefaultDeadline: 200 * time.Millisecond})
defer mgr.Close()
pc := mgr.PacketConn()
defer pc.Close()
_ = pc.SetReadDeadline(time.Now().Add(80 * time.Millisecond))
buf := make([]byte, 1024)
start := time.Now()
_, _, err := pc.ReadFrom(buf)
elapsed := time.Since(start)
if err == nil {
t.Fatalf("expected timeout error")
}
ne, ok := err.(net.Error)
if !ok || !ne.Timeout() {
t.Fatalf("expected net.Error with Timeout() true, got %v", err)
}
if elapsed < 50*time.Millisecond || elapsed > 250*time.Millisecond {
t.Fatalf("read returned at unexpected time: %v", elapsed)
}
}
// TestPacketConn_CloseUnblocksReader ensures Close wakes a pending ReadFrom.
func TestPacketConn_CloseUnblocksReader(t *testing.T) {
mgr := New(Options{DefaultDeadline: 200 * time.Millisecond})
defer mgr.Close()
pc := mgr.PacketConn()
done := make(chan error, 1)
go func() {
buf := make([]byte, 1024)
_, _, err := pc.ReadFrom(buf)
done <- err
}()
time.Sleep(20 * time.Millisecond)
_ = pc.Close()
select {
case err := <-done:
if err == nil {
t.Fatalf("expected error from ReadFrom after Close")
}
case <-time.After(300 * time.Millisecond):
t.Fatalf("Close did not unblock pending ReadFrom")
}
}
// TestPacketConn_DispatchesUniqueResponses puts two concurrent writers on
// the same PacketConn and verifies both responses arrive.
func TestPacketConn_DispatchesUniqueResponses(t *testing.T) {
var n atomic.Int64
up := startUDPUpstream(t, func(q *dns.Msg) *dns.Msg {
n.Add(1)
return reply(q, "192.0.2.210")
})
mgr := New(Options{DefaultDeadline: time.Second, DefaultResolverTimeout: 200 * time.Millisecond})
defer mgr.Close()
if _, err := mgr.AddResolver(ResolverConfig{Protocol: ProtoUDP, Address: up.Address()}); err != nil {
t.Fatal(err)
}
pc := mgr.PacketConn()
defer pc.Close()
q1 := newQ("one.example.")
q1.Id = 1111
q2 := newQ("two.example.")
q2.Id = 2222
w1, _ := q1.Pack()
w2, _ := q2.Pack()
_, _ = pc.WriteTo(w1, nil)
_, _ = pc.WriteTo(w2, nil)
seen := map[uint16]bool{}
deadline := time.Now().Add(2 * time.Second)
for len(seen) < 2 && time.Now().Before(deadline) {
_ = pc.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
buf := make([]byte, 4096)
nr, _, err := pc.ReadFrom(buf)
if err != nil {
continue
}
resp := new(dns.Msg)
if err := resp.Unpack(buf[:nr]); err == nil {
seen[resp.Id] = true
}
}
if !seen[1111] || !seen[2222] {
t.Fatalf("missing responses, saw %#v", seen)
}
}
// TestPacketConn_DoesNotCloseManager verifies the PacketConn doesn't take
// ownership of the Manager — Close on the conn must leave the Manager and
// the underlying upstreams alive.
func TestPacketConn_DoesNotCloseManager(t *testing.T) {
up := startUDPUpstream(t, func(q *dns.Msg) *dns.Msg { return reply(q, "192.0.2.220") })
mgr := New(Options{DefaultDeadline: time.Second, DefaultResolverTimeout: 200 * time.Millisecond})
defer mgr.Close()
if _, err := mgr.AddResolver(ResolverConfig{Protocol: ProtoUDP, Address: up.Address()}); err != nil {
t.Fatal(err)
}
pc := mgr.PacketConn()
if err := pc.Close(); err != nil {
t.Fatal(err)
}
// Manager must still serve direct Resolve calls.
resp, err := mgr.Resolve(context.Background(), newQ("after-close.example."))
if err != nil || resp.Rcode != dns.RcodeSuccess {
t.Fatalf("Manager unusable after PacketConn.Close: err=%v resp=%#v", err, resp)
}
}