/
test-helper.go
122 lines (97 loc) · 3.53 KB
/
test-helper.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
// SPDX-License-Identifier: GPL-3.0-or-later
package testhelper
import (
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
// NewArpReplyReadResult creates mock ARP reply packet data
func NewArpReplyReadResult(srcIP net.IP, srcHwAddr net.HardwareAddr) (data []byte, ci gopacket.CaptureInfo, err error) {
eth := layers.Ethernet{
SrcMAC: srcHwAddr,
DstMAC: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeARP,
}
arp := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: layers.ARPReply, // ARP reply
SourceHwAddress: srcHwAddr, // Your MAC address
SourceProtAddress: []byte(srcIP.To4()), // Your IP address
DstHwAddress: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Target MAC address
DstProtAddress: []byte{192, 168, 1, 1}, // Target IP address
}
var payload gopacket.Payload
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
// Serialize the ARP packet.
gopacket.SerializeLayers(buf, opts, ð, &arp, &payload)
return buf.Bytes(), gopacket.CaptureInfo{}, nil
}
// NewArpRequestReadResult creates mock ARP request packet data
func NewArpRequestReadResult() (data []byte, ci gopacket.CaptureInfo, err error) {
eth := layers.Ethernet{
SrcMAC: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeARP,
}
arp := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: layers.ARPRequest, // ARP reply
SourceHwAddress: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Your MAC address
SourceProtAddress: []byte{192, 168, 1, 100}, // Your IP address
DstHwAddress: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Target MAC address
DstProtAddress: []byte{192, 168, 1, 1}, // Target IP address
}
var payload gopacket.Payload
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
// Serialize the ARP packet.
gopacket.SerializeLayers(buf, opts, ð, &arp, &payload)
return buf.Bytes(), gopacket.CaptureInfo{}, nil
}
// NewSynWithAckResponsePacketBytes creates mock SYN response packet data
func NewSynWithAckResponsePacketBytes(
srcIP net.IP,
srcPort uint16,
listenPort uint16,
) (data []byte, ci gopacket.CaptureInfo, err error) {
eth := layers.Ethernet{
SrcMAC: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeIPv4,
}
ip4 := layers.IPv4{
SrcIP: srcIP.To4(),
DstIP: net.ParseIP("127.0.0.1").To4(),
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
tcp := layers.TCP{
SrcPort: layers.TCPPort(srcPort),
DstPort: layers.TCPPort(listenPort),
SYN: true,
ACK: true,
}
tcp.SetNetworkLayerForChecksum(&ip4)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
// Serialize the ARP packet.
gopacket.SerializeLayers(buf, opts, ð, &ip4, &tcp)
return buf.Bytes(), gopacket.CaptureInfo{}, nil
}