forked from mdlayher/arp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_request_test.go
303 lines (268 loc) · 7.21 KB
/
client_request_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
package arp
import (
"bytes"
"errors"
"io"
"net"
"testing"
)
func TestClientRequestInvalidSourceHardwareAddr(t *testing.T) {
c := &Client{
ifi: &net.Interface{},
}
_, got := c.Resolve(net.IPv4zero)
if want := ErrInvalidHardwareAddr; want != got {
t.Fatalf("unexpected error for invalid source hardware address:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestIPv6Address(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
}
_, got := c.Resolve(net.IPv6loopback)
if want := ErrInvalidIP; want != got {
t.Fatalf("unexpected error for IPv6 address:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestErrWriteTo(t *testing.T) {
errWriteTo := errors.New("test error")
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4zero,
p: &errWriteToPacketConn{
err: errWriteTo,
},
}
_, got := c.Resolve(net.IPv4zero)
if want := errWriteTo; want != got {
t.Fatalf("unexpected error during WriteTo:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestErrReadFrom(t *testing.T) {
errReadFrom := errors.New("test error")
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4zero,
p: &errReadFromPacketConn{
err: errReadFrom,
},
}
_, got := c.Resolve(net.IPv4zero)
if want := errReadFrom; want != got {
t.Fatalf("unexpected error during ReadFrom:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestEthernetFrameUnexpectedEOF(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4zero,
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer([]byte{0}),
},
}
_, got := c.Resolve(net.IPv4zero)
if want := io.ErrUnexpectedEOF; want != got {
t.Fatalf("unexpected error while reading ethernet frame:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestEthernetFrameWrongDestinationHardwareAddr(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad},
},
ip: net.IPv4zero,
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer(append([]byte{
// Ethernet frame with wrong destination hardware address
0, 0, 0, 0, 0, 0, // Wrong destination
0, 0, 0, 0, 0, 0,
0x00, 0x00,
}, make([]byte, 46)...)),
},
}
_, got := c.Resolve(net.IPv4zero)
if want := io.EOF; want != got {
t.Fatalf("unexpected error while reading ethernet frame with wrong destination hardware address:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestEthernetFrameWrongEtherType(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4zero,
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer(append([]byte{
// Ethernet frame with non-ARP EtherType
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0x00, 0x00, // Wrong EtherType
}, make([]byte, 46)...)),
},
}
_, got := c.Resolve(net.IPv4zero)
if want := io.EOF; want != got {
t.Fatalf("unexpected error while reading ethernet frame with wrong EtherType:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestARPPacketUnexpectedEOF(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4zero,
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer(append([]byte{
// Ethernet frame
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0x08, 0x06,
// ARP packet with misleading hardware address length
0, 0,
0, 0,
255, 255, // Misleading hardware address length
}, make([]byte, 40)...)),
},
}
_, got := c.Resolve(net.IPv4zero)
if want := io.ErrUnexpectedEOF; want != got {
t.Fatalf("unexpected error while reading ARP packet:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestARPRequestInsteadOfResponse(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4zero,
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer(append([]byte{
// Ethernet frame
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0x08, 0x06,
// ARP request, not response
0, 1,
0x08, 0x06,
6,
4,
0, 1, // Request, not Response
0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
}, make([]byte, 46)...)),
},
}
_, got := c.Resolve(net.IPv4zero)
if want := io.EOF; want != got {
t.Fatalf("unexpected error while reading ARP response with wrong operation type:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestARPResponseWrongSenderIP(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
},
ip: net.IPv4(192, 168, 1, 1).To4(),
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer(append([]byte{
// Ethernet frame
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0x08, 0x06,
// ARP Packet not bound for this IP address
0, 1,
0x08, 0x06,
6,
4,
0, 2,
0, 0, 0, 0, 0, 0,
192, 168, 1, 10, // Wrong IP address
0, 0, 0, 0, 0, 0,
192, 168, 1, 1,
}, make([]byte, 46)...)),
},
}
_, got := c.Resolve(net.IPv4zero)
if want := io.EOF; want != got {
t.Fatalf("unexpected error while reading ARP response with wrong sender IP:\n- want: %v\n- got: %v",
want, got)
}
}
func TestClientRequestOK(t *testing.T) {
c := &Client{
ifi: &net.Interface{
HardwareAddr: net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad},
},
ip: net.IPv4(192, 168, 1, 1).To4(),
p: &bufferReadFromPacketConn{
b: bytes.NewBuffer(append([]byte{
// Ethernet frame
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x08, 0x06,
// ARP Packet
0, 1,
0x08, 0x06,
6,
4,
0, 2,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
192, 168, 1, 10,
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, // mac needn't match ours
192, 168, 1, 2, // ip needn't match ours
}, make([]byte, 40)...)),
},
}
wantHW := net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
gotHW, err := c.Resolve(net.IPv4(192, 168, 1, 10))
if err != nil {
t.Fatal(err)
}
if want, got := wantHW, gotHW; !bytes.Equal(want, got) {
t.Fatalf("unexpected hardware address for request:\n- want: %v\n- got: %v",
want, got)
}
}
// bufferReadFromPacketConn is a net.PacketConn which copies bytes from its
// embedded buffer into b when when its ReadFrom method is called.
type bufferReadFromPacketConn struct {
b *bytes.Buffer
noopPacketConn
}
func (p *bufferReadFromPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, err := p.b.Read(b)
return n, nil, err
}
// errWriteToPacketConn is a net.PacketConn which always returns its embedded
// error when its WriteTo method is called.
type errWriteToPacketConn struct {
err error
noopPacketConn
}
func (p *errWriteToPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) { return 0, p.err }
// errReadFromPacketConn is a net.PacketConn which always returns its embedded
// error when its ReadFrom method is called.
type errReadFromPacketConn struct {
err error
noopPacketConn
}
func (p *errReadFromPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, p.err }