forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packetdecoder_test.go
207 lines (188 loc) · 4.59 KB
/
packetdecoder_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
package sflow
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
func TestUDPHeader(t *testing.T) {
octets := bytes.NewBuffer([]byte{
0x00, 0x01, // src_port
0x00, 0x02, // dst_port
0x00, 0x03, // udp_length
0x00, 0x00, // checksum
})
dc := NewDecoder()
actual, err := dc.decodeUDPHeader(octets)
require.NoError(t, err)
expected := UDPHeader{
SourcePort: 1,
DestinationPort: 2,
UDPLength: 3,
}
require.Equal(t, expected, actual)
}
func BenchmarkUDPHeader(b *testing.B) {
octets := bytes.NewBuffer([]byte{
0x00, 0x01, // src_port
0x00, 0x02, // dst_port
0x00, 0x03, // udp_length
0x00, 0x00, // checksum
})
dc := NewDecoder()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dc.decodeUDPHeader(octets)
}
}
func TestIPv4Header(t *testing.T) {
octets := bytes.NewBuffer(
[]byte{
0x45, // version + IHL
0x00, // ip_dscp + ip_ecn
0x00, 0x00, // total length
0x00, 0x00, // identification
0x00, 0x00, // flags + frag offset
0x00, // ttl
0x11, // protocol; 0x11 = udp
0x00, 0x00, // header checksum
0x7f, 0x00, 0x00, 0x01, // src ip
0x7f, 0x00, 0x00, 0x02, // dst ip
0x00, 0x01, // src_port
0x00, 0x02, // dst_port
0x00, 0x03, // udp_length
0x00, 0x00, // checksum
},
)
dc := NewDecoder()
actual, err := dc.decodeIPv4Header(octets)
require.NoError(t, err)
expected := IPV4Header{
Version: 0x40,
InternetHeaderLength: 0x05,
DSCP: 0,
ECN: 0,
TotalLength: 0,
Identification: 0,
Flags: 0,
FragmentOffset: 0,
TTL: 0,
Protocol: 0x11,
HeaderChecksum: 0,
SourceIP: [4]byte{127, 0, 0, 1},
DestIP: [4]byte{127, 0, 0, 2},
ProtocolHeader: UDPHeader{
SourcePort: 1,
DestinationPort: 2,
UDPLength: 3,
Checksum: 0,
},
}
require.Equal(t, expected, actual)
}
// Using the same Directive instance, prior paths through the parse tree should
// not affect the latest parse.
func TestIPv4HeaderSwitch(t *testing.T) {
octets := bytes.NewBuffer(
[]byte{
0x45, // version + IHL
0x00, // ip_dscp + ip_ecn
0x00, 0x00, // total length
0x00, 0x00, // identification
0x00, 0x00, // flags + frag offset
0x00, // ttl
0x11, // protocol; 0x11 = udp
0x00, 0x00, // header checksum
0x7f, 0x00, 0x00, 0x01, // src ip
0x7f, 0x00, 0x00, 0x02, // dst ip
0x00, 0x01, // src_port
0x00, 0x02, // dst_port
0x00, 0x03, // udp_length
0x00, 0x00, // checksum
},
)
dc := NewDecoder()
_, err := dc.decodeIPv4Header(octets)
require.NoError(t, err)
octets = bytes.NewBuffer(
[]byte{
0x45, // version + IHL
0x00, // ip_dscp + ip_ecn
0x00, 0x00, // total length
0x00, 0x00, // identification
0x00, 0x00, // flags + frag offset
0x00, // ttl
0x06, // protocol; 0x06 = tcp
0x00, 0x00, // header checksum
0x7f, 0x00, 0x00, 0x01, // src ip
0x7f, 0x00, 0x00, 0x02, // dst ip
0x00, 0x01, // src_port
0x00, 0x02, // dst_port
0x00, 0x00, 0x00, 0x00, // sequence
0x00, 0x00, 0x00, 0x00, // ack_number
0x00, 0x00, // tcp_header_length
0x00, 0x00, // tcp_window_size
0x00, 0x00, // checksum
0x00, 0x00, // tcp_urgent_pointer
},
)
dc = NewDecoder()
actual, err := dc.decodeIPv4Header(octets)
require.NoError(t, err)
expected := IPV4Header{
Version: 64,
InternetHeaderLength: 5,
Protocol: 6,
SourceIP: [4]byte{127, 0, 0, 1},
DestIP: [4]byte{127, 0, 0, 2},
ProtocolHeader: TCPHeader{
SourcePort: 1,
DestinationPort: 2,
},
}
require.Equal(t, expected, actual)
}
func TestUnknownProtocol(t *testing.T) {
octets := bytes.NewBuffer(
[]byte{
0x45, // version + IHL
0x00, // ip_dscp + ip_ecn
0x00, 0x00, // total length
0x00, 0x00, // identification
0x00, 0x00, // flags + frag offset
0x00, // ttl
0x99, // protocol
0x00, 0x00, // header checksum
0x7f, 0x00, 0x00, 0x01, // src ip
0x7f, 0x00, 0x00, 0x02, // dst ip
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
},
)
dc := NewDecoder()
actual, err := dc.decodeIPv4Header(octets)
require.NoError(t, err)
expected := IPV4Header{
Version: 64,
InternetHeaderLength: 5,
Protocol: 153,
SourceIP: [4]byte{127, 0, 0, 1},
DestIP: [4]byte{127, 0, 0, 2},
}
require.Equal(t, expected, actual)
}