|
| 1 | +// Copyright 2025 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +//go:build go1.24 |
| 6 | + |
| 7 | +package http3 |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPrefixedInt(t *testing.T) { |
| 15 | + st1, st2 := newStreamPair(t) |
| 16 | + for _, test := range []struct { |
| 17 | + value int64 |
| 18 | + prefixLen uint8 |
| 19 | + encoded []byte |
| 20 | + }{ |
| 21 | + // https://www.rfc-editor.org/rfc/rfc7541#appendix-C.1.1 |
| 22 | + { |
| 23 | + value: 10, |
| 24 | + prefixLen: 5, |
| 25 | + encoded: []byte{ |
| 26 | + 0b_0000_1010, |
| 27 | + }, |
| 28 | + }, |
| 29 | + // https://www.rfc-editor.org/rfc/rfc7541#appendix-C.1.2 |
| 30 | + { |
| 31 | + value: 1337, |
| 32 | + prefixLen: 5, |
| 33 | + encoded: []byte{ |
| 34 | + 0b0001_1111, |
| 35 | + 0b1001_1010, |
| 36 | + 0b0000_1010, |
| 37 | + }, |
| 38 | + }, |
| 39 | + // https://www.rfc-editor.org/rfc/rfc7541#appendix-C.1.3 |
| 40 | + { |
| 41 | + value: 42, |
| 42 | + prefixLen: 8, |
| 43 | + encoded: []byte{ |
| 44 | + 0b0010_1010, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } { |
| 48 | + highBitMask := ^((byte(1) << test.prefixLen) - 1) |
| 49 | + for _, highBits := range []byte{ |
| 50 | + 0, highBitMask, 0b1010_1010 & highBitMask, |
| 51 | + } { |
| 52 | + gotEnc := appendPrefixedInt(nil, highBits, test.prefixLen, test.value) |
| 53 | + wantEnc := append([]byte{}, test.encoded...) |
| 54 | + wantEnc[0] |= highBits |
| 55 | + if !bytes.Equal(gotEnc, wantEnc) { |
| 56 | + t.Errorf("appendPrefixedInt(nil, 0b%08b, %v, %v) = {%x}, want {%x}", |
| 57 | + highBits, test.prefixLen, test.value, gotEnc, wantEnc) |
| 58 | + } |
| 59 | + |
| 60 | + st1.Write(gotEnc) |
| 61 | + if err := st1.Flush(); err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + gotFirstByte, v, err := st2.readPrefixedInt(test.prefixLen) |
| 65 | + if err != nil || gotFirstByte&highBitMask != highBits || v != test.value { |
| 66 | + t.Errorf("st.readPrefixedInt(%v) = 0b%08b, %v, %v; want 0b%08b, %v, nil", test.prefixLen, gotFirstByte, v, err, highBits, test.value) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestPrefixedString(t *testing.T) { |
| 73 | + st1, st2 := newStreamPair(t) |
| 74 | + for _, test := range []struct { |
| 75 | + value string |
| 76 | + prefixLen uint8 |
| 77 | + encoded []byte |
| 78 | + }{ |
| 79 | + // https://www.rfc-editor.org/rfc/rfc7541#appendix-C.6.1 |
| 80 | + { |
| 81 | + value: "302", |
| 82 | + prefixLen: 7, |
| 83 | + encoded: []byte{ |
| 84 | + 0x82, // H bit + length 2 |
| 85 | + 0x64, 0x02, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + value: "private", |
| 90 | + prefixLen: 5, |
| 91 | + encoded: []byte{ |
| 92 | + 0x25, // H bit + length 5 |
| 93 | + 0xae, 0xc3, 0x77, 0x1a, 0x4b, |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + value: "Mon, 21 Oct 2013 20:13:21 GMT", |
| 98 | + prefixLen: 7, |
| 99 | + encoded: []byte{ |
| 100 | + 0x96, // H bit + length 22 |
| 101 | + 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, |
| 102 | + 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, |
| 103 | + 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + value: "https://www.example.com", |
| 108 | + prefixLen: 7, |
| 109 | + encoded: []byte{ |
| 110 | + 0x91, // H bit + length 17 |
| 111 | + 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, |
| 112 | + 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, |
| 113 | + 0xd3, |
| 114 | + }, |
| 115 | + }, |
| 116 | + // Not Huffman encoded (encoded size == unencoded size). |
| 117 | + { |
| 118 | + value: "a", |
| 119 | + prefixLen: 7, |
| 120 | + encoded: []byte{ |
| 121 | + 0x01, // length 1 |
| 122 | + 0x61, |
| 123 | + }, |
| 124 | + }, |
| 125 | + // Empty string. |
| 126 | + { |
| 127 | + value: "", |
| 128 | + prefixLen: 7, |
| 129 | + encoded: []byte{ |
| 130 | + 0x00, // length 0 |
| 131 | + }, |
| 132 | + }, |
| 133 | + } { |
| 134 | + highBitMask := ^((byte(1) << (test.prefixLen + 1)) - 1) |
| 135 | + for _, highBits := range []byte{ |
| 136 | + 0, highBitMask, 0b1010_1010 & highBitMask, |
| 137 | + } { |
| 138 | + gotEnc := appendPrefixedString(nil, highBits, test.prefixLen, test.value) |
| 139 | + wantEnc := append([]byte{}, test.encoded...) |
| 140 | + wantEnc[0] |= highBits |
| 141 | + if !bytes.Equal(gotEnc, wantEnc) { |
| 142 | + t.Errorf("appendPrefixedString(nil, 0b%08b, %v, %v) = {%x}, want {%x}", |
| 143 | + highBits, test.prefixLen, test.value, gotEnc, wantEnc) |
| 144 | + } |
| 145 | + |
| 146 | + st1.Write(gotEnc) |
| 147 | + if err := st1.Flush(); err != nil { |
| 148 | + t.Fatal(err) |
| 149 | + } |
| 150 | + gotFirstByte, v, err := st2.readPrefixedString(test.prefixLen) |
| 151 | + if err != nil || gotFirstByte&highBitMask != highBits || v != test.value { |
| 152 | + t.Errorf("st.readPrefixedInt(%v) = 0b%08b, %q, %v; want 0b%08b, %q, nil", test.prefixLen, gotFirstByte, v, err, highBits, test.value) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestHuffmanDecodingFailure(t *testing.T) { |
| 159 | + st1, st2 := newStreamPair(t) |
| 160 | + st1.Write([]byte{ |
| 161 | + 0x82, // H bit + length 4 |
| 162 | + 0b_1111_1111, |
| 163 | + 0b_1111_1111, |
| 164 | + 0b_1111_1111, |
| 165 | + 0b_1111_1111, |
| 166 | + }) |
| 167 | + if err := st1.Flush(); err != nil { |
| 168 | + t.Fatal(err) |
| 169 | + } |
| 170 | + if b, v, err := st2.readPrefixedString(7); err == nil { |
| 171 | + t.Fatalf("readPrefixedString(7) = %x, %v, nil; want error", b, v) |
| 172 | + } |
| 173 | +} |
0 commit comments