-
Notifications
You must be signed in to change notification settings - Fork 4
/
collapse_test.go
98 lines (93 loc) · 2.07 KB
/
collapse_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
package ipx_test
import (
"fmt"
"github.com/ns1/ipx"
"net"
"testing"
)
func ExampleCollapse() {
fmt.Println(ipx.Collapse(
[]*net.IPNet{
cidr("192.0.2.0/26"),
cidr("192.0.2.64/26"),
cidr("192.0.2.128/26"),
cidr("192.0.2.192/26"),
},
))
// Output:
// [192.0.2.0/24]
}
func TestCollapse(t *testing.T) {
for _, c := range []struct {
name string
in []string
out []string
}{
{"empty", nil, nil},
{
"simple",
[]string{"192.0.2.0/26", "192.0.2.64/26", "192.0.2.128/26", "192.0.2.192/26"},
[]string{"192.0.2.0/24"},
},
{
"dupe",
[]string{"192.0.2.0/26", "192.0.2.64/26", "192.0.2.128/26", "192.0.2.192/26", "192.0.2.192/26"},
[]string{"192.0.2.0/24"},
},
{
"simple v6",
[]string{"::/26", "0:40::/26", "0:80::/26", "0:c0::/26"},
[]string{"::/24"},
},
{
"dupe v6",
[]string{"::/26", "0:40::/26", "0:80::/26", "0:c0::/26", "0:c0::/26"},
[]string{"::/24"},
},
{
"multi type",
[]string{"0:80::/26", "0:c0::/26", "192.0.2.0/26", "192.0.2.64/26"},
[]string{"192.0.2.0/25", "0:80::/25"},
},
{
"ipv4 child included",
[]string{"192.0.2.0/26", "192.0.2.64/26", "192.0.2.64/27"},
[]string{"192.0.2.0/25"},
},
{
"ipv4 disjoint",
[]string{"192.0.2.0/27", "192.0.2.64/27", "192.0.2.64/27"},
[]string{"192.0.2.0/27", "192.0.2.64/27"},
},
{
"ipv6 child included",
[]string{"0:80::/26", "0:c0::/26", "0:c0::/27"},
[]string{"0:80::/25"},
},
{
"ipv6 disjoint",
[]string{"0:80::/27", "0:c0::/27", "0:c0::/27"},
[]string{"0:80::/27", "0:c0::/27"},
},
} {
t.Run(c.name, func(t *testing.T) {
in := make([]*net.IPNet, 0, len(c.in))
for _, s := range c.in {
in = append(in, cidr(s))
}
out := ipx.Collapse(in)
got := make([]string, 0, len(out))
for _, o := range out {
got = append(got, o.String())
}
if len(c.out) != len(got) {
t.Fatalf("Wanted length %v but got %v: %v", len(c.out), len(got), got)
}
for i := range c.out {
if c.out[i] != got[i] {
t.Errorf("Wanted %v but got %v at position %v", c.out[i], got[i], i)
}
}
})
}
}