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
|
package dhcpv4
import (
"bytes"
"testing"
)
/*
* BootImageID
*/
func TestBootImageIDToBytes(t *testing.T) {
b := BootImageID{
isInstall: true,
imageKind: BSDPBootImageMacOSX,
index: 0x1000,
}
actual := b.toBytes()
expected := []byte{0x81, 0, 0x10, 0}
if !bytes.Equal(actual, expected) {
t.Fatalf("Invalid bytes conversion: expected %v, got %v", expected, actual)
}
b.isInstall = false
actual = b.toBytes()
expected = []byte{0x01, 0, 0x10, 0}
if !bytes.Equal(actual, expected) {
t.Fatalf("Invalid bytes conversion: expected %v, got %v", expected, actual)
}
}
func TestBootImageIDFromBytes(t *testing.T) {
b := BootImageID{
isInstall: false,
imageKind: BSDPBootImageMacOSX,
index: 0x1000,
}
newBootImage := bootImageIDFromBytes(b.toBytes())
if b != newBootImage {
t.Fatalf("Difference in BootImageIDs: expected %v, got %v", b, newBootImage)
}
b = BootImageID{
isInstall: true,
imageKind: BSDPBootImageMacOSX,
index: 0x1011,
}
newBootImage = bootImageIDFromBytes(b.toBytes())
if b != newBootImage {
t.Fatalf("Difference in BootImageIDs: expected %v, got %v", b, newBootImage)
}
}
/*
* BootImage
*/
func TestBootImageToBytes(t *testing.T) {
b := BootImage{
ID: BootImageID{
isInstall: true,
imageKind: BSDPBootImageMacOSX,
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()
if !bytes.Equal(expected, actual) {
t.Fatalf("Invalid bytes conversion: expected %v, got %v", expected, actual)
}
b = BootImage{
ID: BootImageID{
isInstall: false,
imageKind: BSDPBootImageMacOSX,
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()
if !bytes.Equal(expected, actual) {
t.Fatalf("Invalid bytes conversion: expected %v, got %v", 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, read, err := bootImageFromBytes(input)
AssertNil(t, err, "error while marshalling BootImage")
AssertEqual(t, read, len(input), "number of bytes from input")
expectedBootImage := BootImage{
ID: BootImageID{
isInstall: false,
imageKind: BSDPBootImageMacOSX,
index: 0x1010,
},
Name: "bsdp-21",
}
AssertEqual(t, *b, expectedBootImage, "invalid marshalling of BootImage")
}
func TestBootImageFromBytesOnlyBootImageID(t *testing.T) {
// Only a BootImageID, nothing else.
input := []byte{0x1, 0, 0x10, 0x10}
_, _, err := bootImageFromBytes(input)
AssertNotNil(t, err, "short bytestream")
}
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 (intentially off-by-one)
}
_, _, err := bootImageFromBytes(input)
AssertNotNil(t, err, "short bytestream")
}
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 := parseBootImagesFromBSDPOption(input)
AssertNil(t, err, "parsing boot image")
AssertEqual(t, len(bs), 1, "length of boot images")
b := bs[0]
expectedBootImage := BootImageID{
isInstall: false,
imageKind: BSDPBootImageMacOSX,
index: 0x1010,
}
AssertEqual(t, b.ID, expectedBootImage, "boot image ID")
AssertEqual(t, b.Name, "bsdp-21", "boot image name")
}
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 := parseBootImagesFromBSDPOption(input)
AssertNil(t, err, "parsing boot image")
AssertEqual(t, len(bs), 2, "length of boot images")
b1 := bs[0]
b2 := bs[1]
expectedID1 := BootImageID{
isInstall: false,
imageKind: BSDPBootImageMacOSX,
index: 0x1010,
}
expectedID2 := BootImageID{
isInstall: true,
imageKind: BSDPBootImageMacOSXServer,
index: 0x1122,
}
AssertEqual(t, b1.ID, expectedID1, "boot image ID 1")
AssertEqual(t, b2.ID, expectedID2, "boot image ID 2")
AssertEqual(t, b1.Name, "bsdp-21", "boot image 1 name")
AssertEqual(t, b2.Name, "bsdp-222", "boot image 1 name")
}
func TestParseBootImageFail(t *testing.T) {
_, err := parseBootImagesFromBSDPOption([]byte{})
AssertNotNil(t, err, "parseBootImages with empty arg")
_, err = parseBootImagesFromBSDPOption([]byte{1, 2, 3})
AssertNotNil(t, err, "parseBootImages with short arg")
}
/*
* parseVendorOptionsFromOptions
*/
func TestParseVendorOptions(t *testing.T) {
recvOpts := []Option{
Option{
Code: OptionDHCPMessageType,
Data: []byte{MessageTypeAck},
},
Option{
Code: OptionBroadcastAddress,
Data: []byte{0xff, 0xff, 0xff, 0xff},
},
Option{
Code: OptionVendorSpecificInformation,
Data: OptionsToBytes([]Option{
Option{
Code: BSDPOptionMessageType,
Data: []byte{BSDPMessageTypeList},
},
Option{
Code: BSDPOptionVersion,
Data: BSDPVersion1_0,
},
}),
},
}
opts := parseVendorOptionsFromOptions(recvOpts)
AssertEqual(t, len(opts), 2, "len of vendor opts")
}
func TestParseVendorOptionsFromOptionsNotPresent(t *testing.T) {
recvOpts := []Option{
Option{
Code: OptionDHCPMessageType,
Data: []byte{MessageTypeAck},
},
Option{
Code: OptionBroadcastAddress,
Data: []byte{0xff, 0xff, 0xff, 0xff},
},
}
opts := parseVendorOptionsFromOptions(recvOpts)
AssertEqual(t, len(opts), 0, "len of vendor opts")
}
func TestParseVendorOptionsFromOptionsEmpty(t *testing.T) {
options := parseVendorOptionsFromOptions([]Option{})
AssertEqual(t, len(options), 0, "size of options")
}
/*
* ParseBootImageListFromAck
*/
func TestParseBootImageListFromAck(t *testing.T) {
bootImages := []BootImage{
BootImage{
ID: BootImageID{
isInstall: true,
imageKind: BSDPBootImageMacOSX,
index: 0x1010,
},
Name: "bsdp-1",
},
BootImage{
ID: BootImageID{
isInstall: false,
imageKind: BSDPBootImageMacOS9,
index: 0x1111,
},
Name: "bsdp-2",
},
}
var bootImageBytes []byte
for _, image := range bootImages {
bootImageBytes = append(bootImageBytes, image.toBytes()...)
}
ack := DHCPv4{
options: []Option{
Option{
Code: OptionVendorSpecificInformation,
Data: OptionsToBytes([]Option{
Option{
Code: BSDPOptionBootImageList,
Data: bootImageBytes,
},
}),
},
},
}
images, err := ParseBootImageListFromAck(ack)
AssertNil(t, err, "error from ParseBootImageListFromAck")
AssertNotNil(t, images, "parsed boot images from ack")
if len(images) != len(bootImages) {
t.Fatalf("Expected same number of BootImages, got %d instead", len(images))
}
for i := range images {
if images[i] != bootImages[i] {
t.Fatalf("Expected boot images to be same. %v != %v", images[i], bootImages[i])
}
}
}
/*
* NewInformListForInterface
*/
|