forked from calmh/ipfix
-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser_test.go
503 lines (417 loc) · 52.5 KB
/
parser_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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package ipfix
import (
"bytes"
"encoding/hex"
"io"
"sync"
"testing"
)
func TestCanCreateSession(t *testing.T) {
p := NewSession()
if p == nil {
t.Error("New session can't be nil")
}
}
func TestParseTemplateSet(t *testing.T) {
packet, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p := NewSession()
r := bytes.NewBuffer(packet)
msg, err := p.ParseReader(r)
if err != nil {
t.Fatal("ParseReader failed", err)
}
if msg.Header.Version != uint16(0xa) {
t.Errorf("version mismatch %d != 10", msg.Header.Version)
}
if len(msg.DataRecords) != 0 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 2 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
if id := msg.TemplateRecords[0].TemplateID; id != uint16(10299) {
t.Error("Incorrect template ID", id)
}
if id := msg.TemplateRecords[1].TemplateID; id != uint16(49836) {
t.Error("Incorrect template ID", id)
}
}
func TestMarshalTemplateSet(t *testing.T) {
packet, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p := NewSession()
r := bytes.NewBuffer(packet)
msg, err := p.ParseReader(r)
if err != nil {
t.Fatal("ParseReader failed", err)
}
marshalled, err := p.Marshal(msg)
if err != nil {
t.Fatal("Marshal failed", err)
}
if !bytes.Equal(marshalled, packet) {
t.Fatal("Expected didn't match marshalled bytes")
}
marshalled2, err := msg.Marshal()
if err != nil {
t.Fatal("Message marshal failed", err)
} else if !bytes.Equal(marshalled2, packet) {
t.Fatal("Expected didn't match marshalled bytes")
}
}
func TestMarshalDataSet(t *testing.T) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
p := NewSession()
p.ParseBuffer(p0)
msg, err := p.ParseBuffer(p1)
if err != nil {
t.Fatal("ParseReader failed", err)
}
marshalled, err := p.Marshal(msg)
if err != nil {
t.Fatal("Marshal failed", err)
}
if !bytes.Equal(marshalled, p1) {
t.Fatalf("Expected (%v bytes) didn't match marshalled (%v bytes)", len(p1), len(marshalled))
}
}
func TestParseTemplateIDAliasing(t *testing.T) {
packet, _ := hex.DecodeString("000a017c51ec4264000000000b20bdbe0002016c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c250008000412340008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010abcd0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c250008000412340008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010abcd0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p := NewSession(WithIDAliasing(true))
r := bytes.NewBuffer(packet)
msg, err := p.ParseBuffer(r.Bytes())
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if msg.Header.Version != uint16(0xa) {
t.Errorf("version mismatch %d != 10", msg.Header.Version)
}
if len(msg.DataRecords) != 0 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 6 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
for i := 0; i < len(msg.TemplateRecords); i += 2 {
if id := msg.TemplateRecords[i].TemplateID; id != uint16(256) {
t.Error("Incorrect template ID", id)
}
if id := msg.TemplateRecords[i+1].TemplateID; id != uint16(257) {
t.Error("Incorrect template ID", id)
}
}
}
func TestParseDataSet(t *testing.T) {
testParseDataSet(false, t)
}
func TestParseDataSetWithAliasing(t *testing.T) {
testParseDataSet(true, t)
}
func testParseDataSet(withAliasing bool, t *testing.T) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
b := new(bytes.Buffer)
p := NewSession(WithIDAliasing(withAliasing))
// Handle a data set with an unknown template first
b.Write(p1)
b.Write(p0)
b.Write(p1)
msg, err := p.ParseReader(b)
if err != nil {
t.Fatal("ParseReader failed", err)
}
if len(msg.DataRecords) != 0 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 0 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
msg, err = p.ParseReader(b)
if err != nil {
t.Fatal("ParseReader failed", err)
}
if len(msg.DataRecords) != 0 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 2 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
msg, err = p.ParseReader(b)
if err != nil {
t.Fatal("ParseReader failed", err)
}
if len(msg.DataRecords) != 31 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 0 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
}
func TestParseV9DataSet(t *testing.T) {
testParseDataSet(false, t)
}
func TestParseV9DataSetWithAliasing(t *testing.T) {
testParseDataSetV9(true, t)
}
func testParseDataSetV9(withAliasing bool, t *testing.T) {
p0, _ := hex.DecodeString("0009001f198afac45defcbd800103e5700000000000000440113000f00080004000c0004000f000400070002000b000200060001000a0002000e000200020004000100040016000400150004000400010005000100d100040113005c34c899db0a0a0a8a0000000001bbbc40180002ffff000000040000013c198a5160198a8aa40600810000000a0a0a8a34c899db0a0a0a01bc4001bb18ffff000200000006000001b0198a5100198a8aa40600810000000000000000400114000e00080004000c0004000f000400070002000b000200060001000a0002000e0002000200040001000400160004001500040004000100050001011400547f0000017f00000100000000d21f115c00ffff00010000000200000b18198a9138198a913811007f0000017f00000100000000d21f115c000001ffff0000000200000b18198a9138198a913811000000011301087f0000017f00000100000000436b898e10ffff00010000000600000138198a5c04198a97000600810000007f0000017f00000100000000898e436b10ffff00010000000600000138198a5c04198a97000600810000007f0000017f00000100000000436b898e100001ffff0000000600000138198a5c04198a97000600810000007f0000017f00000100000000898e436b100001ffff0000000600000138198a5c04198a9700060081000000c6231a600a0a0a8a0000000001bbe904100002ffff0000000200000068198a9830198a98300600810000000a0a0a8ac6231a600a0a0a01e90401bb10ffff00020000000200000068198a9800198a980006008100000000000114002c7f0000017f00000100000000baf9080700ffff000100000002000007dc198aa6c8198aa6c81100000000003c0115000d00080004000c0004000f000400070002000b0002000a0002000e0002000200040001000400160004001500040004000100050001011500507f0000017f00000100000000000003030001ffff0000000400000900198a9138198aa6c801c07f0000017f0000010000000000000303ffff00010000000400000900198a9138198aa6c801c0011402007f0000017f00000100000000baf90807000001ffff00000002000007dc198aa6c8198aa6c811000a0a0a010a0a0a8a000000000035928c000002ffff0000000200000154198abb20198abb2011000a0a0a8a0a0a0a0100000000dbd8003500ffff0002000000020000008e198abb1c198abb1c11000a0a0a8a0a0a0a0100000000928c003500ffff0002000000020000008e198abb1c198abb1c11000a0a0a010a0a0a8a000000000035dbd8000002ffff0000000200000132198abb20198abb2011000a0a0a8a0a0a0a0100000000cae8003500ffff0002000000020000008e198abb88198abb8811000a0a0a8a0a0a0a0100000000c167003500ffff0002000000020000008e198abb88198abb8811000a0a0a010a0a0a8a000000000035cae8000002ffff0000000200000154198abb8c198abb8c11000a0a0a010a0a0a8a000000000035c167000002ffff0000000200000132198abb8c198abb8c11000a0a0a010a0a0a8a0000000000359012000002ffff0000000200000132198abd90198abd9011000a0a0a010a0a0a8a0000000000358df4000002ffff0000000200000154198abd90198abd9011000a0a0a8a0a0a0a01000000008df4003500ffff0002000000020000008e198abd8c198abd8c11000a0a0a8a0a0a0a01000000009012003500ffff0002000000020000008e198abd8c198abd8c1100000113005a0a0a0a8a976500850a0a0a01e74201bb10ffff00020000000200000068198ac000198ac000060081000000976500850a0a0a8a0000000001bbe742100002ffff0000000200000068198ac020198ac020060081000000")
p := NewSession(WithIDAliasing(withAliasing))
msg, err := p.ParseBuffer(p0)
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if len(msg.DataRecords) != 28 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 3 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
marshalled, err := p.Marshal(msg)
if err != nil {
t.Fatal("Marshal failed", err)
}
//unmarshal and make sure we get the same number of records back out
if msg2, err := p.ParseBuffer(marshalled); err != nil {
t.Fatal("Failed parse marshalled buffer", err)
} else if len(msg2.DataRecords) != 28 {
t.Fatalf("remarshalled message does not match template records: %d != %d", len(msg.DataRecords), len(msg2.DataRecords))
}
}
func TestReadParseBuffer(t *testing.T) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
b := new(bytes.Buffer)
p := NewSession()
// Handle a data set with an unknown template first
b.Write(p1)
b.Write(p0)
b.Write(p1)
bs, _, err := Read(b, nil)
if err != nil {
t.Fatal(err)
}
msg, err := p.ParseBuffer(bs)
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if len(msg.DataRecords) != 0 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 0 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
bs, _, err = Read(b, bs)
if err != nil {
t.Fatal(err)
}
msg, err = p.ParseBuffer(bs)
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if len(msg.DataRecords) != 0 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 2 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
bs, _, err = Read(b, bs)
if err != nil {
t.Fatal(err)
}
msg, err = p.ParseBuffer(bs)
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if len(msg.DataRecords) != 31 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 0 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
}
func TestParallellParseBuffer(t *testing.T) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
p := NewSession()
var wg sync.WaitGroup
wg.Add(1)
go func() {
for i := 0; i < 1000; i++ {
p.ParseBuffer(p0)
}
}()
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
p.ParseBuffer(p0)
for i := 0; i < 1000; i++ {
msg, err := p.ParseBuffer(p1)
if err != nil {
t.Error("ParseReader failed", err)
}
if len(msg.DataRecords) != 31 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 0 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
}
}()
}
}
func TestUnexpectedEOFError(t *testing.T) {
truncated, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258")
b := new(bytes.Buffer)
p := NewSession()
b.Write(truncated)
_, err := p.ParseReader(b)
if err != io.ErrUnexpectedEOF {
t.Fatalf("Received %v instead of io.EOF error", err)
}
}
func TestVersionError(t *testing.T) {
truncated, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46a")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
b := new(bytes.Buffer)
p := NewSession()
b.Write(truncated)
b.Write(p1)
p.ParseReader(b)
_, err := p.ParseReader(b)
if err != ErrVersion {
t.Fatalf("Received %v instead of ErrVersion error", err)
}
}
func TestBadEncodingError(t *testing.T) {
p0, _ := hex.DecodeString("000a009c520239cc002488cc0b20bdbe0002008c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b00104f4d000b000c00040097000400960004800c000400003c258016ffff00003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05ae520239cc0024889e0b20bdbe4f4d029cac10200f520239cc520239ac000000000000000000000000910000000000000136000f426974546f7272656e74204b525043001b2065fbac102003520239cc520239c0000000220000000000000001ab0000000000000168000e4265696e6720616e616c797a656400c27ef905ac1020ff520239cc520239ac00000000000000000000000000000000000000005c00144e657442696f73204e616d65205365727669636500ac102082ac10200f520239cc520239ac000000000000000000000000910000000000000136000f426974546f7272656e74204b52504300dcee3ce9ac10200f520239cc520239ac0000000000000000000000008c000000000000013a000f426974546f7272656e74204b525043006fe9d570ac10200f520239cc520239ad000000000000000000000000910000000000000136000f426974546f7272656e74204b525043005853e3e4ac10200f520239cc520239ae000000000000000000000000910000000000000136000f426974546f7272656e74204b52504300b1621b8b18345cdc520239cc520239ae000000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f520239cc520239ae0000000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430025faf170ac10200f520239cc520239ae0000000000000000000000006b0000000000000059000f426974546f7272656e74204b52504300539525cdac10200f520239cc520239ae000000000000000000000000940000000000000147000f426974546f7272656e74204b52504300bc069bc5ac10200f520239cc520239ae0000000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430056931e40283b004920010470002804d6000000000000000400000000000000000000004a0000000000000062000e4265696e6720616e616c797a65640020010470deeb003280db765c7c72c6934f4d02b9c27ef904520239cc520239c20000001dff0001687474703a2f2f6e796d2e73652f737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737300000000000004ff000000000000048f066e796d2e7365044854545000ac102082ac10200f520239cc520239a50000000000000000000000011e00000000000002ba000f426974546f7272656e74204b5250430001ace2e4ac10200f520239cc520239b10000000000000000000000006b0000000000000059000f426974546f7272656e74204b525043007894a711ac10200f520239cc520239b10000000000000000000000009f0000000000000134000f426974546f7272656e74204b525043005f8bc343ac10200f520239cc520239b1000000000000000000000000920000000000000145000f426974546f7272656e74204b5250430079e53f6eac10200f520239cc520239af000000000000000000000000910000000000000136000f426974546f7272656e74204b525043005be27a31ac10200f520239cc520239af000000000000000000000000940000000000000147000f426974546f7272656e74204b5250430070d14d8eac10200f520239cc520239b1000000000000000000000000920000000000000145000f426974546f7272656e74204b52504300de5e3a01")
p2, _ := hex.DecodeString("000a05a6520239f9002489e30b20bdbe4f4d0596ac10200f520239f9520239e0000000000000000000000000f1000000000000005b000f426974546f7272656e74204b525043006dab2a88ac10200f520239f9520239e00000000000000000000000008f000000000000015d000f426974546f7272656e74204b525043007b778163ac10200f520239f9520239e0000000000000000000000000910000000000000136000f426974546f7272656e74204b525043005fb2e498ac10200f520239f9520239da000000000000000000000001ad000000000000042f000f426974546f7272656e74204b525043006ee75810ac10200f520239f9520239df000000000000000000000000910000000000000136000f426974546f7272656e74204b5250430005526896ac102003520239f9520239f3000000220000000000000001ad0000000000000167000e4265696e6720616e616c797a656400c27ef905ac102003520239f9520239f4000000240000000000000001a80000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f520239f9520239e1000000000000000000000000910000000000000136000f426974546f7272656e74204b525043000e2b6855ac10200f520239f9520239d5000000000000000000000001d00000000000000347000f426974546f7272656e74204b5250430077739721ac10200f520239f9520239dc000000000000000000000000fd0000000000000198000f426974546f7272656e74204b525043005bc8cb08ac10200f520239f9520239e10000000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430057fcb5ceac10200f520239f9520239e1000000000000000000000000910000000000000136000f426974546f7272656e74204b52504300dfccf34aac10200f520239f9520239d90000000000000000000000011e0000000000000296000f426974546f7272656e74204b5250430075414462ac10200f520239f9520239e20000000000000000000000008f000000000000014b000f426974546f7272656e74204b525043003d5b581bac10200f520239f9520239e2000000000000000000000000910000000000000136000f426974546f7272656e74204b52504300050c9e43ac10200f520239f9520239e2000000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ae0335b2ac102003520239f9520239f5000000250000000000000001ab0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f520239f9520239e2000000000000000000000000910000000000000136000f426974546f7272656e74204b525043004e61163cac10200f520239f9520239d800000000000000000000000091000000000000019a000f426974546f7272656e74204b525043007ab7e026ac10200f520239f9520239e2000000000000000000000000910000000000000136000f426974546f7272656e74204b5250430029d08ffcac10200f520239f9520239dd000000000000000000000002820000000000000378000f426974546f7272656e74204b52504300b71ec470ac10200f520239f9520239e20000000000000000000000008f0000000000000134000f426974546f7272656e74204b525043005ccb6c11ac10200f520239f9520239e20000000000000000000000008f0000000000000195000f426974546f7272656e74204b5250430071007099ac10200f520239f9520239e20000000000000000000000008f0000000000000134000f426974546f7272656e74204b52504300c4000455ac102003520239f9520239f60000001e0000000000000001a90000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f520239f9520239e3000000000000000000000000910000000000000136000f426974546f7272656e74204b525043005fe287cd")
b := new(bytes.Buffer)
p := NewSession()
b.Write(p0)
b.Write(p1)
b.Write(p2)
p.ParseReader(b) // The template
p.ParseReader(b) // The broken message
msg, err := p.ParseReader(b) // The good message
if err != nil {
t.Fatalf("Received error %v even though we should be back in sync", err)
}
if len(msg.DataRecords) != 26 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
if len(msg.TemplateRecords) != 0 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
}
var msg Message
func BenchmarkParseReader(b *testing.B) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
pb := new(bytes.Buffer)
pb.Write(p0)
p := NewSession()
_, err := p.ParseReader(pb)
if err != nil {
b.Fatal("ParseReader failed", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(1)
for i := 0; i < b.N; {
pb.Write(p1)
msg, err = p.ParseReader(pb)
if err != nil {
b.Error("ParseReader failed", err)
}
i += len(msg.DataRecords) + len(msg.TemplateRecords)
}
}
func BenchmarkParseBuffer(b *testing.B) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
p := NewSession()
_, err := p.ParseBuffer(p0)
if err != nil {
b.Fatal("ParseReader failed", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(1)
for i := 0; i < b.N; {
msg, err = p.ParseBuffer(p1)
if err != nil {
b.Error("ParseReader failed", err)
}
i += len(msg.DataRecords) + len(msg.TemplateRecords)
}
}
func BenchmarkMarshal(b *testing.B) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
p := NewSession()
_, err := p.ParseBuffer(p0)
if err != nil {
b.Fatal("ParseReader failed", err)
}
msg, err := p.ParseBuffer(p1)
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(1)
for i := 0; i < b.N; i++ {
_, err = p.Marshal(msg)
if err != nil {
b.Error("Marshal failed", err)
}
}
}
func TestParsingTemplateAndDataRecords(t *testing.T) {
packet, _ := hex.DecodeString("000a00405685b3700000000000bc614e000200140100000300080004000c0004000200040100001cc0a800c9c0a80001000000ebc0a800cac0a800010000002a")
p := NewSession()
msg, err := p.ParseBuffer(packet)
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if len(msg.TemplateRecords) != 1 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
if len(msg.DataRecords) != 2 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
}
func TestParsingBufferAll(t *testing.T) {
packet, _ := hex.DecodeString("000a00405685b3700000000000bc614e000200140100000300080004000c0004000200040100001cc0a800c9c0a80001000000ebc0a800cac0a800010000002a000a00405685b3700000000000bc614e000200140100000300080004000c0004000200040100001cc0a800c9c0a80001000000ebc0a800cac0a800010000002a000a00405685b3700000000000bc614e000200140100000300080004000c0004000200040100001cc0a800c9c0a80001000000ebc0a800cac0a800010000002a000a00405685b3700000000000bc614e000200140100000300080004000c0004000200040100001cc0a800c9c0a80001000000ebc0a800cac0a800010000002a")
p := NewSession()
msgs, err := p.ParseBufferAll(packet)
if err != nil {
t.Fatal("ParseBuffer failed", err)
}
if len(msgs) != 4 {
t.Error("Incorrent number of messages", len(msgs))
}
for _, msg := range msgs {
if len(msg.TemplateRecords) != 1 {
t.Error("Incorrect number of template records", len(msg.TemplateRecords))
}
if len(msg.DataRecords) != 2 {
t.Error("Incorrect number of data records", len(msg.DataRecords))
}
}
}
func TestLookupTemplateRecords(t *testing.T) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
p := NewSession()
_, err := p.ParseBuffer(p0)
if err != nil {
t.Fatal("ParseReader failed", err)
}
msg, err := p.ParseBuffer(p1)
trs, err := p.LookupTemplateRecords(msg)
if err != nil {
t.Fatal("LookupTemplateRecords failed", err)
}
if len(trs) != 1 {
t.Fatalf("LookupTemplateRecords returned %d records instead of expected 1", len(trs))
}
}