This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
forked from free5gc/tlv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode_test.go
98 lines (93 loc) · 1.98 KB
/
encode_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
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0
package tlv
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestMarshal(t *testing.T) {
testCases := []struct {
name string
instance interface{}
expectHex string
}{
{
name: "int64",
instance: struct {
Value int64 `tlv:"30"`
}{Value: 32324},
expectHex: "001E00080000000000007E44",
},
{
name: "int32",
instance: struct {
Value int32 `tlv:"31"`
}{Value: 32324},
expectHex: "001F000400007E44",
},
{
name: "int16",
instance: struct {
Value int16 `tlv:"32"`
}{Value: 32324},
expectHex: "002000027E44",
},
{
name: "int8",
instance: struct {
Value int8 `tlv:"33"`
}{Value: 68},
expectHex: "0021000144",
},
{
name: "slice of struct",
instance: struct {
List []struct {
Name []byte `tlv:"20"`
Sequence uint16 `tlv:"40"`
} `tlv:"80"`
}{
List: []struct {
Name []byte `tlv:"20"`
Sequence uint16 `tlv:"40"`
}{
{Name: []byte("Hello"), Sequence: 1},
{Name: []byte("World"), Sequence: 2},
{Name: []byte("free5gc"), Sequence: 3},
},
},
expectHex: "0050000F0014000548656C6C6F002800020001" +
"0050000F00140005576F726C64002800020002" +
"005000110014000766726565356763002800020003",
},
{
name: "slice of binary",
instance: struct {
List []BinaryMarshalTest `tlv:"123"`
}{
List: []BinaryMarshalTest{
{
Value: 1100,
},
{
Value: 1200,
},
{
Value: 3244,
},
},
},
expectHex: "007B000431313030007B000431323030007B000433323434",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// testInstance := reflect.New(reflect.TypeOf(tc.instance)).Interface()
buf, err := Marshal(tc.instance)
require.NoError(t, err)
require.Equal(t, tc.expectHex, fmt.Sprintf("%X", buf))
})
}
}