summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp/bsdp_test.go
blob: 8ab25183f8f0798098d8d705a5b3f89034c4c1c9 (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
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 darwin

package bsdp

import (
	"testing"

	"github.com/insomniacslk/dhcp/dhcpv4"
	"github.com/stretchr/testify/require"
)

/*
 * BootImageID
 */
func TestBootImageIDToBytes(t *testing.T) {
	b := BootImageID{
		IsInstall: true,
		ImageType: BootImageTypeMacOSX,
		Index:     0x1000,
	}
	actual := b.ToBytes()
	expected := []byte{0x81, 0, 0x10, 0}
	require.Equal(t, expected, actual)

	b.IsInstall = false
	actual = b.ToBytes()
	expected = []byte{0x01, 0, 0x10, 0}
	require.Equal(t, expected, actual)
}

func TestBootImageIDFromBytes(t *testing.T) {
	b := BootImageID{
		IsInstall: false,
		ImageType: BootImageTypeMacOSX,
		Index:     0x1000,
	}
	newBootImage, err := BootImageIDFromBytes(b.ToBytes())
	require.NoError(t, err)
	require.Equal(t, b, *newBootImage)

	b = BootImageID{
		IsInstall: true,
		ImageType: BootImageTypeMacOSX,
		Index:     0x1011,
	}
	newBootImage, err = BootImageIDFromBytes(b.ToBytes())
	require.NoError(t, err)
	require.Equal(t, b, *newBootImage)
}

func TestBootImageIDFromBytesFail(t *testing.T) {
	serialized := []byte{0x81, 0, 0x10} // intentionally left short
	deserialized, err := BootImageIDFromBytes(serialized)
	require.Nil(t, deserialized)
	require.Error(t, err)
}

/*
 * BootImage
 */
func TestBootImageToBytes(t *testing.T) {
	b := BootImage{
		ID: BootImageID{
			IsInstall: true,
			ImageType: BootImageTypeMacOSX,
			Index:     0x1000,
		},
		Name: "bsdp-1",
	}
	expected := []byte{
		0x81, 0, 0x10, 0, // boot image ID
		6,                         // len(Name)
		98, 115, 100, 112, 45, 49, // byte-encoding of Name
	}
	actual := b.ToBytes()
	require.Equal(t, expected, actual)

	b = BootImage{
		ID: BootImageID{
			IsInstall: false,
			ImageType: BootImageTypeMacOSX,
			Index:     0x1010,
		},
		Name: "bsdp-21",
	}
	expected = []byte{
		0x1, 0, 0x10, 0x10, // boot image ID
		7,                             // len(Name)
		98, 115, 100, 112, 45, 50, 49, // byte-encoding of Name
	}
	actual = b.ToBytes()
	require.Equal(t, expected, actual)
}

func TestBootImageFromBytes(t *testing.T) {
	input := []byte{
		0x1, 0, 0x10, 0x10, // boot image ID
		7,                             // len(Name)
		98, 115, 100, 112, 45, 50, 49, // byte-encoding of Name
	}
	b, err := BootImageFromBytes(input)
	require.NoError(t, err)
	expectedBootImage := BootImage{
		ID: BootImageID{
			IsInstall: false,
			ImageType: BootImageTypeMacOSX,
			Index:     0x1010,
		},
		Name: "bsdp-21",
	}
	require.Equal(t, expectedBootImage, *b)
}

func TestBootImageFromBytesOnlyBootImageID(t *testing.T) {
	// Only a BootImageID, nothing else.
	input := []byte{0x1, 0, 0x10, 0x10}
	b, err := BootImageFromBytes(input)
	require.Nil(t, b)
	require.Error(t, err)
}

func TestBootImageFromBytesShortBootImage(t *testing.T) {
	input := []byte{
		0x1, 0, 0x10, 0x10, // boot image ID
		7,                         // len(Name)
		98, 115, 100, 112, 45, 50, // Name bytes (intentionally off-by-one)
	}
	b, err := BootImageFromBytes(input)
	require.Nil(t, b)
	require.Error(t, err)
}

func TestParseBootImageSingleBootImage(t *testing.T) {
	input := []byte{
		0x1, 0, 0x10, 0x10, // boot image ID
		7,                             // len(Name)
		98, 115, 100, 112, 45, 50, 49, // byte-encoding of Name
	}
	bs, err := ParseBootImagesFromOption(input)
	require.NoError(t, err)
	require.Equal(t, len(bs), 1, "parsing single boot image should return 1")
	b := bs[0]
	expectedBootImageID := BootImageID{
		IsInstall: false,
		ImageType: BootImageTypeMacOSX,
		Index:     0x1010,
	}
	require.Equal(t, expectedBootImageID, b.ID)
	require.Equal(t, b.Name, "bsdp-21")
}

func TestParseBootImageMultipleBootImage(t *testing.T) {
	input := []byte{
		// boot image 1
		0x1, 0, 0x10, 0x10, // boot image ID
		7,                             // len(Name)
		98, 115, 100, 112, 45, 50, 49, // byte-encoding of Name

		// boot image 2
		0x82, 0, 0x11, 0x22, // boot image ID
		8,                                 // len(Name)
		98, 115, 100, 112, 45, 50, 50, 50, // byte-encoding of Name
	}
	bs, err := ParseBootImagesFromOption(input)
	require.NoError(t, err)
	require.Equal(t, len(bs), 2, "parsing 2 BootImages should return 2")
	b1 := bs[0]
	b2 := bs[1]
	expectedID1 := BootImageID{
		IsInstall: false,
		ImageType: BootImageTypeMacOSX,
		Index:     0x1010,
	}
	expectedID2 := BootImageID{
		IsInstall: true,
		ImageType: BootImageTypeMacOSXServer,
		Index:     0x1122,
	}
	require.Equal(t, expectedID1, b1.ID, "first BootImageID should be equal")
	require.Equal(t, expectedID2, b2.ID, "second BootImageID should be equal")
	require.Equal(t, "bsdp-21", b1.Name, "first BootImage name should be equal")
	require.Equal(t, "bsdp-222", b2.Name, "second BootImage name should be equal")
}

func TestParseBootImageFail(t *testing.T) {
	_, err := ParseBootImagesFromOption([]byte{})
	require.Error(t, err, "parseBootImages with empty arg")

	_, err = ParseBootImagesFromOption([]byte{1, 2, 3})
	require.Error(t, err, "parseBootImages with short arg")

	_, err = ParseBootImagesFromOption([]byte{
		// boot image 1
		0x1, 0, 0x10, 0x10, // boot image ID
		7,                         // len(Name)
		98, 115, 100, 112, 45, 50, // byte-encoding of Name (intentionally shorter)

		// boot image 2
		0x82, 0, 0x11, 0x22, // boot image ID
		8,                                 // len(Name)
		98, 115, 100, 112, 45, 50, 50, 50, // byte-encoding of Name
	})
	require.Error(t, err, "parseBootImages with short arg")
}

/*
 * ParseVendorOptionsFromOptions
 */
func TestParseVendorOptions(t *testing.T) {
	expectedOpts := []dhcpv4.Option{
		dhcpv4.Option{
			Code: OptionMessageType,
			Data: []byte{byte(MessageTypeList)},
		},
		dhcpv4.Option{
			Code: OptionVersion,
			Data: Version1_0,
		},
	}
	recvOpts := []dhcpv4.Option{
		dhcpv4.Option{
			Code: dhcpv4.OptionDHCPMessageType,
			Data: []byte{byte(dhcpv4.MessageTypeAck)},
		},
		dhcpv4.Option{
			Code: dhcpv4.OptionBroadcastAddress,
			Data: []byte{0xff, 0xff, 0xff, 0xff},
		},
		dhcpv4.Option{
			Code: dhcpv4.OptionVendorSpecificInformation,
			Data: dhcpv4.OptionsToBytesWithoutMagicCookie(expectedOpts),
		},
	}
	opts := ParseVendorOptionsFromOptions(recvOpts)
	require.Equal(t, expectedOpts, opts, "Parsed vendorOpts should be the same")
}

func TestParseVendorOptionsFromOptionsNotPresent(t *testing.T) {
	expectedOpts := []dhcpv4.Option{
		dhcpv4.Option{
			Code: dhcpv4.OptionDHCPMessageType,
			Data: []byte{byte(dhcpv4.MessageTypeAck)},
		},
		dhcpv4.Option{
			Code: dhcpv4.OptionBroadcastAddress,
			Data: []byte{0xff, 0xff, 0xff, 0xff},
		},
	}
	opts := ParseVendorOptionsFromOptions(expectedOpts)
	require.Empty(t, opts, "empty vendor opts if not present in DHCP opts")
}

func TestParseVendorOptionsFromOptionsEmpty(t *testing.T) {
	opts := ParseVendorOptionsFromOptions([]dhcpv4.Option{})
	require.Empty(t, opts, "vendor opts should be empty if given an empty input")
}

func TestParseVendorOptionsFromOptionsFail(t *testing.T) {
	opts := []dhcpv4.Option{
		dhcpv4.Option{
			Code: dhcpv4.OptionVendorSpecificInformation,
			Data: []byte{
				0x1, 0x1, 0x1, // Option 1: LIST
				0x2, 0x2, 0x01, // Option 2: Version (intentionally left short)
			},
		},
	}
	vendorOpts := ParseVendorOptionsFromOptions(opts)
	require.Empty(t, vendorOpts, "vendor opts should be empty on parse error")
}

/*
 * ParseBootImageListFromAck
 */
func TestParseBootImageListFromAck(t *testing.T) {
	expectedBootImages := []BootImage{
		BootImage{
			ID: BootImageID{
				IsInstall: true,
				ImageType: BootImageTypeMacOSX,
				Index:     0x1010,
			},
			Name: "bsdp-1",
		},
		BootImage{
			ID: BootImageID{
				IsInstall: false,
				ImageType: BootImageTypeMacOS9,
				Index:     0x1111,
			},
			Name: "bsdp-2",
		},
	}
	var bootImageBytes []byte
	for _, image := range expectedBootImages {
		bootImageBytes = append(bootImageBytes, image.ToBytes()...)
	}
	ack, _ := dhcpv4.New()
	ack.AddOption(dhcpv4.Option{
		Code: dhcpv4.OptionVendorSpecificInformation,
		Data: dhcpv4.OptionsToBytesWithoutMagicCookie([]dhcpv4.Option{
			dhcpv4.Option{
				Code: OptionBootImageList,
				Data: bootImageBytes,
			},
		}),
	})

	images, err := ParseBootImageListFromAck(*ack)
	require.NoError(t, err)
	require.Equal(t, expectedBootImages, images, "should get same BootImages")
}

func TestParseBootImageListFromAckNoVendorOption(t *testing.T) {
	ack, _ := dhcpv4.New()
	ack.AddOption(dhcpv4.Option{
		Code: OptionMessageType,
		Data: []byte{byte(dhcpv4.MessageTypeAck)},
	})
	images, err := ParseBootImageListFromAck(*ack)
	require.NoError(t, err, "no vendor extensions should not return error")
	require.Empty(t, images, "should not get images from ACK without Vendor extensions")
}

func TestParseBootImageListFromAckFail(t *testing.T) {
	ack, _ := dhcpv4.New()
	ack.AddOption(dhcpv4.Option{
		Code: OptionMessageType,
		Data: []byte{byte(dhcpv4.MessageTypeAck)},
	})
	ack.AddOption(dhcpv4.Option{
		Code: dhcpv4.OptionVendorSpecificInformation,
		Data: dhcpv4.OptionsToBytesWithoutMagicCookie([]dhcpv4.Option{
			dhcpv4.Option{
				Code: OptionBootImageList,
				Data: []byte{
					// boot image 1
					0x1, 0, 0x10, 0x10, // boot image ID
					7,                         // len(Name)
					98, 115, 100, 112, 45, 49, // byte-encoding of Name (intentionally short)

					// boot image 2
					0x82, 0, 0x11, 0x22, // boot image ID
					8,                                 // len(Name)
					98, 115, 100, 112, 45, 50, 50, 50, // byte-encoding of Name
				},
			},
		}),
	})

	images, err := ParseBootImageListFromAck(*ack)
	require.Nil(t, images, "should get nil on parse error")
	require.Error(t, err, "should get error on parse error")
}

/*
 * Private funcs
 */
func TestNeedsReplyPort(t *testing.T) {
	require.True(t, needsReplyPort(123))
	require.False(t, needsReplyPort(0))
	require.False(t, needsReplyPort(dhcpv4.ClientPort))
}