Newer
Older
package net
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewPfx(t *testing.T) {
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),
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
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,
},
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
}
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),
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
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)
}
}