-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder_test.go
More file actions
444 lines (404 loc) · 11.2 KB
/
decoder_test.go
File metadata and controls
444 lines (404 loc) · 11.2 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package doge
import (
"bytes"
"testing"
)
func TestDecodeBytes(t *testing.T) {
stream := Decode(hx2b("01020304"))
if !bytes.Equal(stream.Bytes(4), hx2b("01020304")) {
t.Errorf("Bytes: wrong value: %x", stream.Bytes(2))
}
if !stream.Complete() {
t.Errorf("Bytes: stream not complete")
}
}
func TestDecodeUInt16(t *testing.T) {
stream := Decode(hx2b("0102"))
if stream.UInt16() != 0x0201 {
t.Errorf("UInt16: wrong value: %x", stream.UInt16())
}
if !stream.Complete() {
t.Errorf("UInt16: stream not complete")
}
}
func TestDecodeUInt32(t *testing.T) {
stream := Decode(hx2b("01020304"))
if stream.UInt32() != 0x04030201 {
t.Errorf("UInt32: wrong value: %x", stream.UInt32())
}
if !stream.Complete() {
t.Errorf("UInt32: stream not complete")
}
}
func TestDecodeUInt64(t *testing.T) {
stream := Decode(hx2b("0102030405060708"))
if stream.UInt64() != 0x0807060504030201 {
t.Errorf("UInt64: wrong value: %x", stream.UInt64())
}
if !stream.Complete() {
t.Errorf("UInt64: stream not complete")
}
}
func TestDecodeVarUint(t *testing.T) {
// Test VarUint with 1 byte
stream := Decode(hx2b("01"))
if stream.VarUInt() != 0x01 {
t.Errorf("VarUInt: wrong value: %x", stream.VarUInt())
}
if !stream.Complete() {
t.Errorf("VarUInt: stream not complete")
}
}
func TestDecodeVarUint2(t *testing.T) {
// Test VarUint with 2 bytes
stream := Decode(hx2b("FD0102"))
if stream.VarUInt() != 0x0201 {
t.Errorf("VarUInt: wrong value: %x", stream.VarUInt())
}
if !stream.Complete() {
t.Errorf("VarUInt: stream not complete")
}
}
func TestDecodeVarUint3(t *testing.T) {
// Test VarUint with 4 bytes
stream := Decode(hx2b("FE01020304"))
if stream.VarUInt() != 0x04030201 {
t.Errorf("VarUInt: wrong value: %x", stream.VarUInt())
}
if !stream.Complete() {
t.Errorf("VarUInt: stream not complete")
}
}
func TestDecodeVarUint4(t *testing.T) {
// Test VarUint with 8 bytes
stream := Decode(hx2b("FF0102030405060708"))
if stream.VarUInt() != 0x0807060504030201 {
t.Errorf("VarUInt: wrong value: %x", stream.VarUInt())
}
if !stream.Complete() {
t.Errorf("VarUInt: stream not complete")
}
}
func TestDecodeOverrunBytes(t *testing.T) {
// Test overrun of bytes
stream := Decode(hx2b("01020304"))
if stream.Bytes(5) != nil {
t.Errorf("Bytes: should return nil for overrun")
}
if stream.Valid() {
t.Errorf("Bytes: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("Bytes: stream should not be complete for overrun")
}
}
func TestDecodeOverrunUInt16(t *testing.T) {
// Test overrun of UInt16
stream := Decode(hx2b("01"))
if stream.UInt16() != 0 {
t.Errorf("UInt16: stream should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("UInt16: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("UInt16: stream should not be complete for overrun")
}
}
func TestDecodeOverrunUInt32(t *testing.T) {
// Test overrun of UInt32
stream := Decode(hx2b("010203"))
if stream.UInt32() != 0 {
t.Errorf("UInt32: stream should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("UInt32: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("UInt32: stream should not be complete for overrun")
}
}
func TestDecodeOverrunUInt64(t *testing.T) {
// Test overrun of UInt64
stream := Decode(hx2b("01020304050607"))
if stream.UInt64() != 0 {
t.Errorf("UInt64: stream should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("UInt64: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("UInt64: stream should not be complete for overrun")
}
}
func TestDecodeOverrunVarUInt(t *testing.T) {
// Test overrun of VarUInt
stream := Decode([]byte{})
if stream.VarUInt() != 0 {
t.Errorf("VarUInt: should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("VarUInt: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("VarUInt: stream should not be complete for overrun")
}
}
func TestDecodeBool(t *testing.T) {
// Test Bool with true value
stream := Decode(hx2b("01"))
if !stream.Bool() {
t.Errorf("Bool: wrong value: expected true, got false")
}
if !stream.Complete() {
t.Errorf("Bool: stream not complete")
}
}
func TestDecodeBoolFalse(t *testing.T) {
// Test Bool with false value
stream := Decode(hx2b("00"))
if stream.Bool() {
t.Errorf("Bool: wrong value: expected false, got true")
}
if !stream.Complete() {
t.Errorf("Bool: stream not complete")
}
}
func TestDecodeUInt8(t *testing.T) {
stream := Decode(hx2b("42"))
if stream.UInt8() != 0x42 {
t.Errorf("UInt8: wrong value: %x", stream.UInt8())
}
if !stream.Complete() {
t.Errorf("UInt8: stream not complete")
}
}
func TestDecodeInt64(t *testing.T) {
stream := Decode(hx2b("0102030405060708"))
if stream.Int64() != 0x0807060504030201 {
t.Errorf("Int64: wrong value: %x", stream.Int64())
}
if !stream.Complete() {
t.Errorf("Int64: stream not complete")
}
}
func TestDecodeInt64Negative(t *testing.T) {
// Test negative value (0xFFFFFFFFFFFFFFFF = -1 in two's complement)
stream := Decode(hx2b("FFFFFFFFFFFFFFFF"))
if stream.Int64() != -1 {
t.Errorf("Int64: wrong value: expected -1, got %d", stream.Int64())
}
if !stream.Complete() {
t.Errorf("Int64: stream not complete")
}
}
func TestDecodeTag4CC(t *testing.T) {
stream := Decode(hx2b("41424344")) // "ABCD" in big-endian
if stream.Tag4CC() != 0x41424344 {
t.Errorf("Tag4CC: wrong value: %x", stream.Tag4CC())
}
if !stream.Complete() {
t.Errorf("Tag4CC: stream not complete")
}
}
func TestDecodeVarString(t *testing.T) {
// Test VarString with "hello" (length 5 + "hello")
stream := Decode(hx2b("0568656C6C6F"))
if stream.VarString() != "hello" {
t.Errorf("VarString: wrong value: expected 'hello', got '%s'", stream.VarString())
}
if !stream.Complete() {
t.Errorf("VarString: stream not complete")
}
}
func TestDecodeVarStringEmpty(t *testing.T) {
// Test VarString with empty string
stream := Decode(hx2b("00"))
if stream.VarString() != "" {
t.Errorf("VarString: wrong value: expected empty string, got '%s'", stream.VarString())
}
if !stream.Complete() {
t.Errorf("VarString: stream not complete")
}
}
func TestDecodePadString(t *testing.T) {
// Test PadString with "hello" padded to 8 bytes with nulls
stream := Decode(hx2b("68656C6C6F000000")) // "hello\0\0\0"
if stream.PadString(8) != "hello" {
t.Errorf("PadString: wrong value: expected 'hello', got '%s'", stream.PadString(8))
}
if !stream.Complete() {
t.Errorf("PadString: stream not complete")
}
}
func TestDecodePadStringNoPadding(t *testing.T) {
// Test PadString with no padding
stream := Decode(hx2b("68656C6C6F")) // "hello"
if stream.PadString(5) != "hello" {
t.Errorf("PadString: wrong value: expected 'hello', got '%s'", stream.PadString(5))
}
if !stream.Complete() {
t.Errorf("PadString: stream not complete")
}
}
func TestDecodePadStringEmpty(t *testing.T) {
// Test PadString with empty string (all nulls)
stream := Decode(hx2b("00000000")) // all nulls
result := stream.PadString(4)
if result != "" {
t.Errorf("PadString: wrong value: expected empty string, got '%s' (len=%d, bytes=%x)", result, len(result), []byte(result))
}
if !stream.Complete() {
t.Errorf("PadString: stream not complete")
}
}
func TestDecodeRest(t *testing.T) {
stream := Decode(hx2b("0102030405"))
// Read first 2 bytes
stream.Bytes(2)
// Read rest
rest := stream.Rest()
if !bytes.Equal(rest, hx2b("030405")) {
t.Errorf("Rest: wrong value: %x", rest)
}
if !stream.Complete() {
t.Errorf("Rest: stream not complete")
}
}
func TestDecodeRestEmpty(t *testing.T) {
stream := Decode(hx2b("0102030405"))
// Read all bytes first
stream.Bytes(5)
// Read rest (should be empty)
rest := stream.Rest()
if len(rest) != 0 {
t.Errorf("Rest: expected empty, got %x", rest)
}
if !stream.Complete() {
t.Errorf("Rest: stream not complete")
}
}
func TestDecodeHas(t *testing.T) {
stream := Decode(hx2b("0102030405"))
if !stream.Has(5) {
t.Errorf("Has: should have 5 bytes")
}
if !stream.Has(3) {
t.Errorf("Has: should have 3 bytes")
}
if stream.Has(6) {
t.Errorf("Has: should not have 6 bytes")
}
}
func TestDecodeError(t *testing.T) {
stream := Decode(hx2b("0102030405"))
// Read some bytes but not all
stream.Bytes(3)
type TestStruct struct{}
var testStruct TestStruct
err := stream.Error(testStruct)
if err == nil {
t.Errorf("Error: should return error for incomplete stream")
}
if err.Error() != "wrong encoded size for: TestStruct (need 3 found 5)" {
t.Errorf("Error: wrong error message: %s", err.Error())
}
}
func TestDecodeErrorComplete(t *testing.T) {
stream := Decode(hx2b("0102030405"))
// Read all bytes
stream.Bytes(5)
type TestStruct struct{}
var testStruct TestStruct
err := stream.Error(testStruct)
if err != nil {
t.Errorf("Error: should not return error for complete stream: %s", err.Error())
}
}
func TestDecodeErrorPointer(t *testing.T) {
stream := Decode(hx2b("0102030405"))
// Read some bytes but not all
stream.Bytes(3)
type TestStruct struct{}
var testStruct *TestStruct
err := stream.Error(testStruct)
if err == nil {
t.Errorf("Error: should return error for incomplete stream")
}
if err.Error() != "wrong encoded size for: TestStruct (need 3 found 5)" {
t.Errorf("Error: wrong error message: %s", err.Error())
}
}
// Overrun tests for missing methods
func TestDecodeOverrunBool(t *testing.T) {
stream := Decode([]byte{})
if stream.Bool() {
t.Errorf("Bool: should return false for overrun")
}
if stream.Valid() {
t.Errorf("Bool: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("Bool: stream should not be complete for overrun")
}
}
func TestDecodeOverrunUInt8(t *testing.T) {
stream := Decode([]byte{})
if stream.UInt8() != 0 {
t.Errorf("UInt8: should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("UInt8: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("UInt8: stream should not be complete for overrun")
}
}
func TestDecodeOverrunInt64(t *testing.T) {
stream := Decode(hx2b("01020304050607"))
if stream.Int64() != 0 {
t.Errorf("Int64: should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("Int64: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("Int64: stream should not be complete for overrun")
}
}
func TestDecodeOverrunTag4CC(t *testing.T) {
stream := Decode(hx2b("010203"))
if stream.Tag4CC() != 0 {
t.Errorf("Tag4CC: should return 0 for overrun")
}
if stream.Valid() {
t.Errorf("Tag4CC: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("Tag4CC: stream should not be complete for overrun")
}
}
func TestDecodeOverrunVarString(t *testing.T) {
stream := Decode([]byte{})
if stream.VarString() != "" {
t.Errorf("VarString: should return empty string for overrun")
}
if stream.Valid() {
t.Errorf("VarString: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("VarString: stream should not be complete for overrun")
}
}
func TestDecodeOverrunPadString(t *testing.T) {
stream := Decode(hx2b("0102"))
if stream.PadString(5) != "" {
t.Errorf("PadString: should return empty string for overrun")
}
if stream.Valid() {
t.Errorf("PadString: stream should not be valid for overrun")
}
if stream.Complete() {
t.Errorf("PadString: stream should not be complete for overrun")
}
}