-
Notifications
You must be signed in to change notification settings - Fork 54
/
ifaddr_test.go
594 lines (568 loc) · 13.8 KB
/
ifaddr_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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package sockaddr_test
import (
"fmt"
"net"
"os"
"strings"
"testing"
sockaddr "github.com/hashicorp/go-sockaddr"
)
func boolEnvVar(envvar string, emptyDefault bool) bool {
v := os.Getenv(envvar)
switch strings.ToLower(v) {
case "":
return emptyDefault
case "0", "f", "n":
return false
case "1", "t", "y":
return true
default:
fmt.Fprintf(os.Stderr, "Unsupported %s flag %q", envvar, v)
return true
}
}
// havePrivateIP is a helper function that returns true when we believe we
// should have a private IP address. This changes the failure mode of various
// tests that expect a private IP address.
//
// When you have a private IP assigned to the host, set the environment variable
// SOCKADDR_HAVE_PRIVATE_IP=1
func havePrivateIP() bool {
return boolEnvVar("SOCKADDR_HAVE_PRIVATE_IP", true)
}
// havePublicIP is a helper function that returns true when we believe we should
// have a public IP address. This changes the failure mode of various tests
// that expect a public IP address.
//
// When you have a public IP assigned to the host, set the environment variable
// SOCKADDR_HAVE_PUBLIC_IP=1
func havePublicIP() bool {
return boolEnvVar("SOCKADDR_HAVE_PUBLIC_IP", false)
}
func TestGetPrivateIP(t *testing.T) {
reportOnPrivate := func(args ...interface{}) {
if havePrivateIP() {
t.Fatalf(args[0].(string), args[1:]...)
} else {
t.Skipf(args[0].(string), args[1:]...)
}
}
ip, err := sockaddr.GetPrivateIP()
if err != nil {
reportOnPrivate("unable to get a private IP: %v", err)
}
if ip == "" {
reportOnPrivate("it's hard to test this reliably")
}
}
func TestGetPrivateIPs(t *testing.T) {
reportOnPrivate := func(args ...interface{}) {
if havePrivateIP() {
t.Fatalf(args[0].(string), args[1:]...)
} else {
t.Skipf(args[0].(string), args[1:]...)
}
}
ips, err := sockaddr.GetPrivateIPs()
if err != nil {
reportOnPrivate("unable to get a private IPs: %v", err)
}
if ips == "" {
reportOnPrivate("it's hard to test this reliably")
}
}
func TestGetPublicIP(t *testing.T) {
reportOnPublic := func(args ...interface{}) {
if havePublicIP() {
t.Fatalf(args[0].(string), args[1:]...)
} else {
t.Skipf(args[0].(string), args[1:]...)
}
}
ip, err := sockaddr.GetPublicIP()
if err != nil {
reportOnPublic("unable to get a public IP: %v", err)
}
if ip == "" {
reportOnPublic("it's hard to test this reliably")
}
}
func TestGetPublicIPs(t *testing.T) {
reportOnPublic := func(args ...interface{}) {
if havePublicIP() {
t.Fatalf(args[0].(string), args[1:]...)
} else {
t.Skipf(args[0].(string), args[1:]...)
}
}
ips, err := sockaddr.GetPublicIPs()
if err != nil {
reportOnPublic("unable to get a public IPs: %v", err)
}
if ips == "" {
reportOnPublic("it's hard to test this reliably")
}
}
func TestGetInterfaceIP(t *testing.T) {
ip, err := sockaddr.GetInterfaceIP(`^.*[\d]$`)
if err != nil {
t.Fatalf("regexp failed: %v", err)
}
if ip == "" {
t.Skip("it's hard to test this reliably")
}
}
func TestIfAddrAttr(t *testing.T) {
tests := []struct {
name string
ifAddr sockaddr.IfAddr
attr string
expected string
}{
{
name: "name",
ifAddr: sockaddr.IfAddr{
Interface: net.Interface{
Name: "abc0",
},
},
attr: "name",
expected: "abc0",
},
}
for i, test := range tests {
if test.name == "" {
t.Fatalf("test %d must have a name", i)
}
result, err := sockaddr.IfAttr(test.attr, test.ifAddr)
if err != nil {
t.Errorf("failed to get attr %q from %v", test.name, test.ifAddr)
}
if result != test.expected {
t.Errorf("unexpected result")
}
}
}
func TestIfAddrMath(t *testing.T) {
tests := []struct {
name string
ifAddr sockaddr.IfAddr
operation string
value string
expected string
wantFail bool
}{
{
name: "ipv4 address +2",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "address",
value: "+2",
expected: "127.0.0.3/8",
},
{
name: "ipv4 address -2",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "address",
value: "-2",
expected: "126.255.255.255/8",
},
{
name: "ipv4 address + overflow 0xff00ff03",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "address",
value: fmt.Sprintf("+%d", 0xff00ff03),
expected: "126.0.255.4/8",
},
{
name: "ipv4 address - underflow 0xff00ff04",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "address",
value: fmt.Sprintf("-%d", 0xff00ff04),
expected: "127.255.0.253/8",
},
{
name: "ipv6 address +2",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "address",
value: "+2",
expected: "::3",
},
{
name: "ipv6 address -3",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::4/128"),
},
operation: "address",
value: "-3",
expected: "::1",
},
{
name: "ipv6 address + overflow",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
},
operation: "address",
value: fmt.Sprintf("+%d", 0x03),
expected: "::2",
},
{
name: "ipv6 address + underflow",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "address",
value: fmt.Sprintf("-%d", 0x03),
expected: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe",
},
{
name: "ipv4 network +2",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "network",
value: "+2",
expected: "127.0.0.2/8",
},
{
name: "ipv4 network -2",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "network",
value: "-2",
expected: "127.255.255.254/8",
},
{
// Value exceeds /8
name: "ipv4 network + overflow 0xff00ff03",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "network",
value: fmt.Sprintf("+%d", 0xff00ff03),
expected: "127.0.255.3/8",
},
{
// Value exceeds /8
name: "ipv4 network - underflow+wrap 0xff00ff04",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "network",
value: fmt.Sprintf("-%d", 0xff00ff04),
expected: "127.255.0.252/8",
},
{
name: "ipv6 network +6",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("fe80::1/64"),
},
operation: "network",
value: "+6",
expected: "fe80::6/64",
},
{
name: "ipv6 network -6",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("fe80::ff/64"),
},
operation: "network",
value: "-6",
expected: "fe80::ffff:ffff:ffff:fffa/64",
},
{
// Value exceeds /104 mask
name: "ipv6 network + overflow 0xff00ff03",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("fe80::1/104"),
},
operation: "network",
value: fmt.Sprintf("+%d", 0xff00ff03),
expected: "fe80::ff03/104",
},
{
// Value exceeds /104
name: "ipv6 network - underflow+wrap 0xff00ff04",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("fe80::1/104"),
},
operation: "network",
value: fmt.Sprintf("-%d", 0xff00ff04),
expected: "fe80::ff:fc/104",
},
{
name: "ipv4 address missing sign",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "address",
value: "123",
wantFail: true,
},
{
name: "ipv4 network missing sign",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "network",
value: "123",
wantFail: true,
},
{
name: "ipv6 address missing sign",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "address",
value: "123",
wantFail: true,
},
{
name: "ipv6 network missing sign",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "network",
value: "123",
wantFail: true,
},
{
name: "ipv4 address bad value",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "address",
value: "+xyz",
wantFail: true,
},
{
name: "ipv4 network bad value",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "network",
value: "-xyz",
wantFail: true,
},
{
name: "ipv6 address bad value",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "address",
value: "+xyz",
wantFail: true,
},
{
name: "ipv6 network bad value",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "network",
value: "-xyz",
wantFail: true,
},
{
name: "ipv4 bad operation",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "gooz",
value: "+xyz",
wantFail: true,
},
{
name: "ipv6 bad operation",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "frabba",
value: "+xyz",
wantFail: true,
},
{
name: "ipv4 mask operand equals input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("10.20.30.40/8"),
},
operation: "mask",
value: "8",
expected: "10.0.0.0/8",
},
{
name: "ipv4 mask operand larger than input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("192.168.10.20/24"),
},
operation: "mask",
value: "16",
expected: "192.168.0.0/16",
},
{
name: "ipv4 host upper bound mask operand larger than input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("192.168.255.255/24"),
},
operation: "mask",
value: "16",
expected: "192.168.0.0/16",
},
{
name: "ipv4 mask operand smaller than ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("10.20.30.40/8"),
},
operation: "mask",
value: "16",
expected: "10.20.0.0/8",
},
{
name: "ipv4 host upper bound mask operand smaller than input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("10.20.255.255/8"),
},
operation: "mask",
value: "16",
expected: "10.20.0.0/8",
},
{
name: "ipv4 mask bad value upper bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "mask",
value: "33",
wantFail: true,
},
{
name: "ipv4 mask bad value lower bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "mask",
value: "-1",
wantFail: true,
},
{
name: "ipv6 mask operand equals input ipv6 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("2001:0db8:85a3::8a2e:0370:7334/64"),
},
operation: "mask",
value: "64",
expected: "2001:db8:85a3::/64",
},
{
name: "ipv6 mask operand larger than input ipv6 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("2001:0db8:85a3::8a2e:0370:7334/64"),
},
operation: "mask",
value: "32",
expected: "2001:db8::/32",
},
{
name: "ipv6 mask operand smaller than input ipv6 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("2001:0db8:85a3::8a2e:0370:7334/64"),
},
operation: "mask",
value: "96",
expected: "2001:db8:85a3::8a2e:0:0/64",
},
{
name: "ipv6 mask bad value upper bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "mask",
value: "129",
wantFail: true,
},
{
name: "ipv6 mask bad value lower bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "mask",
value: "-1",
wantFail: true,
},
{
name: "unix unsupported operation",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustUnixSock("/tmp/bar"),
},
operation: "address",
value: "+123",
wantFail: true,
},
{
name: "unix unsupported operation",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustUnixSock("/tmp/foo"),
},
operation: "network",
value: "+123",
wantFail: true,
},
{
name: "unix unsupported operation",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustUnixSock("/tmp/foo"),
},
operation: "mask",
value: "8",
wantFail: true,
},
}
for i, test := range tests {
if test.name == "" {
t.Fatalf("test %d must have a name", i)
}
results, err := sockaddr.IfAddrsMath(test.operation, test.value, sockaddr.IfAddrs{test.ifAddr})
if test.wantFail {
if err != nil {
continue
} else {
t.Fatalf("%s: failed to fail math operation %q with value %q on %v", test.name, test.operation, test.value, test.ifAddr)
}
} else if err != nil {
t.Fatalf("%s: failed to compute math operation %q with value %q on %v", test.name, test.operation, test.value, test.ifAddr)
}
if len(results) != 1 {
t.Fatalf("%s: bad", test.name)
}
result := results[0]
switch saType := result.Type(); saType {
case sockaddr.TypeIPv4:
ipv4 := sockaddr.ToIPv4Addr(result.SockAddr)
if ipv4 == nil {
t.Fatalf("bad: %T %+#v", result, result)
}
if got := ipv4.String(); got != test.expected {
t.Errorf("unexpected result %q: want %q got %q", test.name, test.expected, got)
}
case sockaddr.TypeIPv6:
ipv6 := sockaddr.ToIPv6Addr(result.SockAddr)
if ipv6 == nil {
t.Fatalf("bad: %T %+#v", result, result)
}
if got := ipv6.String(); got != test.expected {
t.Errorf("unexpected result %q: want %q got %q", test.name, test.expected, got)
}
default:
t.Fatalf("bad")
}
}
}