OSDN Git Service

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