-
Notifications
You must be signed in to change notification settings - Fork 39
/
packet_test.go
327 lines (276 loc) · 10.1 KB
/
packet_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
package main
import (
"io"
"net"
"reflect"
"testing"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
var (
srcMACTest = net.HardwareAddr{0xFF, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA}
dstMACTest = net.HardwareAddr{0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD}
brMACTest = net.HardwareAddr{0xF2, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA}
vlanIdentifierTest = uint16(30)
srcIPv4Test = net.IP{127, 0, 0, 1}
dstIPv4Test = net.IP{224, 0, 0, 251}
srcIPv6Test = net.ParseIP("::1")
dstIPv6Test = net.ParseIP("ff02::fb")
srcUDPPortTest = layers.UDPPort(5353)
dstUDPPortTest = layers.UDPPort(5353)
questionPayloadTest = []byte{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97,
109, 112, 108, 101, 3, 99, 111, 109, 0, 0, 1, 0, 1}
)
func createMockmDNSPacket(isIPv4 bool, isDNSQuery bool) []byte {
if isIPv4 {
return createRawPacket(isIPv4, isDNSQuery, vlanIdentifierTest, dstIPv4Test, srcMACTest, dstMACTest, dstUDPPortTest)
}
return createRawPacket(isIPv4, isDNSQuery, vlanIdentifierTest, dstIPv6Test, srcMACTest, dstMACTest, dstUDPPortTest)
}
func createRawPacket(isIPv4 bool, isDNSQuery bool, vlanTag uint16, dstIP net.IP, srcMAC net.HardwareAddr, dstMAC net.HardwareAddr, dstPort layers.UDPPort) []byte {
var ethernetLayer, dot1QLayer, ipLayer, udpLayer, dnsLayer gopacket.SerializableLayer
ethernetLayer = &layers.Ethernet{
SrcMAC: srcMAC,
DstMAC: dstMAC,
EthernetType: layers.EthernetTypeDot1Q,
}
if isIPv4 {
dot1QLayer = &layers.Dot1Q{
VLANIdentifier: vlanTag,
Type: layers.EthernetTypeIPv4,
}
ipLayer = &layers.IPv4{
SrcIP: srcIPv4Test,
DstIP: dstIP,
Version: 4,
Protocol: layers.IPProtocolUDP,
Length: 146,
IHL: 5,
TOS: 0,
}
} else {
dot1QLayer = &layers.Dot1Q{
VLANIdentifier: vlanTag,
Type: layers.EthernetTypeIPv6,
}
ipLayer = &layers.IPv6{
SrcIP: srcIPv6Test,
DstIP: dstIP,
Version: 6,
Length: 48,
NextHeader: layers.IPProtocolUDP,
}
}
udpLayer = &layers.UDP{
SrcPort: srcUDPPortTest,
DstPort: dstPort,
}
if isDNSQuery {
dnsLayer = &layers.DNS{
Questions: []layers.DNSQuestion{layers.DNSQuestion{
Name: []byte("example.com"),
Type: layers.DNSTypeA,
Class: layers.DNSClassIN,
}},
QDCount: 1,
}
} else {
dnsLayer = &layers.DNS{
Answers: []layers.DNSResourceRecord{layers.DNSResourceRecord{
Name: []byte("example.com"),
Type: layers.DNSTypeA,
Class: layers.DNSClassIN,
TTL: 1024,
IP: net.IP([]byte{1, 2, 3, 4}),
}},
ANCount: 1,
QR: true,
}
}
buffer := gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(
buffer,
gopacket.SerializeOptions{},
ethernetLayer,
dot1QLayer,
ipLayer,
udpLayer,
dnsLayer,
)
return buffer.Bytes()
}
func TestParseEthernetLayer(t *testing.T) {
decoder := gopacket.DecodersByLayerName["Ethernet"]
options := gopacket.DecodeOptions{Lazy: true}
packet := gopacket.NewPacket(createMockmDNSPacket(true, true), decoder, options)
expectedResult1, expectedResult2 := &srcMACTest, &dstMACTest
computedResult1, computedResult2 := parseEthernetLayer(packet)
if !reflect.DeepEqual(expectedResult1, computedResult1) || !reflect.DeepEqual(expectedResult2, computedResult2) {
t.Error("Error in parseEthernetLayer()")
}
}
func TestParseVLANTag(t *testing.T) {
decoder := gopacket.DecodersByLayerName["Ethernet"]
options := gopacket.DecodeOptions{Lazy: true}
packet := gopacket.NewPacket(createMockmDNSPacket(true, true), decoder, options)
expectedLayer := &layers.Dot1Q{
VLANIdentifier: vlanIdentifierTest,
Type: layers.EthernetTypeIPv4,
}
expectedResult := &expectedLayer.VLANIdentifier
computedResult := parseVLANTag(packet)
if !reflect.DeepEqual(expectedResult, computedResult) {
t.Error("Error in parseEthernetLayer()")
}
}
func TestParseIPLayer(t *testing.T) {
decoder := gopacket.DecodersByLayerName["Ethernet"]
options := gopacket.DecodeOptions{Lazy: true}
isIPv4 := true
ipv4Packet := gopacket.NewPacket(createMockmDNSPacket(isIPv4, true), decoder, options)
computedIsIPv6 := parseIPLayer(ipv4Packet)
if computedIsIPv6 == true {
t.Error("Error in parseIPLayer() for IPv4 addresses")
}
isIPv4 = false
ipv6Packet := gopacket.NewPacket(createMockmDNSPacket(isIPv4, true), decoder, options)
computedIsIPv6 = parseIPLayer(ipv6Packet)
if computedIsIPv6 == false {
t.Error("Error in parseIPLayer() for IPv6 addresses")
}
}
func TestParseUDPLayer(t *testing.T) {
decoder := gopacket.DecodersByLayerName["Ethernet"]
options := gopacket.DecodeOptions{Lazy: true}
packet := gopacket.NewPacket(createMockmDNSPacket(true, true), decoder, options)
questionPacketPayload := parseUDPLayer(packet)
if !reflect.DeepEqual(questionPayloadTest, questionPacketPayload) {
t.Error("Error in parseUDPLayer()")
}
}
func TestParseDNSPayload(t *testing.T) {
decoder := gopacket.DecodersByLayerName["Ethernet"]
options := gopacket.DecodeOptions{Lazy: true}
questionPacket := gopacket.NewPacket(createMockmDNSPacket(true, true), decoder, options)
questionPacketPayload := parseUDPLayer(questionPacket)
questionExpectedResult := true
questionComputedResult := parseDNSPayload(questionPacketPayload)
if !reflect.DeepEqual(questionExpectedResult, questionComputedResult) {
t.Error("Error in parseDNSPayload() for DNS queries")
}
answerPacket := gopacket.NewPacket(createMockmDNSPacket(true, false), decoder, options)
answerPacketPayload := parseUDPLayer(answerPacket)
answerExpectedResult := false
answerComputedResult := parseDNSPayload(answerPacketPayload)
if !reflect.DeepEqual(answerExpectedResult, answerComputedResult) {
t.Error("Error in parseDNSPayload() for DNS answers")
}
}
type dataSource struct {
sentPackets int
data [][]byte
}
func (dataSource *dataSource) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
// Return one packet for each call.
// If all the expected packets have already been returned in the past, return an EOF error
// to end the reading of packets from this source.
if dataSource.sentPackets >= len(dataSource.data) {
return nil, ci, io.EOF
}
data = dataSource.data[dataSource.sentPackets]
ci = gopacket.CaptureInfo{
Timestamp: time.Time{},
CaptureLength: len(data),
Length: ci.CaptureLength,
InterfaceIndex: 0,
}
dataSource.sentPackets++
return data, ci, nil
}
func createMockPacketSource() (packetSource *gopacket.PacketSource, packet gopacket.Packet) {
// send one legitimate packet
// Return the packetSource and the legitimate packet
data := [][]byte{
createMockmDNSPacket(true, true)}
dataSource := &dataSource{
sentPackets: 0,
data: data,
}
decoder := gopacket.DecodersByLayerName["Ethernet"]
packetSource = gopacket.NewPacketSource(dataSource, decoder)
packet = gopacket.NewPacket(data[len(data)-1], decoder, gopacket.DecodeOptions{Lazy: true})
return
}
func areBonjourPacketsEqual(a, b bonjourPacket) (areEqual bool) {
areEqual = (*a.vlanTag == *b.vlanTag) && (a.srcMAC.String() == b.srcMAC.String()) && (a.isDNSQuery == b.isDNSQuery)
// While comparing Bonjour packets, we do not want to compare packets entirely.
// In particular, packet.metadata may be slightly different, we do not need them to be the same.
// So we only compare the layers part of the packets.
areEqual = areEqual && reflect.DeepEqual(a.packet.Layers(), b.packet.Layers())
return
}
func TestFilterBonjourPacketsLazily(t *testing.T) {
mockPacketSource, packet := createMockPacketSource()
packetChan := parsePacketsLazily(mockPacketSource)
expectedResult := bonjourPacket{
packet: packet,
vlanTag: &vlanIdentifierTest,
srcMAC: &srcMACTest,
isDNSQuery: true,
}
computedResult := <-packetChan
if !areBonjourPacketsEqual(expectedResult, computedResult) {
t.Error("Error in parsePacketsLazily()")
}
}
type mockPacketWriter struct {
packet gopacket.Packet
}
func (pw *mockPacketWriter) WritePacketData(bytes []byte) (err error) {
decoder := gopacket.DecodersByLayerName["Ethernet"]
pw.packet = gopacket.NewPacket(bytes, decoder, gopacket.DecodeOptions{Lazy: true})
return
}
func TestSendBonjourPacket(t *testing.T) {
// Craft a test packet
initialDataIPv4 := createMockmDNSPacket(true, true)
initialDataIPv6 := createMockmDNSPacket(false, true)
decoder := gopacket.DecodersByLayerName["Ethernet"]
initialPacketIPv4 := gopacket.NewPacket(initialDataIPv4, decoder, gopacket.DecodeOptions{Lazy: true})
initialPacketIPv6 := gopacket.NewPacket(initialDataIPv6, decoder, gopacket.DecodeOptions{Lazy: true})
srcMACv4, dstMACv4 := parseEthernetLayer(initialPacketIPv4)
bonjourTestPacketIPv4 := bonjourPacket{
packet: initialPacketIPv4,
vlanTag: parseVLANTag(initialPacketIPv4),
srcMAC: srcMACv4,
dstMAC: dstMACv4,
isDNSQuery: true,
isIPv6: false,
}
srcMACv6, dstMACv6 := parseEthernetLayer(initialPacketIPv6)
bonjourTestPacketIPv6 := bonjourPacket{
packet: initialPacketIPv6,
vlanTag: parseVLANTag(initialPacketIPv6),
srcMAC: srcMACv6,
dstMAC: dstMACv6,
isDNSQuery: true,
isIPv6: true,
}
newVlanTag := uint16(29)
expectedDstMACv4 := net.HardwareAddr{0x01, 0x00, 0x5E, 0x00, 0x00, 0xFB}
expectedDataIPv4 := createRawPacket(true, true, newVlanTag, dstIPv4Test, brMACTest, expectedDstMACv4, dstUDPPortTest)
expectedPacketIPv4 := gopacket.NewPacket(expectedDataIPv4, decoder, gopacket.DecodeOptions{Lazy: true})
expectedDstMACv6 := net.HardwareAddr{0x33, 0x33, 0x00, 0x00, 0x00, 0xFB}
expectedDataIPv6 := createRawPacket(false, true, newVlanTag, dstIPv6Test, brMACTest, expectedDstMACv6, dstUDPPortTest)
expectedPacketIPv6 := gopacket.NewPacket(expectedDataIPv6, decoder, gopacket.DecodeOptions{Lazy: true})
pw := &mockPacketWriter{packet: nil}
sendBonjourPacket(pw, &bonjourTestPacketIPv4, newVlanTag, brMACTest)
if !reflect.DeepEqual(expectedPacketIPv4.Layers(), pw.packet.Layers()) {
t.Error("Error in sendBonjourPacket() for IPv4")
}
sendBonjourPacket(pw, &bonjourTestPacketIPv6, newVlanTag, brMACTest)
if !reflect.DeepEqual(expectedPacketIPv6.Layers(), pw.packet.Layers()) {
t.Error("Error in sendBonjourPacket() for IPv6")
}
}