forked from HewlettPackard/structex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
281 lines (226 loc) · 6.32 KB
/
decoder_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
/*
Copyright 2021 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package structex
import (
"bytes"
"fmt"
"testing"
)
const (
bufferSize = 512
)
type testReader struct {
bytes [bufferSize]byte
offset int
nbytes int
}
func newReader(b []byte) *testReader {
var tr = new(testReader)
copy(tr.bytes[:], b)
tr.offset = 0
tr.nbytes = len(b)
return tr
}
func (tr *testReader) ReadByte() (byte, error) {
if tr.offset == tr.nbytes {
return 0, fmt.Errorf("Buffer underrun")
}
b := tr.bytes[tr.offset]
tr.offset++
return b, nil
}
func unpackAndTest(t *testing.T, s interface{}, tr *testReader, testFunc func(t *testing.T, s interface{})) {
if err := Decode(tr, s); err != nil {
t.Error(err)
}
testFunc(t, s)
}
func TestBasicDecoder(t *testing.T) {
type ts struct {
A uint8
B uint16
C uint32
D uint64
}
var s = new(ts)
var tr = newReader([]byte{
0x00,
0x01, 0x00,
0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != 0 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x00, s.A)
}
if s.B != 0x0001 {
t.Errorf("Test Value Incorrect: Expected: %#04x Actual: %#04x", 0x0001, s.B)
}
})
}
func TestBitfieldDecoder(t *testing.T) {
type ts struct {
A int `bitfield:"3"`
B int `bitfield:"4"`
C int `bitfield:"1"`
}
var s = new(ts)
var tr = newReader([]byte{0xC7})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != 0x07 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x07, s.A)
}
if s.B != 0x08 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.B)
}
if s.C != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.C)
}
})
}
func TestNestedDecoder(t *testing.T) {
type ns struct {
M int `bitfield:"3"`
N int `bitfield:"4"`
O int `bitfield:"1"`
}
type ts struct {
A uint8
B uint8
C ns
}
var s = new(ts)
var tr = newReader([]byte{0x01, 0x02, 0xC7})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x01, s.A)
}
if s.B != 0x02 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x02, s.B)
}
if s.C.M != 0x07 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x07, s.C.M)
}
if s.C.N != 0x08 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.C.N)
}
if s.C.O != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.C.O)
}
})
}
func TestArrayDecoder(t *testing.T) {
type as struct {
A uint8 `bitfield:"4"`
B uint8 `bitfield:"4"`
}
type ts struct {
Count uint8 `countOf:"Cs"`
Size uint8 `sizeOf:"Ss"`
Cs []as
Ss []as
A [1]byte
}
var s = new(ts)
var tr = newReader([]byte{2, 2, 0x11, 0x22, 0x33, 0x44, 0x55})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.Count != 2 {
t.Errorf("Count Value Incorrect: Expected: %#02x Actual: %#02x", 2, s.Count)
}
if s.Size != 2 {
t.Errorf("Size Value Incorrect: Expected: %#02x Actual: %#02x", 2, s.Size)
}
if s.Cs[0].A != 1 || s.Cs[0].B != 1 {
val := s.Cs[0].A | (s.Cs[0].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x11, val)
}
if s.Cs[1].A != 2 || s.Cs[1].B != 2 {
val := s.Cs[1].A | (s.Cs[1].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x22, val)
}
if s.Ss[0].A != 3 || s.Ss[0].B != 3 {
val := s.Ss[0].A | (s.Ss[0].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x33, val)
}
if s.Ss[1].A != 4 || s.Ss[1].B != 4 {
val := s.Ss[1].A | (s.Ss[1].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x44, val)
}
if s.A[0] != 0x55 {
t.Errorf("Array Value Incorrect: Expected %#02x Actual: %#02x", 0x55, s.A[0])
}
})
}
func TestHeaderStyleDecoder(t *testing.T) {
type hdr struct {
Count uint8 `countOf:"Details"`
}
type detail struct {
Val uint8
}
type ts struct {
Hdr hdr
Details []detail
}
s := new(ts)
tr := newReader([]byte{4, 0, 1, 2, 3})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
s := i.(*ts)
if s.Hdr.Count != 4 {
t.Errorf("Count Value Incorrect: Expected: %d Actual: %d", 4, s.Hdr.Count)
}
if len(s.Details) != 4 {
t.Errorf("Details Array Len Incorrect: Expected: %d Actual: %d", 4, len(s.Details))
}
for i := uint8(0); i < s.Hdr.Count; i++ {
if s.Details[i].Val != i {
t.Errorf("Detail Value Incorrect: Expected: %d Actual: %d", i, s.Details[i].Val)
}
}
})
}
func TestBytesBuffer(t *testing.T) {
type ts struct {
A byte
B byte
}
var s = new(ts)
b := bytes.NewBuffer([]byte{0x01, 0x02})
if err := DecodeByteBuffer(b, s); err != nil {
t.Errorf("Unpack Buffer failed: Err: %v", err)
}
if s.A != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x01, s.A)
}
if s.B != 0x02 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x02, s.B)
}
}
func TestTruncate(t *testing.T) {
type ts struct {
A [1024]byte `truncate:""`
}
var s = new(ts)
if err := DecodeByteBuffer(bytes.NewBuffer([]byte{0}), s); err != nil {
t.Errorf("Truncate test failed: %s", err)
}
}