-
Notifications
You must be signed in to change notification settings - Fork 96
/
helpers_test.go
397 lines (372 loc) · 8.98 KB
/
helpers_test.go
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package dhcp4
import (
"bytes"
"net"
"reflect"
"sort"
"testing"
"time"
)
// Verify that all options are returned by Options.SelectOrderOrAll if
// the input order value is nil.
func TestSelectOrderOrAllNil(t *testing.T) {
assertOptionsSlices(t, 0, "nil order", allOptionsSlice, optMap.SelectOrderOrAll(nil))
}
// Verify that all options are returned by Options.SelectOrderOrAll if
// the input order value is not nil over several tests.
func TestSelectOrderOrAllNotNil(t *testing.T) {
for i, tt := range selectOrderTests {
assertOptionsSlices(t, i, tt.description, tt.result, optMap.SelectOrderOrAll(tt.order))
}
}
// Verify that no options are returned by Options.SelectOrder if
// the input order value is nil.
func TestSelectOrderNil(t *testing.T) {
assertOptionsSlices(t, 0, "nil order", nil, optMap.SelectOrder(nil))
}
// Verify that all options are returned by Options.SelectOrder if
// the input order value is not nil over several tests.
func TestSelectOrderNotNil(t *testing.T) {
for i, tt := range selectOrderTests {
assertOptionsSlices(t, i, tt.description, tt.result, optMap.SelectOrder(tt.order))
}
}
func TestIPRange(t *testing.T) {
var tests = []struct {
start net.IP
stop net.IP
result int
}{
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 1, 1),
result: 1,
},
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 1, 254),
result: 254,
},
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 10, 1),
result: 2305,
},
{
start: net.IPv4(172, 16, 1, 1),
stop: net.IPv4(192, 168, 1, 1),
result: 345505793,
},
}
for _, tt := range tests {
if result := IPRange(tt.start, tt.stop); result != tt.result {
t.Fatalf("IPRange(%s, %s), unexpected result: %v != %v",
tt.start, tt.stop, result, tt.result)
}
}
}
func TestIPAdd(t *testing.T) {
var tests = []struct {
start net.IP
add int
result net.IP
}{
{
start: net.IPv4(192, 168, 1, 1),
add: 0,
result: net.IPv4(192, 168, 1, 1),
},
{
start: net.IPv4(192, 168, 1, 1),
add: 253,
result: net.IPv4(192, 168, 1, 254),
},
{
start: net.IPv4(192, 168, 1, 1),
add: 1024,
result: net.IPv4(192, 168, 5, 1),
},
{
start: net.IPv4(192, 168, 1, 1),
add: 4096,
result: net.IPv4(192, 168, 17, 1),
},
}
for _, tt := range tests {
if result := IPAdd(tt.start, tt.add); !result.Equal(tt.result) {
t.Fatalf("IPAdd(%s, %d), unexpected result: %v != %v",
tt.start, tt.add, result, tt.result)
}
}
}
func TestIPLess(t *testing.T) {
var tests = []struct {
a net.IP
b net.IP
result bool
}{
{
a: net.IPv4(192, 168, 1, 1),
b: net.IPv4(192, 168, 1, 1),
result: false,
},
{
a: net.IPv4(192, 168, 1, 1),
b: net.IPv4(192, 168, 0, 1),
result: false,
},
{
a: net.IPv4(192, 168, 0, 1),
b: net.IPv4(192, 168, 1, 1),
result: true,
},
{
a: net.IPv4(192, 168, 0, 1),
b: net.IPv4(192, 168, 10, 1),
result: true,
},
}
for _, tt := range tests {
if result := IPLess(tt.a, tt.b); result != tt.result {
t.Fatalf("IPLess(%s, %s), unexpected result: %v != %v",
tt.a, tt.b, result, tt.result)
}
}
}
func TestIPInRange(t *testing.T) {
var tests = []struct {
start net.IP
stop net.IP
ip net.IP
result bool
}{
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 2, 1),
ip: net.IPv4(192, 168, 3, 1),
result: false,
},
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 10, 1),
ip: net.IPv4(192, 168, 0, 1),
result: false,
},
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 10, 1),
ip: net.IPv4(192, 168, 5, 1),
result: true,
},
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 3, 1),
ip: net.IPv4(192, 168, 3, 0),
result: true,
},
{
start: net.IPv4(192, 168, 1, 1),
stop: net.IPv4(192, 168, 1, 1),
ip: net.IPv4(192, 168, 1, 1),
result: true,
},
}
for _, tt := range tests {
if result := IPInRange(tt.start, tt.stop, tt.ip); result != tt.result {
t.Fatalf("IPInRange(%s, %s, %s), unexpected result: %v != %v",
tt.start, tt.stop, tt.ip, result, tt.result)
}
}
}
func TestOptionsLeaseTime(t *testing.T) {
var tests = []struct {
duration time.Duration
result []byte
}{
{
duration: 0 * time.Second,
result: []byte{0, 0, 0, 0},
},
{
duration: 2 * time.Second,
result: []byte{0, 0, 0, 2},
},
{
duration: 60 * time.Second,
result: []byte{0, 0, 0, 60},
},
{
duration: 6 * time.Hour,
result: []byte{0, 0, 84, 96},
},
{
duration: 24 * time.Hour,
result: []byte{0, 1, 81, 128},
},
{
duration: 365 * 24 * time.Hour,
result: []byte{1, 225, 51, 128},
},
}
for _, tt := range tests {
if result := OptionsLeaseTime(tt.duration); !bytes.Equal(result, tt.result) {
t.Fatalf("OptionsLeaseTime(%s), unexpected result: %v != %v",
tt.duration, result, tt.result)
}
}
}
func TestJoinIPs(t *testing.T) {
var tests = []struct {
ips []net.IP
result []byte
}{
{
ips: []net.IP{net.IPv4(10, 0, 0, 1)},
result: []byte{10, 0, 0, 1},
},
{
ips: []net.IP{net.IPv4(192, 168, 1, 1), net.IPv4(192, 168, 2, 1)},
result: []byte{192, 168, 1, 1, 192, 168, 2, 1},
},
{
ips: []net.IP{net.IPv4(10, 0, 0, 1), net.IPv4(255, 255, 255, 254)},
result: []byte{10, 0, 0, 1, 255, 255, 255, 254},
},
{
ips: []net.IP{net.IPv4(8, 8, 8, 8), net.IPv4(8, 8, 4, 4), net.IPv4(192, 168, 1, 1)},
result: []byte{8, 8, 8, 8, 8, 8, 4, 4, 192, 168, 1, 1},
},
}
for _, tt := range tests {
if result := JoinIPs(tt.ips); !bytes.Equal(result, tt.result) {
t.Fatalf("JoinIPs(%s), unexpected result: %v != %v",
tt.ips, result, tt.result)
}
}
}
// byOptionCode implements sort.Interface for []Option.
type byOptionCode []Option
func (b byOptionCode) Len() int { return len(b) }
func (b byOptionCode) Less(i int, j int) bool { return b[i].Code < b[j].Code }
func (b byOptionCode) Swap(i int, j int) { b[i], b[j] = b[j], b[i] }
// assertOptionsSlices is a test helper which verifies that two options slices
// are identical. Several parameters are passed for easy identification of
// failing tests.
func assertOptionsSlices(t *testing.T, i int, description string, want []Option, got []Option) {
// Verify slices are same length
if want, got := len(want), len(got); want != got {
t.Fatalf("%02d: test %q, mismatched length: %d != %d",
i, description, want, got)
}
// Sort slices
sort.Sort(byOptionCode(want))
sort.Sort(byOptionCode(got))
// Verify slices are identical
if len(want) > 0 && len(got) > 0 && !reflect.DeepEqual(want, got) {
t.Fatalf("%02d: test %q, unexpected options: %v != %v",
i, description, want, got)
}
}
// optMap is an Options map which contains a number of option
// codes and values, used for testing.
var optMap = Options{
OptionSubnetMask: []byte{255, 255, 255, 0},
OptionRouter: []byte{192, 168, 1, 1},
OptionDomainNameServer: []byte{192, 168, 1, 2},
OptionTimeServer: []byte{192, 168, 1, 3},
OptionLogServer: []byte{192, 168, 1, 4},
}
// allOptionsSlice is a []Option derived from optMap. It is used
// for some tests.
var allOptionsSlice = []Option{
Option{
Code: OptionSubnetMask,
Value: optMap[OptionSubnetMask],
},
Option{
Code: OptionRouter,
Value: optMap[OptionRouter],
},
Option{
Code: OptionDomainNameServer,
Value: optMap[OptionDomainNameServer],
},
Option{
Code: OptionTimeServer,
Value: optMap[OptionTimeServer],
},
Option{
Code: OptionLogServer,
Value: optMap[OptionLogServer],
},
}
// selectOrderTests is a set of tests used for Options.SelectOrder
// and Options.SelectOrderOrAll methods.
var selectOrderTests = []struct {
description string
order []byte
result []Option
}{
{
description: "subnet mask only",
order: []byte{
byte(OptionSubnetMask),
},
result: []Option{
Option{
Code: OptionSubnetMask,
Value: optMap[OptionSubnetMask],
},
},
},
{
description: "subnet mask and time server",
order: []byte{
byte(OptionSubnetMask),
byte(OptionTimeServer),
},
result: []Option{
Option{
Code: OptionSubnetMask,
Value: optMap[OptionSubnetMask],
},
Option{
Code: OptionTimeServer,
Value: optMap[OptionTimeServer],
},
},
},
{
description: "domain name server, time server, router",
order: []byte{
byte(OptionDomainNameServer),
byte(OptionTimeServer),
byte(OptionRouter),
},
result: []Option{
Option{
Code: OptionDomainNameServer,
Value: optMap[OptionDomainNameServer],
},
Option{
Code: OptionTimeServer,
Value: optMap[OptionTimeServer],
},
Option{
Code: OptionRouter,
Value: optMap[OptionRouter],
},
},
},
{
description: "all options in order",
order: []byte{
byte(OptionSubnetMask),
byte(OptionRouter),
byte(OptionDomainNameServer),
byte(OptionTimeServer),
byte(OptionLogServer),
},
result: allOptionsSlice,
},
}