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
|
// Copyright 2018 the u-root Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.12
package nclient4
import (
"bytes"
"context"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/hugelgupf/socketpair"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/server4"
)
type handler struct {
mu sync.Mutex
received []*dhcpv4.DHCPv4
// Each received packet can have more than one response (in theory,
// from different servers sending different Advertise, for example).
responses [][]*dhcpv4.DHCPv4
}
func (h *handler) handle(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) {
h.mu.Lock()
defer h.mu.Unlock()
h.received = append(h.received, m)
if len(h.responses) > 0 {
for _, resp := range h.responses[0] {
_, _ = conn.WriteTo(resp.ToBytes(), peer)
}
h.responses = h.responses[1:]
}
}
func serveAndClient(ctx context.Context, responses [][]*dhcpv4.DHCPv4, opts ...ClientOpt) (*Client, net.PacketConn) {
// Fake PacketConn connection.
clientRawConn, serverRawConn, err := socketpair.PacketSocketPair()
if err != nil {
panic(err)
}
clientConn := NewBroadcastUDPConn(clientRawConn, &net.UDPAddr{Port: ClientPort})
serverConn := NewBroadcastUDPConn(serverRawConn, &net.UDPAddr{Port: ServerPort})
o := []ClientOpt{WithRetry(1), WithTimeout(2 * time.Second)}
o = append(o, opts...)
mc, err := NewWithConn(clientConn, net.HardwareAddr{0xa, 0xb, 0xc, 0xd, 0xe, 0xf}, o...)
if err != nil {
panic(err)
}
h := &handler{responses: responses}
s, err := server4.NewServer("", nil, h.handle, server4.WithConn(serverConn))
if err != nil {
panic(err)
}
go func() {
_ = s.Serve()
}()
return mc, serverConn
}
func ComparePacket(got *dhcpv4.DHCPv4, want *dhcpv4.DHCPv4) error {
if got == nil && got == want {
return nil
}
if (want == nil || got == nil) && (got != want) {
return fmt.Errorf("packet got %v, want %v", got, want)
}
if !bytes.Equal(got.ToBytes(), want.ToBytes()) {
return fmt.Errorf("packet got %v, want %v", got, want)
}
return nil
}
func pktsExpected(got []*dhcpv4.DHCPv4, want []*dhcpv4.DHCPv4) error {
if len(got) != len(want) {
return fmt.Errorf("got %d packets, want %d packets", len(got), len(want))
}
for i := range got {
if err := ComparePacket(got[i], want[i]); err != nil {
return err
}
}
return nil
}
func newPacketWeirdHWAddr(op dhcpv4.OpcodeType, xid dhcpv4.TransactionID) *dhcpv4.DHCPv4 {
p, err := dhcpv4.New()
if err != nil {
panic(fmt.Sprintf("newpacket: %v", err))
}
p.OpCode = op
p.TransactionID = xid
p.ClientHWAddr = net.HardwareAddr{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 1, 2, 3, 4, 5, 6}
return p
}
func newPacket(op dhcpv4.OpcodeType, xid dhcpv4.TransactionID) *dhcpv4.DHCPv4 {
p, err := dhcpv4.New()
if err != nil {
panic(fmt.Sprintf("newpacket: %v", err))
}
p.OpCode = op
p.TransactionID = xid
p.ClientHWAddr = net.HardwareAddr{0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
return p
}
func TestSendAndRead(t *testing.T) {
for _, tt := range []struct {
desc string
send *dhcpv4.DHCPv4
server []*dhcpv4.DHCPv4
// If want is nil, we assume server[0] contains what is wanted.
want *dhcpv4.DHCPv4
wantErr error
}{
{
desc: "two response packets",
send: newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
server: []*dhcpv4.DHCPv4{
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
want: newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
{
desc: "one response packet",
send: newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
server: []*dhcpv4.DHCPv4{
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
want: newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
{
desc: "one response packet, one invalid XID, one invalid opcode, one invalid hwaddr",
send: newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
server: []*dhcpv4.DHCPv4{
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x77, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacketWeirdHWAddr(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
want: newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
{
desc: "discard wrong XID",
send: newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
server: []*dhcpv4.DHCPv4{
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0, 0, 0, 0}),
},
want: nil, // Explicitly empty.
wantErr: ErrNoResponse,
},
{
desc: "no response, timeout",
send: newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
wantErr: ErrNoResponse,
},
} {
t.Run(tt.desc, func(t *testing.T) {
// Both server and client only get 2 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
mc, _ := serveAndClient(ctx, [][]*dhcpv4.DHCPv4{tt.server},
// Use an unbuffered channel to make sure we
// have no deadlocks.
withBufferCap(0))
defer mc.Close()
rcvd, err := mc.SendAndRead(context.Background(), DefaultServers, tt.send, nil)
if err != tt.wantErr {
t.Error(err)
}
if err := ComparePacket(rcvd, tt.want); err != nil {
t.Errorf("got unexpected packets: %v", err)
}
})
}
}
func TestParallelSendAndRead(t *testing.T) {
pkt := newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33})
// Both the server and client only get 2 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
mc, _ := serveAndClient(ctx, [][]*dhcpv4.DHCPv4{},
WithTimeout(10*time.Second),
// Use an unbuffered channel to make sure nothing blocks.
withBufferCap(0))
defer mc.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if _, err := mc.SendAndRead(context.Background(), DefaultServers, pkt, nil); err != ErrNoResponse {
t.Errorf("SendAndRead(%v) = %v, want %v", pkt, err, ErrNoResponse)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(4 * time.Second)
if err := mc.Close(); err != nil {
t.Errorf("closing failed: %v", err)
}
}()
wg.Wait()
}
func TestReuseXID(t *testing.T) {
pkt := newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33})
// Both the server and client only get 2 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
mc, _ := serveAndClient(ctx, [][]*dhcpv4.DHCPv4{})
defer mc.Close()
if _, err := mc.SendAndRead(context.Background(), DefaultServers, pkt, nil); err != ErrNoResponse {
t.Errorf("SendAndRead(%v) = %v, want %v", pkt, err, ErrNoResponse)
}
if _, err := mc.SendAndRead(context.Background(), DefaultServers, pkt, nil); err != ErrNoResponse {
t.Errorf("SendAndRead(%v) = %v, want %v", pkt, err, ErrNoResponse)
}
}
func TestSimpleSendAndReadDiscardGarbage(t *testing.T) {
pkt := newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33})
responses := newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33})
// Both the server and client only get 2 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
mc, udpConn := serveAndClient(ctx, [][]*dhcpv4.DHCPv4{{responses}})
defer mc.Close()
// Too short for valid DHCPv4 packet.
_, _ = udpConn.WriteTo([]byte{0x01}, nil)
_, _ = udpConn.WriteTo([]byte{0x01, 0x2}, nil)
rcvd, err := mc.SendAndRead(ctx, DefaultServers, pkt, nil)
if err != nil {
t.Errorf("SendAndRead(%v) = %v, want nil", pkt, err)
}
if err := ComparePacket(rcvd, responses); err != nil {
t.Errorf("got unexpected packets: %v", err)
}
}
func TestMultipleSendAndRead(t *testing.T) {
for _, tt := range []struct {
desc string
send []*dhcpv4.DHCPv4
server [][]*dhcpv4.DHCPv4
wantErr []error
}{
{
desc: "two requests, two responses",
send: []*dhcpv4.DHCPv4{
newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x33, 0x33, 0x33, 0x33}),
newPacket(dhcpv4.OpcodeBootRequest, [4]byte{0x44, 0x44, 0x44, 0x44}),
},
server: [][]*dhcpv4.DHCPv4{
[]*dhcpv4.DHCPv4{ // Response for first packet.
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x33, 0x33, 0x33, 0x33}),
},
[]*dhcpv4.DHCPv4{ // Response for second packet.
newPacket(dhcpv4.OpcodeBootReply, [4]byte{0x44, 0x44, 0x44, 0x44}),
},
},
wantErr: []error{
nil,
nil,
},
},
} {
// Both server and client only get 2 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
mc, _ := serveAndClient(ctx, tt.server)
defer mc.Close()
for i, send := range tt.send {
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
rcvd, err := mc.SendAndRead(ctx, DefaultServers, send, nil)
if wantErr := tt.wantErr[i]; err != wantErr {
t.Errorf("SendAndReadOne(%v): got %v, want %v", send, err, wantErr)
}
if err := pktsExpected([]*dhcpv4.DHCPv4{rcvd}, tt.server[i]); err != nil {
t.Errorf("got unexpected packets: %v", err)
}
}
}
}
|