Debian Codegraph @

msg_helpers_test.go

raw symbols SCIP
1 package dns
2
3 import (
4 "bytes"
5 "net"
6 "testing"
7 )
8
9 // TestPacketDataNsec tests generated using fuzz.go and with a message pack
10 // containing the following bytes: 0000\x00\x00000000\x00\x002000000\x0060000\x00\x130000000000000000000"
11 // That bytes sequence created the overflow error and further permutations of that sequence were able to trigger
12 // the other code paths.
13 func TestPackDataNsec(t *testing.T) {
14 type args struct {
15 bitmap []uint16
16 msg []byte
17 off int
18 }
19 tests := []struct {
20 name string
21 args args
22 wantOff int
23 wantBytes []byte
24 wantErr bool
25 wantErrMsg string
26 }{
27 {
28 name: "overflow",
29 args: args{
30 bitmap: []uint16{
31 8962, 8963, 8970, 8971, 8978, 8979,
32 8986, 8987, 8994, 8995, 9002, 9003,
33 9010, 9011, 9018, 9019, 9026, 9027,
34 9034, 9035, 9042, 9043, 9050, 9051,
35 9058, 9059, 9066,
36 },
37 msg: []byte{
38 48, 48, 48, 48, 0, 0, 0,
39 1, 0, 0, 0, 0, 0, 0, 50,
40 48, 48, 48, 48, 48, 48,
41 0, 54, 48, 48, 48, 48,
42 0, 19, 48, 48,
43 },
44 off: 48,
45 },
46 wantErr: true,
47 wantErrMsg: "dns: overflow packing nsec",
48 wantOff: 48,
49 },
50 {
51 name: "disordered nsec bits",
52 args: args{
53 bitmap: []uint16{
54 8962,
55 1,
56 },
57 msg: []byte{
58 48, 48, 48, 48, 0, 0, 0, 1, 0, 0, 0, 0,
59 0, 0, 50, 48, 48, 48, 48, 48, 48, 0, 54, 48,
60 48, 48, 48, 0, 19, 48, 48, 48, 48, 48, 48, 0,
61 0, 0, 1, 0, 0, 0, 0, 0, 0, 50, 48, 48,
62 48, 48, 48, 48, 0, 54, 48, 48, 48, 48, 0, 19,
63 48, 48, 48, 48, 48, 48, 0, 0, 0, 1, 0, 0,
64 0, 0, 0, 0, 50, 48, 48, 48, 48, 48, 48, 0,
65 54, 48, 48, 48, 48, 0, 19, 48, 48, 48, 48, 48,
66 48, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 50,
67 48, 48, 48, 48, 48, 48, 0, 54, 48, 48, 48, 48,
68 0, 19, 48, 48, 48, 48, 48, 48, 0, 0, 0, 1,
69 0, 0, 0, 0, 0, 0, 50, 48, 48, 48, 48, 48,
70 48, 0, 54, 48, 48, 48, 48, 0, 19, 48, 48,
71 },
72 off: 0,
73 },
74 wantErr: true,
75 wantErrMsg: "dns: nsec bits out of order",
76 wantOff: 155,
77 },
78 {
79 name: "simple message with only one window",
80 args: args{
81 bitmap: []uint16{
82 1,
83 },
84 msg: []byte{
85 48, 48, 48, 48, 0, 0,
86 0, 1, 0, 0, 0, 0,
87 0, 0, 50, 48, 48, 48,
88 48, 48, 48, 0, 54, 48,
89 48, 48, 48, 0, 19, 48, 48,
90 },
91 off: 0,
92 },
93 wantErr: false,
94 wantOff: 3,
95 wantBytes: []byte{0, 1, 64},
96 },
97 {
98 name: "multiple types",
99 args: args{
100 bitmap: []uint16{
101 TypeNS, TypeSOA, TypeRRSIG, TypeDNSKEY, TypeNSEC3PARAM,
102 },
103 msg: []byte{
104 48, 48, 48, 48, 0, 0,
105 0, 1, 0, 0, 0, 0,
106 0, 0, 50, 48, 48, 48,
107 48, 48, 48, 0, 54, 48,
108 48, 48, 48, 0, 19, 48, 48,
109 },
110 off: 0,
111 },
112 wantErr: false,
113 wantOff: 9,
114 wantBytes: []byte{0, 7, 34, 0, 0, 0, 0, 2, 144},
115 },
116 }
117 for _, tt := range tests {
118 t.Run(tt.name, func(t *testing.T) {
119 gotOff, err := packDataNsec(tt.args.bitmap, tt.args.msg, tt.args.off)
120 if (err != nil) != tt.wantErr {
121 t.Errorf("packDataNsec() error = %v, wantErr %v", err, tt.wantErr)
122 return
123 }
124 if err != nil && tt.wantErrMsg != err.Error() {
125 t.Errorf("packDataNsec() error msg = %v, wantErrMsg %v", err.Error(), tt.wantErrMsg)
126 return
127 }
128 if gotOff != tt.wantOff {
129 t.Errorf("packDataNsec() = %v, want off %v", gotOff, tt.wantOff)
130 }
131 if err == nil && tt.args.off < len(tt.args.msg) && gotOff < len(tt.args.msg) {
132 if want, got := tt.wantBytes, tt.args.msg[tt.args.off:gotOff]; !bytes.Equal(got, want) {
133 t.Errorf("packDataNsec() = %v, want bytes %v", got, want)
134 }
135 }
136 })
137 }
138 }
139
140 func TestPackDataNsecDirtyBuffer(t *testing.T) {
141 zeroBuf := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0}
142 dirtyBuf := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
143 off1, _ := packDataNsec([]uint16{TypeNS, TypeSOA, TypeRRSIG}, zeroBuf, 0)
144 off2, _ := packDataNsec([]uint16{TypeNS, TypeSOA, TypeRRSIG}, dirtyBuf, 0)
145 if off1 != off2 {
146 t.Errorf("off1 %v != off2 %v", off1, off2)
147 }
148 if !bytes.Equal(zeroBuf[:off1], dirtyBuf[:off2]) {
149 t.Errorf("dirty buffer differs from zero buffer: %v, %v", zeroBuf[:off1], dirtyBuf[:off2])
150 }
151 }
152
153 func BenchmarkPackDataNsec(b *testing.B) {
154 benches := []struct {
155 name string
156 types []uint16
157 }{
158 {"empty", nil},
159 {"typical", []uint16{TypeNS, TypeSOA, TypeRRSIG, TypeDNSKEY, TypeNSEC3PARAM}},
160 {"multiple_windows", []uint16{1, 300, 350, 10000, 20000}},
161 }
162 for _, bb := range benches {
163 b.Run(bb.name, func(b *testing.B) {
164 buf := make([]byte, 100)
165 for n := 0; n < b.N; n++ {
166 packDataNsec(bb.types, buf, 0)
167 }
168 })
169 }
170 }
171 func TestUnpackString(t *testing.T) {
172 msg := []byte("\x00abcdef\x0f\\\"ghi\x04mmm\x7f")
173 msg[0] = byte(len(msg) - 1)
174
175 got, _, err := unpackString(msg, 0)
176 if err != nil {
177 t.Fatal(err)
178 }
179
180 if want := `abcdef\015\\\"ghi\004mmm\127`; want != got {
181 t.Errorf("expected %q, got %q", want, got)
182 }
183 }
184
185 func BenchmarkUnpackString(b *testing.B) {
186 b.Run("Escaped", func(b *testing.B) {
187 msg := []byte("\x00abcdef\x0f\\\"ghi\x04mmm")
188 msg[0] = byte(len(msg) - 1)
189
190 for n := 0; n < b.N; n++ {
191 got, _, err := unpackString(msg, 0)
192 if err != nil {
193 b.Fatal(err)
194 }
195
196 if want := `abcdef\015\\\"ghi\004mmm`; want != got {
197 b.Errorf("expected %q, got %q", want, got)
198 }
199 }
200 })
201 b.Run("Unescaped", func(b *testing.B) {
202 msg := []byte("\x00large.example.com")
203 msg[0] = byte(len(msg) - 1)
204
205 for n := 0; n < b.N; n++ {
206 got, _, err := unpackString(msg, 0)
207 if err != nil {
208 b.Fatal(err)
209 }
210
211 if want := "large.example.com"; want != got {
212 b.Errorf("expected %q, got %q", want, got)
213 }
214 }
215 })
216 }
217
218 func TestPackDataAplPrefix(t *testing.T) {
219 tests := []struct {
220 name string
221 negation bool
222 ip net.IP
223 mask net.IPMask
224 expect []byte
225 }{
226 {
227 "1:192.0.2.0/24",
228 false,
229 net.ParseIP("192.0.2.0").To4(),
230 net.CIDRMask(24, 32),
231 []byte{0x00, 0x01, 0x18, 0x03, 192, 0, 2},
232 },
233 {
234 "2:2001:db8:cafe::0/48",
235 false,
236 net.ParseIP("2001:db8:cafe::"),
237 net.CIDRMask(48, 128),
238 []byte{0x00, 0x02, 0x30, 0x06, 0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe},
239 },
240 {
241 "with trailing zero bytes 2:2001:db8:cafe::0/64",
242 false,
243 net.ParseIP("2001:db8:cafe::"),
244 net.CIDRMask(64, 128),
245 []byte{0x00, 0x02, 0x40, 0x06, 0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe},
246 },
247 {
248 "no non-zero bytes 2::/16",
249 false,
250 net.ParseIP("::"),
251 net.CIDRMask(16, 128),
252 []byte{0x00, 0x02, 0x10, 0x00},
253 },
254 {
255 "!2:2001:db8::/32",
256 true,
257 net.ParseIP("2001:db8::"),
258 net.CIDRMask(32, 128),
259 []byte{0x00, 0x02, 0x20, 0x84, 0x20, 0x01, 0x0d, 0xb8},
260 },
261 {
262 "normalize 1:198.51.103.255/22",
263 false,
264 net.ParseIP("198.51.103.255").To4(),
265 net.CIDRMask(22, 32),
266 []byte{0x00, 0x01, 0x16, 0x03, 198, 51, 100}, // 1:198.51.100.0/22
267 },
268 }
269 for _, tt := range tests {
270 t.Run(tt.name, func(t *testing.T) {
271 ap := &APLPrefix{
272 Negation: tt.negation,
273 Network: net.IPNet{IP: tt.ip, Mask: tt.mask},
274 }
275 out := make([]byte, 16)
276 off, err := packDataAplPrefix(ap, out, 0)
277 if err != nil {
278 t.Fatalf("expected no error, got %q", err)
279 }
280 if !bytes.Equal(tt.expect, out[:off]) {
281 t.Fatalf("expected output %02x, got %02x", tt.expect, out[:off])
282 }
283 // Make sure the packed bytes would be accepted by its own unpack
284 _, _, err = unpackDataAplPrefix(out, 0)
285 if err != nil {
286 t.Fatalf("expected no error, got %q", err)
287 }
288 })
289 }
290 }
291
292 func TestPackDataAplPrefix_Failures(t *testing.T) {
293 tests := []struct {
294 name string
295 ip net.IP
296 mask net.IPMask
297 }{
298 {
299 "family mismatch",
300 net.ParseIP("2001:db8::"),
301 net.CIDRMask(24, 32),
302 },
303 {
304 "unrecognized family",
305 []byte{0x42},
306 []byte{0xff},
307 },
308 }
309 for _, tt := range tests {
310 t.Run(tt.name, func(t *testing.T) {
311 ap := &APLPrefix{Network: net.IPNet{IP: tt.ip, Mask: tt.mask}}
312 msg := make([]byte, 16)
313 off, err := packDataAplPrefix(ap, msg, 0)
314 if err == nil {
315 t.Fatal("expected error, got none")
316 }
317 if off != len(msg) {
318 t.Fatalf("expected %d, got %d", len(msg), off)
319 }
320 })
321 }
322 }
323
324 func TestPackDataAplPrefix_BufferBounds(t *testing.T) {
325 ap := &APLPrefix{
326 Negation: false,
327 Network: net.IPNet{
328 IP: net.ParseIP("2001:db8::"),
329 Mask: net.CIDRMask(32, 128),
330 },
331 }
332 wire := []byte{0x00, 0x02, 0x20, 0x04, 0x20, 0x01, 0x0d, 0xb8}
333
334 t.Run("small", func(t *testing.T) {
335 msg := make([]byte, len(wire))
336 _, err := packDataAplPrefix(ap, msg, 1) // offset
337 if err == nil {
338 t.Fatal("expected error, got none")
339 }
340 })
341
342 t.Run("exact fit", func(t *testing.T) {
343 msg := make([]byte, len(wire))
344 off, err := packDataAplPrefix(ap, msg, 0)
345 if err != nil {
346 t.Fatalf("expected no error, got %q", err)
347 }
348 if !bytes.Equal(wire, msg[:off]) {
349 t.Fatalf("expected %02x, got %02x", wire, msg[:off])
350 }
351 })
352 }
353
354 func TestPackDataApl(t *testing.T) {
355 in := []APLPrefix{
356 {
357 Negation: true,
358 Network: net.IPNet{
359 IP: net.ParseIP("198.51.0.0").To4(),
360 Mask: net.CIDRMask(16, 32),
361 },
362 },
363 {
364 Negation: false,
365 Network: net.IPNet{
366 IP: net.ParseIP("2001:db8:beef::"),
367 Mask: net.CIDRMask(48, 128),
368 },
369 },
370 }
371 expect := []byte{
372 // 1:192.51.0.0/16
373 0x00, 0x01, 0x10, 0x82, 0xc6, 0x33,
374 // 2:2001:db8:beef::0/48
375 0x00, 0x02, 0x30, 0x06, 0x20, 0x01, 0x0d, 0xb8, 0xbe, 0xef,
376 }
377
378 msg := make([]byte, 32)
379 off, err := packDataApl(in, msg, 0)
380 if err != nil {
381 t.Fatalf("expected no error, got %q", err)
382 }
383 if !bytes.Equal(expect, msg[:off]) {
384 t.Fatalf("expected %02x, got %02x", expect, msg[:off])
385 }
386 }
387
388 func TestUnpackDataAplPrefix(t *testing.T) {
389 tests := []struct {
390 name string
391 wire []byte
392 negation bool
393 ip net.IP
394 mask net.IPMask
395 }{
396 {
397 "1:192.0.2.0/24",
398 []byte{0x00, 0x01, 0x18, 0x03, 192, 0, 2},
399 false,
400 net.ParseIP("192.0.2.0").To4(),
401 net.CIDRMask(24, 32),
402 },
403 {
404 "2:2001:db8::/32",
405 []byte{0x00, 0x02, 0x20, 0x04, 0x20, 0x01, 0x0d, 0xb8},
406 false,
407 net.ParseIP("2001:db8::"),
408 net.CIDRMask(32, 128),
409 },
410 {
411 "!2:2001:db8:8000::/33",
412 []byte{0x00, 0x02, 0x21, 0x85, 0x20, 0x01, 0x0d, 0xb8, 0x80},
413 true,
414 net.ParseIP("2001:db8:8000::"),
415 net.CIDRMask(33, 128),
416 },
417 {
418 "1:0.0.0.0/0",
419 []byte{0x00, 0x01, 0x00, 0x00},
420 false,
421 net.ParseIP("0.0.0.0").To4(),
422 net.CIDRMask(0, 32),
423 },
424 }
425 for _, tt := range tests {
426 t.Run(tt.name, func(t *testing.T) {
427 got, off, err := unpackDataAplPrefix(tt.wire, 0)
428 if err != nil {
429 t.Fatalf("expected no error, got %q", err)
430 }
431 if off != len(tt.wire) {
432 t.Fatalf("expected offset %d, got %d", len(tt.wire), off)
433 }
434 if got.Negation != tt.negation {
435 t.Errorf("expected negation %v, got %v", tt.negation, got.Negation)
436 }
437 if !tt.ip.Equal(got.Network.IP) {
438 t.Errorf("expected IP %02x, got %02x", tt.ip, got.Network.IP)
439 }
440 if !bytes.Equal(got.Network.Mask, tt.mask) {
441 t.Errorf("expected mask %02x, got %02x", tt.mask, got.Network.Mask)
442 }
443 })
444 }
445 }
446
447 func TestUnpackDataAplPrefix_Errors(t *testing.T) {
448 tests := []struct {
449 name string
450 wire []byte
451 want string
452 }{
453 {
454 "incomplete header",
455 []byte{0x00, 0x01, 0x18},
456 "dns: overflow unpacking APL prefix",
457 },
458 {
459 "unrecognized family",
460 []byte{0x00, 0x03, 0x00, 0x00},
461 "dns: unrecognized APL address family",
462 },
463 {
464 "prefix too large for family IPv4",
465 []byte{0x00, 0x01, 0x21, 0x04, 192, 0, 2, 0},
466 "dns: APL prefix too long",
467 },
468 {
469 "prefix too large for family IPv6",
470 []byte{0x00, 0x02, 0x81, 0x85, 0x20, 0x01, 0x0d, 0xb8, 0x80},
471 "dns: APL prefix too long",
472 },
473 {
474 "afdlen too long for address family IPv4",
475 []byte{0x00, 0x01, 22, 0x05, 192, 0, 2, 0, 0},
476 "dns: APL length too long",
477 },
478 {
479 "overflow unpacking APL address",
480 []byte{0x00, 0x01, 0x10, 0x02, 192},
481 "dns: overflow unpacking APL address",
482 },
483 {
484 "address included trailing zeros",
485 []byte{0x00, 0x01, 0x10, 0x04, 192, 0, 2, 0},
486 "dns: extra APL address bits",
487 },
488 }
489 for _, tt := range tests {
490 t.Run(tt.name, func(t *testing.T) {
491 _, _, err := unpackDataAplPrefix(tt.wire, 0)
492 if err == nil {
493 t.Fatal("expected error, got none")
494 }
495
496 if err.Error() != tt.want {
497 t.Errorf("expected %s, got %s", tt.want, err.Error())
498 }
499 })
500 }
501 }
502
503 func TestUnpackDataApl(t *testing.T) {
504 wire := []byte{
505 // 2:2001:db8:cafe:4200:0/56
506 0x00, 0x02, 0x38, 0x07, 0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0x42,
507 // 1:192.0.2.0/24
508 0x00, 0x01, 0x18, 0x03, 192, 0, 2,
509 // !1:192.0.2.128/25
510 0x00, 0x01, 0x19, 0x84, 192, 0, 2, 128,
511 // 1:10.0.0.0/24
512 0x00, 0x01, 0x18, 0x01, 0x0a,
513 // !1:10.0.0.1/32
514 0x00, 0x01, 0x20, 0x84, 0x0a, 0, 0, 1,
515 // !1:0.0.0.0/0
516 0x00, 0x01, 0x00, 0x80,
517 // 2::0/0
518 0x00, 0x02, 0x00, 0x00,
519 }
520 expect := []APLPrefix{
521 {
522 Negation: false,
523 Network: net.IPNet{
524 IP: net.ParseIP("2001:db8:cafe:4200::"),
525 Mask: net.CIDRMask(56, 128),
526 },
527 },
528 {
529 Negation: false,
530 Network: net.IPNet{
531 IP: net.ParseIP("192.0.2.0").To4(),
532 Mask: net.CIDRMask(24, 32),
533 },
534 },
535 {
536 Negation: true,
537 Network: net.IPNet{
538 IP: net.ParseIP("192.0.2.128").To4(),
539 Mask: net.CIDRMask(25, 32),
540 },
541 },
542 {
543 Negation: false,
544 Network: net.IPNet{
545 IP: net.ParseIP("10.0.0.0").To4(),
546 Mask: net.CIDRMask(24, 32),
547 },
548 },
549 {
550 Negation: true,
551 Network: net.IPNet{
552 IP: net.ParseIP("10.0.0.1").To4(),
553 Mask: net.CIDRMask(32, 32),
554 },
555 },
556 {
557 Negation: true,
558 Network: net.IPNet{
559 IP: net.ParseIP("0.0.0.0").To4(),
560 Mask: net.CIDRMask(0, 32),
561 },
562 },
563 {
564 Negation: false,
565 Network: net.IPNet{
566 IP: net.ParseIP("::").To16(),
567 Mask: net.CIDRMask(0, 128),
568 },
569 },
570 }
571
572 got, off, err := unpackDataApl(wire, 0)
573 if err != nil {
574 t.Fatalf("expected no error, got %q", err)
575 }
576 if off != len(wire) {
577 t.Fatalf("expected offset %d, got %d", len(wire), off)
578 }
579 if len(got) != len(expect) {
580 t.Fatalf("expected %d prefixes, got %d", len(expect), len(got))
581 }
582 for i, exp := range expect {
583 if got[i].Negation != exp.Negation {
584 t.Errorf("[%d] expected negation %v, got %v", i, exp.Negation, got[i].Negation)
585 }
586 if !exp.Network.IP.Equal(got[i].Network.IP) {
587 t.Errorf("[%d] expected IP %02x, got %02x", i, exp.Network.IP, got[i].Network.IP)
588 }
589 if !bytes.Equal(got[i].Network.Mask, exp.Network.Mask) {
590 t.Errorf("[%d] expected mask %02x, got %02x", i, exp.Network.Mask, got[i].Network.Mask)
591 }
592 }
593 }