7 "github.com/stretchr/testify/require"
10 func TestReadByteSliceEquality(t *testing.T) {
12 var buf = bytes.NewBuffer(nil)
16 var testBytes = []byte("ThisIsSomeTestArray")
19 WriteByteSlice(testBytes, buf, &n, &err)
23 bufBytes = buf.Bytes()
25 // Read the byteslice, should return the same byteslice
26 buf = bytes.NewBuffer(bufBytes)
28 res := ReadByteSlice(buf, 0, &n2, &err)
33 t.Error("Read bytes did not match write bytes length")
36 if !bytes.Equal(testBytes, res) {
37 t.Error("Returned the wrong bytes")
42 func TestReadByteSliceLimit(t *testing.T) {
44 var buf = bytes.NewBuffer(nil)
48 var testBytes = []byte("ThisIsSomeTestArray")
51 WriteByteSlice(testBytes, buf, &n, &err)
55 bufBytes = buf.Bytes()
57 // Read the byteslice, should work fine with no limit.
58 buf = bytes.NewBuffer(bufBytes)
60 ReadByteSlice(buf, 0, &n2, &err)
65 t.Error("Read bytes did not match write bytes length")
68 // Limit to the byteslice length, should succeed.
69 buf = bytes.NewBuffer(bufBytes)
70 t.Logf("%X", bufBytes)
72 ReadByteSlice(buf, len(bufBytes), &n3, &err)
77 t.Error("Read bytes did not match write bytes length")
80 // Limit to the byteslice length, should succeed.
81 buf = bytes.NewBuffer(bufBytes)
83 ReadByteSlice(buf, len(bufBytes)-1, &n4, &err)
84 if err != ErrBinaryReadOverflow {
85 t.Error("Expected ErrBinaryReadsizeOverflow")
90 func TestPutByteSlice(t *testing.T) {
91 var buf []byte = make([]byte, 1000)
92 var testBytes = []byte("ThisIsSomeTestArray")
93 n, err := PutByteSlice(buf, testBytes)
97 if !bytes.Equal(buf[0:2], []byte{1, 19}) {
98 t.Error("Expected first two bytes to encode varint 19")
101 t.Error("Expected to write 21 bytes")
103 if !bytes.Equal(buf[2:21], testBytes) {
104 t.Error("Expected last 19 bytes to encode string")
106 if !bytes.Equal(buf[21:], make([]byte, 1000-21)) {
107 t.Error("Expected remaining bytes to be zero")
111 func TestGetByteSlice(t *testing.T) {
112 var buf []byte = make([]byte, 1000)
113 var testBytes = []byte("ThisIsSomeTestArray")
114 n, err := PutByteSlice(buf, testBytes)
119 got, n, err := GetByteSlice(buf)
124 t.Error("Expected to read 21 bytes")
126 if !bytes.Equal(got, testBytes) {
127 t.Error("Expected to read %v, got %v", testBytes, got)
132 // + https://github.com/tendermint/go-wire/issues/25
133 // + https://github.com/tendermint/go-wire/issues/37
134 func TestFuzzBinaryLengthOverflowsCaught(t *testing.T) {
135 n, err := int(0), error(nil)
137 bs := ReadBinary(x, bytes.NewReader([]byte{8, 127, 255, 255, 255, 255, 255, 255, 255}), 0, &n, &err)
138 require.Equal(t, err, ErrBinaryReadOverflow, "expected to detect a length overflow")
139 require.Nil(t, bs, "expecting no bytes read out")