golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/wire_test.go (about) 1 // Copyright 2023 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.21 6 7 package quic 8 9 import ( 10 "bytes" 11 "testing" 12 ) 13 14 func TestConsumeVarint(t *testing.T) { 15 for _, test := range []struct { 16 b []byte 17 want uint64 18 wantLen int 19 }{ 20 {[]byte{0x00}, 0, 1}, 21 {[]byte{0x3f}, 63, 1}, 22 {[]byte{0x40, 0x00}, 0, 2}, 23 {[]byte{0x7f, 0xff}, 16383, 2}, 24 {[]byte{0x80, 0x00, 0x00, 0x00}, 0, 4}, 25 {[]byte{0xbf, 0xff, 0xff, 0xff}, 1073741823, 4}, 26 {[]byte{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0, 8}, 27 {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 4611686018427387903, 8}, 28 // Example cases from https://www.rfc-editor.org/rfc/rfc9000.html#section-a.1 29 {[]byte{0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c}, 151288809941952652, 8}, 30 {[]byte{0x9d, 0x7f, 0x3e, 0x7d}, 494878333, 4}, 31 {[]byte{0x7b, 0xbd}, 15293, 2}, 32 {[]byte{0x25}, 37, 1}, 33 {[]byte{0x40, 0x25}, 37, 2}, 34 } { 35 got, gotLen := consumeVarint(test.b) 36 if got != test.want || gotLen != test.wantLen { 37 t.Errorf("consumeVarint(%x) = %v, %v; want %v, %v", test.b, got, gotLen, test.want, test.wantLen) 38 } 39 // Extra data in the buffer is ignored. 40 b := append(test.b, 0) 41 got, gotLen = consumeVarint(b) 42 if got != test.want || gotLen != test.wantLen { 43 t.Errorf("consumeVarint(%x) = %v, %v; want %v, %v", b, got, gotLen, test.want, test.wantLen) 44 } 45 // Short buffer results in an error. 46 for i := 1; i <= len(test.b); i++ { 47 b = test.b[:len(test.b)-i] 48 got, gotLen = consumeVarint(b) 49 if got != 0 || gotLen >= 0 { 50 t.Errorf("consumeVarint(%x) = %v, %v; want 0, -1", b, got, gotLen) 51 } 52 } 53 } 54 } 55 56 func TestAppendVarint(t *testing.T) { 57 for _, test := range []struct { 58 v uint64 59 want []byte 60 }{ 61 {0, []byte{0x00}}, 62 {63, []byte{0x3f}}, 63 {16383, []byte{0x7f, 0xff}}, 64 {1073741823, []byte{0xbf, 0xff, 0xff, 0xff}}, 65 {4611686018427387903, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 66 // Example cases from https://www.rfc-editor.org/rfc/rfc9000.html#section-a.1 67 {151288809941952652, []byte{0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c}}, 68 {494878333, []byte{0x9d, 0x7f, 0x3e, 0x7d}}, 69 {15293, []byte{0x7b, 0xbd}}, 70 {37, []byte{0x25}}, 71 } { 72 got := appendVarint([]byte{}, test.v) 73 if !bytes.Equal(got, test.want) { 74 t.Errorf("AppendVarint(nil, %v) = %x, want %x", test.v, got, test.want) 75 } 76 if gotLen, wantLen := sizeVarint(test.v), len(got); gotLen != wantLen { 77 t.Errorf("SizeVarint(%v) = %v, want %v", test.v, gotLen, wantLen) 78 } 79 } 80 } 81 82 func TestConsumeUint32(t *testing.T) { 83 for _, test := range []struct { 84 b []byte 85 want uint32 86 wantLen int 87 }{ 88 {[]byte{0x01, 0x02, 0x03, 0x04}, 0x01020304, 4}, 89 {[]byte{0x01, 0x02, 0x03}, 0, -1}, 90 } { 91 if got, n := consumeUint32(test.b); got != test.want || n != test.wantLen { 92 t.Errorf("consumeUint32(%x) = %v, %v; want %v, %v", test.b, got, n, test.want, test.wantLen) 93 } 94 } 95 } 96 97 func TestConsumeUint64(t *testing.T) { 98 for _, test := range []struct { 99 b []byte 100 want uint64 101 wantLen int 102 }{ 103 {[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 0x0102030405060708, 8}, 104 {[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 0, -1}, 105 } { 106 if got, n := consumeUint64(test.b); got != test.want || n != test.wantLen { 107 t.Errorf("consumeUint32(%x) = %v, %v; want %v, %v", test.b, got, n, test.want, test.wantLen) 108 } 109 } 110 } 111 112 func TestConsumeVarintBytes(t *testing.T) { 113 for _, test := range []struct { 114 b []byte 115 want []byte 116 wantLen int 117 }{ 118 {[]byte{0x00}, []byte{}, 1}, 119 {[]byte{0x40, 0x00}, []byte{}, 2}, 120 {[]byte{0x04, 0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, 5}, 121 {[]byte{0x40, 0x04, 0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, 6}, 122 } { 123 got, gotLen := consumeVarintBytes(test.b) 124 if !bytes.Equal(got, test.want) || gotLen != test.wantLen { 125 t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {%x}, %v", test.b, got, gotLen, test.want, test.wantLen) 126 } 127 // Extra data in the buffer is ignored. 128 b := append(test.b, 0) 129 got, gotLen = consumeVarintBytes(b) 130 if !bytes.Equal(got, test.want) || gotLen != test.wantLen { 131 t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {%x}, %v", b, got, gotLen, test.want, test.wantLen) 132 } 133 // Short buffer results in an error. 134 for i := 1; i <= len(test.b); i++ { 135 b = test.b[:len(test.b)-i] 136 got, gotLen := consumeVarintBytes(b) 137 if len(got) > 0 || gotLen > 0 { 138 t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen) 139 } 140 } 141 142 } 143 } 144 145 func TestConsumeVarintBytesErrors(t *testing.T) { 146 for _, b := range [][]byte{ 147 {0x01}, 148 {0x40, 0x01}, 149 } { 150 got, gotLen := consumeVarintBytes(b) 151 if len(got) > 0 || gotLen > 0 { 152 t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen) 153 } 154 } 155 } 156 157 func TestConsumeUint8Bytes(t *testing.T) { 158 for _, test := range []struct { 159 b []byte 160 want []byte 161 wantLen int 162 }{ 163 {[]byte{0x00}, []byte{}, 1}, 164 {[]byte{0x01, 0x00}, []byte{0x00}, 2}, 165 {[]byte{0x04, 0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, 5}, 166 } { 167 got, gotLen := consumeUint8Bytes(test.b) 168 if !bytes.Equal(got, test.want) || gotLen != test.wantLen { 169 t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {%x}, %v", test.b, got, gotLen, test.want, test.wantLen) 170 } 171 // Extra data in the buffer is ignored. 172 b := append(test.b, 0) 173 got, gotLen = consumeUint8Bytes(b) 174 if !bytes.Equal(got, test.want) || gotLen != test.wantLen { 175 t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {%x}, %v", b, got, gotLen, test.want, test.wantLen) 176 } 177 // Short buffer results in an error. 178 for i := 1; i <= len(test.b); i++ { 179 b = test.b[:len(test.b)-i] 180 got, gotLen := consumeUint8Bytes(b) 181 if len(got) > 0 || gotLen > 0 { 182 t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen) 183 } 184 } 185 186 } 187 } 188 189 func TestConsumeUint8BytesErrors(t *testing.T) { 190 for _, b := range [][]byte{ 191 {0x01}, 192 {0x04, 0x01, 0x02, 0x03}, 193 } { 194 got, gotLen := consumeUint8Bytes(b) 195 if len(got) > 0 || gotLen > 0 { 196 t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen) 197 } 198 } 199 } 200 201 func TestAppendUint8Bytes(t *testing.T) { 202 var got []byte 203 got = appendUint8Bytes(got, []byte{}) 204 got = appendUint8Bytes(got, []byte{0xaa, 0xbb}) 205 want := []byte{ 206 0x00, 207 0x02, 0xaa, 0xbb, 208 } 209 if !bytes.Equal(got, want) { 210 t.Errorf("appendUint8Bytes {}, {aabb} = {%x}; want {%x}", got, want) 211 } 212 } 213 214 func TestAppendVarintBytes(t *testing.T) { 215 var got []byte 216 got = appendVarintBytes(got, []byte{}) 217 got = appendVarintBytes(got, []byte{0xaa, 0xbb}) 218 want := []byte{ 219 0x00, 220 0x02, 0xaa, 0xbb, 221 } 222 if !bytes.Equal(got, want) { 223 t.Errorf("appendVarintBytes {}, {aabb} = {%x}; want {%x}", got, want) 224 } 225 }