-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpack_test.go
338 lines (296 loc) · 7.5 KB
/
pack_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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package gologix
import (
"bytes"
"testing"
"github.com/danomagnum/gologix/lgxtypes"
)
func TestPack(t *testing.T) {
type S2 struct {
Flag1 bool
U32 uint32
PostFlag bool
}
type S struct {
Flag0 bool
Flag1 bool `pack:"nopack"`
Flag2 bool
Flag3 bool
Flag4 bool
Flag5 bool
Flag6 bool
Flag7 bool
Flag8 bool
Flag9 bool
Flag10 bool
Flag11 bool
U32 uint32
Sub1 S2
Flags [16]bool //`pack:"nopack"`
Sub2 S2
}
s := S{}
s.Flag1 = true
s.Flag3 = true
s.Flag9 = true
s.Flag10 = true
s.Flag11 = true
//s.Flag7 = true
//s.Flag8 = true
s.U32 = 0xFFFF_FFFF
s.Sub1.Flag1 = true
s.Sub1.U32 = 0xEEEE_EEEE
s.Flags[0] = true
s.Flags[15] = true
s.Sub2.Flag1 = true
s.Sub2.U32 = 0xDDDD_DDDD
b := bytes.Buffer{}
//binary.Write(&b, binary.LittleEndian, s)
Pack(&b, s)
have := b.Bytes()
want := []byte{
0, // flag0
1, // flag1 gets packed on its own because of nopack
130, 3, // flag2+ all get packed into combined words
0xFF, 0xFF, 0xFF, 0xFF, // U32
1, 0, 0, 0, // single flag1 in S2 gets buffered to 4 bytes
0xEE, 0xEE, 0xEE, 0xEE, // U32 in Sub1
0, // post flag in sub1 //TODO: does this need to pad?
1, 128, 0, // 16 bool array padded out to 4 byte boundry
1, 0, 0, 0, // single flag1 in S2 buffered to 4 bytes
0xDD, 0xDD, 0xDD, 0xDD, // U32 in Sub2
0, // last postflag in sub2
}
if !check_bytes(have, want) {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", have, want)
}
have2 := S{}
_, err := Unpack(bytes.NewBuffer(b.Bytes()), &have2)
if err != nil {
t.Errorf("problem unpacking bytes. %v", err)
}
if have2 != s {
t.Errorf("ResultMismatch.\n Have \n%v\n Want \n%v\n", have2, s)
}
}
func TestPack2(t *testing.T) {
type S2 struct {
Flag1 bool
U32 uint32
PostFlag bool
LongInt uint64
}
type S struct {
Flag0 bool
Flag1 bool `pack:"nopack"`
Flag2 bool
Flag3 bool
Flag4 bool
Flag5 bool
Flag6 bool
Flag7 bool
Flag8 bool
Flag9 bool
Flag10 bool
Flag11 bool
U32 uint32
Sub1 S2
Flags [16]bool //`pack:"nopack"`
Sub2 S2
}
s := S{}
s.Flag1 = true
s.Flag3 = true
s.Flag9 = true
s.Flag10 = true
s.Flag11 = true
//s.Flag7 = true
//s.Flag8 = true
s.U32 = 0xFFFF_FFFF
s.Sub1.Flag1 = true
s.Sub1.U32 = 0xEEEE_EEEE
s.Sub1.LongInt = 0xCCCC_CCCC_CCCC_CCCC
s.Flags[0] = true
s.Flags[15] = true
s.Sub2.Flag1 = true
s.Sub2.U32 = 0xDDDD_DDDD
s.Sub2.LongInt = 0xBBBB_BBBB_BBBB_BBBB
b := bytes.Buffer{}
//binary.Write(&b, binary.LittleEndian, s)
Pack(&b, s)
have := b.Bytes()
want := []byte{
0, // flag0
1, // flag1 gets packed on its own because of nopack
130, 3, // flag2+ all get packed into combined words
0xFF, 0xFF, 0xFF, 0xFF, // U32
1, 0, 0, 0, // start of Sub1 on byte 8. single flag1 in S2 gets buffered to 4 bytes
0xEE, 0xEE, 0xEE, 0xEE, // U32 in Sub1
0, // post flag in sub1 //TODO: does this need to pad?
0, 0, 0, 0, 0, 0, 0, // padding for lint
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, // lint
1, 128, 0, 0, 0, 0, 0, 0, // 16 bool array padded out to 8 byte boundry for sub2
1, 0, 0, 0, // single flag1 in S2 buffered to 4 bytes
0xDD, 0xDD, 0xDD, 0xDD, // U32 in Sub2
0, // last postflag in sub2
0, 0, 0, 0, 0, 0, 0, // padding for lint in sub2
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, // lint in sub2
}
if !check_bytes(have, want) {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", have, want)
}
have2 := S{}
_, err := Unpack(bytes.NewBuffer(b.Bytes()), &have2)
if err != nil {
t.Errorf("problem unpacking bytes. %v", err)
}
if have2 != s {
t.Errorf("ResultMismatch.\n Have \n%v\n Want \n%v\n", have2, s)
}
}
// this test verifies the "Type Encoding String" described in TypeEncodeCIPRW.pdf is correct for the
// example UDT1, UDT2, UDT3 they give. It then verifies the CRC16 checksum of that string.
func TestEncodeUDT(t *testing.T) {
type UDT3 struct {
U3A int8
U3B [4]int8
}
type UDT2 struct {
U2A int32
U2B [3]int8
U2C UDT3
U2D [2]UDT3
}
type UDT1 struct {
U1A int8
U1B [2]int8
U1C UDT2
U1D [4]UDT3
}
encoding, crc, err := TypeEncode(UDT1{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want := "UDT1,SINT,SINT[2],UDT2,DINT,SINT[3],UDT3,SINT,SINT[4],UDT3,SINT,SINT[4][2],UDT3,SINT,SINT[4][4]"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc := uint16(0x5F58)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
}
func TestEncodeString(t *testing.T) {
encoding, crc, err := TypeEncode(lgxtypes.STRING{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want := "STRING,DINT,SINT[82]"
if encoding != want {
t.Errorf("encoding mismatch. Got %v want %v", encoding, want)
}
crc_want := uint16(0x0FCE)
if crc != crc_want {
t.Errorf("CRC mismatch. Got 0x%04x want 0x%04x", crc, crc_want)
}
}
func TestEncodeBuiltinTypes(t *testing.T) {
type TimerWrapper struct {
TON lgxtypes.TIMER
}
type ControlWrapper struct {
Control lgxtypes.CONTROL
}
type CounterWrapper struct {
Counter lgxtypes.COUNTER
}
type StringWrapper struct {
String lgxtypes.STRING
}
type TypeWithTimer struct {
Field0 int32
Flag1 bool
Flag2 bool
Timer lgxtypes.TIMER
Field1 int32
}
encoding, crc, err := TypeEncode(TimerWrapper{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want := "TimerWrapper,TIMER,DINT,DINT,DINT"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc := uint16(0xC23B)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
encoding, crc, err = TypeEncode(lgxtypes.TIMER{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want = "TIMER,DINT,DINT,DINT"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc = uint16(0x0F83)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
encoding, crc, err = TypeEncode(TypeWithTimer{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want = "TypeWithTimer,DINT,SINT,TIMER,DINT,DINT,DINT,DINT"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc = uint16(0x9a39)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
encoding, crc, err = TypeEncode(ControlWrapper{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want = "ControlWrapper,CONTROL,DINT,DINT,DINT"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc = uint16(0x6207)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
encoding, crc, err = TypeEncode(CounterWrapper{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want = "CounterWrapper,COUNTER,DINT,DINT,DINT"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc = uint16(0x8436)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
encoding, crc, err = TypeEncode(StringWrapper{})
if err != nil {
t.Errorf("problem encoding UDT1. %v", err)
return
}
want = "StringWrapper,STRING,DINT,SINT[82]"
if encoding != want {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", encoding, want)
}
want_crc = uint16(0x45FD)
if crc != want_crc {
t.Errorf("CRC0 mismatch. Have %x Want %x", crc, want_crc)
}
}