-
Notifications
You must be signed in to change notification settings - Fork 4
/
netfuncs_test.go
157 lines (147 loc) · 2.83 KB
/
netfuncs_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
package ipx_test
import (
"fmt"
"github.com/ns1/ipx"
"net"
"testing"
)
func TestSupernet(t *testing.T) {
for _, c := range []struct {
name, in string
newPrefix int
out string
}{
{"ipv4 one level", "10.0.0.128/25", 24, "10.0.0.0/24"},
{"ipv6 one level", "29a2:241a:f62c::/64", 44, "29a2:241a:f620::/44"},
} {
t.Run(c.name, func(t *testing.T) {
_, ipN, _ := net.ParseCIDR(c.in)
out := ipx.Supernet(ipN, c.newPrefix).String()
if out != c.out {
t.Fatalf("wanted %v but got %v", c.out, out)
}
})
}
}
func ExampleSupernet() {
ipN := cidr("192.0.2.0/24")
super := ipx.Supernet(ipN, 20)
fmt.Println(super)
// Output:
// 192.0.0.0/20
}
func BenchmarkSupernet(b *testing.B) {
type bench struct {
cidr string
newPrefix int
}
for _, g := range []struct {
name string
benches []bench
}{
{
"ipv4",
[]bench{
{"192.0.2.0/24", 20},
{"192.0.2.0/24", 15},
},
},
{
"ipv6",
[]bench{
{"::/24", 20},
{"::/24", 15},
},
},
} {
b.Run(g.name, func(b *testing.B) {
for _, c := range g.benches {
ipN := cidr(c.cidr)
ones, _ := ipN.Mask.Size()
b.Run(fmt.Sprintf("%v-%v", ones, c.newPrefix), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = ipx.Supernet(ipN, c.newPrefix)
}
})
}
})
}
}
func ExampleBroadcast() {
ipN := cidr("10.0.1.0/24")
fmt.Println(ipx.Broadcast(ipN))
// Output:
// 10.0.1.255
}
func TestBroadcast(t *testing.T) {
for _, c := range []struct {
name, in, out string
}{
{"ipv4", "10.0.1.0/24", "10.0.1.255"},
{"ipv6", "29a2:241a:f620::/44", "29a2:241a:f62f:ffff:ffff:ffff:ffff:ffff"},
} {
t.Run(c.name, func(t *testing.T) {
_, in, _ := net.ParseCIDR(c.in)
out := ipx.Broadcast(in).String()
if out != c.out {
t.Fatalf("wanted %v but got %v", c.out, out)
}
})
}
}
func BenchmarkBroadcast(b *testing.B) {
for _, g := range []struct {
name string
cidrs []*net.IPNet
}{
{
"ipv4",
[]*net.IPNet{
cidr("10.0.1.0/31"),
cidr("10.1.0.0/16"),
cidr("0.0.0.0/0"),
},
},
{
"ipv6",
[]*net.IPNet{
cidr("::/127"),
cidr("::/64"),
cidr("::/0"),
},
},
} {
b.Run(g.name, func(b *testing.B) {
for _, c := range g.cidrs {
ones, _ := c.Mask.Size()
b.Run(fmt.Sprint(ones), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = ipx.Broadcast(c)
}
})
}
})
}
}
func ExampleIsSubnet() {
a, b := cidr("10.0.0.0/16"), cidr("10.0.1.0/24")
fmt.Println(ipx.IsSubnet(a, b))
fmt.Println(ipx.IsSubnet(a, a))
fmt.Println(ipx.IsSubnet(b, a))
// Output:
// true
// true
// false
}
func ExampleIsSupernet() {
a, b := cidr("10.0.1.0/24"), cidr("10.0.0.0/16")
fmt.Println(ipx.IsSupernet(a, b))
fmt.Println(ipx.IsSupernet(a, a))
fmt.Println(ipx.IsSupernet(b, a))
// Output:
// true
// true
// false
}