-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrecord_test.go
More file actions
745 lines (696 loc) · 18.8 KB
/
record_test.go
File metadata and controls
745 lines (696 loc) · 18.8 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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package fastdns
import (
"bytes"
"net"
"net/netip"
"strings"
"testing"
)
// TestMessageAppendHOST serializes A and AAAA answers into the message buffer.
func TestMessageAppendHOST(t *testing.T) {
cases := []struct {
Raw []byte
IPs []netip.Addr
TTL uint32
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x01, // TYPE A
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x04, // RDLENGTH
0x01, 0x01, 0x01, 0x01, // RDATA 1.1.1.1
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x01, // TYPE A
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x04, // RDLENGTH
0x08, 0x08, 0x08, 0x08, // RDATA 8.8.8.8
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x01, // TYPE A
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x04, // RDLENGTH
0x7b, 0x2d, 0x06, 0x4e, // RDATA 123.45.6.78
},
IPs: []netip.Addr{
netip.AddrFrom4([4]byte{1, 1, 1, 1}),
netip.AddrFrom4([4]byte{8, 8, 8, 8}),
netip.AddrFrom4([4]byte{123, 45, 6, 78}),
},
TTL: 300,
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x1c, // TYPE AAAA
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x10, // RDLENGTH
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // RDATA ::1
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x1c, // TYPE AAAA
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x10, // RDLENGTH
0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88, // RDATA 2001:4860:4860::8888
},
IPs: []netip.Addr{
netip.MustParseAddr("::1"),
netip.MustParseAddr("2001:4860:4860::8888"),
},
TTL: 300,
},
}
for _, c := range cases {
req := new(Message)
req.Question.Class = ClassINET
req.AppendHOST(c.TTL, c.IPs)
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendHOST(%v) error got=%#v want=%#v", c.IPs, got, want)
}
}
}
// TestMessageAppendCNAME emits CNAME chains and optional address records.
func TestMessageAppendCNAME(t *testing.T) {
cases := []struct {
Raw []byte
CNAMEs []string
IPs []netip.Addr
TTL uint32
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x05, // TYPE CNAME
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x09, // RDLENGTH
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
CNAMEs: []string{"phus.lu"},
IPs: nil,
TTL: 300,
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x05, // TYPE CNAME
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x09, // RDLENGTH
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0xc0, 0x28, // NAME: pointer to first CNAME (offset 40)
0x00, 0x1c, // TYPE AAAA
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x10, // RDLENGTH
0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88, // RDATA 2001:4860:4860::8888
},
CNAMEs: []string{"phus.lu"},
IPs: []netip.Addr{netip.MustParseAddr("2001:4860:4860::8888")},
TTL: 300,
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x05, // TYPE CNAME
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x09, // RDLENGTH
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0xc0, 0x28, // NAME: pointer to first CNAME (offset 40)
0x00, 0x05, // TYPE CNAME
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x0c, // RDLENGTH
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0xc0, 0x40, // NAME: pointer to second CNAME (offset 64)
0x00, 0x01, // TYPE A
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x04, // RDLENGTH
0x01, 0x01, 0x01, 0x01, // RDATA 1.1.1.1
0xc0, 0x40, // NAME: pointer to second CNAME (offset 64)
0x00, 0x01, // TYPE A
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x04, // RDLENGTH
0x08, 0x08, 0x08, 0x08, // RDATA 8.8.8.8
},
CNAMEs: []string{"phus.lu", "ip.phus.lu"},
IPs: []netip.Addr{
netip.AddrFrom4([4]byte{1, 1, 1, 1}),
netip.AddrFrom4([4]byte{8, 8, 8, 8}),
},
TTL: 300,
},
}
for _, c := range cases {
req := new(Message)
req.Question.Name = EncodeDomain(nil, "ip.phus.lu")
req.Question.Class = ClassINET
req.AppendCNAME(c.TTL, c.CNAMEs, c.IPs)
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendCNAME(%v) error got=%#v want=%#v", c.IPs, got, want)
}
}
}
// TestMessageAppendSRV writes SRV records with varying priorities.
func TestMessageAppendSRV(t *testing.T) {
cases := []struct {
Raw []byte
TTL uint32
SRV net.SRV
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x21, // TYPE SRV
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x12, // RDLENGTH
0x03, 0xe8, // PRIORITY 1000
0x03, 0xe8, // WEIGHT 1000
0x00, 0x50, // PORT 80
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
TTL: 300,
SRV: net.SRV{Target: "ip.phus.lu", Port: 80, Priority: 1000, Weight: 1000},
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x21, // TYPE SRV
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x12, // RDLENGTH
0x04, 0x00, // PRIORITY 1024
0x04, 0x00, // WEIGHT 1024
0x01, 0xbb, // PORT 443
0x02, 's', 'g',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
TTL: 300,
SRV: net.SRV{Target: "sg.phus.lu", Port: 443, Priority: 1024, Weight: 1024},
},
}
for _, c := range cases {
req := new(Message)
req.Question.Class = ClassINET
req.AppendSRV(c.TTL, []net.SRV{c.SRV})
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendSRV(%v) error got=%#v want=%#v", c.SRV, got, want)
}
}
}
// TestMessageAppendNS writes NS records for different zones.
func TestMessageAppendNS(t *testing.T) {
cases := []struct {
Raw []byte
TTL uint32
Nameservers []net.NS
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x02, // TYPE NS
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x10, // RDLENGTH
0x03, 'n', 's', '1',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
},
TTL: 300,
Nameservers: []net.NS{{Host: "ns1.google.com"}},
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x02, // TYPE NS
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x10, // RDLENGTH
0x03, 'n', 's', '1',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x02, // TYPE NS
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x10, // RDLENGTH
0x03, 'n', 's', '2',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
},
TTL: 300,
Nameservers: []net.NS{{Host: "ns1.google.com"}, {Host: "ns2.google.com"}},
},
}
for _, c := range cases {
req := new(Message)
req.Question.Name = EncodeDomain(nil, "ip.phus.lu")
req.Question.Class = ClassINET
req.AppendNS(c.TTL, c.Nameservers)
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendNS(%v) error got=%#v want=%#v", c.Nameservers, got, want)
}
}
}
// TestMessageAppendSOA emits an SOA record with expected fields.
func TestMessageAppendSOA(t *testing.T) {
cases := []struct {
Raw []byte
TTL uint32
MName net.NS
RName net.NS
Serial uint32
Refresh uint32
Retry uint32
Expire uint32
Minimum uint32
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x06, // TYPE SOA
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x3a, // RDLENGTH
0x03, 'n', 's', '1',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x09, 'd', 'n', 's', '-', 'a', 'd', 'm', 'i', 'n',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x40, 0x00, 0x00, 0x00, // SERIAL 1073741824
0x00, 0x00, 0x03, 0x84, // REFRESH 900
0x00, 0x00, 0x03, 0x84, // RETRY 900
0x00, 0x00, 0x07, 0x08, // EXPIRE 1800
0x00, 0x00, 0x00, 0x3c, // MINIMUM 60
},
TTL: 300,
MName: net.NS{Host: "ns1.google.com"},
RName: net.NS{Host: "dns-admin.google.com"},
Serial: 1073741824,
Refresh: 900,
Retry: 900,
Expire: 1800,
Minimum: 60,
},
}
for _, c := range cases {
req := new(Message)
req.Question.Name = EncodeDomain(nil, "www.google.com")
req.Question.Class = ClassINET
req.AppendSOA(c.TTL, c.MName, c.RName, c.Serial, c.Refresh, c.Retry, c.Expire, c.Minimum)
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendSOA(%v) error got=%#v want=%#v", c.MName, got, want)
}
}
}
// TestMessageAppendMX serializes MX answers with priorities.
func TestMessageAppendMX(t *testing.T) {
cases := []struct {
Raw []byte
TTL uint32
MX net.MX
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x0f, // TYPE MX
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x0e, // RDLENGTH
0x00, 0x0a, // PREF 10
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
TTL: 300,
MX: net.MX{Host: "ip.phus.lu", Pref: 10},
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x0f, // TYPE MX
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x0e, // RDLENGTH
0x00, 0x0a, // PREF 10
0x02, 's', 'g',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
TTL: 300,
MX: net.MX{Host: "sg.phus.lu", Pref: 10},
},
}
for _, c := range cases {
req := new(Message)
req.Question.Class = ClassINET
req.AppendMX(c.TTL, []net.MX{c.MX})
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendMX(%v) error got=%#v want=%#v", c.MX, got, want)
}
}
}
// TestMessageAppendPTR assembles PTR responses for reverse lookups.
func TestMessageAppendPTR(t *testing.T) {
cases := []struct {
Raw []byte
PTR string
TTL uint32
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x0c, // TYPE PTR
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x0c, // RDLENGTH
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
PTR: "ip.phus.lu",
TTL: 300,
},
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x0c, // TYPE PTR
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x0c, // RDLENGTH
0x02, 's', 'g',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
},
PTR: "sg.phus.lu",
TTL: 300,
},
}
for _, c := range cases {
req := new(Message)
req.Question.Class = ClassINET
req.AppendPTR(c.TTL, c.PTR)
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendPTR(%v) error got=%#v want=%#v", c.PTR, got, want)
}
}
}
// TestMessageAppendTXT builds TXT records including multi-chunk payloads.
func TestMessageAppendTXT(t *testing.T) {
cases := []struct {
Raw []byte
TXT string
TTL uint32
}{
{
Raw: []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x10, // TYPE TXT
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x00, 0x0e, // RDLENGTH
0x0d, // TXT length 13
'i', 'a', 'm', 'a', 't', 'x', 't', 'r', 'e', 'c', 'o', 'r', 'd',
},
TXT: "iamatxtrecord",
TTL: 300,
},
{
Raw: func() []byte {
payload := []byte{
0xc0, 0x0c, // NAME: pointer to question (offset 12)
0x00, 0x10, // TYPE TXT
0x00, 0x01, // CLASS IN
0x00, 0x00, 0x01, 0x2c, // TTL 300s
0x01, 0x0f, // RDLENGTH 271 bytes
0xff, // chunk length 255
}
payload = append(payload, bytes.Repeat([]byte{'0'}, 255)...)
payload = append(payload,
0x0e, // remaining chunk length 14
'0', 'i', 'a', 'm', 'a', 't', 'x', 't', 'r', 'e', 'c', 'o', 'r', 'd',
)
return payload
}(),
TXT: strings.Repeat("0", 256) + "iamatxtrecord",
TTL: 300,
},
}
for _, c := range cases {
req := new(Message)
req.Question.Class = ClassINET
req.AppendTXT(c.TTL, c.TXT)
if got, want := req.Raw, c.Raw; !bytes.Equal(got, want) {
t.Errorf("AppendTXT(%v) error got=%#v want=%#v", c.TXT, got, want)
}
}
}
// BenchmarkMessageAppendHOST measures host record serialization speed.
func BenchmarkMessageAppendHOST(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
ips := []netip.Addr{netip.AddrFrom4([4]byte{8, 8, 8, 8})}
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendHOST(3000, ips)
}
}
// BenchmarkMessageAppendCNAME measures CNAME serialization speed.
func BenchmarkMessageAppendCNAME(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
cnames := []string{"cname.example.org"}
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendCNAME(3000, cnames, nil)
}
}
// BenchmarkMessageAppendSRV measures SRV serialization speed.
func BenchmarkMessageAppendSRV(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
srv := net.SRV{Target: "service1.example.org", Port: 443, Priority: 100, Weight: 100}
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendSRV(3000, []net.SRV{srv})
}
}
// BenchmarkMessageAppendNS measures NS serialization speed.
func BenchmarkMessageAppendNS(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
nameservers := []net.NS{{Host: "ns1.google.com"}, {Host: "ns2.google.com"}}
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendNS(300, nameservers)
}
}
// BenchmarkMessageAppendSOA measures SOA serialization speed.
func BenchmarkMessageAppendSOA(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendSOA(300, net.NS{Host: "ns1.google.com"}, net.NS{Host: "dns-admin.google.com"}, 1073741824, 900, 900, 1800, 60)
}
}
// BenchmarkMessageAppendPTR measures PTR serialization speed.
func BenchmarkMessageAppendPTR(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
ptr := "ptr.example.org"
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendPTR(3000, ptr)
}
}
// BenchmarkMessageAppendMX measures MX serialization speed.
func BenchmarkMessageAppendMX(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
mx := net.MX{Host: "mail.google.com", Pref: 100}
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendMX(3000, []net.MX{mx})
}
}
// BenchmarkMessageAppendTXT measures TXT serialization speed.
func BenchmarkMessageAppendTXT(b *testing.B) {
payload := []byte{
0x00, 0x02, // Transaction ID
0x01, 0x00, // Flags: recursion desired
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
0x02, 'i', 'p',
0x04, 'p', 'h', 'u', 's',
0x02, 'l', 'u',
0x00,
0x00, 0x01, // QTYPE A
0x00, 0x01, // QCLASS IN
}
req := new(Message)
if err := ParseMessage(req, payload, false); err != nil {
b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
}
size := len(req.Raw)
txt := "iamatxtrecord"
for i := 0; i < b.N; i++ {
req.Raw = req.Raw[:size]
req.AppendTXT(3000, txt)
}
}