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.
14 func testConstant(t
*testing
.T
, w
uint, max
int) {
15 buf
:= make([]byte, MaxVarintLen64
)
16 n
:= PutUvarint(buf
, 1<<w
-1)
18 t
.Errorf("MaxVarintLen%d = %d; want %d", w
, max
, n
)
22 func TestConstants(t
*testing
.T
) {
23 testConstant(t
, 16, MaxVarintLen16
)
24 testConstant(t
, 32, MaxVarintLen32
)
25 testConstant(t
, 64, MaxVarintLen64
)
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
])
33 t
.Errorf("Varint(%d): got %d", x
, y
)
36 t
.Errorf("Varint(%d): got n = %d; want %d", x
, m
, n
)
39 y
, err
:= ReadVarint(bytes
.NewReader(buf
))
41 t
.Errorf("ReadVarint(%d): %s", x
, err
)
44 t
.Errorf("ReadVarint(%d): got %d", x
, y
)
48 func testUvarint(t
*testing
.T
, x
uint64) {
49 buf
:= make([]byte, MaxVarintLen64
)
50 n
:= PutUvarint(buf
, x
)
51 y
, m
:= Uvarint(buf
[0:n
])
53 t
.Errorf("Uvarint(%d): got %d", x
, y
)
56 t
.Errorf("Uvarint(%d): got n = %d; want %d", x
, m
, n
)
59 y
, err
:= ReadUvarint(bytes
.NewReader(buf
))
61 t
.Errorf("ReadUvarint(%d): %s", x
, err
)
64 t
.Errorf("ReadUvarint(%d): got %d", x
, y
)
89 func TestVarint(t
*testing
.T
) {
90 for _
, x
:= range tests
{
94 for x
:= int64(0x7); x
!= 0; x
<<= 1 {
100 func TestUvarint(t
*testing
.T
) {
101 for _
, x
:= range tests
{
102 testUvarint(t
, uint64(x
))
104 for x
:= uint64(0x7); x
!= 0; x
<<= 1 {
109 func TestBufferTooSmall(t
*testing
.T
) {
110 buf
:= []byte{0x80, 0x80, 0x80, 0x80}
111 for i
:= 0; i
<= len(buf
); i
++ {
114 if x
!= 0 || n
!= 0 {
115 t
.Errorf("Uvarint(%v): got x = %d, n = %d", buf
, x
, n
)
118 x
, err
:= ReadUvarint(bytes
.NewReader(buf
))
119 if x
!= 0 || err
!= io
.EOF
{
120 t
.Errorf("ReadUvarint(%v): got x = %d, err = %s", buf
, x
, err
)
125 // Ensure that we catch overflows of bytes going past MaxVarintLen64.
126 // See issue https://golang.org/issues/41185
127 func TestBufferTooBigWithOverflow(t
*testing
.T
) {
135 name
: "invalid: 1000 bytes",
137 b
:= make([]byte, 1000)
148 name
: "valid: math.MaxUint64-40",
149 in
: []byte{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
150 wantValue
: math
.MaxUint64
- 40,
154 name
: "invalid: with more than MaxVarintLen64 bytes",
155 in
: []byte{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
160 name
: "invalid: 10th byte",
161 in
: []byte{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
167 for _
, tt
:= range tests
{
169 t
.Run(tt
.name
, func(t
*testing
.T
) {
170 value
, n
:= Uvarint(tt
.in
)
171 if g
, w
:= n
, tt
.wantN
; g
!= w
{
172 t
.Errorf("bytes returned=%d, want=%d", g
, w
)
174 if g
, w
:= value
, tt
.wantValue
; g
!= w
{
175 t
.Errorf("value=%d, want=%d", g
, w
)
181 func testOverflow(t
*testing
.T
, buf
[]byte, x0
uint64, n0
int, err0 error
) {
183 if x
!= 0 || n
!= n0
{
184 t
.Errorf("Uvarint(% X): got x = %d, n = %d; want 0, %d", buf
, x
, n
, n0
)
187 r
:= bytes
.NewReader(buf
)
189 x
, err
:= ReadUvarint(r
)
190 if x
!= x0 || err
!= err0
{
191 t
.Errorf("ReadUvarint(%v): got x = %d, err = %s; want %d, %s", buf
, x
, err
, x0
, err0
)
193 if read
:= len - r
.Len(); read
> MaxVarintLen64
{
194 t
.Errorf("ReadUvarint(%v): read more than MaxVarintLen64 bytes, got %d", buf
, read
)
198 func TestOverflow(t
*testing
.T
) {
199 testOverflow(t
, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, 0, -10, overflow
)
200 testOverflow(t
, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, 0, -11, overflow
)
201 testOverflow(t
, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 1<<64-1, -11, overflow
) // 11 bytes, should overflow
204 func TestNonCanonicalZero(t
*testing
.T
) {
205 buf
:= []byte{0x80, 0x80, 0x80, 0}
207 if x
!= 0 || n
!= 4 {
208 t
.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, 4", buf
, x
, n
)
213 func BenchmarkPutUvarint32(b
*testing
.B
) {
214 buf
:= make([]byte, MaxVarintLen32
)
216 for i
:= 0; i
< b
.N
; i
++ {
217 for j
:= uint(0); j
< MaxVarintLen32
; j
++ {
218 PutUvarint(buf
, 1<<(j
*7))
223 func BenchmarkPutUvarint64(b
*testing
.B
) {
224 buf
:= make([]byte, MaxVarintLen64
)
226 for i
:= 0; i
< b
.N
; i
++ {
227 for j
:= uint(0); j
< MaxVarintLen64
; j
++ {
228 PutUvarint(buf
, 1<<(j
*7))