| 1 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 2 | |
| 3 | import ( |
| 4 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 5 | ) |
| 6 | |
| 7 | // This tests everything valid about SVCB but parsing. |
| 8 | // Parsing tests belong to parse_test.go. |
| 9 | func TestSVCBscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestSVCB().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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 := svcbStringToKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/svcbStringToKey().(o.key) |
| 32 | kv := makeSVCBKeyValuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/makeSVCBKeyValue().(keyCode) |
| 33 | if kv == nil { |
| 34 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("failed to parse svc key: ", o.key) |
| 35 | continue |
| 36 | } |
| 37 | if kv.Keyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#Key.() != keyCode { |
| 38 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("key constant is not in sync: ", keyCode) |
| 39 | continue |
| 40 | } |
| 41 | err := kv.parsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#parse.(o.data) |
| 42 | if err != nil { |
| 43 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("failed to parse svc pair: ", o.key) |
| 44 | continue |
| 45 | } |
| 46 | b, err := kv.packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#pack.() |
| 47 | if err != nil { |
| 48 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("failed to pack value of svc pair: ", o.key, err) |
| 49 | continue |
| 50 | } |
| 51 | if len(b) != int(kv.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#len.()) { |
| 52 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected packed svc value %s to be of length %d but got %d", o.key, int(kv.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#len.()), len(b)) |
| 53 | } |
| 54 | err = kv.unpackscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#unpack.(b) |
| 55 | if err != nil { |
| 56 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("failed to unpack value of svc pair: ", o.key, err) |
| 57 | continue |
| 58 | } |
| 59 | if str := kv.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#String.(); str != o.data { |
| 60 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("`%s' should be equal to\n`%s', but is `%s'", o.key, o.data, str) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestDecodeBadSVCBscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestDecodeBadSVCB().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 66 | svcbs := []struct { |
| 67 | key SVCBKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKey# |
| 68 | data []byte |
| 69 | }{ |
| 70 | { |
| 71 | key: SVCB_ALPNscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_ALPN., |
| 72 | data: []byte{3, 0, 0}, // There aren't three octets after 3 |
| 73 | }, |
| 74 | { |
| 75 | key: SVCB_NO_DEFAULT_ALPNscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_NO_DEFAULT_ALPN., |
| 76 | data: []byte{0}, |
| 77 | }, |
| 78 | { |
| 79 | key: SVCB_PORTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_PORT., |
| 80 | data: []byte{}, |
| 81 | }, |
| 82 | { |
| 83 | key: SVCB_IPV4HINTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_IPV4HINT., |
| 84 | data: []byte{0, 0, 0}, |
| 85 | }, |
| 86 | { |
| 87 | key: SVCB_IPV6HINTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_IPV6HINT., |
| 88 | data: []byte{0, 0, 0}, |
| 89 | }, |
| 90 | { |
| 91 | key: SVCB_OHTTPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_OHTTP., |
| 92 | data: []byte{0}, |
| 93 | }, |
| 94 | } |
| 95 | for _, o := range svcbs { |
| 96 | err := makeSVCBKeyValuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/makeSVCBKeyValue().(SVCBKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKey#(o.key)).unpackscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#unpack.(o.data) |
| 97 | if err == nil { |
| 98 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("accepted invalid svc value with key ", SVCBKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKey#(o.key).Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKey#String().()) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestPresentationSVCBAlpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestPresentationSVCBAlpn().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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(SVCBAlpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#) |
| 112 | e.Alpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#Alpn. = []string{input} |
| 113 | if e.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#String().() != want { |
| 114 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("improper conversion with String(), wanted %v got %v", want, e.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#String().()) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestSVCBAlpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestSVCBAlpn().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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 := NewRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/NewRR().(s) |
| 129 | if err != nil { |
| 130 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("failed to parse RR: ", err) |
| 131 | continue |
| 132 | } |
| 133 | alpn := rr.(*SVCBscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB#).Valuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB#Value.[0].(*SVCBAlpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#).Alpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#Alpn. |
| 134 | if len(v) != len(alpn) { |
| 135 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("parsing alpn failed, wanted %v got %v", v, alpn) |
| 136 | } |
| 137 | for i := range v { |
| 138 | if v[i] != alpn[i] { |
| 139 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("parsing alpn failed, wanted %v got %v", v, alpn) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestCompareSVCBscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestCompareSVCB().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 146 | val1 := []SVCBKeyValuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#{ |
| 147 | &SVCBPortscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBPort#{ |
| 148 | Portscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBPort#Port.: 117, |
| 149 | }, |
| 150 | &SVCBAlpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#{ |
| 151 | Alpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#Alpn.: []string{"h2", "h3"}, |
| 152 | }, |
| 153 | } |
| 154 | val2 := []SVCBKeyValuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#{ |
| 155 | &SVCBAlpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#{ |
| 156 | Alpnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBAlpn#Alpn.: []string{"h2", "h3"}, |
| 157 | }, |
| 158 | &SVCBPortscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBPort#{ |
| 159 | Portscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBPort#Port.: 117, |
| 160 | }, |
| 161 | } |
| 162 | if !areSVCBPairArraysEqualscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/areSVCBPairArraysEqual().(val1, val2) { |
| 163 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("svcb pairs were compared without sorting") |
| 164 | } |
| 165 | if val1[0].Keyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#Key.() != SVCB_PORTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_PORT. || val2[0].Keyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCBKeyValue#Key.() != SVCB_ALPNscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SVCB_ALPN. { |
| 166 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("original svcb pairs were reordered during comparison") |
| 167 | } |
| 168 | } |