-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
310 lines (287 loc) · 8.41 KB
/
decode_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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package arq_test
import (
"bytes"
"encoding/binary"
"io"
"testing"
"time"
"github.com/sholiday/arq"
"github.com/stretchr/testify/assert"
)
func TestDecodeBasic(t *testing.T) {
t.Run("int8", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := int8(42)
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual int8
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("int32", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := int32(42)
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual int32
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("int64", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := int64(42)
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual int64
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("uint8", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := uint8(42)
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual uint8
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("uint32", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := uint32(42)
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual uint32
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("uint64", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := uint64(42)
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual uint64
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("bool", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := true
err := binary.Write(buf, binary.BigEndian, uint8(1))
if !assert.Nil(t, err) {
return
}
var actual bool
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
}
func TestDecodeArray(t *testing.T) {
t.Run("byte-array", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := [4]byte{0xFE, 0xED, 0xFA, 0xCE}
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual [4]byte
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("uint32-array", func(t *testing.T) {
buf := new(bytes.Buffer)
expected := [4]uint32{1, 2, 3, 4}
err := binary.Write(buf, binary.BigEndian, expected)
if !assert.Nil(t, err) {
return
}
var actual [4]uint32
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
}
func TestDecodeString(t *testing.T) {
testCases := []struct {
name string
notNull uint8
length uint64
values []byte
expected string
expectedError error
}{
{"Basic", 1, 4, []byte("arq!"), "arq!", nil},
{"Null", 0, 0, []byte{}, "", nil},
{"NullWithData", 0, 4, []byte("arq!"), "", nil},
{"Length", 1, 3, []byte("arq!"), "arq", nil},
// These should all return an error.
{"Eof", 1, 100, []byte("arq!"), "", io.ErrUnexpectedEOF},
{"TooLarge", 1, 5000, make([]byte, 5000), "", arq.ErrTooLong},
}
for _, tCase := range testCases {
t.Run(tCase.name, func(t *testing.T) {
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, tCase.notNull))
if tCase.notNull == 1 {
assert.Nil(t, binary.Write(buf, binary.BigEndian, tCase.length))
assert.Nil(t, binary.Write(buf, binary.BigEndian, tCase.values))
}
var actual string
err := arq.DecodeArq(buf, &actual)
assert.Equal(t, err, tCase.expectedError)
assert.Equal(t, tCase.expected, actual)
})
}
}
func TestDecodeStruct(t *testing.T) {
type testStruct struct {
B bool
By byte
U32 uint32
}
expected := testStruct{false, 0xFF, 94043}
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, expected))
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
}
func TestDecodeSlice(t *testing.T) {
t.Run("BytesLen32", func(t *testing.T) {
type testStruct struct {
By []byte `arq:"len-uint32"`
}
expected := testStruct{[]byte("arq!")}
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, uint32(4)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, []byte("arq!")))
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("BytesLen64", func(t *testing.T) {
type testStruct struct {
By []byte `arq:"len-uint64"`
}
expected := testStruct{[]byte("arq!")}
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, uint64(4)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, []byte("arq!")))
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("Int32sLen32", func(t *testing.T) {
type testStruct struct {
By []int32 `arq:"len-uint32"`
}
expected := testStruct{[]int32{94040, 94043}}
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, uint32(2)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, int32(94040)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, int32(94043)))
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("Int32sLen64", func(t *testing.T) {
type testStruct struct {
By []int32 `arq:"len-uint64"`
}
expected := testStruct{[]int32{94040, 94043}}
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, uint64(2)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, int32(94040)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, int32(94043)))
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
}
type testUnmarshalable struct {
MyValue string
}
func (ts *testUnmarshalable) UnmarshalArq(r io.Reader) error {
ts.MyValue = "arq!"
return nil
}
func TestDecodeArqUnmarshaler(t *testing.T) {
t.Run("Direct", func(t *testing.T) {
expected := testUnmarshalable{"arq!"}
buf := new(bytes.Buffer)
var actual testUnmarshalable
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
t.Run("StructMember", func(t *testing.T) {
type testStruct struct {
Um testUnmarshalable
}
expected := testStruct{testUnmarshalable{"arq!"}}
buf := new(bytes.Buffer)
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
assert.Equal(t, expected, actual)
})
// TODO(sholiday): Make this test pass.
// Roughly I think we don't handle ptrs as members.
//
// t.Run("StructMemberPtr", func(t *testing.T) {
// type testStruct struct {
// Um *testUnmarshalable
// }
// expected := testStruct{&testUnmarshalable{"arq!"}}
// buf := new(bytes.Buffer)
// var actual testStruct
// assert.Nil(t, arq.DecodeArq(buf, &actual))
// assert.Equal(t, expected, actual)
// })
}
func TestDecodeTime(t *testing.T) {
t.Run("IsNull", func(t *testing.T) {
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, uint8(0)))
var actual time.Time
assert.True(t, actual.IsZero())
})
t.Run("IsNotNull", func(t *testing.T) {
expected := time.Now()
nanos := expected.UnixNano()
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, uint8(1)))
assert.Nil(t, binary.Write(buf, binary.BigEndian, nanos/int64(time.Millisecond)))
var actual time.Time
assert.Nil(t, arq.DecodeArq(buf, &actual))
expectedMs := expected.UnixNano() / int64(time.Millisecond)
actualMs := actual.UnixNano() / int64(time.Millisecond)
assert.Equal(t, expectedMs, actualMs)
})
t.Run("Nsec", func(t *testing.T) {
expected := time.Now()
nanos := expected.UnixNano()
sec := nanos / 1e9
ns := nanos - sec*1e9
assert.Equal(t, expected.Unix(), sec)
buf := new(bytes.Buffer)
assert.Nil(t, binary.Write(buf, binary.BigEndian, expected.Unix()))
assert.Nil(t, binary.Write(buf, binary.BigEndian, ns))
type testStruct struct {
T time.Time `arq:"nsec"`
}
var actual testStruct
assert.Nil(t, arq.DecodeArq(buf, &actual))
expectedMs := expected.UnixNano() / int64(time.Millisecond)
actualMs := actual.T.UnixNano() / int64(time.Millisecond)
assert.Equal(t, expectedMs, actualMs)
})
}