Debian Codegraph @

internal/unpack/unpack_test.go

raw
1 package unpack
2
3 import (
4 "fmt"
5 "testing"
6
7 "golang.org/x/crypto/cryptobyte"
8 )
9
10 func TestName(t *testing.T) {
11 testcases := []struct {
12 buf []byte
13 start int
14 name string
15 off int
16 mname bool // If true, use MName instead of Name.
17 }{
18 // Name
19 // miek.nl (4 miek 2 nl 0)
20 {[]byte{4, 109, 105, 101, 107, 2, 110, 108, 0}, 0, "miek.nl.", 9, false},
21 // beginning of a message, ID (98, 24),... then miek.nl as question = 0 15 (mx as type) and 0 01 as
22 // class. But then 192 12 which is a pointer to miek.nl, so lets decode that.
23 {[]byte{98, 24, 129, 128, 0, 1, 0, 5, 0, 0, 0, 1, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1, 192, 12, 0}, 25, "miek.nl.", 27, false},
24
25 // MName
26 // Wire: 16 "support.norrland" 10 "nordlo.com" 0
27 {[]byte{16, 's', 'u', 'p', 'p', 'o', 'r', 't', '.', 'n', 'o', 'r', 'r', 'l', 'a', 'n', 'd', 10, 'n', 'o', 'r', 'd', 'l', 'o', '.', 'c', 'o', 'm', 0}, 0, `support\.norrland.nordlo\.com.`, 29, true},
28 // Wire: 16 "support.norrland" 10 "nordlo.com" 0
29 {[]byte{16, 's', 'u', 'p', 'p', 'o', 'r', 't', '.', 'n', 'o', 'r', 'r', 'l', 'a', 'n', 'd', 10, 'n', 'o', 'r', 'd', 'l', 'o', '.', 'c', 'o', 'm', 0}, 0, `support\.norrland.nordlo\.com.`, 29, true},
30 }
31 for i, tc := range testcases {
32 t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
33 var (
34 name string
35 err error
36 )
37 s := cryptobyte.String(tc.buf[tc.start:])
38 sl := (len(s))
39 if tc.mname {
40 name, err = MName(&s, tc.buf)
41 } else {
42 name, err = Name(&s, tc.buf)
43 }
44 if err != nil {
45 t.Fatal(err)
46 }
47 if off := tc.start + sl - len(s); off != tc.off {
48 t.Errorf("expected offset %d, got %d", tc.off, off)
49 }
50 if name != tc.name {
51 t.Errorf("expected name %s, got %s", tc.name, name)
52 }
53 })
54 }
55 }