forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
byteslice_test.go
140 lines (121 loc) · 3.2 KB
/
byteslice_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package wire
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadByteSliceEquality(t *testing.T) {
var buf = bytes.NewBuffer(nil)
var bufBytes []byte
// Write a byteslice
var testBytes = []byte("ThisIsSomeTestArray")
var n int
var err error
WriteByteSlice(testBytes, buf, &n, &err)
if err != nil {
t.Error(err.Error())
}
bufBytes = buf.Bytes()
// Read the byteslice, should return the same byteslice
buf = bytes.NewBuffer(bufBytes)
var n2 int
res := ReadByteSlice(buf, 0, &n2, &err)
if err != nil {
t.Error(err.Error())
}
if n != n2 {
t.Error("Read bytes did not match write bytes length")
}
if !bytes.Equal(testBytes, res) {
t.Error("Returned the wrong bytes")
}
}
func TestReadByteSliceLimit(t *testing.T) {
var buf = bytes.NewBuffer(nil)
var bufBytes []byte
// Write a byteslice
var testBytes = []byte("ThisIsSomeTestArray")
var n int
var err error
WriteByteSlice(testBytes, buf, &n, &err)
if err != nil {
t.Error(err.Error())
}
bufBytes = buf.Bytes()
// Read the byteslice, should work fine with no limit.
buf = bytes.NewBuffer(bufBytes)
var n2 int
ReadByteSlice(buf, 0, &n2, &err)
if err != nil {
t.Error(err.Error())
}
if n != n2 {
t.Error("Read bytes did not match write bytes length")
}
// Limit to the byteslice length, should succeed.
buf = bytes.NewBuffer(bufBytes)
t.Logf("%X", bufBytes)
var n3 int
ReadByteSlice(buf, len(bufBytes), &n3, &err)
if err != nil {
t.Error(err.Error())
}
if n != n3 {
t.Error("Read bytes did not match write bytes length")
}
// Limit to the byteslice length, should succeed.
buf = bytes.NewBuffer(bufBytes)
var n4 int
ReadByteSlice(buf, len(bufBytes)-1, &n4, &err)
if err != ErrBinaryReadOverflow {
t.Error("Expected ErrBinaryReadsizeOverflow")
}
}
func TestPutByteSlice(t *testing.T) {
var buf []byte = make([]byte, 1000)
var testBytes = []byte("ThisIsSomeTestArray")
n, err := PutByteSlice(buf, testBytes)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(buf[0:2], []byte{1, 19}) {
t.Error("Expected first two bytes to encode varint 19")
}
if n != 21 {
t.Error("Expected to write 21 bytes")
}
if !bytes.Equal(buf[2:21], testBytes) {
t.Error("Expected last 19 bytes to encode string")
}
if !bytes.Equal(buf[21:], make([]byte, 1000-21)) {
t.Error("Expected remaining bytes to be zero")
}
}
func TestGetByteSlice(t *testing.T) {
var buf []byte = make([]byte, 1000)
var testBytes = []byte("ThisIsSomeTestArray")
n, err := PutByteSlice(buf, testBytes)
if err != nil {
t.Error(err.Error())
}
got, n, err := GetByteSlice(buf)
if err != nil {
t.Error(err.Error())
}
if n != 21 {
t.Error("Expected to read 21 bytes")
}
if !bytes.Equal(got, testBytes) {
t.Errorf("Expected to read %v, got %v", testBytes, got)
}
}
// Issues:
// + https://github.com/tendermint/go-wire/issues/25
// + https://github.com/tendermint/go-wire/issues/37
func TestFuzzBinaryLengthOverflowsCaught(t *testing.T) {
n, err := int(0), error(nil)
var x []byte
bs := ReadBinary(x, bytes.NewReader([]byte{8, 127, 255, 255, 255, 255, 255, 255, 255}), 0, &n, &err)
require.Equal(t, err, ErrBinaryReadOverflow, "expected to detect a length overflow")
require.Nil(t, bs, "expecting no bytes read out")
}