| 1 |
// Copyright 2011 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 |
package binary |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"io" |
| 10 |
"math" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func testConstant(t *testing.T, w uint, max int) { |
| 15 |
buf := make([]byte, MaxVarintLen64) |
| 16 |
n := PutUvarint(buf, 1<<w-1) |
| 17 |
if n != max { |
| 18 |
t.Errorf("MaxVarintLen%d = %d; want %d", w, max, n) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func TestConstants(t *testing.T) { |
| 23 |
testConstant(t, 16, MaxVarintLen16) |
| 24 |
testConstant(t, 32, MaxVarintLen32) |
| 25 |
testConstant(t, 64, MaxVarintLen64) |
| 26 |
} |
| 27 |
|
| 28 |
func testVarint(t *testing.T, x int64) { |
| 29 |
buf := make([]byte, MaxVarintLen64) |
| 30 |
n := PutVarint(buf, x) |
| 31 |
y, m := Varint(buf[0:n]) |
| 32 |
if x != y { |
| 33 |
t.Errorf("Varint(%d): got %d", x, y) |
| 34 |
} |
| 35 |
if n != m { |
| 36 |
t.Errorf("Varint(%d): got n = %d; want %d", x, m, n) |
| 37 |
} |
| 38 |
|
| 39 |
buf2 := []byte("prefix") |
| 40 |
buf2 = AppendVarint(buf2, x) |
| 41 |
if string(buf2) != "prefix"+string(buf[:n]) { |
| 42 |
t.Errorf("AppendVarint(%d): got %q, want %q", x, buf2, "prefix"+string(buf[:n])) |
| 43 |
} |
| 44 |
|
| 45 |
y, err := ReadVarint(bytes.NewReader(buf)) |
| 46 |
if err != nil { |
| 47 |
t.Errorf("ReadVarint(%d): %s", x, err) |
| 48 |
} |
| 49 |
if x != y { |
| 50 |
t.Errorf("ReadVarint(%d): got %d", x, y) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func testUvarint(t *testing.T, x uint64) { |
| 55 |
buf := make([]byte, MaxVarintLen64) |
| 56 |
n := PutUvarint(buf, x) |
| 57 |
y, m := Uvarint(buf[0:n]) |
| 58 |
if x != y { |
| 59 |
t.Errorf("Uvarint(%d): got %d", x, y) |
| 60 |
} |
| 61 |
if n != m { |
| 62 |
t.Errorf("Uvarint(%d): got n = %d; want %d", x, m, n) |
| 63 |
} |
| 64 |
|
| 65 |
buf2 := []byte("prefix") |
| 66 |
buf2 = AppendUvarint(buf2, x) |
| 67 |
if string(buf2) != "prefix"+string(buf[:n]) { |
| 68 |
t.Errorf("AppendUvarint(%d): got %q, want %q", x, buf2, "prefix"+string(buf[:n])) |
| 69 |
} |
| 70 |
|
| 71 |
y, err := ReadUvarint(bytes.NewReader(buf)) |
| 72 |
if err != nil { |
| 73 |
t.Errorf("ReadUvarint(%d): %s", x, err) |
| 74 |
} |
| 75 |
if x != y { |
| 76 |
t.Errorf("ReadUvarint(%d): got %d", x, y) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
var tests = []int64{ |
| 81 |
-1 << 63, |
| 82 |
-1<<63 + 1, |
| 83 |
-1, |
| 84 |
0, |
| 85 |
1, |
| 86 |
2, |
| 87 |
10, |
| 88 |
20, |
| 89 |
63, |
| 90 |
64, |
| 91 |
65, |
| 92 |
127, |
| 93 |
128, |
| 94 |
129, |
| 95 |
255, |
| 96 |
256, |
| 97 |
257, |
| 98 |
1<<63 - 1, |
| 99 |
} |
| 100 |
|
| 101 |
func TestVarint(t *testing.T) { |
| 102 |
for _, x := range tests { |
| 103 |
testVarint(t, x) |
| 104 |
testVarint(t, -x) |
| 105 |
} |
| 106 |
for x := int64(0x7); x != 0; x <<= 1 { |
| 107 |
testVarint(t, x) |
| 108 |
testVarint(t, -x) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestUvarint(t *testing.T) { |
| 113 |
for _, x := range tests { |
| 114 |
testUvarint(t, uint64(x)) |
| 115 |
} |
| 116 |
for x := uint64(0x7); x != 0; x <<= 1 { |
| 117 |
testUvarint(t, x) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestBufferTooSmall(t *testing.T) { |
| 122 |
buf := []byte{0x80, 0x80, 0x80, 0x80} |
| 123 |
for i := 0; i <= len(buf); i++ { |
| 124 |
buf := buf[0:i] |
| 125 |
x, n := Uvarint(buf) |
| 126 |
if x != 0 || n != 0 { |
| 127 |
t.Errorf("Uvarint(%v): got x = %d, n = %d", buf, x, n) |
| 128 |
} |
| 129 |
|
| 130 |
x, err := ReadUvarint(bytes.NewReader(buf)) |
| 131 |
wantErr := io.EOF |
| 132 |
if i > 0 { |
| 133 |
wantErr = io.ErrUnexpectedEOF |
| 134 |
} |
| 135 |
if x != 0 || err != wantErr { |
| 136 |
t.Errorf("ReadUvarint(%v): got x = %d, err = %s", buf, x, err) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Ensure that we catch overflows of bytes going past MaxVarintLen64. |
| 142 |
// See issue https://golang.org/issues/41185 |
| 143 |
func TestBufferTooBigWithOverflow(t *testing.T) { |
| 144 |
tests := []struct { |
| 145 |
in []byte |
| 146 |
name string |
| 147 |
wantN int |
| 148 |
wantValue uint64 |
| 149 |
}{ |
| 150 |
{ |
| 151 |
name: "invalid: 1000 bytes", |
| 152 |
in: func() []byte { |
| 153 |
b := make([]byte, 1000) |
| 154 |
for i := range b { |
| 155 |
b[i] = 0xff |
| 156 |
} |
| 157 |
b[999] = 0 |
| 158 |
return b |
| 159 |
}(), |
| 160 |
wantN: -11, |
| 161 |
wantValue: 0, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "valid: math.MaxUint64-40", |
| 165 |
in: []byte{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, |
| 166 |
wantValue: math.MaxUint64 - 40, |
| 167 |
wantN: 10, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "invalid: with more than MaxVarintLen64 bytes", |
| 171 |
in: []byte{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, |
| 172 |
wantN: -11, |
| 173 |
wantValue: 0, |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "invalid: 10th byte", |
| 177 |
in: []byte{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, |
| 178 |
wantN: -10, |
| 179 |
wantValue: 0, |
| 180 |
}, |
| 181 |
} |
| 182 |
|
| 183 |
for _, tt := range tests { |
| 184 |
t.Run(tt.name, func(t *testing.T) { |
| 185 |
value, n := Uvarint(tt.in) |
| 186 |
if g, w := n, tt.wantN; g != w { |
| 187 |
t.Errorf("bytes returned=%d, want=%d", g, w) |
| 188 |
} |
| 189 |
if g, w := value, tt.wantValue; g != w { |
| 190 |
t.Errorf("value=%d, want=%d", g, w) |
| 191 |
} |
| 192 |
}) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func testOverflow(t *testing.T, buf []byte, x0 uint64, n0 int, err0 error) { |
| 197 |
x, n := Uvarint(buf) |
| 198 |
if x != 0 || n != n0 { |
| 199 |
t.Errorf("Uvarint(% X): got x = %d, n = %d; want 0, %d", buf, x, n, n0) |
| 200 |
} |
| 201 |
|
| 202 |
r := bytes.NewReader(buf) |
| 203 |
len := r.Len() |
| 204 |
x, err := ReadUvarint(r) |
| 205 |
if x != x0 || err != err0 { |
| 206 |
t.Errorf("ReadUvarint(%v): got x = %d, err = %s; want %d, %s", buf, x, err, x0, err0) |
| 207 |
} |
| 208 |
if read := len - r.Len(); read > MaxVarintLen64 { |
| 209 |
t.Errorf("ReadUvarint(%v): read more than MaxVarintLen64 bytes, got %d", buf, read) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestOverflow(t *testing.T) { |
| 214 |
testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, 0, -10, errOverflow) |
| 215 |
testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, 0, -11, errOverflow) |
| 216 |
testOverflow(t, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 1<<64-1, -11, errOverflow) // 11 bytes, should overflow |
| 217 |
} |
| 218 |
|
| 219 |
func TestNonCanonicalZero(t *testing.T) { |
| 220 |
buf := []byte{0x80, 0x80, 0x80, 0} |
| 221 |
x, n := Uvarint(buf) |
| 222 |
if x != 0 || n != 4 { |
| 223 |
t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, 4", buf, x, n) |
| 224 |
|
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func BenchmarkPutUvarint32(b *testing.B) { |
| 229 |
buf := make([]byte, MaxVarintLen32) |
| 230 |
b.SetBytes(4) |
| 231 |
for i := 0; i < b.N; i++ { |
| 232 |
for j := uint(0); j < MaxVarintLen32; j++ { |
| 233 |
PutUvarint(buf, 1<<(j*7)) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func BenchmarkPutUvarint64(b *testing.B) { |
| 239 |
buf := make([]byte, MaxVarintLen64) |
| 240 |
b.SetBytes(8) |
| 241 |
for i := 0; i < b.N; i++ { |
| 242 |
for j := uint(0); j < MaxVarintLen64; j++ { |
| 243 |
PutUvarint(buf, 1<<(j*7)) |
| 244 |
} |
| 245 |
} |
| 246 |
} |