forked from edgexfoundry/device-rfid-llrp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_data_test.go
347 lines (296 loc) · 8.67 KB
/
reader_data_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
339
340
341
342
343
344
345
346
347
//
// Copyright (C) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package llrp
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
)
func TestClient_withRecordedData(t *testing.T) {
testRecordedData(t, "testdata")
if *roDirectory != "" {
testRecordedData(t, filepath.Join("testdata", *roDirectory))
}
}
func BenchmarkUnmarshalRO(b *testing.B) {
for _, nTags := range []int{
100, 200, 300, 400,
} {
nTags := nTags
b.Run(strconv.Itoa(nTags)+"Tags", func(b *testing.B) {
ro := &ROAccessReport{}
for i := 0; i < nTags; i++ {
ro.TagReportData = append(ro.TagReportData, TagReportData{
EPC96: EPC96{
EPC: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
},
C1G2PC: &C1G2PC{
EPCMemoryLength: 12,
HasUserMemory: false,
HasXPC: false,
IsISO15961: false,
AttributesOrAFI: 0x21,
},
})
}
data, err := ro.MarshalBinary()
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
target := &ROAccessReport{}
if err := target.UnmarshalBinary(data); err != nil {
b.Error(err)
}
}
})
}
}
func testRecordedData(t *testing.T, dir string) {
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
if !strings.HasSuffix(f.Name(), ".json") {
continue
}
prefix := f.Name()[:len(f.Name())-len(".json")]
parts := strings.SplitN(f.Name(), "-", 2)
if len(parts) != 2 {
continue
}
msg := parts[0]
t.Run(prefix, compareMessages(msg, filepath.Join(dir, prefix)))
}
}
func compareMessages(msgName, prefix string) func(t *testing.T) {
type binRoundTrip interface {
UnmarshalBinary(data []byte) error
MarshalBinary() ([]byte, error)
}
var v binRoundTrip
switch "Msg" + msgName {
case MsgGetReaderCapabilitiesResponse.String():
v = &GetReaderCapabilitiesResponse{}
case MsgGetReaderConfigResponse.String():
v = &GetReaderConfigResponse{}
case MsgGetAccessSpecsResponse.String():
v = &GetAccessSpecsResponse{}
case MsgGetROSpecsResponse.String():
v = &GetROSpecsResponse{}
case MsgCloseConnectionResponse.String():
v = &CloseConnectionResponse{}
case MsgReaderEventNotification.String():
v = &ReaderEventNotification{}
case MsgROAccessReport.String():
v = &ROAccessReport{}
// note no default needed: the first couple lines of the test check for `nil`.
}
// This tests the following two conversions using data captured from a reader:
// JSON -> Go -> binary & check it matches original binary
// binary -> Go -> JSON & check it matches original JSON
return func(t *testing.T) {
if v == nil {
t.Fatalf("unknown message type: %s; "+
"did you add a new one but forget to add it to the switch?", msgName)
}
var originalJSON, originalBin, marshaledBin, marshaledJSON []byte
var err error
// load data files
originalJSON, err = ioutil.ReadFile(prefix + ".json")
if err != nil {
t.Fatalf("can't read .json file: %v", err)
}
originalBin, err = ioutil.ReadFile(prefix + ".bytes")
if err != nil {
t.Fatalf("can't read .bytes file: %v", err)
}
// unmarshal original JSON form
if err = json.Unmarshal(originalJSON, v); err != nil {
t.Error(err)
}
// marshal resulting struct to binary
if marshaledBin, err = v.MarshalBinary(); err != nil {
t.Error(err)
}
// get a new v (so we're not duplicating list items)
v = reflect.New(reflect.TypeOf(v).Elem()).Interface().(binRoundTrip)
// unmarshal original binary form to struct
if err = v.UnmarshalBinary(originalBin); err != nil {
t.Error(err)
}
// marshal struct back to JSON
marshaledJSON, err = json.MarshalIndent(v, "", "\t")
if err != nil {
t.Error(err)
}
// Check the JSON first, since it'll be easier to eyeball differences.
// If names change, unmarshaling is likely to leave many zero values,
// which will make the binary mismatched.
// The name differences should be pretty obvious when viewing the JSON.
checkJSONEq(t, originalJSON, marshaledJSON)
// Finally, confirm binary the matches.
checkBytesEq(t, originalBin, marshaledBin)
}
}
// checkJSONEq checks that two json data byte arrays are equal line-by-line,
// ignoring leading and trailing whitespace within each line,
// as well as any remaining whitespace at the end of the data.
//
// If not, it prints a side-by-side diff around the first line difference.
//
// Returns true if the two arrays are equal; false otherwise.
func checkJSONEq(t *testing.T, original, marshaled []byte) (matched bool) {
t.Helper()
lines1, lines2 := bytes.Split(original, []byte("\n")), bytes.Split(marshaled, []byte("\n"))
larger, smaller := lines1, lines2
if len(lines1) < len(lines2) {
larger, smaller = lines2, lines1
}
matched = true
firstDiff := 0
for ; matched && firstDiff < len(smaller); firstDiff++ {
matched = bytes.Equal(
bytes.TrimSpace(lines1[firstDiff]),
bytes.TrimSpace(lines2[firstDiff]))
}
firstDiff--
// the larger of the two may only have blank lines
for i := firstDiff + 1; matched && i < len(larger); i++ {
matched = len(bytes.TrimSpace(larger[i])) == 0
}
if matched {
return
}
const contextAbove = 4
start := firstDiff - contextAbove
if start < 0 {
start = 0
}
const contextTotal = 16
end := start + contextTotal
if end > len(lines1)-1 {
end = len(lines1) - 1
}
if end > len(lines2)-1 {
end = len(lines2) - 1
}
longest := 1
for i := start; i < end; i++ {
lines1[i] = bytes.ReplaceAll(lines1[i], []byte("\t"), []byte(" "))
lines2[i] = bytes.ReplaceAll(lines2[i], []byte("\t"), []byte(" "))
if len(lines1[i]) > longest {
longest = len(lines1[i])
}
if len(lines2[i]) > longest {
longest = len(lines2[i])
}
}
diff := bytes.Buffer{}
fmt.Fprintf(&diff, "%-[1]*s | %s\n", longest,
" --Original JSON Data--", " --Marshaled Result--")
for i := start; i < end; i++ {
if i == firstDiff {
msg := "--first diff below this line--"
fmt.Fprintf(&diff, "%[1]*s\n", longest+3+len(msg)/2, msg)
}
fmt.Fprintf(&diff, "%-[1]*s | %s\n", longest, lines1[i], lines2[i])
}
t.Errorf("JSON data mismatched; first difference around line %d:\n%s",
firstDiff, diff.String())
return matched
}
func checkBytesEq(t *testing.T, original, marshaled []byte) (matched bool) {
t.Helper()
matched = len(original) == len(marshaled)
smaller := len(marshaled)
if len(original) < len(marshaled) {
smaller = len(original)
matched = false
}
firstDiff := 0
for ; firstDiff < smaller; firstDiff++ {
if original[firstDiff] != marshaled[firstDiff] {
matched = false
break
}
}
if matched {
return
}
if len(original) == 0 || len(marshaled) == 0 {
t.Errorf("byte data mismatched: one is empty: \n"+
" original: %# 02x\n"+
"marshaled: %# 02x", original, marshaled)
return
}
// these constants control how much context to show
const bpl = 8 // bytes per line (per byte array, so really, 2x this)
const before = 2 // lines before first diff
const after = 4 // lines after first diff
start := ((firstDiff / bpl) - before) * bpl
if start < 0 {
start = 0
}
end := start + bpl*(before+after)
ob := original[start:]
mb := marshaled[start:]
format := fmt.Sprintf("%%04d-%%04d > %%#- %d.%dx | %%# .%dx\n", bpl*5, bpl, bpl)
buff := bytes.Buffer{}
for i := start; i < end && (len(ob) > 0 || len(mb) > 0); i += bpl {
if i <= firstDiff && firstDiff < i+bpl {
buff.Write(bytes.Repeat([]byte(" "), 5*(firstDiff-i)+12))
buff.WriteString(expand((bpl+1)*5+4, "-", "v", "first diff", "v\n"))
}
fmt.Fprintf(&buff, format, i, i+bpl, ob, mb)
if len(ob) > bpl {
ob = ob[bpl:]
} else {
ob = nil
}
if len(mb) > bpl {
mb = mb[bpl:]
} else {
mb = nil
}
}
t.Errorf("byte data mismatched starting at byte %d\n"+
" index > %[2]*s | marshaled binary\n%s",
firstDiff, bpl*5, "original binary", buff.String())
return matched
}
// expand returns a string that centers s between l and r
// by inserting fill as many times as need
// so that the final string has at least the given width.
//
// If width <= len(l+s+r), this return l+s+r,
// even if that would be larger than width.
// Otherwise, it inserts the minimum number of fill values
// so that the len the returned value >= width.
// If len(fill) == 1, it'll insert just to width.
// If it's larger, the return might exceed width by as much.
// If it's an empty string, this'll panic.
func expand(width int, fill, l, s, r string) string {
width -= len(s) + len(l) + len(r)
nFills := atLeastZero(width / len(fill))
rFill := atLeastZero(nFills / 2)
lFill := atLeastZero(nFills - rFill)
return l + strings.Repeat(fill, rFill) + s + strings.Repeat(fill, lFill) + r
}
func atLeastZero(i int) int {
if i < 0 {
return 0
}
return i
}