This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
net_utils_test.go
271 lines (235 loc) · 7.38 KB
/
net_utils_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
package netaddr
import (
"fmt"
"math/big"
"net"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecrement(t *testing.T) {
for _, tc := range []*struct {
in, out net.IP
}{
{ParseIP("192.168.2.5"), ParseIP("192.168.2.4")},
{ParseIP("192.168.0.0"), ParseIP("192.167.255.255")},
{ParseIP("10.0.0.0"), ParseIP("9.255.255.255")},
{ParseIP("0.0.0.0"), ParseIP("255.255.255.255")}, // 0 will cycle
{ParseIP("::"), ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")}, // 0 will cycle
{ParseIP("1::"), ParseIP("0:ffff:ffff:ffff:ffff:ffff:ffff:ffff")}, // 0 will cycle
} {
actual := decrementIP(tc.in)
assert.Equal(t, tc.out, actual)
}
}
func TestExpandNet(t *testing.T) {
n, _ := ParseNet("203.0.113.0/29")
ips := expandNet(n, 10)
assert.Equal(t, 8, len(ips))
assert.Equal(t, net.ParseIP("203.0.113.0").To4(), ips[0])
assert.Equal(t, net.ParseIP("203.0.113.7").To4(), ips[7])
}
func TestExpandNetLimit(t *testing.T) {
n, _ := ParseNet("203.0.113.0/29")
ips := expandNet(n, 5)
assert.Equal(t, 5, len(ips))
assert.Equal(t, net.ParseIP("203.0.113.0").To4(), ips[0])
assert.Equal(t, net.ParseIP("203.0.113.4").To4(), ips[4])
}
func TestExpandNetLarge(t *testing.T) {
n, _ := ParseNet("2001:db8::/56")
ips := expandNet(n, 1000)
assert.Equal(t, 1000, len(ips))
assert.Equal(t, net.ParseIP("2001:db8::0"), ips[0])
assert.Equal(t, net.ParseIP("2001:db8::100"), ips[256])
assert.Equal(t, net.ParseIP("2001:db8::3e7"), ips[999])
}
func TestNetSize(t *testing.T) {
n, _ := ParseNet("10.0.0.0/24")
assert.Equal(t, int64(256), NetSize(n).Int64())
}
func TestNetSizeHost(t *testing.T) {
n, _ := ParseNet("203.0.113.29/32")
assert.Equal(t, int64(1), NetSize(n).Int64())
}
func TestNetSizeSlash8(t *testing.T) {
n, _ := ParseNet("15.0.0.0/8")
assert.Equal(t, int64(16777216), NetSize(n).Int64())
}
func TestNetSizeV6(t *testing.T) {
n, _ := ParseNet("2001:db8::/64")
assert.Equal(t, big.NewInt(0).Lsh(big.NewInt(1), 64), NetSize(n))
}
func TestNetSizeV6Huge(t *testing.T) {
n, _ := ParseNet("2000::/8")
assert.Equal(t, big.NewInt(0).Lsh(big.NewInt(1), 120), NetSize(n))
}
func TestNetSizeV6Host(t *testing.T) {
n, _ := ParseNet("2001:db8::1/128")
assert.Equal(t, big.NewInt(1), NetSize(n))
}
func TestParseIP(t *testing.T) {
assert.Equal(t, net.ParseIP("0.0.0.0").To4(), ParseIP("0.0.0.0"))
// The net package parses ipv4 as an ipv6 embedded v4. They aren't the
// same so the netaddr package distinguishes between them.
assert.Equal(t, net.ParseIP("10.0.0.1").To4(), ParseIP("10.0.0.1"))
assert.Equal(t, net.ParseIP("10.0.0.1"), ParseIP("::ffff:10.0.0.1"))
assert.NotEqual(t, net.ParseIP("10.0.0.1"), ParseIP("10.0.0.1"))
assert.NotEqual(t, net.ParseIP("10.0.0.1").To4(), ParseIP("::ffff:10.0.0.1"))
assert.Equal(t, net.ParseIP("2001:db8::1"), ParseIP("2001:db8::1"))
}
func TestNetIP(t *testing.T) {
assert.Equal(t, net.ParseIP("0.0.0.0").To4(), NewIP(4))
assert.Equal(t, net.ParseIP("::"), NewIP(16))
}
// Just a little shortcut for parsing a CIDR and get the net.IPNet.
func parse(str string) (n *net.IPNet) {
parsed, err := ParseCIDRToNet(str)
if err == nil {
n = parsed
}
return
}
func TestParseNet(t *testing.T) {
n, err := ParseNet("10.0.0.0/24")
assert.Equal(t, parse("10.0.0.0/24"), n)
assert.Nil(t, err)
n, err = ParseNet("2001:db8::/64")
assert.Equal(t, parse("2001:db8::/64"), n)
assert.Nil(t, err)
}
func TestParseNetNonZeroHost(t *testing.T) {
n, err := ParseNet("10.0.20.0/21")
assert.NotNil(t, err)
assert.Nil(t, n)
n, err = ParseNet("2001:db8::1/64")
assert.NotNil(t, err)
assert.Nil(t, n)
}
func TestParseNetInvalidAddresses(t *testing.T) {
n, err := ParseNet("10.0.324.0/24")
assert.NotNil(t, err)
assert.Nil(t, n)
}
func TestParseCIDR(t *testing.T) {
ip, n, err := ParseCIDR("10.0.0.1/24")
assert.Equal(t, net.ParseIP("10.0.0.1").To4(), ip)
assert.Equal(t, parse("10.0.0.0/24"), n)
assert.Equal(t, 4, len(n.IP))
assert.Equal(t, 4, len(ip))
assert.Equal(t, 4, len(n.Mask))
assert.Nil(t, err)
ip, n, err = ParseCIDR("2001:db8::/64")
assert.Equal(t, net.ParseIP("2001:db8::"), ip)
assert.Equal(t, parse("2001:db8::/64"), n)
assert.Equal(t, 16, len(n.IP))
assert.Equal(t, 16, len(ip))
assert.Equal(t, 16, len(n.Mask))
assert.Nil(t, err)
}
func TestParseCIDRToNet(t *testing.T) {
ipNet, err := ParseCIDRToNet("10.0.0.1/24")
assert.Equal(t, net.ParseIP("10.0.0.1").To4(), ipNet.IP)
assert.Equal(t, 4, len(ipNet.IP))
assert.Equal(t, 4, len(ipNet.Mask))
assert.Nil(t, err)
ones, bits := ipNet.Mask.Size()
assert.Equal(t, 24, ones)
assert.Equal(t, 32, bits)
ipNet, err = ParseCIDRToNet("2001:db8::1/64")
assert.Equal(t, net.ParseIP("2001:db8::1"), ipNet.IP)
assert.Equal(t, 16, len(ipNet.IP))
assert.Equal(t, 16, len(ipNet.Mask))
assert.Nil(t, err)
ones, bits = ipNet.Mask.Size()
assert.Equal(t, 64, ones)
assert.Equal(t, 128, bits)
}
func TestParseCIDRErrors(t *testing.T) {
tests := []struct {
cidr string
}{
{cidr: ""},
{cidr: "10.0.0.1"},
{cidr: "bogus"},
{cidr: "300.1.2.3/24"},
{cidr: "4.1.2.3/33"},
{cidr: "2001:db8::/129"},
{cidr: "2001:db8::"},
}
for _, tt := range tests {
t.Run(tt.cidr, func(t *testing.T) {
ip, n, err := ParseCIDR(tt.cidr)
assert.NotNil(t, err)
assert.Equal(t, 0, len(ip))
assert.Nil(t, n)
ipNet, err := ParseCIDRToNet(tt.cidr)
assert.NotNil(t, err)
assert.Nil(t, ipNet)
})
}
}
func TestNetworkAddr(t *testing.T) {
assert.Equal(t, ParseIP("203.0.113.0"), NetworkAddr(parse("203.0.113.0/24")))
assert.Equal(t, ParseIP("10.0.0.0"), NetworkAddr(parse("10.0.0.29/16")))
assert.Equal(t, ParseIP("10.1.64.0"), NetworkAddr(parse("10.1.66.3/18")))
assert.Equal(t, ParseIP("2001:db8::"), NetworkAddr(parse("2001:db8::/64")))
assert.Equal(t, ParseIP("2001:d00::"), NetworkAddr(parse("2001:db8::/24")))
}
func TestBroadcastAddr(t *testing.T) {
assert.Equal(t, ParseIP("203.0.113.255"), BroadcastAddr(parse("203.0.113.0/24")))
assert.Equal(t, ParseIP("10.0.255.255"), BroadcastAddr(parse("10.0.0.0/16")))
assert.Equal(t, ParseIP("10.1.127.255"), BroadcastAddr(parse("10.1.66.3/18")))
// IPv6 doesn't really have a broadcast address but it is still useful to
// find the last address in a cidr
assert.Equal(t, ParseIP("2001:db8::ffff:ffff:ffff:ffff"), BroadcastAddr(parse("2001:db8::/64")))
assert.Equal(t, ParseIP("2001:dff:ffff:ffff:ffff:ffff:ffff:ffff"), BroadcastAddr(parse("2001:db8::/24")))
}
func TestIPLessThan(t *testing.T) {
ips := []net.IP{
ParseIP("10.0.0.0"),
ParseIP("2001::"),
ParseIP("192.168.1.1"),
ParseIP("192.168.1.2"),
ParseIP("10.0.0.1"),
ParseIP("0:0:0:0:0:ffff:c0a8:1"), // ipv4 version of 192.168.0.1
ParseIP("192.168.0.2").To16(), // this should come after the ipv4s
ParseIP("10.0.1.3"),
ParseIP("::"),
ParseIP("1:1::"),
ParseIP("10.2.2.3"),
ParseIP("10.2.1.2"),
ParseIP("10.0.0.0"),
ParseIP("10.0.1.2"),
ParseIP("2001:43::"),
ParseIP("10.2.1.1"),
}
sort.SliceStable(ips, func(i, j int) bool {
return IPLessThan(ips[i], ips[j])
})
assert.Equal(t, "["+
"10.0.0.0 "+
"10.0.0.0 "+
"10.0.0.1 "+
"10.0.1.2 "+
"10.0.1.3 "+
"10.2.1.1 "+
"10.2.1.2 "+
"10.2.2.3 "+
"192.168.1.1 "+
"192.168.1.2 "+
":: "+
"192.168.0.1 "+
"192.168.0.2 "+
"1:1:: "+
"2001:: "+
"2001:43::"+
"]", fmt.Sprintf("%s", ips))
}
func TestIPv4(t *testing.T) {
assert.Equal(t, ParseIP("127.0.0.1"), IPv4(127, 0, 0, 1))
}
func TestIPv4Net(t *testing.T) {
lo, _ := ParseCIDRToNet("127.0.0.1/8")
assert.Equal(t, *lo, IPv4Net(127, 0, 0, 1, 8))
}