forked from robfig/pcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap_test.go
157 lines (127 loc) · 3.25 KB
/
pcap_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
package pcap
import (
"fmt"
"net"
"testing"
"time"
)
func TestPcap(t *testing.T) {
h, err := OpenOffline("test/pcap_files/Network_Join_Nokia_Mobile.pcap")
if h == nil {
t.Fail()
return
}
_ = err
}
type pcapNewHandleFunc func(intf string, filter string, readTo int32) (h *Pcap, err error)
func testPcapHandle(t *testing.T, newHandle pcapNewHandleFunc) {
port := 54321
h, err := newHandle("lo", fmt.Sprintf("udp dst port %d", port), 2000)
if h == nil || err != nil {
if h != nil {
h.Close()
}
t.Fatalf("Failed to create/init pcap handle err:%s", err)
}
numPkts := 5
go udpSvr(port, numPkts, t)
go udpClient(port, numPkts, 1*time.Second, t)
pktsRecvd := 0
for pkt := h.Next(); pkt != nil; pkt = h.Next() {
pkt.Decode()
t.Logf("Packet:%s dataLen:%d", pkt, len(pkt.Payload))
pktsRecvd += 1
}
if pktsRecvd != numPkts {
t.Fatalf("Capture failed pkts-send:%d, pkts-recvd:%d", numPkts, pktsRecvd)
}
t.Logf("Successfully captured %d packets", numPkts)
}
func TestPcapCreate(t *testing.T) {
testPcapHandle(t, pcapCreate)
}
func TestPcapOpenLive(t *testing.T) {
testPcapHandle(t, pcapOpenLive)
}
func pcapCreate(intf string, filter string, readTo int32) (h *Pcap, err error) {
h, err = Create("lo")
if h == nil || err != nil {
return
}
err = h.SetSnapLen(65535)
if err != nil {
return
}
err = h.SetReadTimeout(readTo)
if err != nil {
return
}
err = h.SetBufferSize(3 * 1024 * 1024)
if err != nil {
return
}
err = h.Activate()
if err != nil {
return
}
err = h.SetFilter(filter)
if err != nil {
return
}
return
}
func pcapOpenLive(intf string, filter string, readTo int32) (h *Pcap, err error) {
h, err = OpenLive(intf, 65535, true, readTo)
if h == nil || err != nil {
return
}
err = h.SetFilter(filter)
if err != nil {
return
}
return
}
// Udp client which sends a fixed num of packets to given port after a fixed delay.
// Delay ensures that capture code is ready to recv packets.
func udpClient(port int, numPkts int, wait time.Duration, t *testing.T) {
time.Sleep(wait)
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
t.Logf("ERROR Failed to resolve udp addr err:%s", err)
return
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
t.Logf("ERROR Failed to dial udp port:%d err:%s", port, err)
return
}
t.Logf("Start packets to port:%d", port)
pkt := []byte("hello")
for i := 0; i < numPkts; i++ {
if l, err := conn.Write(pkt); err != nil || l != len(pkt) {
t.Logf("ERROR Failed to send packet size:%d wlen:%d err:%s", len(pkt), l, err)
}
}
t.Logf("Completed sending packets to port:%d", port)
}
// Udp server which listens on a port and recvs a fixed number of packets.
func udpSvr(port int, numPkts int, t *testing.T) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", port))
if err != nil {
t.Logf("ERROR UDP Server: failed to resolve udp addr err:%s", err)
return
}
sock, err := net.ListenUDP("udp", addr)
if err != nil {
t.Logf("ERROR UDP Server: failed to listen on port:%d err:%s", port, err)
return
}
buf := make([]byte, 10, 1024)
for i := 0; i < numPkts; i++ {
_, _, err := sock.ReadFromUDP(buf)
if err != nil {
t.Logf("Failed to recv packets on port:%d err:%s", port, err)
}
}
sock.Close()
}