Repository navigation
Expand file tree
/
Copy pathreader_test.go
More file actions
253 lines (224 loc) · 6.48 KB
/
Copy pathreader_test.go
File metadata and controls
253 lines (224 loc) · 6.48 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package binary
import (
"bytes"
"io"
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestReader_Slice(t *testing.T) {
r := newSliceReader([]byte("0123456789"))
out, err := r.Slice(3)
assert.NoError(t, err)
assert.Len(t, out, 3)
assert.Equal(t, "012", string(out))
assert.Equal(t, 7, r.Len())
}
func TestReaderEOF(t *testing.T) {
b, _ := Marshal(newBigStruct())
for size := 0; size < len(b)-1; size++ {
var output bigStruct
assert.Error(t, Unmarshal(b[0:size], &output))
}
}
func TestReaderOverflow(t *testing.T) {
tests := map[string][]byte{
"too long": bytes.Repeat([]byte{0x80}, 10),
"too large": append(bytes.Repeat([]byte{0x80}, 9), 2),
}
for name, data := range tests {
t.Run(name, func(t *testing.T) {
_, err := newSliceReader(data).ReadUvarint()
assert.Equal(t, overflow, err)
})
}
}
func TestBulkUvarints(t *testing.T) {
var out []uint32
assert.NoError(t, Unmarshal([]byte{3, 1, 0x80, 1, 0xff, 1}, &out))
assert.Equal(t, []uint32{1, 128, 255}, out)
assert.Equal(t, io.EOF, Unmarshal([]byte{1, 0x80}, &out))
assert.Equal(t, overflow, Unmarshal(append([]byte{1}, bytes.Repeat([]byte{0x80}, 10)...), &out))
}
func TestBulkUvarintOverflow(t *testing.T) {
tests := map[string][]byte{
"too long": append([]byte{1}, bytes.Repeat([]byte{0x80}, 10)...),
"too large": append([]byte{1}, append(bytes.Repeat([]byte{0x80}, 9), 2)...),
}
for name, data := range tests {
t.Run(name, func(t *testing.T) {
var out []uint64
assert.Equal(t, overflow, Unmarshal(data, &out))
})
}
}
func TestBulkVarints(t *testing.T) {
input := []int32{-64, -1, 0, 1, 64}
data, err := Marshal(&input)
assert.NoError(t, err)
var output []int32
assert.NoError(t, Unmarshal(data, &output))
assert.Equal(t, input, output)
assert.Equal(t, io.EOF, Unmarshal([]byte{1, 0x80}, &output))
assert.Equal(t, overflow, Unmarshal(append([]byte{1}, bytes.Repeat([]byte{0x80}, 10)...), &output))
data = append([]byte{1}, bytes.Repeat([]byte{0x80}, 9)...)
data = append(data, 2)
var overflowOutput []int64
assert.Equal(t, overflow, Unmarshal(data, &overflowOutput))
}
func TestStreamSliceErrors(t *testing.T) {
tests := []struct {
name string
out any
}{
{"int8", new([]int8)},
{"int16", new([]int16)},
{"int32", new([]int32)},
{"int64", new([]int64)},
{"uint16", new([]uint16)},
{"uint32", new([]uint32)},
{"uint64", new([]uint64)},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := NewDecoder(bytes.NewReader([]byte{1})).Decode(tc.out)
assert.Equal(t, io.EOF, err)
})
}
}
func TestStreamReader(t *testing.T) {
input := newBigStruct()
b, _ := Marshal(input)
dec := NewDecoder(newNetworkSource(b))
out := new(bigStruct)
assert.NoError(t, dec.Decode(out))
}
func TestReaderBranches(t *testing.T) {
var nilBuffer *bytes.Buffer
if _, ok := newReader(nilBuffer).(*sliceReader); !ok {
t.Fatal("typed nil buffer did not use the slice reader")
}
d := NewDecoder(bytes.NewReader(nil))
assert.Equal(t, io.ErrUnexpectedEOF, func() error {
_, err := d.Slice(-1)
return err
}())
n := 64<<10 + 1
data, err := NewDecoder(bytes.NewReader(bytes.Repeat([]byte{'x'}, n))).Slice(n)
assert.NoError(t, err)
assert.Len(t, data, n)
_, err = NewDecoder(bytes.NewReader(bytes.Repeat([]byte{'x'}, n-1))).Slice(n)
assert.Equal(t, io.EOF, err)
}
// --------------------------------------- Big Structure (Every Field Type) ---------------------------------------
// structure with every possible codec type
type bigStruct struct {
String string
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Float32 float32
Float64 float64
Strings []string
Bytes []byte
Bools []bool
Uint8s []uint8
Uint16s []uint16
Uint32s []uint32
Uint64s []uint64
Int8s []int8
Int16s []int16
Int32s []int32
Int64s []int64
Float32s []float32
Float64s []float64
MapStr map[string]simpleStruct
MapPtr map[string]*simpleStruct
MapUint8 map[uint8]uint8
MapUint16 map[uint16]uint16
MapUint32 map[uint32]uint32
MapUint64 map[uint64]uint64
MapInt8 map[int8]int8
MapInt16 map[int16]int16
MapInt32 map[int32]int32
MapInt64 map[int64]int64
Time *time.Time
Nil *time.Time
Pointer *simpleStruct
Value simpleStruct
Array [6]byte
Byte byte
Bool bool
}
func newBigStruct() *bigStruct {
timestamp := time.Date(2013, 1, 2, 3, 4, 5, 6, time.UTC)
child := simpleStruct{
Name: "Roman",
Timestamp: timestamp,
Payload: []byte("hi"),
Ssid: []uint32{1, 2, 3},
}
return &bigStruct{
String: "hello",
Byte: 0x3a,
Bool: true,
Uint8: math.MaxUint8,
Uint16: math.MaxUint16,
Uint32: math.MaxUint32,
Uint64: math.MaxUint64,
Int8: math.MaxInt8,
Int16: math.MaxInt16,
Int32: math.MaxInt32,
Int64: math.MaxInt64,
Float32: math.MaxFloat32,
Float64: math.MaxFloat64,
Strings: []string{"a", "b", "c"},
Bytes: []byte("hello-bytes"),
Bools: []bool{true, false, true},
Uint8s: []uint8{0, math.MaxUint8},
Uint16s: []uint16{0, math.MaxUint16},
Uint32s: []uint32{0, math.MaxUint32},
Uint64s: []uint64{0, math.MaxUint64},
Int8s: []int8{math.MinInt8, math.MaxInt8},
Int16s: []int16{math.MinInt16, math.MaxInt16},
Int32s: []int32{math.MinInt32, math.MaxInt32},
Int64s: []int64{math.MinInt64, math.MaxInt64},
Float32s: []float32{0, math.MaxFloat32},
Float64s: []float64{0, math.MaxFloat64},
MapStr: map[string]simpleStruct{"a": child, "b": child},
MapPtr: map[string]*simpleStruct{"a": &child, "b": nil},
MapUint8: map[uint8]uint8{1: 1},
MapUint16: map[uint16]uint16{1: 1},
MapUint32: map[uint32]uint32{1: 1},
MapUint64: map[uint64]uint64{1: 1},
MapInt8: map[int8]int8{1: 1},
MapInt16: map[int16]int16{1: 1},
MapInt32: map[int32]int32{1: 1},
MapInt64: map[int64]int64{1: 1},
Array: [6]byte{1, 2, 3, 4, 5, 6},
Time: ×tamp,
Nil: nil,
Pointer: &child,
Value: child,
}
}
// --------------------------------------- Fake Network Reader ---------------------------------------
type networkSource struct {
r io.Reader
}
func newNetworkSource(data []byte) io.Reader {
return &networkSource{
r: bytes.NewBuffer(data),
}
}
func (s *networkSource) Read(p []byte) (n int, err error) {
return s.r.Read(p)
}