Newer
Older
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
func TestPrefixToProto(t *testing.T) {
tests := []struct {
name string
pfx Prefix
expected api.Prefix
}{
{
name: "IPv4",
pfx: Prefix{
addr: IP{
lower: 200,
isLegacy: true,
},
pfxlen: 24,
},
expected: api.Prefix{
Address: &api.IP{
Lower: 200,
IsLegacy: true,
},
Pfxlen: 24,
},
},
{
name: "IPv6",
pfx: Prefix{
addr: IP{
higher: 100,
lower: 200,
isLegacy: false,
},
pfxlen: 64,
},
expected: api.Prefix{
Address: &api.IP{
Higher: 100,
Lower: 200,
IsLegacy: false,
},
Pfxlen: 64,
},
},
}
for _, test := range tests {
res := test.pfx.ToProto()
assert.Equal(t, test.expected, res, test.name)
}
}
func TestNewPrefixFromProtoPrefix(t *testing.T) {
tests := []struct {
name string
proto api.Prefix
expected Prefix
}{
{
name: "IPv4",
proto: api.Prefix{
Address: &api.IP{
Higher: 0,
Lower: 2000,
IsLegacy: true,
},
Pfxlen: 24,
},
expected: Prefix{
addr: IP{
higher: 0,
lower: 2000,
isLegacy: true,
},
pfxlen: 24,
},
},
{
name: "IPv6",
proto: api.Prefix{
Address: &api.IP{
Higher: 1000,
Lower: 2000,
IsLegacy: false,
},
Pfxlen: 64,
},
expected: Prefix{
addr: IP{
higher: 1000,
lower: 2000,
isLegacy: false,
},
pfxlen: 64,
},
},
}
for _, test := range tests {
res := NewPrefixFromProtoPrefix(test.proto)
assert.Equal(t, test.expected, res, test.name)
}
}
p := NewPfx(IPv4(123), 11)
if p.addr != IPv4(123) || p.pfxlen != 11 {
t.Errorf("NewPfx() failed: Unexpected values")
}
}
func TestAddr(t *testing.T) {
tests := []struct {
name string
pfx: NewPfx(IPv4(100), 5),
expected: IPv4(100),
},
}
for _, test := range tests {
res := test.pfx.Addr()
if res != test.expected {
t.Errorf("Unexpected result for test %s: Got %d Expected %d", test.name, res, test.expected)
}
}
}
func TestPfxlen(t *testing.T) {
tests := []struct {
name string
expected: 5,
},
}
for _, test := range tests {
res := test.pfx.Pfxlen()
if res != test.expected {
t.Errorf("Unexpected result for test %s: Got %d Expected %d", test.name, res, test.expected)
}
}
}
func TestGetSupernet(t *testing.T) {
tests := []struct {
name string
a Prefix
b Prefix
expected Prefix
name: "Supernet of 10.0.0.0 and 11.100.123.0 -> 10.0.0.0/7",
addr: IPv4FromOctets(10, 0, 0, 0),
addr: IPv4FromOctets(11, 100, 123, 0),
addr: IPv4FromOctets(10, 0, 0, 0),
name: "Supernet of 10.0.0.0 and 192.168.0.0 -> 0.0.0.0/0",
addr: IPv4FromOctets(10, 0, 0, 0),
addr: IPv4FromOctets(192, 168, 0, 0),
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
addr: IPv4(0),
pfxlen: 0,
},
},
{
name: "Supernet of 2001:678:1e0:100:23::/64 and 2001:678:1e0:1ff::/64 -> 2001:678:1e0:100::/56",
a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x100, 0x23, 0, 0, 0),
pfxlen: 64,
},
b: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x1ff, 0, 0, 0, 0),
pfxlen: 64,
},
expected: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x100, 0, 0, 0, 0),
pfxlen: 56,
},
},
{
name: "Supernet of 2001:678:1e0::/128 and 2001:678:1e0::1/128 -> 2001:678:1e0:100::/127",
a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0),
pfxlen: 128,
},
b: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 1),
pfxlen: 128,
},
expected: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0),
pfxlen: 127,
},
},
{
name: "Supernet of all ones and all zeros -> ::/0",
a: Prefix{
addr: IPv6FromBlocks(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF),
pfxlen: 128,
},
b: Prefix{
addr: IPv6(0, 0),
pfxlen: 128,
},
expected: Prefix{
addr: IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0),
t.Run(test.name, func(t *testing.T) {
s := test.a.GetSupernet(test.b)
assert.Equal(t, test.expected, s)
})
}
}
func TestContains(t *testing.T) {
tests := []struct {
name string
pfxlen: 24,
},
expected: true,
},
{
name: "Test 2",
pfxlen: 0,
},
expected: false,
},
{
name: "Test 3",
pfxlen: 9,
},
expected: true,
},
{
name: "Test 4",
pfxlen: 24,
},
expected: true,
},
{
name: "Test 5",
pfxlen: 24,
},
expected: false,
},
{
name: "Test 6",
addr: IPv4FromOctets(169, 0, 0, 0),
addr: IPv4FromOctets(169, 1, 1, 0),
pfxlen: 26,
},
expected: false,
},
{
name: "IPv6: 2001:678:1e0:100::/56 is subnet of 2001:678:1e0::/48",
a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0),
pfxlen: 48,
},
expected: true,
b: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x100, 0, 0, 0, 0),
pfxlen: 56,
},
},
{
name: "IPv6: 2001:678:1e0:100::/56 is subnet of 2001:678:1e0::/48",
a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x200, 0, 0, 0, 0),
pfxlen: 56,
},
b: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x100, 0, 0, 0, 0),
pfxlen: 64,
},
expected: false,
},
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
}
for _, test := range tests {
res := test.a.Contains(test.b)
if res != test.expected {
t.Errorf("Unexpected result %v for test %s: %s contains %s\n", res, test.name, test.a.String(), test.b.String())
}
}
}
func TestMin(t *testing.T) {
tests := []struct {
name string
a uint8
b uint8
expected uint8
}{
{
name: "Min 100 200",
a: 100,
b: 200,
expected: 100,
},
{
name: "Min 200 100",
a: 200,
b: 100,
expected: 100,
},
{
name: "Min 111 112",
a: 111,
b: 112,
expected: 111,
},
}
for _, test := range tests {
res := min(test.a, test.b)
if res != test.expected {
t.Errorf("Unexpected result for test %s: Got %d Expected %d", test.name, res, test.expected)
}
}
}
func TestEqual(t *testing.T) {
tests := []struct {
name string
a: NewPfx(IPv4(100), 8),
b: NewPfx(IPv4(100), 8),
a: NewPfx(IPv4(100), 8),
b: NewPfx(IPv4(200), 8),
expected: false,
},
}
for _, test := range tests {
res := test.a.Equal(test.b)
if res != test.expected {
t.Errorf("Unexpected result for %q: Got %v Expected %v", test.name, res, test.expected)
}
}
}
func TestString(t *testing.T) {
tests := []struct {
name string
pfx: NewPfx(IPv4FromOctets(10, 0, 0, 0), 8),
pfx: NewPfx(IPv4FromOctets(10, 0, 0, 0), 16),
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
expected: "10.0.0.0/16",
},
}
for _, test := range tests {
res := test.pfx.String()
if res != test.expected {
t.Errorf("Unexpected result for %q: Got %q Expected %q", test.name, res, test.expected)
}
}
}
func TestStrToAddr(t *testing.T) {
tests := []struct {
name string
input string
wantFail bool
expected uint32
}{
{
name: "Invalid address #1",
input: "10.10.10",
wantFail: true,
},
{
name: "Invalid address #2",
input: "",
wantFail: true,
},
{
name: "Invalid address #3",
input: "10.10.10.10.10",
wantFail: true,
},
{
name: "Invalid address #4",
input: "10.256.0.0",
wantFail: true,
},
{
name: "Valid address",
input: "10.0.0.0",
wantFail: false,
expected: 167772160,
},
}
for _, test := range tests {
res, err := StrToAddr(test.input)
if err != nil {
if test.wantFail {
continue
}
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
continue
}
if test.wantFail {
t.Errorf("Unexpected success for test %q", test.name)
continue
}
assert.Equal(t, test.expected, res)
}
}