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
|
package bsdp
import (
"net"
"testing"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
)
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",
},
}
ack, _ := dhcpv4.New()
ack.AddOption(&OptVendorSpecificInformation{
[]dhcpv4.Option{&OptBootImageList{expectedBootImages}},
})
images, err := ParseBootImageListFromAck(*ack)
require.NoError(t, err)
require.NotEmpty(t, images, "should get BootImages")
require.Equal(t, expectedBootImages, images, "should get same BootImages")
}
func TestParseBootImageListFromAckNoVendorOption(t *testing.T) {
ack, _ := dhcpv4.New()
images, err := ParseBootImageListFromAck(*ack)
require.Error(t, err)
require.Empty(t, images, "no BootImages")
}
func TestNeedsReplyPort(t *testing.T) {
require.True(t, needsReplyPort(123))
require.False(t, needsReplyPort(0))
require.False(t, needsReplyPort(dhcpv4.ClientPort))
}
func TestNewInformList_NoReplyPort(t *testing.T) {
hwAddr := net.HardwareAddr{1, 2, 3, 4, 5, 6}
localIP := net.IPv4(10, 10, 11, 11)
m, err := NewInformList(hwAddr, localIP, 0)
require.NoError(t, err)
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionVendorSpecificInformation))
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionParameterRequestList))
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionMaximumDHCPMessageSize))
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionEnd))
opt := m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
require.NotNil(t, opt, "vendor opts not present")
vendorInfo := opt.(*OptVendorSpecificInformation)
require.True(t, dhcpv4.HasOption(vendorInfo, OptionMessageType))
require.True(t, dhcpv4.HasOption(vendorInfo, OptionVersion))
opt = vendorInfo.GetOneOption(OptionMessageType)
require.Equal(t, MessageTypeList, opt.(*OptMessageType).Type)
}
func TestNewInformList_ReplyPort(t *testing.T) {
hwAddr := net.HardwareAddr{1, 2, 3, 4, 5, 6}
localIP := net.IPv4(10, 10, 11, 11)
replyPort := uint16(11223)
// Bad reply port
_, err := NewInformList(hwAddr, localIP, replyPort)
require.Error(t, err)
// Good reply port
replyPort = uint16(999)
m, err := NewInformList(hwAddr, localIP, replyPort)
require.NoError(t, err)
opt := m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
vendorInfo := opt.(*OptVendorSpecificInformation)
require.True(t, dhcpv4.HasOption(vendorInfo, OptionReplyPort))
opt = vendorInfo.GetOneOption(OptionReplyPort)
require.Equal(t, replyPort, opt.(*OptReplyPort).Port)
}
func newAck(hwAddr []byte, transactionID uint32) *dhcpv4.DHCPv4 {
ack, _ := dhcpv4.New()
ack.SetTransactionID(transactionID)
ack.SetHwType(iana.HwTypeEthernet)
ack.SetClientHwAddr(hwAddr)
ack.SetHwAddrLen(uint8(len(hwAddr)))
ack.AddOption(&dhcpv4.OptMessageType{MessageType: dhcpv4.MessageTypeAck})
ack.AddOption(&dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd})
return ack
}
func TestInformSelectForAck_Broadcast(t *testing.T) {
hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
tid := uint32(22)
serverID := net.IPv4(1, 2, 3, 4)
bootImage := BootImage{
ID: BootImageID{
IsInstall: true,
ImageType: BootImageTypeMacOSX,
Index: 0x1000,
},
Name: "bsdp-1",
}
ack := newAck(hwAddr, tid)
ack.SetBroadcast()
ack.AddOption(&dhcpv4.OptServerIdentifier{ServerID: serverID})
m, err := InformSelectForAck(*ack, 0, bootImage)
require.NoError(t, err)
require.Equal(t, dhcpv4.OpcodeBootRequest, m.Opcode())
require.Equal(t, ack.HwType(), m.HwType())
require.Equal(t, ack.ClientHwAddr(), m.ClientHwAddr())
require.Equal(t, ack.TransactionID(), m.TransactionID())
require.True(t, m.IsBroadcast())
// Validate options.
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionClassIdentifier))
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionParameterRequestList))
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionDHCPMessageType))
opt := m.GetOneOption(dhcpv4.OptionDHCPMessageType)
require.Equal(t, dhcpv4.MessageTypeInform, opt.(*dhcpv4.OptMessageType).MessageType)
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionEnd))
// Validate vendor opts.
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionVendorSpecificInformation))
opt = m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
vendorInfo := opt.(*OptVendorSpecificInformation)
require.True(t, dhcpv4.HasOption(vendorInfo, OptionMessageType))
opt = vendorInfo.GetOneOption(OptionMessageType)
require.Equal(t, MessageTypeSelect, opt.(*OptMessageType).Type)
require.True(t, dhcpv4.HasOption(vendorInfo, OptionVersion))
require.True(t, dhcpv4.HasOption(vendorInfo, OptionSelectedBootImageID))
opt = vendorInfo.GetOneOption(OptionSelectedBootImageID)
require.Equal(t, bootImage.ID, opt.(*OptSelectedBootImageID).ID)
require.True(t, dhcpv4.HasOption(vendorInfo, OptionServerIdentifier))
opt = vendorInfo.GetOneOption(OptionServerIdentifier)
require.True(t, serverID.Equal(opt.(*OptServerIdentifier).ServerID))
}
func TestInformSelectForAck_NoServerID(t *testing.T) {
hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
tid := uint32(22)
bootImage := BootImage{
ID: BootImageID{
IsInstall: true,
ImageType: BootImageTypeMacOSX,
Index: 0x1000,
},
Name: "bsdp-1",
}
ack := newAck(hwAddr, tid)
_, err := InformSelectForAck(*ack, 0, bootImage)
require.Error(t, err, "expect error for no server identifier option")
}
func TestInformSelectForAck_BadReplyPort(t *testing.T) {
hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
tid := uint32(22)
serverID := net.IPv4(1, 2, 3, 4)
bootImage := BootImage{
ID: BootImageID{
IsInstall: true,
ImageType: BootImageTypeMacOSX,
Index: 0x1000,
},
Name: "bsdp-1",
}
ack := newAck(hwAddr, tid)
ack.SetBroadcast()
ack.AddOption(&dhcpv4.OptServerIdentifier{ServerID: serverID})
_, err := InformSelectForAck(*ack, 11223, bootImage)
require.Error(t, err, "expect error for > 1024 replyPort")
}
func TestInformSelectForAck_ReplyPort(t *testing.T) {
hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
tid := uint32(22)
serverID := net.IPv4(1, 2, 3, 4)
bootImage := BootImage{
ID: BootImageID{
IsInstall: true,
ImageType: BootImageTypeMacOSX,
Index: 0x1000,
},
Name: "bsdp-1",
}
ack := newAck(hwAddr, tid)
ack.SetBroadcast()
ack.AddOption(&dhcpv4.OptServerIdentifier{ServerID: serverID})
replyPort := uint16(999)
m, err := InformSelectForAck(*ack, replyPort, bootImage)
require.NoError(t, err)
require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionVendorSpecificInformation))
opt := m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
vendorInfo := opt.(*OptVendorSpecificInformation)
require.True(t, dhcpv4.HasOption(vendorInfo, OptionReplyPort))
opt = vendorInfo.GetOneOption(OptionReplyPort)
require.Equal(t, replyPort, opt.(*OptReplyPort).Port)
}
|