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
137 lines (112 loc) · 4.09 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
package netaddr
import (
"math/big"
"net"
"testing"
"github.com/stretchr/testify/assert"
)
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 := net.ParseCIDR(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 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.0/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")))
}