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
|
package dhcpv4
import (
"net"
"testing"
"github.com/stretchr/testify/require"
)
func TestTransactionIDModifier(t *testing.T) {
d, err := New()
require.NoError(t, err)
d = WithTransactionID(TransactionID{0xdd, 0xcc, 0xbb, 0xaa})(d)
require.Equal(t, TransactionID{0xdd, 0xcc, 0xbb, 0xaa}, d.TransactionID())
}
func TestBroadcastModifier(t *testing.T) {
d, err := New()
require.NoError(t, err)
// set and test broadcast
d = WithBroadcast(true)(d)
require.Equal(t, true, d.IsBroadcast())
// set and test unicast
d = WithBroadcast(false)(d)
require.Equal(t, true, d.IsUnicast())
}
func TestHwAddrModifier(t *testing.T) {
d, err := New()
require.NoError(t, err)
hwaddr := net.HardwareAddr{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
d = WithHwAddr(hwaddr)(d)
require.Equal(t, hwaddr, d.ClientHwAddr())
}
func TestWithOptionModifier(t *testing.T) {
d, err := New()
require.NoError(t, err)
d = WithOption(&OptDomainName{DomainName: "slackware.it"})(d)
opt := d.GetOneOption(OptionDomainName)
require.NotNil(t, opt)
dnOpt := opt.(*OptDomainName)
require.Equal(t, "slackware.it", dnOpt.DomainName)
}
func TestUserClassModifier(t *testing.T) {
d, err := New()
require.NoError(t, err)
userClass := WithUserClass([]byte("linuxboot"), false)
d = userClass(d)
expected := []byte{
77, // OptionUserClass
9, // length
'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
}
require.Equal(t, "User Class Information -> linuxboot", d.options[0].String())
require.Equal(t, expected, d.options[0].ToBytes())
}
func TestUserClassModifierRFC(t *testing.T) {
d, _ := New()
userClass := WithUserClass([]byte("linuxboot"), true)
d = userClass(d)
expected := []byte{
77, // OptionUserClass
10, // length
9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
}
require.Equal(t, "User Class Information -> linuxboot", d.options[0].String())
require.Equal(t, expected, d.options[0].ToBytes())
}
func TestWithNetboot(t *testing.T) {
d, _ := New()
d = WithNetboot(d)
require.Equal(t, "Parameter Request List -> [TFTP Server Name, Bootfile Name]", d.options[0].String())
}
func TestWithNetbootExistingTFTP(t *testing.T) {
d, _ := New()
OptParams := &OptParameterRequestList{
RequestedOpts: []OptionCode{OptionTFTPServerName},
}
d.AddOption(OptParams)
d = WithNetboot(d)
require.Equal(t, "Parameter Request List -> [TFTP Server Name, Bootfile Name]", d.options[0].String())
}
func TestWithNetbootExistingBootfileName(t *testing.T) {
d, _ := New()
OptParams := &OptParameterRequestList{
RequestedOpts: []OptionCode{OptionBootfileName},
}
d.AddOption(OptParams)
d = WithNetboot(d)
require.Equal(t, "Parameter Request List -> [Bootfile Name, TFTP Server Name]", d.options[0].String())
}
func TestWithNetbootExistingBoth(t *testing.T) {
d, _ := New()
OptParams := &OptParameterRequestList{
RequestedOpts: []OptionCode{OptionBootfileName, OptionTFTPServerName},
}
d.AddOption(OptParams)
d = WithNetboot(d)
require.Equal(t, "Parameter Request List -> [Bootfile Name, TFTP Server Name]", d.options[0].String())
}
func TestWithRequestedOptions(t *testing.T) {
// Check if OptionParameterRequestList is created when not present
d, err := New()
require.NoError(t, err)
d = WithRequestedOptions(OptionFQDN)(d)
require.NotNil(t, d)
o := d.GetOneOption(OptionParameterRequestList)
require.NotNil(t, o)
opts := o.(*OptParameterRequestList)
require.ElementsMatch(t, opts.RequestedOpts, []OptionCode{OptionFQDN})
// Check if already set options are preserved
d = WithRequestedOptions(OptionHostName)(d)
require.NotNil(t, d)
o = d.GetOneOption(OptionParameterRequestList)
require.NotNil(t, o)
opts = o.(*OptParameterRequestList)
require.ElementsMatch(t, opts.RequestedOpts, []OptionCode{OptionFQDN, OptionHostName})
}
func TestWithRelay(t *testing.T) {
d, err := New()
require.NoError(t, err)
ip := net.ParseIP("10.0.0.1")
require.NotNil(t, ip)
d = WithRelay(ip)(d)
require.NotNil(t, d)
require.True(t, d.IsUnicast(), "expected unicast")
require.Equal(t, ip, d.GatewayIPAddr())
require.Equal(t, uint8(1), d.HopCount())
}
func TestWithNetmask(t *testing.T) {
d := &DHCPv4{}
d = WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d)
require.Equal(t, 1, len(d.options))
require.Equal(t, OptionSubnetMask, d.options[0].Code())
osm := d.options[0].(*OptSubnetMask)
require.Equal(t, net.IPv4Mask(255, 255, 255, 0), osm.SubnetMask)
}
func TestWithLeaseTime(t *testing.T) {
d := &DHCPv4{}
d = WithLeaseTime(uint32(3600))(d)
require.Equal(t, 1, len(d.options))
require.Equal(t, OptionIPAddressLeaseTime, d.options[0].Code())
olt := d.options[0].(*OptIPAddressLeaseTime)
require.Equal(t, uint32(3600), olt.LeaseTime)
}
func TestWithDNS(t *testing.T) {
d := &DHCPv4{}
d = WithDNS(net.ParseIP("10.0.0.1"), net.ParseIP("10.0.0.2"))(d)
require.Equal(t, 1, len(d.options))
require.Equal(t, OptionDomainNameServer, d.options[0].Code())
olt := d.options[0].(*OptDomainNameServer)
require.Equal(t, 2, len(olt.NameServers))
require.Equal(t, net.ParseIP("10.0.0.1"), olt.NameServers[0])
require.Equal(t, net.ParseIP("10.0.0.2"), olt.NameServers[1])
require.NotEqual(t, net.ParseIP("10.0.0.1"), olt.NameServers[1])
}
func TestWithDomainSearchList(t *testing.T) {
d := &DHCPv4{}
d = WithDomainSearchList("slackware.it", "dhcp.slackware.it")(d)
require.Equal(t, 1, len(d.options))
osl := d.options[0].(*OptDomainSearch)
require.Equal(t, OptionDNSDomainSearchList, osl.Code())
require.NotNil(t, osl.DomainSearch)
require.Equal(t, 2, len(osl.DomainSearch.Labels))
require.Equal(t, "slackware.it", osl.DomainSearch.Labels[0])
require.Equal(t, "dhcp.slackware.it", osl.DomainSearch.Labels[1])
}
func TestWithRouter(t *testing.T) {
d := &DHCPv4{}
rtr := net.ParseIP("10.0.0.254")
d = WithRouter(rtr)(d)
require.Equal(t, 1, len(d.options))
ortr := d.options[0].(*OptRouter)
require.Equal(t, OptionRouter, ortr.Code())
require.Equal(t, 1, len(ortr.Routers))
require.Equal(t, rtr, ortr.Routers[0])
}
|