summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/dhcpv4_test.go
blob: 5a1220709b6aa771184555535a18f02ed8c8d09f (plain)
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
package dhcpv4

import (
	"bytes"
	"github.com/insomniacslk/dhcp/iana"
	"net"
	"testing"
)

// NOTE: if one of the following Assert* fails where expected and got values are
// the same, you probably have to cast one of them to match the other one's
// type, e.g. comparing int and byte, even the same value, will fail.
func AssertEqual(t *testing.T, a, b interface{}, what string) {
	if a != b {
		t.Fatalf("Invalid %s. %v != %v", what, a, b)
	}
}

func AssertEqualBytes(t *testing.T, a, b []byte, what string) {
	if !bytes.Equal(a, b) {
		t.Fatalf("Invalid %s. %v != %v", what, a, b)
	}
}

func AssertEqualIPAddr(t *testing.T, a, b net.IP, what string) {
	if !net.IP.Equal(a, b) {
		t.Fatalf("Invalid %s. %v != %v", what, a, b)
	}
}

func TestFromBytes(t *testing.T) {
	data := []byte{
		1,                      // dhcp request
		1,                      // ethernet hw type
		6,                      // hw addr length
		3,                      // hop count
		0xaa, 0xbb, 0xcc, 0xdd, // transaction ID, big endian (network)
		0, 3, // number of seconds
		0, 1, // broadcast
		0, 0, 0, 0, // client IP address
		0, 0, 0, 0, // your IP address
		0, 0, 0, 0, // server IP address
		0, 0, 0, 0, // gateway IP address
		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding
	}
	// server host name
	expectedHostname := []byte{}
	for i := 0; i < 64; i++ {
		expectedHostname = append(expectedHostname, 0)
	}
	data = append(data, expectedHostname...)
	// boot file name
	expectedBootfilename := []byte{}
	for i := 0; i < 128; i++ {
		expectedBootfilename = append(expectedBootfilename, 0)
	}
	data = append(data, expectedBootfilename...)
	// magic cookie, then no options
	data = append(data, []byte{99, 130, 83, 99}...)

	d, err := FromBytes(data)
	if err != nil {
		t.Fatal(err)
	}
	AssertEqual(t, d.Opcode(), OpcodeBootRequest, "opcode")
	AssertEqual(t, d.HwType(), iana.HwTypeEthernet, "hardware type")
	AssertEqual(t, d.HwAddrLen(), byte(6), "hardware address length")
	AssertEqual(t, d.HopCount(), byte(3), "hop count")
	AssertEqual(t, d.TransactionID(), uint32(0xaabbccdd), "transaction ID")
	AssertEqual(t, d.NumSeconds(), uint16(3), "number of seconds")
	AssertEqual(t, d.Flags(), uint16(1), "flags")
	AssertEqualIPAddr(t, d.ClientIPAddr(), net.IPv4zero, "client IP address")
	AssertEqualIPAddr(t, d.YourIPAddr(), net.IPv4zero, "your IP address")
	AssertEqualIPAddr(t, d.ServerIPAddr(), net.IPv4zero, "server IP address")
	AssertEqualIPAddr(t, d.GatewayIPAddr(), net.IPv4zero, "gateway IP address")
	hwaddr := d.ClientHwAddr()
	AssertEqualBytes(t, hwaddr[:], []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "flags")
	hostname := d.ServerHostName()
	AssertEqualBytes(t, hostname[:], expectedHostname, "server host name")
	bootfilename := d.BootFileName()
	AssertEqualBytes(t, bootfilename[:], expectedBootfilename, "boot file name")
	// no need to check Magic Cookie as it is already validated in FromBytes
	// above
}

func TestFromBytesZeroLength(t *testing.T) {
	data := []byte{}
	_, err := FromBytes(data)
	if err == nil {
		t.Fatal("Expected error, got nil")
	}
}

func TestFromBytesShortLength(t *testing.T) {
	data := []byte{1, 1, 6, 0}
	_, err := FromBytes(data)
	if err == nil {
		t.Fatal("Expected error, got nil")
	}
}

func TestFromBytesInvalidOptions(t *testing.T) {
	data := []byte{
		1,                      // dhcp request
		1,                      // ethernet hw type
		6,                      // hw addr length
		0,                      // hop count
		0xaa, 0xbb, 0xcc, 0xdd, // transaction ID
		3, 0, // number of seconds
		1, 0, // broadcast
		0, 0, 0, 0, // client IP address
		0, 0, 0, 0, // your IP address
		0, 0, 0, 0, // server IP address
		0, 0, 0, 0, // gateway IP address
		0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding
	}
	// server host name
	for i := 0; i < 64; i++ {
		data = append(data, 0)
	}
	// boot file name
	for i := 0; i < 128; i++ {
		data = append(data, 0)
	}
	// invalid magic cookie, forcing option parsing to fail
	data = append(data, []byte{99, 130, 83, 98}...)
	_, err := FromBytes(data)
	if err == nil {
		t.Fatal("Expected error, got nil")
	}
}

func TestSettersAndGetters(t *testing.T) {
	data := []byte{
		1,                      // dhcp request
		1,                      // ethernet hw type
		6,                      // hw addr length
		3,                      // hop count
		0xaa, 0xbb, 0xcc, 0xdd, // transaction ID, big endian (network)
		0, 3, // number of seconds
		0, 1, // broadcast
		1, 2, 3, 4, // client IP address
		5, 6, 7, 8, // your IP address
		9, 10, 11, 12, // server IP address
		13, 14, 15, 16, // gateway IP address
		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding
	}
	// server host name
	expectedHostname := []byte{}
	for i := 0; i < 64; i++ {
		expectedHostname = append(expectedHostname, 0)
	}
	data = append(data, expectedHostname...)
	// boot file name
	expectedBootfilename := []byte{}
	for i := 0; i < 128; i++ {
		expectedBootfilename = append(expectedBootfilename, 0)
	}
	data = append(data, expectedBootfilename...)
	// magic cookie, then no options
	data = append(data, []byte{99, 130, 83, 99}...)
	d, err := FromBytes(data)
	if err != nil {
		t.Fatal(err)
	}

	// getter/setter for Opcode
	AssertEqual(t, d.Opcode(), OpcodeBootRequest, "opcode")
	d.SetOpcode(OpcodeBootReply)
	AssertEqual(t, d.Opcode(), OpcodeBootReply, "opcode")

	// getter/setter for HwType
	AssertEqual(t, d.HwType(), iana.HwTypeEthernet, "hardware type")
	d.SetHwType(iana.HwTypeARCNET)
	AssertEqual(t, d.HwType(), iana.HwTypeARCNET, "hardware type")

	// getter/setter for HwAddrLen
	AssertEqual(t, d.HwAddrLen(), uint8(6), "hardware address length")
	d.SetHwAddrLen(12)
	AssertEqual(t, d.HwAddrLen(), uint8(12), "hardware address length")

	// getter/setter for HopCount
	AssertEqual(t, d.HopCount(), uint8(3), "hop count")
	d.SetHopCount(1)
	AssertEqual(t, d.HopCount(), uint8(1), "hop count")

	// getter/setter for TransactionID
	AssertEqual(t, d.TransactionID(), uint32(0xaabbccdd), "transaction ID")
	d.SetTransactionID(0xeeff0011)
	AssertEqual(t, d.TransactionID(), uint32(0xeeff0011), "transaction ID")

	// getter/setter for TransactionID
	AssertEqual(t, d.NumSeconds(), uint16(3), "number of seconds")
	d.SetNumSeconds(15)
	AssertEqual(t, d.NumSeconds(), uint16(15), "number of seconds")

	// getter/setter for Flags
	AssertEqual(t, d.Flags(), uint16(1), "flags")
	d.SetFlags(0)
	AssertEqual(t, d.Flags(), uint16(0), "flags")

	// getter/setter for ClientIPAddr
	AssertEqualIPAddr(t, d.ClientIPAddr(), net.IPv4(1, 2, 3, 4), "client IP address")
	d.SetClientIPAddr(net.IPv4(4, 3, 2, 1))
	AssertEqualIPAddr(t, d.ClientIPAddr(), net.IPv4(4, 3, 2, 1), "client IP address")

	// getter/setter for YourIPAddr
	AssertEqualIPAddr(t, d.YourIPAddr(), net.IPv4(5, 6, 7, 8), "your IP address")
	d.SetYourIPAddr(net.IPv4(8, 7, 6, 5))
	AssertEqualIPAddr(t, d.YourIPAddr(), net.IPv4(8, 7, 6, 5), "your IP address")

	// getter/setter for ServerIPAddr
	AssertEqualIPAddr(t, d.ServerIPAddr(), net.IPv4(9, 10, 11, 12), "server IP address")
	d.SetServerIPAddr(net.IPv4(12, 11, 10, 9))
	AssertEqualIPAddr(t, d.ServerIPAddr(), net.IPv4(12, 11, 10, 9), "server IP address")

	// getter/setter for GatewayIPAddr
	AssertEqualIPAddr(t, d.GatewayIPAddr(), net.IPv4(13, 14, 15, 16), "gateway IP address")
	d.SetGatewayIPAddr(net.IPv4(16, 15, 14, 13))
	AssertEqualIPAddr(t, d.GatewayIPAddr(), net.IPv4(16, 15, 14, 13), "gateway IP address")

	// getter/setter for ClientHwAddr
	hwaddr := d.ClientHwAddr()
	AssertEqualBytes(t, hwaddr[:], []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "client hardware address")
	d.SetFlags(0)

	// getter/setter for ServerHostName
	serverhostname := d.ServerHostName()
	AssertEqualBytes(t, serverhostname[:], expectedHostname, "server host name")
	newHostname := []byte{'t', 'e', 's', 't'}
	for i := 0; i < 60; i++ {
		newHostname = append(newHostname, 0)
	}
	d.SetServerHostName(newHostname)
	serverhostname = d.ServerHostName()
	AssertEqualBytes(t, serverhostname[:], newHostname, "server host name")

	// getter/setter for BootFileName
	bootfilename := d.BootFileName()
	AssertEqualBytes(t, bootfilename[:], expectedBootfilename, "boot file name")
	newBootfilename := []byte{'t', 'e', 's', 't'}
	for i := 0; i < 124; i++ {
		newBootfilename = append(newBootfilename, 0)
	}
	d.SetBootFileName(newBootfilename)
	bootfilename = d.BootFileName()
	AssertEqualBytes(t, bootfilename[:], newBootfilename, "boot file name")
}

func TestToStringMethods(t *testing.T) {
	d, err := New()
	if err != nil {
		t.Fatal(err)
	}
	// OpcodeToString
	d.SetOpcode(OpcodeBootRequest)
	AssertEqual(t, d.OpcodeToString(), "BootRequest", "OpcodeToString")
	d.SetOpcode(OpcodeBootReply)
	AssertEqual(t, d.OpcodeToString(), "BootReply", "OpcodeToString")
	d.SetOpcode(OpcodeType(0))
	AssertEqual(t, d.OpcodeToString(), "Invalid", "OpcodeToString")

	// HwTypeToString
	d.SetHwType(iana.HwTypeEthernet)
	AssertEqual(t, d.HwTypeToString(), "Ethernet", "HwTypeToString")
	d.SetHwType(iana.HwTypeARCNET)
	AssertEqual(t, d.HwTypeToString(), "ARCNET", "HwTypeToString")

	// FlagsToString
	d.SetUnicast()
	AssertEqual(t, d.FlagsToString(), "Unicast", "FlagsToString")
	d.SetBroadcast()
	AssertEqual(t, d.FlagsToString(), "Broadcast", "FlagsToString")
	d.SetFlags(0xffff)
	AssertEqual(t, d.FlagsToString(), "Broadcast (reserved bits not zeroed)", "FlagsToString")

	// ClientHwAddrToString
	d.SetHwAddrLen(6)
	d.SetClientHwAddr([]byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	AssertEqual(t, d.ClientHwAddrToString(), "aa:bb:cc:dd:ee:ff", "ClientHwAddrToString")

	// ServerHostNameToString
	d.SetServerHostName([]byte("my.host.local"))
	AssertEqual(t, d.ServerHostNameToString(), "my.host.local", "ServerHostNameToString")

	// BootFileNameToString
	d.SetBootFileName([]byte("/my/boot/file"))
	AssertEqual(t, d.BootFileNameToString(), "/my/boot/file", "BootFileNameToString")
}

func TestToBytes(t *testing.T) {
	// the following bytes match what dhcpv4.New would create. Keep them in
	// sync!
	expected := []byte{
		1,                      // Opcode BootRequest
		1,                      // HwType Ethernet
		6,                      // HwAddrLen
		0,                      // HopCount
		0x11, 0x22, 0x33, 0x44, // TransactionID
		0, 0, // NumSeconds
		0, 0, // Flags
		0, 0, 0, 0, // ClientIPAddr
		0, 0, 0, 0, // YourIPAddr
		0, 0, 0, 0, // ServerIPAddr
		0, 0, 0, 0, // GatewayIPAddr
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ClientHwAddr
	}
	// ServerHostName
	for i := 0; i < 64; i++ {
		expected = append(expected, 0)
	}
	// BootFileName
	for i := 0; i < 128; i++ {
		expected = append(expected, 0)
	}
	// Magic Cookie
	expected = append(expected, MagicCookie...)

	d, err := New()
	if err != nil {
		t.Fatal(err)
	}
	// fix TransactionID to match the expected one, since it's randomly
	// generated in New()
	d.SetTransactionID(0x11223344)
	got := d.ToBytes()
	AssertEqualBytes(t, expected, got, "ToBytes")
}

// TODO
//      test broadcast/unicast flags
//      test Options setter/getter
//      test Summary() and String()