This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
interpreter_test.go
152 lines (129 loc) · 11.8 KB
/
interpreter_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
package ipfix
import (
"bytes"
"encoding/hex"
"net"
"reflect"
"testing"
)
func TestInterpretUint(t *testing.T) {
bs := []byte{0xf7, 2, 3, 4, 5, 6, 7, 8}
v := interpretBytes(&bs, Uint64)
if v != uint64(0xf702030405060708) {
t.Errorf("%d != %d", v, uint64(0x0102030405060708))
}
bs = []byte{0xf7, 2, 3, 4}
v = interpretBytes(&bs, Uint32)
if v != uint32(0xf7020304) {
t.Errorf("%d != %d", v, 0x01020304)
}
bs = []byte{0xf7, 4}
v = interpretBytes(&bs, Uint16)
if v != uint16(0xf704) {
t.Errorf("%d != %d", v, 0x0104)
}
bs = []byte{0xf7}
v = interpretBytes(&bs, Uint8)
if v != uint8(0xf7) {
t.Errorf("%d != %d", v, 0xf7)
}
}
func TestInterpretInt(t *testing.T) {
bs := []byte{1, 2, 3, 4, 5, 6, 7, 8}
v := interpretBytes(&bs, Int64)
if v != int64(0x0102030405060708) {
t.Errorf("%d != %d", v, uint64(0x0102030405060708))
}
bs = []byte{1, 2, 3, 4}
v = interpretBytes(&bs, Int32)
if v != int32(0x01020304) {
t.Errorf("%d != %d", v, 0x01020304)
}
bs = []byte{1, 4}
v = interpretBytes(&bs, Int16)
if v != int16(0x0104) {
t.Errorf("%d != %d", v, 0x0104)
}
bs = []byte{14}
v = interpretBytes(&bs, Int8)
if v != int8(14) {
t.Errorf("%d != %d", v, 14)
}
}
func TestInterpretBool(t *testing.T) {
bs := []byte{2}
v := interpretBytes(&bs, Boolean)
if v != false {
t.Errorf("%v != %v", v, false)
}
bs = []byte{1}
v = interpretBytes(&bs, Boolean)
if v != true {
t.Errorf("%v != %v", v, true)
}
}
func TestInterpretString(t *testing.T) {
bs := []byte{0x48, 0x61, 0x6c, 0x6c, 0xc3, 0xa5, 0x0a}
v := interpretBytes(&bs, String)
if v != "Hallå\n" {
t.Errorf("%v != %v", v, "Hallå\n")
}
}
func TestInterpret(t *testing.T) {
p0, _ := hex.DecodeString("000a008c51ec4264000000000b20bdbe0002007c283b0008001c0010800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c25001b0010c2ac0008000c0004800c000400003c258003000800003c258004000800003c258012ffff00003c258001ffff00003c25801cffff00003c2500080004")
p1, _ := hex.DecodeString("000a05b051ec4270000000000b20bdbec2ac05a0ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043000116fcb8ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005e489f46ac10200300000026000000000000019f0000000000000160000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043007aa7519c0808080800000000000000000000008d00000000000000550003444e5300ac102082ac10200f0000000000000000000000940000000000000147000f426974546f7272656e74204b52504300b228265c1859c1570000000000000000000000000000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000920000000000000145000f426974546f7272656e74204b525043007b75a68ad92bb37f00000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004f972c247449d8f200000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430048b682a4ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300595cc40dac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b5250430057451cc1ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b525043005465e5a8ac1020ff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200f00000000000000000000008f000000000000014b000f426974546f7272656e74204b5250430001ab3c06ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b52504300befcacc8ffffffff00000000000000000000000000000000000000af001a44726f70626f78204c414e2073796e6320646973636f766572790764726f70626f78ac102013ac10200300000025000000000000019e0000000000000167000e4265696e6720616e616c797a656400c27ef905ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043006ca28bcdac10200f000000000000000000000091000000000000011c000f426974546f7272656e74204b52504300b13531caac10200f000000000000000000000068000000000000005f000f426974546f7272656e74204b5250430053df9212ac10200f0000000000000000000000940000000000000159000f426974546f7272656e74204b525043005f43f0b2ac10200f0000000000000000000001220000000000000252000f426974546f7272656e74204b52504300567ce6fbac10200100000000000000000000005a000000000000005a00034e545000ac102080ac10200f00000000000000000000008c000000000000013a000f426974546f7272656e74204b5250430055550ef7ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b52504300ba9322a2ac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043004579e7114b01bf5300000000000000000000006e0000000000000064000f426974546f7272656e74204b52504300ac10200fac10200f0000000000000000000000910000000000000136000f426974546f7272656e74204b525043005cf46adf")
b := new(bytes.Buffer)
p := NewSession()
b.Write(p0)
b.Write(p1)
p.ParseReader(b)
msg, err := p.ParseReader(b)
if err != nil {
t.Fatal("ParseReader failed", err)
}
i := NewInterpreter(p)
expected0 := InterpretedField{Name: "destinationIPv4Address", FieldID: 12, EnterpriseID: 0, Value: &net.IP{172, 16, 32, 15}}
expected1 := InterpretedField{Name: "", FieldID: 12, EnterpriseID: 15397, RawValue: []byte{0, 0, 0, 0}}
fl := i.Interpret(msg.DataRecords[0])
if !reflect.DeepEqual(fl[0], expected0) {
t.Error(fl[1], "!=\n", expected1)
}
if !reflect.DeepEqual(fl[1], expected1) {
t.Error(fl[1], "!=\n", expected1)
}
fl = i.InterpretInto(msg.DataRecords[0], make([]InterpretedField, len(fl)))
if !reflect.DeepEqual(fl[0], expected0) {
t.Error(fl[0], "!=\n", expected0)
}
if !reflect.DeepEqual(fl[1], expected1) {
t.Error(fl[1], "!=\n", expected1)
}
}
func TestIssue12(t *testing.T) {
p0, _ := hex.DecodeString("000a05885936edb4000949ca00000000000200440103000f00080004000c0004000f000400070002000b000200060001000a0002000e000200020004000100040098000800990008000400010005000100d1000401030268c0a83973c0a839a300000000edd6a4121b0002ffff00000009000004c00000015c7e901c4f0000015c7e901c970600f1000000c0a839a3c0a8397300000000a412edd61fffff00020000000900000b920000015c7e901c4f0000015c7e901c970600f1000000c0a839a3c0a8397300000000a412ede61fffff00020000000c000017aa0000015c7e901f6b0000015c7e901fbb0600f1000000c0a83973c0a839a300000000ede6a4121b0002ffff0000000a000004f40000015c7e901f6b0000015c7e901fbb0600f1000000c0a83973c0a839a300000000aa31a4101b0002ffff0000000b000005010000015c7e9027630000015c7e9027af0600f1000000c0a839a3c0a8397300000000a410aa311fffff00020000000e00003a8a0000015c7e9027630000015c7e9027af0600f1000000c0a839a3c0a8397300000000a412ee2c1fffff00020000000900000b8d0000015c7e902fd70000015c7e9030230600f1000000c0a83973c0a839a300000000ee2ca4121b0002ffff00000009000004c00000015c7e902fd70000015c7e9030230600f1000000c0a83973c0a839a300000000ee42a4121b0002ffff0000000a000004f40000015c7e9032f30000015c7e90333f0600f1000000c0a839a3c0a8397300000000a412ee421fffff00020000000c000017a90000015c7e9032f30000015c7e90333f0600f1000000c0a839a3c0a8397300000000a410aaa91fffff00020000000e00003a920000015c7e903aeb0000015c7e903b430600f1000000c0a83973c0a839a300000000aaa9a4101b0002ffff0000000c000005350000015c7e903aeb0000015c7e903b430600f100000001050064c0a839a34d580808c0a839fe90ed003500ffff0002000000020000006e0000015c7e9040a70000015c7e9040a711004d580808c0a839a300000000003590ed000002ffff000000020000009a0000015c7e9040a70000015c7e9040a71100000001030268c0a83913c0a839a30000000003f40801180002ffff00000016000006580000015c7e8f90f70000015c7e907bb7060081000000c0a83973c0a839a300000000805ca4121b0002ffff00000009000004c00000015c7e90435f0000015c7e9043ab0600f1000000c0a839a3c0a8397300000000a412805c1fffff00020000000900000b930000015c7e90435f0000015c7e9043ab0600f1000000c0a83973c0a839a3000000008070a4121b0002ffff0000000b000005280000015c7e90467b0000015c7e9046c70600f1000000c0a839a3c0a8397300000000a41280701fffff00020000000c000017aa0000015c7e90467b0000015c7e9046c70600f10000007f0000017f00000100000000e87700501b0001ffff00000005000001b30000015c7e9048db0000015c7e9048fb0600f10000007f0000017f000001000000000050e8771b0001ffff000000050000027f0000015c7e9048db0000015c7e9048fb0600f10000007f0000017f00000100000000e87700501bffff000100000005000001b30000015c7e9048db0000015c7e9048fb0600f10000007f0000017f000001000000000050e8771bffff0001000000050000027f0000015c7e9048db0000015c7e9048fb0600f1000000c0a839a3c0a8399d00000000c9a60cea02ffff000200000003000000b40000015c7e9040cb0000015c7e904c7b0600f1000000c0a839a3c0a8399d00000000c9a30cea02ffff000200000003000000b40000015c7e9040c30000015c7e904c7b0600f1000000c0a839a3c0a8399d00000000c9a40cea02ffff000200000003000000b40000015c7e9040c70000015c7e904c7b0600f1000000")
p1, _ := hex.DecodeString("000a05765936edbb000949e40000000001030138c0a839a3c0a8399d00000000c9a50cea02ffff000200000003000000b40000015c7e9040cb0000015c7e904c7b0600f1000000c0a83973c0a839a300000000ab0ea4101b0002ffff0000000b000005010000015c7e904e730000015c7e904ec30600f1000000c0a839a3c0a8397300000000a410ab0e1fffff00020000000e00003aa20000015c7e904e730000015c7e904ec30600f1000000c0a839a3c0a8391300000000080103f418ffff0002000000110000062c0000015c7e8f90f70000015c7e908edb060081000000c0a83973c0a839a30000000080c1a4121b0002ffff00000009000004c00000015c7e9056e70000015c7e90572f0600f1000000c0a839a3c0a8397300000000a41280c11fffff00020000000900000b920000015c7e9056eb0000015c7e90572f0600f100000000000104005cc0a839a3c0a839a3000000000301ffff00010000000c000004200000015c7e904c7b0000015c7e90583301c0c0a839a3c0a839a30000000003010001ffff0000000c000004200000015c7e904c7b0000015c7e90583301c001050064c0a839a3c2bea801c0a839fe007b007b00ffff000200000004000001300000015c7e90410b0000015c7e90587b1100c2bea801c0a839a300000000007b007b000002ffff00000004000001300000015c7e90410f0000015c7e90587f1100000001030138c0a839a3c0a8397300000000a41280d01fffff00020000000c000017a60000015c7e905a030000015c7e905a470600f1000000c0a83973c0a839a30000000080d0a4121b0002ffff0000000a000004f40000015c7e905a030000015c7e905a470600f10000007f0000017f000001000000000050e8781bffff0001000000050000027f0000015c7e9060cb0000015c7e9060eb0600f10000007f0000017f000001000000000050e8781b0001ffff000000050000027f0000015c7e9060cb0000015c7e9060eb0600f10000007f0000017f00000100000000e87800501b0001ffff000000050000019a0000015c7e9060cb0000015c7e9060eb0600f10000007f0000017f00000100000000e87800501bffff0001000000050000019a0000015c7e9060cb0000015c7e9060eb0600f100000000000105003400000000ffffffff0000000000440043000002ffff00000001000001480000015c7e9061ab0000015c7e9061ab11000001030202c0a839a3c0a8390c000000000801025d18ffff000200000011000005b80000015c7e8f9e5f0000015c7e909c4b060081000000c0a83973c0a839a300000000ab69a4101b0002ffff0000000b000005010000015c7e9061fb0000015c7e9062470600f1000000c0a839a3c0a8397300000000a410ab691fffff00020000000e00003aa60000015c7e9061fb0000015c7e9062470600f10000007f0000017f000001000000000050e8791b0001ffff000000050000027f0000015c7e9064670000015c7e90648f0600f10000007f0000017f000001000000000050e8791bffff0001000000050000027f0000015c7e9064670000015c7e90648f0600f10000007f0000017f00000100000000e87900501b0001ffff00000005000001b30000015c7e9064670000015c7e90648f0600f10000007f0000017f00000100000000e87900501bffff000100000005000001b30000015c7e9064670000015c7e90648f0600f10000007f0000017f00000100000000e87a00501b0001ffff00000005000001b30000015c7e9068330000015c7e9068530600f10000007f0000017f000001000000000050e87a1bffff0001000000050000027f0000015c7e9068330000015c7e9068530600f10000007f0000017f00000100000000e87a00501bffff000100000005000001b30000015c7e9068330000015c7e9068530600f1000000")
b := new(bytes.Buffer)
p := NewSession()
b.Write(p0)
b.Write(p1)
p.ParseReader(b)
msg, err := p.ParseReader(b)
if err != nil {
t.Fatal("ParseReader failed", err)
}
i := NewInterpreter(p)
fl := i.Interpret(msg.DataRecords[0])
var ok bool
for _, f := range fl {
if f.Name == "octetDeltaCount" {
ok = true
if val, ok := f.Value.(uint64); !ok || val != 0xb4 {
t.Errorf("Got unexpected type or value, want uint64(0xb4), got %#v", f.Value)
}
break
}
}
if !ok {
t.Error("Didn't find expected field")
}
}