-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlv_shorthand_test.go
69 lines (61 loc) · 2.25 KB
/
tlv_shorthand_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
package bertlv
import (
"github.com/euicc-go/bertlv/primitive"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewValue(t *testing.T) {
tlv := NewValue(Primitive.ContextSpecific(0), []byte{0xff})
assert.Equal(t, Tag{0x80}, tlv.Tag)
assert.Len(t, tlv.Value, 1)
assert.Len(t, tlv.Children, 0)
assert.Equal(t, []byte{0x80, 0x01, 0xff}, tlv.Bytes())
assert.Panics(t, func() { NewValue(Constructed.ContextSpecific(0), nil) })
}
func TestNewChildren(t *testing.T) {
tlv := NewChildren(Constructed.ContextSpecific(0))
assert.Equal(t, Tag{0xa0}, tlv.Tag)
assert.Len(t, tlv.Value, 0)
assert.Equal(t, []byte{0xa0, 0x00}, tlv.Bytes())
assert.Panics(t, func() { NewChildren(Primitive.ContextSpecific(0)) })
}
func TestNewChildrenIter(t *testing.T) {
tlv := NewChildrenIter(Constructed.ContextSpecific(0), func(yield func(*TLV) bool) {
yield(NewValue(Primitive.ContextSpecific(0), []byte{0xff}))
yield(NewValue(Primitive.ContextSpecific(1), []byte{0xff}))
})
assert.Equal(t, Tag{0xa0}, tlv.Tag)
assert.Len(t, tlv.Value, 0)
assert.Equal(t, []byte{0xa0, 0x6, 0x80, 0x1, 0xff, 0x81, 0x1, 0xff}, tlv.Bytes())
}
func TestMarshalValue(t *testing.T) {
tlv, err := MarshalValue(
Primitive.ContextSpecific(0),
primitive.MarshalInt[int8](-1),
)
assert.NoError(t, err)
assert.Equal(t, Tag{0x80}, tlv.Tag)
assert.Len(t, tlv.Value, 1)
assert.Len(t, tlv.Children, 0)
assert.Equal(t, []byte{0x80, 0x01, 0xff}, tlv.Bytes())
}
func TestTLV_MarshalValue(t *testing.T) {
tlv := NewValue(Primitive.ContextSpecific(0), nil)
assert.NoError(t, tlv.MarshalValue(primitive.MarshalInt[int8](-1)))
assert.Equal(t, []byte{0x80, 0x01, 0xff}, tlv.Bytes())
var value int8
assert.NoError(t, tlv.UnmarshalValue(primitive.UnmarshalInt[int8](&value)))
assert.Equal(t, int8(-1), value)
}
func TestTLV_MarshalValueError(t *testing.T) {
tlv := NewChildren(Constructed.ContextSpecific(0))
var value int8
assert.Error(t, tlv.MarshalValue(primitive.MarshalInt[int8](value)))
assert.Error(t, tlv.UnmarshalValue(primitive.UnmarshalInt[int8](&value)))
}
func TestTLV_String(t *testing.T) {
tlv := NewValue(Primitive.ContextSpecific(0), []byte{0xff})
assert.Equal(t, "[0] (1 byte)", tlv.String())
tlv = NewChildren(Constructed.ContextSpecific(0))
assert.Equal(t, "[0] (0 elem)", tlv.String())
}