|
1
|
|
|
package decode |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"bytes" |
|
5
|
|
|
"testing" |
|
6
|
|
|
|
|
7
|
|
|
"github.com/et-nik/binngo/binn" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/stretchr/testify/assert" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
func TestDecodeIntList(t *testing.T) { |
|
13
|
|
|
b := []byte{ |
|
14
|
|
|
binn.ListType, // [type] list (container) |
|
15
|
|
|
0x0B, // [size] container total size |
|
16
|
|
|
0x03, // [count] items |
|
17
|
|
|
0x20, // [type] = uint8 |
|
18
|
|
|
0x7B, // [data] (123) |
|
19
|
|
|
0x41, // [type] = int16 |
|
20
|
|
|
0xFE, 0x38, // [data] (-456) |
|
21
|
|
|
0x40, // [type] = uint16 |
|
22
|
|
|
0x03, 0x15, // [data] (789) |
|
23
|
|
|
} |
|
24
|
|
|
v := []int{} |
|
25
|
|
|
r := bytes.NewReader(b) |
|
26
|
|
|
|
|
27
|
|
|
err := decode(r, &v) |
|
28
|
|
|
|
|
29
|
|
|
assert.Nil(t, err) |
|
30
|
|
|
assert.Equal(t, []int{123, -456, 789}, v) |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
func TestDecodeStringList(t *testing.T) { |
|
34
|
|
|
b := []byte{ |
|
35
|
|
|
binn.ListType, // [type] list (container) |
|
36
|
|
|
23, // [size] container total size |
|
37
|
|
|
0x02, // [count] items |
|
38
|
|
|
binn.StringType, // [type] = string |
|
39
|
|
|
0x05, // [size] |
|
40
|
|
|
'h', 'e', 'l', 'l', 'o', 0x00, // [data] (null terminated) |
|
41
|
|
|
binn.StringType, // [type] = string |
|
42
|
|
|
0x05, // [size] |
|
43
|
|
|
'w', 'o', 'r', 'l', 'd', 0x00, // [data] (null terminated) |
|
44
|
|
|
} |
|
45
|
|
|
v := []string{} |
|
46
|
|
|
r := bytes.NewReader(b) |
|
47
|
|
|
|
|
48
|
|
|
err := decode(r, &v) |
|
49
|
|
|
|
|
50
|
|
|
assert.Nil(t, err) |
|
51
|
|
|
assert.Equal(t, []string{"hello", "world"}, v) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
func TestDecodeIterfaceList(t *testing.T) { |
|
55
|
|
|
b := []byte{ |
|
56
|
|
|
binn.ListType, // [type] list (container) |
|
57
|
|
|
26, // [size] container total size |
|
58
|
|
|
0x03, // [count] items |
|
59
|
|
|
binn.StringType, // [type] = string |
|
60
|
|
|
0x05, // [size] |
|
61
|
|
|
'h', 'e', 'l', 'l', 'o', 0x00, // [data] (null terminated) |
|
62
|
|
|
binn.StringType, // [type] = string |
|
63
|
|
|
0x05, // [size] |
|
64
|
|
|
'w', 'o', 'r', 'l', 'd', 0x00, // [data] (null terminated) |
|
65
|
|
|
0x40, // [type] = uint16 |
|
66
|
|
|
0x03, 0x15, // [data] (789) |
|
67
|
|
|
} |
|
68
|
|
|
var v []interface{} |
|
69
|
|
|
r := bytes.NewReader(b) |
|
70
|
|
|
|
|
71
|
|
|
err := decode(r, &v) |
|
72
|
|
|
|
|
73
|
|
|
assert.Nil(t, err) |
|
74
|
|
|
assert.Equal(t, []interface{}{"hello", "world", uint16(789)}, v) |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func TestDecodeStringMap(t *testing.T) { |
|
78
|
|
|
b := []byte{ |
|
79
|
|
|
0xE1, // [type] map (container) |
|
80
|
|
|
0x1A, // [size] container total size |
|
81
|
|
|
0x02, // [count] key/value pairs |
|
82
|
|
|
0x00, 0x00, 0x00, 0x01, // key |
|
83
|
|
|
0xA0, // [type] = string |
|
84
|
|
|
0x03, // [size] |
|
85
|
|
|
'a', 'd', 'd', 0x00, // [data] (null terminated) |
|
86
|
|
|
0x00, 0x00, 0x00, 0x02, // key |
|
87
|
|
|
0xE0, // [type] list (container) |
|
88
|
|
|
0x09, // [size] container total size |
|
89
|
|
|
0x02, // [count] items |
|
90
|
|
|
0x41, // [type] = int16 |
|
91
|
|
|
0xCF, 0xC7, // [data] (-12345) |
|
92
|
|
|
0x40, // [type] = uint16 |
|
93
|
|
|
0x1A, 0x85, // [data] (6789) |
|
94
|
|
|
} |
|
95
|
|
|
v := map[int]interface{}{} |
|
96
|
|
|
r := bytes.NewReader(b) |
|
97
|
|
|
|
|
98
|
|
|
err := decode(r, &v) |
|
99
|
|
|
|
|
100
|
|
|
assert.Nil(t, err) |
|
101
|
|
|
assert.Equal(t, map[int]interface{}{ |
|
102
|
|
|
1: "add", |
|
103
|
|
|
2: []interface{}{int16(-12345), uint16(6789)}, |
|
104
|
|
|
}, v) |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
func TestDecodeStringObjectToStruct(t *testing.T) { |
|
108
|
|
|
b := []byte{ |
|
109
|
|
|
0xE2, // type = object (container) |
|
110
|
|
|
0x11, // container total size |
|
111
|
|
|
0x01, // key/value pairs count |
|
112
|
|
|
0x05, 'h', 'e', 'l', 'l', 'o', // key |
|
113
|
|
|
0xA0, // type = string |
|
114
|
|
|
0x05, 'w', 'o', 'r', 'l', 'd', 0x00, // value (null terminated) |
|
115
|
|
|
} |
|
116
|
|
|
type ts struct { |
|
117
|
|
|
Hello string `binn:"hello"` |
|
118
|
|
|
} |
|
119
|
|
|
var v ts |
|
120
|
|
|
r := bytes.NewReader(b) |
|
121
|
|
|
|
|
122
|
|
|
err := decode(r, &v) |
|
123
|
|
|
|
|
124
|
|
|
assert.Nil(t, err) |
|
125
|
|
|
assert.Equal(t, ts{Hello: "world"}, v) |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
func TestDecodeStringObjectToMap(t *testing.T) { |
|
129
|
|
|
b := []byte{ |
|
130
|
|
|
0xE2, // type = object (container) |
|
131
|
|
|
0x11, // container total size |
|
132
|
|
|
0x01, // key/value pairs count |
|
133
|
|
|
0x05, 'h', 'e', 'l', 'l', 'o', // key |
|
134
|
|
|
0xA0, // type = string |
|
135
|
|
|
0x05, 'w', 'o', 'r', 'l', 'd', 0x00, // value (null terminated) |
|
136
|
|
|
} |
|
137
|
|
|
m := map[string]string{} |
|
138
|
|
|
r := bytes.NewReader(b) |
|
139
|
|
|
|
|
140
|
|
|
err := decode(r, &m) |
|
141
|
|
|
|
|
142
|
|
|
assert.Nil(t, err) |
|
143
|
|
|
assert.Equal(t, map[string]string{"hello": "world"}, m) |
|
144
|
|
|
} |
|
145
|
|
|
|