-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraw_linux_test.go
363 lines (296 loc) · 8.28 KB
/
raw_linux_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// +build linux
package raw
import (
"bytes"
"net"
"syscall"
"testing"
"time"
"unsafe"
"golang.org/x/net/bpf"
)
// Test to ensure that socket is bound with correct sockaddr_ll information
type bindSocket struct {
bind syscall.Sockaddr
noopSocket
}
func (s *bindSocket) Bind(sa syscall.Sockaddr) error {
s.bind = sa
return nil
}
func Test_newPacketConnBind(t *testing.T) {
s := &bindSocket{}
ifIndex := 1
protocol := uint16(1)
_, err := newPacketConn(
&net.Interface{
Index: ifIndex,
},
s,
protocol,
)
if err != nil {
t.Fatal(err)
}
sall, ok := s.bind.(*syscall.SockaddrLinklayer)
if !ok {
t.Fatalf("bind sockaddr has incorrect type: %T", s.bind)
}
if want, got := ifIndex, sall.Ifindex; want != got {
t.Fatalf("unexpected network interface index:\n- want: %v\n- got: %v", want, got)
}
if want, got := protocol, sall.Protocol; want != got {
t.Fatalf("unexpected protocol:\n- want: %v\n- got: %v", want, got)
}
}
// Test for incorrect sockaddr type after recvfrom on a socket.
type addrRecvfromSocket struct {
addr syscall.Sockaddr
noopSocket
}
func (s *addrRecvfromSocket) Recvfrom(p []byte, flags int) (int, syscall.Sockaddr, error) {
return 0, s.addr, nil
}
func Test_packetConnReadFromRecvfromInvalidSockaddr(t *testing.T) {
p, err := newPacketConn(
&net.Interface{},
&addrRecvfromSocket{
addr: &syscall.SockaddrInet4{},
},
0,
)
if err != nil {
t.Fatal(err)
}
_, _, err = p.ReadFrom(nil)
if want, got := syscall.EINVAL, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
}
// Test for malformed hardware address after recvfrom on a socket
func Test_packetConnReadFromRecvfromInvalidHardwareAddr(t *testing.T) {
p, err := newPacketConn(
&net.Interface{},
&addrRecvfromSocket{
addr: &syscall.SockaddrLinklayer{
Halen: 5,
},
},
0,
)
if err != nil {
t.Fatal(err)
}
_, _, err = p.ReadFrom(nil)
if want, got := syscall.EINVAL, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
}
// Test for a correct ReadFrom with data and address.
type recvfromSocket struct {
p []byte
flags int
addr syscall.Sockaddr
noopSocket
}
func (s *recvfromSocket) Recvfrom(p []byte, flags int) (int, syscall.Sockaddr, error) {
copy(p, s.p)
s.flags = flags
return len(s.p), s.addr, nil
}
func Test_packetConnReadFromRecvfromOK(t *testing.T) {
const wantN = 4
data := []byte{0, 1, 2, 3}
deadbeefHW := net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
s := &recvfromSocket{
p: data,
addr: &syscall.SockaddrLinklayer{
Halen: 6,
Addr: [8]byte{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0x00, 0x00},
},
}
p, err := newPacketConn(
&net.Interface{},
s,
0,
)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 8)
n, addr, err := p.ReadFrom(buf)
if err != nil {
t.Fatal(err)
}
if want, got := 0, s.flags; want != got {
t.Fatalf("unexpected flags:\n- want: %v\n- got: %v", want, got)
}
raddr, ok := addr.(*Addr)
if !ok {
t.Fatalf("read sockaddr has incorrect type: %T", addr)
}
if want, got := deadbeefHW, raddr.HardwareAddr; !bytes.Equal(want, got) {
t.Fatalf("unexpected hardware address:\n- want: %v\n- got: %v", want, got)
}
if want, got := wantN, n; want != got {
t.Fatalf("unexpected data length:\n- want: %v\n- got: %v", want, got)
}
if want, got := data, buf[:n]; !bytes.Equal(want, got) {
t.Fatalf("unexpected data:\n- want: %v\n- got: %v", want, got)
}
}
// Test for incorrect sockaddr type for WriteTo.
func Test_packetConnWriteToInvalidSockaddr(t *testing.T) {
_, err := (&packetConn{}).WriteTo(nil, &net.IPAddr{})
if want, got := syscall.EINVAL, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
}
// Test for malformed hardware address with WriteTo.
func Test_packetConnWriteToInvalidHardwareAddr(t *testing.T) {
addrs := []net.HardwareAddr{
// Too short.
{0xde, 0xad, 0xbe, 0xef, 0xde},
// Explicitly nil.
nil,
}
for _, addr := range addrs {
_, err := (&packetConn{}).WriteTo(nil, &Addr{
HardwareAddr: addr,
})
if want, got := syscall.EINVAL, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
}
}
// Test for a correct WriteTo with data and address.
type sendtoSocket struct {
p []byte
flags int
addr syscall.Sockaddr
noopSocket
}
func (s *sendtoSocket) Sendto(p []byte, flags int, to syscall.Sockaddr) error {
copy(s.p, p)
s.flags = flags
s.addr = to
return nil
}
func Test_packetConnWriteToSendtoOK(t *testing.T) {
const wantN = 4
data := []byte{0, 1, 2, 3}
deadbeefHW := net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
s := &sendtoSocket{
p: make([]byte, wantN),
}
p, err := newPacketConn(
&net.Interface{},
s,
0,
)
if err != nil {
t.Fatal(err)
}
n, err := p.WriteTo(data, &Addr{
HardwareAddr: deadbeefHW,
})
if err != nil {
t.Fatal(err)
}
if want, got := 0, s.flags; want != got {
t.Fatalf("unexpected flags:\n- want: %v\n- got: %v", want, got)
}
if want, got := wantN, n; want != got {
t.Fatalf("unexpected data length:\n- want: %v\n- got: %v", want, got)
}
if want, got := data, s.p; !bytes.Equal(want, got) {
t.Fatalf("unexpected data:\n- want: %v\n- got: %v", want, got)
}
sall, ok := s.addr.(*syscall.SockaddrLinklayer)
if !ok {
t.Fatalf("write sockaddr has incorrect type: %T", s.addr)
}
if want, got := deadbeefHW, sall.Addr[:][:sall.Halen]; !bytes.Equal(want, got) {
t.Fatalf("unexpected hardware address:\n- want: %v\n- got: %v", want, got)
}
}
// Test that socket close functions as intended.
type captureCloseSocket struct {
closed bool
noopSocket
}
func (s *captureCloseSocket) Close() error {
s.closed = true
return nil
}
func Test_packetConnClose(t *testing.T) {
s := &captureCloseSocket{}
p := &packetConn{
s: s,
}
if err := p.Close(); err != nil {
t.Fatal(err)
}
if !s.closed {
t.Fatalf("socket should be closed, but is not")
}
}
// Test that LocalAddr returns the hardware address of the network interface
// which is being used by the socket.
func Test_packetConnLocalAddr(t *testing.T) {
deadbeefHW := net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
p := &packetConn{
ifi: &net.Interface{
HardwareAddr: deadbeefHW,
},
}
if want, got := deadbeefHW, p.LocalAddr().(*Addr).HardwareAddr; !bytes.Equal(want, got) {
t.Fatalf("unexpected hardware address:\n- want: %v\n- got: %v", want, got)
}
}
// Test that BPF filter attachment works as intended.
type setSockoptSocket struct {
setsockopt func(level, name int, v unsafe.Pointer, l uint32) error
noopSocket
}
func (s *setSockoptSocket) SetSockopt(level, name int, v unsafe.Pointer, l uint32) error {
return s.setsockopt(level, name, v, l)
}
func Test_packetConnSetBPF(t *testing.T) {
filter, err := bpf.Assemble([]bpf.Instruction{
bpf.RetConstant{Val: 0},
})
if err != nil {
t.Fatalf("failed to assemble filter: %v", err)
}
fn := func(level, name int, _ unsafe.Pointer, _ uint32) error {
// Though we can't check the filter itself, we can check the setsockopt
// level and name for correctness.
if want, got := syscall.SOL_SOCKET, level; want != got {
t.Fatalf("unexpected setsockopt level:\n- want: %v\n- got: %v", want, got)
}
if want, got := syscall.SO_ATTACH_FILTER, name; want != got {
t.Fatalf("unexpected setsockopt name:\n- want: %v\n- got: %v", want, got)
}
return nil
}
s := &setSockoptSocket{
setsockopt: fn,
}
p := &packetConn{
s: s,
}
if err := p.SetBPF(filter); err != nil {
t.Fatalf("failed to attach filter: %v", err)
}
}
// noopSocket is a socket implementation which noops every operation. It is
// the basis for more specific socket implementations.
type noopSocket struct{}
func (noopSocket) Bind(sa syscall.Sockaddr) error { return nil }
func (noopSocket) Close() error { return nil }
func (noopSocket) FD() int { return 0 }
func (noopSocket) Recvfrom(p []byte, flags int) (int, syscall.Sockaddr, error) { return 0, nil, nil }
func (noopSocket) Sendto(p []byte, flags int, to syscall.Sockaddr) error { return nil }
func (noopSocket) SetSockopt(level, name int, v unsafe.Pointer, l uint32) error { return nil }
func (noopSocket) SetTimeout(timeout time.Duration) error { return nil }