Debian Codegraph @

svcb_test.go

raw symbols SCIP
1 package dns
2
3 import (
4 "testing"
5 )
6
7 // This tests everything valid about SVCB but parsing.
8 // Parsing tests belong to parse_test.go.
9 func TestSVCB(t *testing.T) {
10 svcbs := []struct {
11 key string
12 data string
13 }{
14 {`mandatory`, `alpn,key65000`},
15 {`alpn`, `h2,h2c`},
16 {`port`, `499`},
17 {`ipv4hint`, `3.4.3.2,1.1.1.1`},
18 {`no-default-alpn`, ``},
19 {`ipv6hint`, `1::4:4:4:4,1::3:3:3:3`},
20 {`ech`, `YUdWc2JHOD0=`},
21 {`dohpath`, `/dns-query{?dns}`},
22 {`key65000`, `4\ 3`},
23 {`key65001`, `\"\ `},
24 {`key65002`, ``},
25 {`key65003`, `=\"\"`},
26 {`key65004`, `\254\ \ \030\000`},
27 {`ohttp`, ``},
28 }
29
30 for _, o := range svcbs {
31 keyCode := svcbStringToKey(o.key)
32 kv := makeSVCBKeyValue(keyCode)
33 if kv == nil {
34 t.Error("failed to parse svc key: ", o.key)
35 continue
36 }
37 if kv.Key() != keyCode {
38 t.Error("key constant is not in sync: ", keyCode)
39 continue
40 }
41 err := kv.parse(o.data)
42 if err != nil {
43 t.Error("failed to parse svc pair: ", o.key)
44 continue
45 }
46 b, err := kv.pack()
47 if err != nil {
48 t.Error("failed to pack value of svc pair: ", o.key, err)
49 continue
50 }
51 if len(b) != int(kv.len()) {
52 t.Errorf("expected packed svc value %s to be of length %d but got %d", o.key, int(kv.len()), len(b))
53 }
54 err = kv.unpack(b)
55 if err != nil {
56 t.Error("failed to unpack value of svc pair: ", o.key, err)
57 continue
58 }
59 if str := kv.String(); str != o.data {
60 t.Errorf("`%s' should be equal to\n`%s', but is `%s'", o.key, o.data, str)
61 }
62 }
63 }
64
65 func TestDecodeBadSVCB(t *testing.T) {
66 svcbs := []struct {
67 key SVCBKey
68 data []byte
69 }{
70 {
71 key: SVCB_ALPN,
72 data: []byte{3, 0, 0}, // There aren't three octets after 3
73 },
74 {
75 key: SVCB_NO_DEFAULT_ALPN,
76 data: []byte{0},
77 },
78 {
79 key: SVCB_PORT,
80 data: []byte{},
81 },
82 {
83 key: SVCB_IPV4HINT,
84 data: []byte{0, 0, 0},
85 },
86 {
87 key: SVCB_IPV6HINT,
88 data: []byte{0, 0, 0},
89 },
90 {
91 key: SVCB_OHTTP,
92 data: []byte{0},
93 },
94 }
95 for _, o := range svcbs {
96 err := makeSVCBKeyValue(SVCBKey(o.key)).unpack(o.data)
97 if err == nil {
98 t.Error("accepted invalid svc value with key ", SVCBKey(o.key).String())
99 }
100 }
101 }
102
103 func TestPresentationSVCBAlpn(t *testing.T) {
104 tests := map[string]string{
105 "h2": "h2",
106 "http": "http",
107 "\xfa": `\250`,
108 "some\"other,chars": `some\"other\\\044chars`,
109 }
110 for input, want := range tests {
111 e := new(SVCBAlpn)
112 e.Alpn = []string{input}
113 if e.String() != want {
114 t.Errorf("improper conversion with String(), wanted %v got %v", want, e.String())
115 }
116 }
117 }
118
119 func TestSVCBAlpn(t *testing.T) {
120 tests := map[string][]string{
121 `. 1 IN SVCB 10 one.test. alpn=h2`: {"h2"},
122 `. 2 IN SVCB 20 two.test. alpn=h2,h3-19`: {"h2", "h3-19"},
123 `. 3 IN SVCB 30 three.test. alpn="f\\\\oo\\,bar,h2"`: {`f\oo,bar`, "h2"},
124 `. 4 IN SVCB 40 four.test. alpn="part1,part2,part3\\,part4\\\\"`: {"part1", "part2", `part3,part4\`},
125 `. 5 IN SVCB 50 five.test. alpn=part1\,\p\a\r\t2\044part3\092,part4\092\\`: {"part1", "part2", `part3,part4\`},
126 }
127 for s, v := range tests {
128 rr, err := NewRR(s)
129 if err != nil {
130 t.Error("failed to parse RR: ", err)
131 continue
132 }
133 alpn := rr.(*SVCB).Value[0].(*SVCBAlpn).Alpn
134 if len(v) != len(alpn) {
135 t.Fatalf("parsing alpn failed, wanted %v got %v", v, alpn)
136 }
137 for i := range v {
138 if v[i] != alpn[i] {
139 t.Fatalf("parsing alpn failed, wanted %v got %v", v, alpn)
140 }
141 }
142 }
143 }
144
145 func TestCompareSVCB(t *testing.T) {
146 val1 := []SVCBKeyValue{
147 &SVCBPort{
148 Port: 117,
149 },
150 &SVCBAlpn{
151 Alpn: []string{"h2", "h3"},
152 },
153 }
154 val2 := []SVCBKeyValue{
155 &SVCBAlpn{
156 Alpn: []string{"h2", "h3"},
157 },
158 &SVCBPort{
159 Port: 117,
160 },
161 }
162 if !areSVCBPairArraysEqual(val1, val2) {
163 t.Error("svcb pairs were compared without sorting")
164 }
165 if val1[0].Key() != SVCB_PORT || val2[0].Key() != SVCB_ALPN {
166 t.Error("original svcb pairs were reordered during comparison")
167 }
168 }