-
Notifications
You must be signed in to change notification settings - Fork 12
/
handler_test.go
337 lines (299 loc) · 8.01 KB
/
handler_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
package fastdns
import (
"encoding/hex"
"net"
"net/netip"
"testing"
)
func mockMessage() (msg *Message) {
// domain = hk.phus.lu
payload, _ := hex.DecodeString("00028180000100010000000002686b0470687573026c750000010001c00c000100010000012b0004771c56be")
msg = AcquireMessage()
err := ParseMessage(msg, payload, true)
if err != nil {
panic(err)
}
return
}
func TestHandlerError(t *testing.T) {
var cases = []struct {
Hex string
Rcode Rcode
}{
{
"000281830000000000000000",
RcodeNXDomain,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
if rw.RemoteAddr().IsValid() {
t.Errorf("MemResponseWriter shall return empty addr")
}
if rw.LocalAddr().IsValid() {
t.Errorf("MemResponseWriter shall return empty addr")
}
for _, c := range cases {
Error(rw, req, c.Rcode)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("Error(%v) error got=%#v want=%#v", c.Rcode, got, want)
}
}
}
func TestHandlerHost1(t *testing.T) {
var cases = []struct {
Hex string
IP netip.Addr
TTL uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000100010000012c000401020408",
netip.AddrFrom4([4]byte{1, 2, 4, 8}),
300,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
HOST1(rw, req, c.TTL, c.IP)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("HOST1(%v) error got=%#v want=%#v", c.IP, got, want)
}
}
}
func TestHandlerHost(t *testing.T) {
var cases = []struct {
Hex string
IP netip.Addr
TTL uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000100010000012c000401020408",
netip.AddrFrom4([4]byte{1, 2, 4, 8}),
300,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
HOST(rw, req, c.TTL, []netip.Addr{c.IP})
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("HOST(%v) error got=%#v want=%#v", c.IP, got, want)
}
}
}
func TestHandlerCNAME(t *testing.T) {
var cases = []struct {
Hex string
CNAME string
TTL uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000500010000012c001305636e616d65076578616d706c6503636f6d00",
"cname.example.com",
300,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
CNAME(rw, req, c.TTL, []string{c.CNAME}, nil)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("CNAME(%v) error got=%#v want=%#v", c.CNAME, got, want)
}
}
}
func TestHandlerSRV(t *testing.T) {
var cases = []struct {
Hex string
TTL uint32
SRV net.SRV
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c002100010000012c001c03e803e81f41087365727669636531076578616d706c6503636f6d00",
300,
net.SRV{Target: "service1.example.com", Port: 8001, Priority: 1000, Weight: 1000},
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
SRV(rw, req, c.TTL, []net.SRV{c.SRV})
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("SRV(%v) error got=%#v want=%#v", c.SRV, got, want)
}
}
}
func TestHandlerNS(t *testing.T) {
var cases = []struct {
Hex string
TTL uint32
Nameserver net.NS
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000200010000012c0010026e73076578616d706c6503636f6d00",
300,
net.NS{Host: "ns.example.com"},
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
NS(rw, req, c.TTL, []net.NS{c.Nameserver})
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("NS(%v) error got=%#v want=%#v", c.Nameserver, got, want)
}
}
}
func TestHandlerSOA(t *testing.T) {
var cases = []struct {
Hex string
TTL uint32
MName net.NS
RName net.NS
Serial uint32
Refresh uint32
Retry uint32
Expire uint32
Minimum uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000600010000012c003a036e733106676f6f676c6503636f6d0009646e732d61646d696e06676f6f676c6503636f6d00400000000000038400000384000007080000003c",
300,
net.NS{Host: "ns1.google.com"},
net.NS{Host: "dns-admin.google.com"},
1073741824,
900,
900,
1800,
60,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
SOA(rw, req, c.TTL, c.MName, c.RName, c.Serial, c.Refresh, c.Retry, c.Expire, c.Minimum)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("SOA(%v) error got=%#v want=%#v", c.MName, got, want)
}
}
}
func TestHandlerMX(t *testing.T) {
var cases = []struct {
Hex string
TTL uint32
MX net.MX
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000f00010000012c0013000a03707472076578616d706c65036f726700",
300,
net.MX{Host: "ptr.example.org", Pref: 10},
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
MX(rw, req, c.TTL, []net.MX{c.MX})
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("MX(%v) error got=%#v want=%#v", c.MX, got, want)
}
}
}
func TestHandlerPTR(t *testing.T) {
var cases = []struct {
Hex string
PTR string
TTL uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000c00010000012c001103707472076578616d706c65036f726700",
"ptr.example.org",
300,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
PTR(rw, req, c.TTL, c.PTR)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("PTR(%v) error got=%#v want=%#v", c.PTR, got, want)
}
}
}
func TestHandlerTXT(t *testing.T) {
var cases = []struct {
Hex string
TXT string
TTL uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c001000010000012c000e0d69616d617478747265636f7264",
"iamatxtrecord",
300,
},
}
rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
TXT(rw, req, c.TTL, c.TXT)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("TXT(%v) error got=%#v want=%#v", c.TXT, got, want)
}
}
}
type nilResponseWriter struct{}
func (rw *nilResponseWriter) RemoteAddr() netip.AddrPort { return netip.AddrPort{} }
func (rw *nilResponseWriter) LocalAddr() netip.AddrPort { return netip.AddrPort{} }
func (rw *nilResponseWriter) Write(p []byte) (n int, err error) { return len(p), nil }
func BenchmarkHOST1(b *testing.B) {
req := mockMessage()
ip := netip.AddrFrom4([4]byte{8, 8, 8, 8})
for i := 0; i < b.N; i++ {
HOST1(&nilResponseWriter{}, req, 3000, ip)
}
}
func BenchmarkHOST(b *testing.B) {
req := mockMessage()
ips := []netip.Addr{netip.AddrFrom4([4]byte{8, 8, 8, 8})}
for i := 0; i < b.N; i++ {
HOST(&nilResponseWriter{}, req, 3000, ips)
}
}
func BenchmarkCNAME(b *testing.B) {
req := mockMessage()
cnames := []string{"cname.example.org"}
for i := 0; i < b.N; i++ {
CNAME(&nilResponseWriter{}, req, 3000, cnames, nil)
}
}
func BenchmarkSRV(b *testing.B) {
req := mockMessage()
srv := net.SRV{Target: "service1.example.org", Port: 8001, Priority: 1000, Weight: 1000}
for i := 0; i < b.N; i++ {
SRV(&nilResponseWriter{}, req, 3000, []net.SRV{srv})
}
}
func BenchmarkNS(b *testing.B) {
req := mockMessage()
nameservers := []net.NS{{Host: "ns1.google.com"}, {Host: "ns2.google.com"}}
for i := 0; i < b.N; i++ {
NS(&nilResponseWriter{}, req, 3000, nameservers)
}
}
func BenchmarkSOA(b *testing.B) {
req := mockMessage()
for i := 0; i < b.N; i++ {
SOA(&nilResponseWriter{}, req, 3000, net.NS{Host: "ns1.google.com"}, net.NS{Host: "dns-admin.google.com"}, 42, 900, 900, 1800, 60)
}
}
func BenchmarkPTR(b *testing.B) {
req := mockMessage()
ptr := "ptr.example.org"
for i := 0; i < b.N; i++ {
PTR(&nilResponseWriter{}, req, 3000, ptr)
}
}
func BenchmarkMX(b *testing.B) {
req := mockMessage()
mx := net.MX{Host: "mail.google.com", Pref: 100}
for i := 0; i < b.N; i++ {
MX(&nilResponseWriter{}, req, 3000, []net.MX{mx})
}
}
func BenchmarkTXT(b *testing.B) {
req := mockMessage()
txt := "iamatxtrecord"
for i := 0; i < b.N; i++ {
TXT(&nilResponseWriter{}, req, 3000, txt)
}
}