-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_test.go
257 lines (239 loc) · 5.1 KB
/
ip_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
package wgpg
import (
"bytes"
"testing"
)
var (
validaddresses = [...][5]string{
{
"0.0.0.0/0",
"0.0.0.0/0",
"0.0.0.0/0",
"255.255.255.255/0",
"0.0.0.0/32",
}, {
"1.2.3.4/8",
"1.0.0.0/8",
"1.0.0.0/8",
"1.255.255.255/8",
"1.2.3.4/32",
}, {
"1.2.3.4/16",
"1.2.0.0/16",
"1.2.0.0/16",
"1.2.255.255/16",
"1.2.3.4/32",
}, {
"1.2.3.4/24",
"1.2.3.0/24",
"1.2.3.0/24",
"1.2.3.255/24",
"1.2.3.4/32",
}, {
"1.2.3.4/32",
"1.2.3.4/32",
"1.2.3.4/32",
"1.2.3.4/32",
"1.2.3.4/32",
}, {
"255.255.255.255/32",
"255.255.255.255/32",
"255.255.255.255/32",
"255.255.255.255/32",
"255.255.255.255/32",
}, {
"::1/128",
"::1/128",
"::1/128",
"::1/128",
"::1/128",
},
}
invalidaddresses = [...]string{
"1.2.3.4/33",
"0.0.0.256/0",
"1::0::1/128",
"::1/129",
"10.10.10.10.10",
"0.0.0.0",
"::",
}
)
func TestRoundtrip(t *testing.T) {
for _, s := range validaddresses {
want := s[0]
ip, err := ParseIP(want)
if err != nil {
t.Errorf("Failed to parse address %q: %s", want, err.Error())
continue
}
have := ip.String()
t.Logf("Testing roundtrip: %q -> %q", want, have)
if have != want {
t.Errorf("Roundtrip failed. want: %q, have: %q", want, have)
continue
}
}
}
func TestNetwork(t *testing.T) {
for _, s := range validaddresses {
in := s[0]
want := s[1]
ip, err := ParseIP(in)
if err != nil {
t.Errorf("Failed to parse address %q: %s", in, err.Error())
continue
}
network := ip.Network()
have := network.String()
t.Logf("Testing network: %q -> %q", in, have)
if have != want {
t.Errorf("Network failed. want: %q, have: %q [%x]", want, have, ip.Addr)
continue
}
}
}
func TestV4Prefix(t *testing.T) {
want := ip4in6prefix
for _, s := range validaddresses {
in := s[0]
ip, err := ParseIP(in)
if err != nil {
t.Errorf("Failed to parse address %q: %s", in, err.Error())
continue
}
t.Logf("Testing v4 Prefix: %q -> [%x]", in, ip.Addr)
hasprefix := bytes.HasPrefix(ip.Addr[:], want[:])
isv4 := ip.AddrLen == 32
if isv4 != hasprefix {
t.Errorf("IPv4 needs 4in6 prefix. ip: %q, bytes: [%x]", in, ip.Addr)
continue
}
}
}
func TestRange(t *testing.T) {
for _, s := range validaddresses {
in, wantstart, wantend := s[0], s[2], s[3]
ip, err := ParseIP(in)
if err != nil {
t.Errorf("Failed to parse address %q: %s", in, err.Error())
continue
}
havestart, haveend := ip.Range()
t.Logf("Testing range %q: [%q %q]", in, havestart, haveend)
if wantstart != havestart.String() {
t.Errorf("Invalid range for %q. want start: %q, have start %q", in, wantstart, havestart)
continue
}
if wantend != haveend.String() {
t.Errorf("Invalid range for %q. want end: %q, have end %q", in, wantstart, haveend)
continue
}
}
}
func TestHost(t *testing.T) {
for _, s := range validaddresses {
in, want := s[0], s[4]
ip, err := ParseIP(in)
if err != nil {
t.Errorf("Failed to parse address %q: %s", in, err.Error())
continue
}
have := ip.Host().String()
if have != want {
t.Errorf("Expected host %q for %q, have %q", want, in, have)
continue
}
}
}
func iterHosts(t *testing.T, network IP) {
ones, bits := network.NetMask().Size()
if bits == ones {
t.Skip("range has no addressess")
}
n := 1 << uint(bits-ones)
if n > 1<<22 {
t.Skipf("n too large: %d", n)
}
if testing.Short() && n > 1<<16 {
t.Skipf("n too large for short test: %d", n)
}
t.Parallel()
s, e := network.Range()
for i := 0; i < n-1; i++ {
ip, err := GetIP(s, e, i)
if err != nil {
t.Fatalf("Unexpected error: %q", err.Error())
}
if !network.Contains(ip) {
t.Fatalf("No error, but ip out of range: %q not in %q", ip, network)
}
}
ip, err := GetIP(s, e, n-1)
if err == nil {
t.Fatalf("Expected out of addresses error for %q in %q", ip, network)
}
}
func TestGetIP(t *testing.T) {
for _, s := range validaddresses {
in := s[0]
ip, err := ParseIP(in)
if err != nil {
t.Errorf("Failed to parse address %q: %s", in, err.Error())
continue
}
t.Run(in, func(t *testing.T) {
iterHosts(t, ip)
})
}
}
func TestInvalid(t *testing.T) {
for _, in := range invalidaddresses {
ip, err := ParseIP(in)
if err == nil {
t.Errorf("Expected error for invalid address %q, got %q", in, ip)
}
}
}
func TestIPSetParse(t *testing.T) {
var buf bytes.Buffer
sep := ""
for _, s := range validaddresses {
buf.WriteString(sep)
buf.WriteString(s[0])
sep = ", "
}
in := buf.String()
var is IPSet
err := is.UnmarshalText([]byte(in))
if err != nil {
t.Fatal("Unexpected error", err)
}
inter := is.String()
t.Log(inter)
var is2 IPSet
err = is2.UnmarshalText([]byte(inter))
if err != nil {
t.Fatal("Unexpected error", err)
}
have := is2.String()
t.Log(have)
if inter != have {
t.Fatalf("IPSet doesn't roundtrip:\n%q != %q", inter, have)
}
}
func TestIPSetParseInvalid(t *testing.T) {
var buf bytes.Buffer
sep := ""
for _, s := range invalidaddresses {
buf.WriteString(sep)
buf.WriteString(s)
sep = ", "
}
in := buf.String()
var is IPSet
err := is.UnmarshalText([]byte(in))
if err == nil {
t.Fatal("Expected error")
}
}