-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathnet_test.go
198 lines (165 loc) · 5.8 KB
/
net_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
package patricia
import (
"net"
"net/netip"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseIPFromString(t *testing.T) {
var v4IP *IPv4Address
var v6IP *IPv6Address
var err error
_, _, err = ParseIPFromString("sdfkjsdfdsfd")
assert.Error(t, err)
_, _, err = ParseIPFromString("")
assert.Error(t, err)
v4IP, v6IP, err = ParseIPFromString("0.0.0.0/0")
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(0), v4IP.Length)
assert.Nil(t, v6IP)
v4IP, v6IP, err = ParseIPFromString("127.0.0.1")
assert.NoError(t, err)
assert.Equal(t, uint(32), v4IP.Length)
assert.NotNil(t, v4IP)
assert.Nil(t, v6IP)
_, _, err = ParseIPFromString("127.0.0.1/999")
assert.Error(t, err)
_, _, err = ParseIPFromString("127.0.0.1/33")
assert.Error(t, err)
v4IP, v6IP, err = ParseIPFromString("1.0.16.0/0")
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(0), v4IP.Length)
assert.Nil(t, v6IP)
v4IP, v6IP, err = ParseIPFromString("127.0.0.1/10")
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(10), v4IP.Length)
assert.Nil(t, v6IP)
v4IP, v6IP, err = ParseIPFromString("127.0.0.1/32")
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(32), v4IP.Length)
assert.Nil(t, v6IP)
v4IP, v6IP, err = ParseIPFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x20010db885a30000), v6IP.Left)
assert.Equal(t, uint64(0x00008a2e03707334), v6IP.Right)
v4IP, v6IP, err = ParseIPFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x20010db885a30000), v6IP.Left)
assert.Equal(t, uint64(0x00008a2e03707334), v6IP.Right)
_, _, err = ParseIPFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334/129")
assert.Error(t, err)
v4IP, v6IP, err = ParseIPFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334/16")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(16), v6IP.Length)
assert.Equal(t, uint64(0x2001000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0), v6IP.Right)
ip := net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
assert.NotNil(t, ip)
v4IP, v6IP, err = ParseFromIP(&ip)
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x20010db885a30000), v6IP.Left)
assert.Equal(t, uint64(0x00008a2e03707334), v6IP.Right)
ip = net.ParseIP("127.0.0.1")
assert.NotNil(t, ip)
v4IP, v6IP, err = ParseFromIP(&ip)
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(32), v4IP.Length)
assert.Equal(t, uint32(0x7f000001), v4IP.Address)
assert.Nil(t, v6IP)
_, ipr, _ := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/16")
v4IP, v6IP, err = ParseFromIPAddr(ipr)
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(16), v6IP.Length)
assert.Equal(t, uint64(0x2001000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0), v6IP.Right)
_, ipr, _ = net.ParseCIDR("127.0.0.1/10")
v4IP, v6IP, err = ParseFromIPAddr(ipr)
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(10), v4IP.Length)
assert.Nil(t, v6IP)
v4IP, v6IP, err = ParseIPFromString("::ffff:10.10.10.10/128")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x0000000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0000ffff0a0a0a0a), v6IP.Right)
v4IP, v6IP, err = ParseIPFromString("::ffff:10.10.10.10/120")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(120), v6IP.Length)
assert.Equal(t, uint64(0x0000000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0000ffff0a0a0a00), v6IP.Right)
v4IP, v6IP, err = ParseIPFromString("::ffff:10.10.10.10/118")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(118), v6IP.Length)
assert.Equal(t, uint64(0x0000000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0000ffff0a0a0800), v6IP.Right)
v4IP, v6IP, err = ParseIPFromString("::ffff:10.10.10.10")
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x0000000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0000ffff0a0a0a0a), v6IP.Right)
}
func TestParseIPFromNetIP(t *testing.T) {
addr := netip.MustParseAddr("::ffff:10.10.10.10")
v4IP, v6IP, err := ParseFromNetIPAddr(addr)
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x0000000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0000ffff0a0a0a0a), v6IP.Right)
addr = netip.MustParseAddr("127.0.0.1")
assert.NotNil(t, addr)
v4IP, v6IP, err = ParseFromNetIPAddr(addr)
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(32), v4IP.Length)
assert.Equal(t, uint32(0x7f000001), v4IP.Address)
assert.Nil(t, v6IP)
prefix := netip.MustParsePrefix("127.0.0.1/10")
v4IP, v6IP, err = ParseFromNetIPPrefix(prefix)
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(10), v4IP.Length)
assert.Nil(t, v6IP)
prefix = netip.MustParsePrefix("::ffff:10.10.10.10/128")
v4IP, v6IP, err = ParseFromNetIPPrefix(prefix)
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(128), v6IP.Length)
assert.Equal(t, uint64(0x0000000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0000ffff0a0a0a0a), v6IP.Right)
_, _, err = ParseFromNetIPAddr(netip.Addr{})
assert.Error(t, err)
_, _, err = ParseFromNetIPPrefix(netip.Prefix{})
assert.Error(t, err)
_, _, err = ParseFromNetIPAddr(netip.MustParseAddr("0.0.0.0"))
assert.Error(t, err)
}