forked from hexdigest/bertlv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbertlv_test.go
597 lines (531 loc) · 12.2 KB
/
bertlv_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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package bertlv
import (
"bytes"
"io"
"sync/atomic"
"testing"
"time"
"github.com/gojuno/minimock"
"github.com/stretchr/testify/assert"
)
func TestEncodeInt(t *testing.T) {
tests := []struct {
name string
in int
want1 []byte
}{
{
name: "one byte",
in: 111,
want1: []byte{111},
},
{
name: "two byte",
in: 411,
want1: []byte{0x01, 0x9b},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1 := EncodeInt(tt.in)
assert.Equal(t, tt.want1, got1, "EncodeInt returned unexpected result")
})
}
}
func TestTagValue_Len(t *testing.T) {
tests := []struct {
name string
tv TagValue
want1 []byte
}{
{
name: "one byte len",
tv: TagValue{V: []byte{1, 2, 3}},
want1: []byte{3},
},
{
name: "tree byte len",
tv: TagValue{V: make([]byte, 132768)},
want1: []byte{0x83, 0x02, 0x06, 0xa0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1 := tt.tv.Len()
assert.Equal(t, tt.want1, got1, "TagValue.Len returned unexpected result")
})
}
}
func TestTagValue_Tag(t *testing.T) {
tests := []struct {
name string
tv TagValue
want1 []byte
}{
{
name: "0xbf0c",
tv: TagValue{T: 0xbf0c},
want1: []byte{0xbf, 0x0c},
},
{
name: "0x4f",
tv: TagValue{T: 0x4f},
want1: []byte{0x4f},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1 := tt.tv.Tag()
assert.Equal(t, tt.want1, got1, "TagValue.Tag returned unexpected result")
})
}
}
func TestReadTag(t *testing.T) {
tests := []struct {
name string
r io.Reader
want1 int
want2 int
wantErr bool
}{
{
name: "0xbf0c",
r: bytes.NewReader([]byte{0xbf, 0x0c}),
want1: 0xbf0c,
want2: 2,
wantErr: false,
},
{
name: "0x4f",
r: bytes.NewReader([]byte{0x4f}),
want1: 0x4f,
want2: 1,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1, got2, err := ReadTag(tt.r)
assert.Equal(t, tt.want1, got1, "ReadTag returned unexpected result")
assert.Equal(t, tt.want2, got2, "ReadTag returned unexpected result")
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestReadLen(t *testing.T) {
type args struct {
r io.Reader
}
tests := []struct {
name string
args func(t minimock.Tester) args
want1 int
want2 int
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
}{
{
name: "one byte",
args: func(t minimock.Tester) args {
return args{r: bytes.NewReader([]byte{0x05})}
},
want1: 5,
want2: 1,
wantErr: false,
},
{
name: "tree byte",
args: func(t minimock.Tester) args {
return args{r: bytes.NewReader([]byte{0x83, 0x02, 0x06, 0xa0})}
},
want1: 132768,
want2: 4,
wantErr: false,
},
{
name: "indefinite length",
args: func(t minimock.Tester) args {
return args{r: bytes.NewReader([]byte{0x80, 0x02, 0x06, 0xa0})}
},
want1: 0,
want2: 1,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, errIndefiniteLength, err)
},
},
{
name: "invalid length",
args: func(t minimock.Tester) args {
return args{r: bytes.NewReader([]byte{0x85, 0x02, 0x06, 0xa0})}
},
want1: 0,
want2: 1,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, errInvalidLength, err)
},
},
{
name: "first read error",
args: func(t minimock.Tester) args {
return args{r: NewReaderMock(t).ReadMock.Return(2, io.EOF)}
},
want1: 0,
want2: 2,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, io.EOF, err)
},
},
{
name: "second read error",
args: func(t minimock.Tester) args {
var calls uint32
return args{r: NewReaderMock(t).ReadMock.Set(func(p []byte) (int, error) {
switch atomic.AddUint32(&calls, 1) {
case 1:
p[0] = 0x83
return 1, nil
case 2:
assert.Len(t, p, 3)
return 0, io.ErrUnexpectedEOF
}
t.Fatalf("too many calls to reader mock")
return 0, nil
})}
},
want1: 0,
want2: 1,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, io.ErrUnexpectedEOF, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := minimock.NewController(t)
defer mc.Finish()
tArgs := tt.args(mc)
got1, got2, err := ReadLen(tArgs.r)
assert.Equal(t, tt.want1, got1, "ReadLen returned unexpected result")
assert.Equal(t, tt.want2, got2, "ReadLen returned unexpected result")
if tt.wantErr {
if assert.Error(t, err) && tt.inspectErr != nil {
tt.inspectErr(err, t)
}
} else {
assert.NoError(t, err)
}
})
}
}
func TestTagValue_WriteTo(t *testing.T) {
type args struct {
w io.Writer
}
tests := []struct {
name string
tv TagValue
args func(t minimock.Tester) args
want1 int64
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
}{
{
name: "success",
tv: TagValue{T: 0x9f38, V: []byte("hello world")},
args: func(t minimock.Tester) args {
return args{
w: NewWriterMock(t).WriteMock.Return(14, nil),
}
},
want1: 14,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := minimock.NewController(t)
defer mc.Wait(time.Second)
tArgs := tt.args(mc)
got1, err := tt.tv.WriteTo(tArgs.w)
assert.Equal(t, tt.want1, got1, "TagValue.WriteTo returned unexpected result")
if tt.wantErr {
if assert.Error(t, err) && tt.inspectErr != nil {
tt.inspectErr(err, t)
}
} else {
assert.NoError(t, err)
}
})
}
}
func TestTagValue_ReadFrom(t *testing.T) {
type args struct {
r io.Reader
}
tests := []struct {
name string
init func(t minimock.Tester) TagValue
inspect func(r TagValue, t *testing.T) //inspects TagValue after execution of ReadFrom
args func(t minimock.Tester) args
want1 int64
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
}{
{
name: "tag read error",
init: func(t minimock.Tester) TagValue { return TagValue{} },
args: func(t minimock.Tester) args {
return args{
r: NewReaderMock(t).ReadMock.Return(0, io.EOF),
}
},
want1: 0,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, io.EOF, err)
},
},
{
name: "len read error",
init: func(t minimock.Tester) TagValue { return TagValue{} },
args: func(t minimock.Tester) args {
var calls uint32
return args{
r: NewReaderMock(t).ReadMock.Set(func(p []byte) (int, error) {
switch atomic.AddUint32(&calls, 1) {
case 1:
p[0] = 0x4f
return 1, nil
case 2:
return 0, io.ErrUnexpectedEOF
}
t.Fatalf("too many calls to reader")
return 0, nil
}),
}
},
want1: 1,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Contains(t, err.Error(), "failed to read length")
},
},
{
name: "success",
init: func(t minimock.Tester) TagValue { return TagValue{} },
args: func(t minimock.Tester) args {
var calls uint32
return args{
r: NewReaderMock(t).ReadMock.Set(func(p []byte) (int, error) {
switch atomic.AddUint32(&calls, 1) {
case 1:
p[0] = 0x4f
return 1, nil
case 2:
p[0] = 11
return 1, nil
case 3:
copy(p, []byte("hello world"))
return 11, nil
}
t.Fatalf("too many calls to reader")
return 0, nil
}),
}
},
want1: 13,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := minimock.NewController(t)
defer mc.Wait(time.Second)
tArgs := tt.args(mc)
receiver := tt.init(mc)
got1, err := receiver.ReadFrom(tArgs.r)
if tt.inspect != nil {
tt.inspect(receiver, t)
}
assert.Equal(t, tt.want1, got1, "TagValue.ReadFrom returned unexpected result")
if tt.wantErr {
if assert.Error(t, err) && tt.inspectErr != nil {
tt.inspectErr(err, t)
}
} else {
assert.NoError(t, err)
}
})
}
}
func TestDecode(t *testing.T) {
tests := []struct {
name string
p []byte
want1 []TagValue
wantErr bool
inspectErr func(err error, t *testing.T)
}{
{
name: "unexpected EOF",
p: []byte{0x4f, 0x05, 0, 1, 2},
want1: nil,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, io.ErrUnexpectedEOF, err)
},
},
{
name: "EOF",
p: []byte{0x4f, 0x03, 0, 1, 2},
want1: []TagValue{{T: 0x4f, V: []byte{0, 1, 2}}},
wantErr: false,
},
{
name: "multiple records",
p: []byte{0x4f, 0x03, 0, 1, 2, 0x4f, 0x02, 0, 1},
want1: []TagValue{
{T: 0x4f, V: []byte{0, 1, 2}},
{T: 0x4f, V: []byte{0, 1}},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := minimock.NewController(t)
defer mc.Wait(time.Second)
got1, err := Decode(tt.p)
assert.Equal(t, tt.want1, got1, "Decode returned unexpected result")
if tt.wantErr {
if assert.Error(t, err) && tt.inspectErr != nil {
tt.inspectErr(err, t)
}
} else {
assert.NoError(t, err)
}
})
}
}
func TestFind(t *testing.T) {
//p, err := hex.DecodeString("a528500a566973612044656269745f2d047275656e9f380f9f66049f02069f37045f2a029f1a02bf0c00")
//p, err := hex.DecodeString("500a566973612044656269745f2d047275656e9f380f9f66049f02069f37045f2a029f1a02bf0c00")
//p, err := hex.DecodeString("500a56697361204465626974")
//p, err := hex.DecodeString("5f2d047275656e9f380f9f66049f02069f37045f2a029f1a02bf0c00")
//require.NoError(t, err)
tests := []struct {
name string
tag int
p []byte
want1 *TagValue
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
}{
{
name: "decode error",
tag: 0x2f,
p: []byte{0x2f, 0x05, 0x00, 0x00},
want1: nil,
wantErr: true,
},
{
name: "no tag found",
tag: 0x2f,
p: []byte{0x4f, 0x02, 0x00, 0x00},
want1: nil,
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, ErrNotFound, err)
},
},
{
name: "tag found",
tag: 0x4f,
p: []byte{0x4f, 0x02, 0x00, 0x00},
want1: &TagValue{T: 0x4f, V: []byte{0, 0}},
wantErr: false,
},
{
name: "tag found recursively",
tag: 0x4f,
p: []byte{0xbf, 0x0c, 0x04, 0x4f, 0x02, 55, 44},
want1: &TagValue{T: 0x4f, V: []byte{55, 44}},
wantErr: false,
},
{
name: "error during recursive search",
tag: 0x4f,
p: []byte{0xbf, 0x0c, 0x04, 0x4f, 0x05, 55, 44},
want1: nil,
wantErr: true,
},
/*
{
name: "EOF",
p: p,
want1: nil,
wantErr: false,
},
*/
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := minimock.NewController(t)
defer mc.Wait(time.Second)
got1, err := Find(tt.tag, tt.p)
assert.Equal(t, tt.want1, got1, "Find returned unexpected result")
if tt.wantErr {
if assert.Error(t, err) && tt.inspectErr != nil {
tt.inspectErr(err, t)
}
} else {
assert.NoError(t, err)
}
})
}
}
func TestTagValue_MarshalJSON(t *testing.T) {
tests := []struct {
name string
tv TagValue
want1 []byte
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
}{
{
name: "leaf",
tv: TagValue{T: 0x4f, V: []byte{0x12, 0x34}},
want1: []byte(`{"4f":"1234"}`),
wantErr: false,
},
{
name: "recursive",
tv: TagValue{T: 0xbf0c, V: []byte{0x4f, 0x02, 0x12, 0x34}},
want1: []byte(`{"bf0c":[{"4f":"1234"}]}`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := minimock.NewController(t)
defer mc.Wait(time.Second)
got1, err := tt.tv.MarshalJSON()
assert.Equal(t, tt.want1, got1, "TagValue.MarshalJSON returned unexpected result")
if tt.wantErr {
if assert.Error(t, err) && tt.inspectErr != nil {
tt.inspectErr(err, t)
}
} else {
assert.NoError(t, err)
}
})
}
}