|
1
|
|
|
package decode_test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"reflect" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/et-nik/binngo/binn" |
|
9
|
|
|
"github.com/et-nik/binngo/decode" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/stretchr/testify/assert" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
func TestUnknownValueError_ExpectedBool(t *testing.T) { |
|
15
|
|
|
b := []byte{binn.True} |
|
16
|
|
|
var r int |
|
17
|
|
|
|
|
18
|
|
|
err := decode.Unmarshal(b, &r) |
|
19
|
|
|
|
|
20
|
|
|
assert.NotNil(t, err) |
|
21
|
|
|
var e *decode.UnknownValueError |
|
22
|
|
|
assert.ErrorAs(t, err, &e) |
|
23
|
|
|
assert.Equal(t, reflect.Bool, err.(*decode.UnknownValueError).Expected) |
|
24
|
|
|
assert.Equal(t, reflect.Int, err.(*decode.UnknownValueError).Got) |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
func TestUnknownValueError_ExpectedSlice(t *testing.T) { |
|
28
|
|
|
b := []byte{ |
|
29
|
|
|
binn.ListType, // [type] list (container) |
|
30
|
|
|
0x05, // [size] container total size |
|
31
|
|
|
0x01, // [count] items |
|
32
|
|
|
0x20, // [type] = uint8 |
|
33
|
|
|
0x7B, // [data] (123) |
|
34
|
|
|
} |
|
35
|
|
|
var r int |
|
36
|
|
|
|
|
37
|
|
|
err := decode.Unmarshal(b, &r) |
|
38
|
|
|
|
|
39
|
|
|
assert.NotNil(t, err) |
|
40
|
|
|
var e *decode.UnknownValueError |
|
41
|
|
|
assert.ErrorAs(t, err, &e) |
|
42
|
|
|
assert.Equal(t, reflect.Slice, err.(*decode.UnknownValueError).Expected) |
|
43
|
|
|
assert.Equal(t, reflect.Int, err.(*decode.UnknownValueError).Got) |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
func TestInvalidCount(t *testing.T) { |
|
47
|
|
|
b := []byte{ |
|
48
|
|
|
binn.ListType, // [type] list (container) |
|
49
|
|
|
0x05, // [size] container total size |
|
50
|
|
|
0x03, // [count] items |
|
51
|
|
|
0x20, // [type] = uint8 |
|
52
|
|
|
0x7B, // [data] (123) |
|
53
|
|
|
} |
|
54
|
|
|
v := []int{} |
|
55
|
|
|
|
|
56
|
|
|
err := decode.Unmarshal(b, &v) |
|
57
|
|
|
|
|
58
|
|
|
assert.NotNil(t, err) |
|
59
|
|
|
assert.Equal(t, []int{123}, v) |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
func TestSimpleStorages(t *testing.T) { |
|
63
|
|
|
tests := []struct { |
|
64
|
|
|
name string |
|
65
|
|
|
binary []byte |
|
66
|
|
|
expected interface{} |
|
67
|
|
|
}{ |
|
68
|
|
|
{ |
|
69
|
|
|
"true", |
|
70
|
|
|
[]byte{binn.True}, |
|
71
|
|
|
true, |
|
72
|
|
|
}, |
|
73
|
|
|
{ |
|
74
|
|
|
"uint8", |
|
75
|
|
|
[]byte{binn.Uint8Type, 33}, |
|
76
|
|
|
uint8(33), |
|
77
|
|
|
}, |
|
78
|
|
|
{ |
|
79
|
|
|
"int16", |
|
80
|
|
|
[]byte{binn.Int16Type, 0xCF, 0xC7}, |
|
81
|
|
|
int16(-12345), |
|
82
|
|
|
}, |
|
83
|
|
|
{ |
|
84
|
|
|
"int32", |
|
85
|
|
|
[]byte{binn.Int32Type, 0xFF, 0x43, 0x9E, 0xB2}, |
|
86
|
|
|
int32(-12345678), |
|
87
|
|
|
}, |
|
88
|
|
|
{ |
|
89
|
|
|
"uint64", |
|
90
|
|
|
[]byte{binn.Int64Type, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, |
|
91
|
|
|
int64(9223372036854775806), |
|
92
|
|
|
}, |
|
93
|
|
|
{ |
|
94
|
|
|
"string", |
|
95
|
|
|
[]byte{binn.StringType, 0x05, 'h', 'e', 'l', 'l', 'o', 0x00}, |
|
96
|
|
|
"hello", |
|
97
|
|
|
}, |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
for _, test := range tests { |
|
101
|
|
|
t.Run(test.name, func(t *testing.T) { |
|
102
|
|
|
var v interface{} |
|
103
|
|
|
err := decode.Unmarshal(test.binary, &v) |
|
104
|
|
|
|
|
105
|
|
|
assert.Nil(t, err) |
|
106
|
|
|
assert.Equal(t, test.expected, v) |
|
107
|
|
|
}) |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
func TestUnmashalObject(t *testing.T) { |
|
112
|
|
|
b := []byte { |
|
113
|
|
|
0xE2, // [type] object (container) |
|
114
|
|
|
0x14, // [size] container total size |
|
115
|
|
|
0x02, // [count] key/value pairs |
|
116
|
|
|
|
|
117
|
|
|
0x02, 'i', 'd', // key |
|
118
|
|
|
0x20, // [type] = uint8 |
|
119
|
|
|
0x01, // [data] (1) |
|
120
|
|
|
|
|
121
|
|
|
0x04, 'n', 'a', 'm', 'e', // key |
|
122
|
|
|
0xA0, // [type] = string |
|
123
|
|
|
0x04, // [size] |
|
124
|
|
|
'J', 'o', 'h', 'n', 0x00, // [data] (null terminated) |
|
125
|
|
|
} |
|
126
|
|
|
type ts struct { |
|
127
|
|
|
ID uint8 `binn:"id"` |
|
128
|
|
|
Name string `binn:"name"` |
|
129
|
|
|
} |
|
130
|
|
|
obj := ts{} |
|
131
|
|
|
|
|
132
|
|
|
err := decode.Unmarshal(b, &obj) |
|
133
|
|
|
|
|
134
|
|
|
assert.Nil(t, err) |
|
135
|
|
|
assert.Equal(t, ts{1, "John"}, obj) |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
func TestUnmarshalStringObjectToMap(t *testing.T) { |
|
139
|
|
|
b := []byte{ |
|
140
|
|
|
0xE2, // type = object (container) |
|
141
|
|
|
0x11, // container total size |
|
142
|
|
|
0x01, // key/value pairs count |
|
143
|
|
|
0x05, 'h', 'e', 'l', 'l', 'o', // key |
|
144
|
|
|
0xA0, // type = string |
|
145
|
|
|
0x05, 'w', 'o', 'r', 'l', 'd', 0x00, // value (null terminated) |
|
146
|
|
|
} |
|
147
|
|
|
m := map[string]string{} |
|
148
|
|
|
|
|
149
|
|
|
err := decode.Unmarshal(b, &m) |
|
150
|
|
|
|
|
151
|
|
|
assert.Nil(t, err) |
|
152
|
|
|
assert.Equal(t, map[string]string{"hello": "world"}, m) |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// https://github.com/liteserver/binn/blob/master/spec.md#a-list-of-objects |
|
156
|
|
|
func TestListOfObjects(t *testing.T) { |
|
157
|
|
|
b := []byte{ |
|
158
|
|
|
0xE0, // [type] list (container) |
|
159
|
|
|
0x2B, // [size] container total size |
|
160
|
|
|
0x02, // [count] items |
|
161
|
|
|
|
|
162
|
|
|
0xE2, // [type] object (container) |
|
163
|
|
|
0x14, // [size] container total size |
|
164
|
|
|
0x02, // [count] key/value pairs |
|
165
|
|
|
|
|
166
|
|
|
0x02, 'i', 'd', // key |
|
167
|
|
|
0x20, // [type] = uint8 |
|
168
|
|
|
0x01, // [data] (1) |
|
169
|
|
|
|
|
170
|
|
|
0x04, 'n', 'a', 'm', 'e', // key |
|
171
|
|
|
0xA0, // [type] = string |
|
172
|
|
|
0x04, // [size] |
|
173
|
|
|
'J', 'o', 'h', 'n', 0x00, // [data] (null terminated) |
|
174
|
|
|
|
|
175
|
|
|
0xE2, // [type] object (container) |
|
176
|
|
|
0x14, // [size] container total size |
|
177
|
|
|
0x02, // [count] key/value pairs |
|
178
|
|
|
|
|
179
|
|
|
0x02, 'i', 'd', // key |
|
180
|
|
|
0x20, // [type] = uint8 |
|
181
|
|
|
0x02, // [data] (2) |
|
182
|
|
|
|
|
183
|
|
|
0x04, 'n', 'a', 'm', 'e', // key |
|
184
|
|
|
0xA0, // [type] = string |
|
185
|
|
|
0x04, // [size] |
|
186
|
|
|
'E', 'r', 'i', 'c', 0x00, // [data] (null terminated) |
|
187
|
|
|
} |
|
188
|
|
|
type ts struct { |
|
189
|
|
|
ID uint8 `binn:"id"` |
|
190
|
|
|
Name string `binn:"name"` |
|
191
|
|
|
} |
|
192
|
|
|
l := []ts{} |
|
193
|
|
|
|
|
194
|
|
|
err := decode.Unmarshal(b, &l) |
|
195
|
|
|
|
|
196
|
|
|
if assert.Nil(t, err) { |
|
197
|
|
|
assert.Equal(t, []ts{ |
|
198
|
|
|
{1, "John"}, |
|
199
|
|
|
{2, "Eric"}, |
|
200
|
|
|
}, l) |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
func TestJSON(t *testing.T) { |
|
205
|
|
|
b := []byte{'t', 'r', 'u', 'e'} |
|
206
|
|
|
var r bool |
|
207
|
|
|
|
|
208
|
|
|
err := json.Unmarshal(b, &r) |
|
209
|
|
|
|
|
210
|
|
|
assert.Nil(t, err) |
|
211
|
|
|
assert.True(t, r) |
|
212
|
|
|
} |
|
213
|
|
|
|