-
Notifications
You must be signed in to change notification settings - Fork 12
/
pack_test.go
203 lines (181 loc) · 4.45 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
package gologix
import (
"bytes"
"testing"
)
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 byte
U3B [4]byte
}
type UDT2 struct {
U2A int32
U2B [3]byte
U2C UDT3
U2D [2]UDT3
}
type UDT1 struct {
U1A byte
U1B [2]byte
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)
}
}