Debian Codegraph @

svcb/svcb_test.go

raw
1 package svcb
2
3 import (
4 "testing"
5
6 "golang.org/x/crypto/cryptobyte"
7 )
8
9 // This tests everything valid about SVCB but parsing. Parsing tests belong to parse_test.go.
10 func TestSVCB(t *testing.T) {
11 svcbs := []struct {
12 key string
13 data string
14 }{
15 {`mandatory`, `alpn,key65000`},
16 {`alpn`, `h2,h2c`},
17 {`port`, `499`},
18 {`ipv4hint`, `3.4.3.2,1.1.1.1`},
19 {`no-default-alpn`, ``},
20 {`ipv6hint`, `1::4:4:4:4,1::3:3:3:3`},
21 {`ech`, `YUdWc2JHOD0=`},
22 {`dohpath`, `/dns-query{?dns}`},
23 {`ohttp`, ``},
24 {`key65000`, `4\ 3`},
25 {`key65001`, `\"\ `},
26 {`key65002`, ``},
27 {`key65003`, `=\"\"`},
28 {`key65004`, `\254\ \ \030\000`},
29 }
30
31 for _, o := range svcbs {
32 keyCode := StringToKey(o.key)
33 pairFn := KeyToPair(keyCode)
34 if pairFn == nil {
35 t.Error("failed to lookup svc key: ", o.key)
36 continue
37 }
38 pair := pairFn()
39 if PairToKey(pair) != keyCode {
40 t.Error("key constant is not in sync: ", keyCode)
41 continue
42 }
43 err := Parse(pair, o.data, "")
44 if err != nil {
45 t.Error("failed to parse svc pair: ", o.key)
46 continue
47 }
48
49 b := make([]byte, pair.Len())
50 off, err := _pack(pair, b, 0)
51 if err != nil {
52 t.Error("failed to pack value of svc pair: ", o.key, err)
53 continue
54 }
55 if pair.Len() != off {
56 t.Errorf("expected packed svc value %s to be of length %d but got %d", o.key, pair.Len(), off)
57 }
58
59 if str := pair.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 sc := cryptobyte.String(b[4:]) // skip the TLV
64 err = _unpack(pair, &sc)
65 if err != nil {
66 t.Error("failed to unpack value of svc pair: ", o.key, err)
67 }
68 }
69 }
70
71 func TestALPNPresentation(t *testing.T) {
72 tests := map[string]string{
73 "h2": "h2",
74 "http": "http",
75 "\xfa": `\250`,
76 "some\"other,chars": `some\"other\\\044chars`,
77 }
78 for input, want := range tests {
79 e := new(ALPN)
80 e.Alpn = []string{input}
81 if e.String() != want {
82 t.Errorf("improper conversion with String(), wanted %v got %v", want, e.String())
83 }
84 }
85 }