Source file
src/net/ip_test.go
1
2
3
4
5 package net
6
7 import (
8 "bytes"
9 "math/rand"
10 "reflect"
11 "runtime"
12 "testing"
13 )
14
15 var parseIPTests = []struct {
16 in string
17 out IP
18 }{
19 {"127.0.1.2", IPv4(127, 0, 1, 2)},
20 {"127.0.0.1", IPv4(127, 0, 0, 1)},
21 {"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
22 {"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
23 {"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
24 {"0:0:0:0::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
25
26 {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
27 {"2001:4860:0000:2001:0000:0000:0000:0068", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
28
29 {"-0.0.0.0", nil},
30 {"0.-1.0.0", nil},
31 {"0.0.-2.0", nil},
32 {"0.0.0.-3", nil},
33 {"127.0.0.256", nil},
34 {"abc", nil},
35 {"123:", nil},
36 {"fe80::1%lo0", nil},
37 {"fe80::1%911", nil},
38 {"", nil},
39
40 {"0:0:0:0:000000:ffff:127.1.2.3", nil},
41
42 {"0:0:0:0:00000:ffff:127.1.2.3", nil},
43 {"a1:a2:a3:a4::b1:b2:b3:b4", nil},
44 {"127.001.002.003", nil},
45 {"::ffff:127.001.002.003", nil},
46 {"123.000.000.000", nil},
47 {"1.2..4", nil},
48 {"0123.0.0.1", nil},
49 }
50
51 func TestParseIP(t *testing.T) {
52 for _, tt := range parseIPTests {
53 if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
54 t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
55 }
56 if tt.in == "" {
57
58 continue
59 }
60 var out IP
61 if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
62 t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
63 }
64 }
65 }
66
67 func TestLookupWithIP(t *testing.T) {
68 _, err := LookupIP("")
69 if err == nil {
70 t.Errorf(`LookupIP("") succeeded, should fail`)
71 }
72 _, err = LookupHost("")
73 if err == nil {
74 t.Errorf(`LookupIP("") succeeded, should fail`)
75 }
76
77
78
79 for _, tt := range parseIPTests {
80 if tt.out != nil {
81 addrs, err := LookupHost(tt.in)
82 if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
83 t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
84 }
85 } else if !testing.Short() {
86
87
88
89 addrs, err := LookupHost(tt.in)
90 if err == nil {
91 t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
92 }
93 }
94
95 if tt.out != nil {
96 ips, err := LookupIP(tt.in)
97 if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
98 t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
99 }
100 } else if !testing.Short() {
101 ips, err := LookupIP(tt.in)
102
103 if err == nil {
104 t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
105 }
106 }
107 }
108 }
109
110 func BenchmarkParseIP(b *testing.B) {
111 testHookUninstaller.Do(uninstallTestHooks)
112
113 for i := 0; i < b.N; i++ {
114 for _, tt := range parseIPTests {
115 ParseIP(tt.in)
116 }
117 }
118 }
119
120 func BenchmarkParseIPValidIPv4(b *testing.B) {
121 testHookUninstaller.Do(uninstallTestHooks)
122
123 for i := 0; i < b.N; i++ {
124 ParseIP("192.0.2.1")
125 }
126 }
127
128 func BenchmarkParseIPValidIPv6(b *testing.B) {
129 testHookUninstaller.Do(uninstallTestHooks)
130
131 for i := 0; i < b.N; i++ {
132 ParseIP("2001:DB8::1")
133 }
134 }
135
136
137 func TestMarshalEmptyIP(t *testing.T) {
138 for _, in := range [][]byte{nil, []byte("")} {
139 var out = IP{1, 2, 3, 4}
140 if err := out.UnmarshalText(in); err != nil || out != nil {
141 t.Errorf("UnmarshalText(%v) = %v, %v; want nil, nil", in, out, err)
142 }
143 }
144 var ip IP
145 got, err := ip.MarshalText()
146 if err != nil {
147 t.Fatal(err)
148 }
149 if !reflect.DeepEqual(got, []byte("")) {
150 t.Errorf(`got %#v, want []byte("")`, got)
151 }
152
153 buf := make([]byte, 4)
154 got, err = ip.AppendText(buf)
155 if err != nil {
156 t.Fatal(err)
157 }
158 if !reflect.DeepEqual(got, []byte("\x00\x00\x00\x00")) {
159 t.Errorf(`got %#v, want []byte("\x00\x00\x00\x00")`, got)
160 }
161 }
162
163 var ipStringTests = []*struct {
164 in IP
165 str string
166 byt []byte
167 error
168 }{
169
170 {
171 IP{192, 0, 2, 1},
172 "192.0.2.1",
173 []byte("192.0.2.1"),
174 nil,
175 },
176 {
177 IP{0, 0, 0, 0},
178 "0.0.0.0",
179 []byte("0.0.0.0"),
180 nil,
181 },
182
183
184 {
185 IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 0, 2, 1},
186 "192.0.2.1",
187 []byte("192.0.2.1"),
188 nil,
189 },
190 {
191 IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0},
192 "0.0.0.0",
193 []byte("0.0.0.0"),
194 nil,
195 },
196
197
198 {
199 IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1},
200 "2001:db8::123:12:1",
201 []byte("2001:db8::123:12:1"),
202 nil,
203 },
204 {
205 IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1},
206 "2001:db8::1",
207 []byte("2001:db8::1"),
208 nil,
209 },
210 {
211 IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1},
212 "2001:db8:0:1:0:1:0:1",
213 []byte("2001:db8:0:1:0:1:0:1"),
214 nil,
215 },
216 {
217 IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0},
218 "2001:db8:1:0:1:0:1:0",
219 []byte("2001:db8:1:0:1:0:1:0"),
220 nil,
221 },
222 {
223 IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
224 "2001::1:0:0:1",
225 []byte("2001::1:0:0:1"),
226 nil,
227 },
228 {
229 IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0},
230 "2001:db8:0:0:1::",
231 []byte("2001:db8:0:0:1::"),
232 nil,
233 },
234 {
235 IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
236 "2001:db8::1:0:0:1",
237 []byte("2001:db8::1:0:0:1"),
238 nil,
239 },
240 {
241 IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0xa, 0, 0xb, 0, 0xc, 0, 0xd},
242 "2001:db8::a:b:c:d",
243 []byte("2001:db8::a:b:c:d"),
244 nil,
245 },
246 {
247 IPv6unspecified,
248 "::",
249 []byte("::"),
250 nil,
251 },
252
253
254 {
255 nil,
256 "<nil>",
257 nil,
258 nil,
259 },
260
261
262 {
263 IP{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
264 "?0123456789abcdef",
265 nil,
266 &AddrError{Err: "invalid IP address", Addr: "0123456789abcdef"},
267 },
268 }
269
270 func TestIPString(t *testing.T) {
271 for _, tt := range ipStringTests {
272 if out := tt.in.String(); out != tt.str {
273 t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.str)
274 }
275 if out, err := tt.in.MarshalText(); !bytes.Equal(out, tt.byt) || !reflect.DeepEqual(err, tt.error) {
276 t.Errorf("IP.MarshalText(%v) = %v, %v, want %v, %v", tt.in, out, err, tt.byt, tt.error)
277 }
278 buf := make([]byte, 4, 32)
279 if out, err := tt.in.AppendText(buf); !bytes.Equal(out[4:], tt.byt) || !reflect.DeepEqual(err, tt.error) {
280 t.Errorf("IP.AppendText(%v) = %v, %v, want %v, %v", tt.in, out[4:], err, tt.byt, tt.error)
281 }
282 }
283 }
284
285 func TestIPAppendTextNoAllocs(t *testing.T) {
286
287 for _, tt := range ipStringTests[:len(ipStringTests)-1] {
288 allocs := int(testing.AllocsPerRun(1000, func() {
289 buf := make([]byte, 0, 64)
290 _, _ = tt.in.AppendText(buf)
291 }))
292 if allocs != 0 {
293 t.Errorf("IP(%q) AppendText allocs: %d times, want 0", tt.in, allocs)
294 }
295 }
296 }
297
298 func BenchmarkIPMarshalText(b *testing.B) {
299 b.Run("IPv4", func(b *testing.B) {
300 b.ReportAllocs()
301 b.ResetTimer()
302 ip := IP{192, 0, 2, 1}
303 for range b.N {
304 _, _ = ip.MarshalText()
305 }
306 })
307 b.Run("IPv6", func(b *testing.B) {
308 b.ReportAllocs()
309 b.ResetTimer()
310 ip := IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0xa, 0, 0xb, 0, 0xc, 0, 0xd}
311 for range b.N {
312 _, _ = ip.MarshalText()
313 }
314 })
315 b.Run("IPv6_long", func(b *testing.B) {
316 b.ReportAllocs()
317 b.ResetTimer()
318
319 ip := IP{253, 122, 17, 92, 161, 224, 171, 18, 72, 67, 205, 150, 98, 107, 67, 11}
320 for range b.N {
321 _, _ = ip.MarshalText()
322 }
323 })
324 }
325
326 var sink string
327
328 func BenchmarkIPString(b *testing.B) {
329 testHookUninstaller.Do(uninstallTestHooks)
330
331 b.Run("IPv4", func(b *testing.B) {
332 benchmarkIPString(b, IPv4len)
333 })
334
335 b.Run("IPv6", func(b *testing.B) {
336 benchmarkIPString(b, IPv6len)
337 })
338 }
339
340 func benchmarkIPString(b *testing.B, size int) {
341 b.ReportAllocs()
342 b.ResetTimer()
343 for i := 0; i < b.N; i++ {
344 for _, tt := range ipStringTests {
345 if tt.in != nil && len(tt.in) == size {
346 sink = tt.in.String()
347 }
348 }
349 }
350 }
351
352 var ipMaskTests = []struct {
353 in IP
354 mask IPMask
355 out IP
356 }{
357 {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
358 {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 168, 1, 64)},
359 {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0")), IPv4(192, 168, 1, 96)},
360 {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
361 {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d80::")},
362 {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d08::")},
363 }
364
365 func TestIPMask(t *testing.T) {
366 for _, tt := range ipMaskTests {
367 if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
368 t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
369 }
370 }
371 }
372
373 var ipMaskStringTests = []struct {
374 in IPMask
375 out string
376 }{
377 {IPv4Mask(255, 255, 255, 240), "fffffff0"},
378 {IPv4Mask(255, 0, 128, 0), "ff008000"},
379 {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
380 {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000"},
381 {nil, "<nil>"},
382 }
383
384 func TestIPMaskString(t *testing.T) {
385 for _, tt := range ipMaskStringTests {
386 if out := tt.in.String(); out != tt.out {
387 t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
388 }
389 }
390 }
391
392 func BenchmarkIPMaskString(b *testing.B) {
393 testHookUninstaller.Do(uninstallTestHooks)
394
395 for i := 0; i < b.N; i++ {
396 for _, tt := range ipMaskStringTests {
397 sink = tt.in.String()
398 }
399 }
400 }
401
402 var parseCIDRTests = []struct {
403 in string
404 ip IP
405 net *IPNet
406 err error
407 }{
408 {"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
409 {"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
410 {"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
411 {"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
412 {"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
413 {"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
414 {"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
415 {"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
416 {"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
417 {"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
418 {"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
419 {"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil},
420 {"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil},
421 {"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
422 {"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
423 {"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
424 {"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
425 {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/255.255.255.0"}},
426 {"192.168.1.1/35", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/35"}},
427 {"2001:db8::1/-1", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-1"}},
428 {"2001:db8::1/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-0"}},
429 {"-0.0.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "-0.0.0.0/32"}},
430 {"0.-1.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.-1.0.0/32"}},
431 {"0.0.-2.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.-2.0/32"}},
432 {"0.0.0.-3/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.-3/32"}},
433 {"0.0.0.0/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.0/-0"}},
434 {"127.000.000.001/32", nil, nil, &ParseError{Type: "CIDR address", Text: "127.000.000.001/32"}},
435 {"", nil, nil, &ParseError{Type: "CIDR address", Text: ""}},
436 }
437
438 func TestParseCIDR(t *testing.T) {
439 for _, tt := range parseCIDRTests {
440 ip, net, err := ParseCIDR(tt.in)
441 if !reflect.DeepEqual(err, tt.err) {
442 t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
443 }
444 if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
445 t.Errorf("ParseCIDR(%q) = %v, {%v, %v}; want %v, {%v, %v}", tt.in, ip, net.IP, net.Mask, tt.ip, tt.net.IP, tt.net.Mask)
446 }
447 }
448 }
449
450 var ipNetContainsTests = []struct {
451 ip IP
452 net *IPNet
453 ok bool
454 }{
455 {IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true},
456 {IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false},
457 {IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 0, 255, 252)}, true},
458 {IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 255, 0, 252)}, false},
459 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: CIDRMask(47, 128)}, true},
460 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: CIDRMask(47, 128)}, false},
461 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:0:ffff::"))}, true},
462 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("0:0:0:ffff::"))}, false},
463 }
464
465 func TestIPNetContains(t *testing.T) {
466 for _, tt := range ipNetContainsTests {
467 if ok := tt.net.Contains(tt.ip); ok != tt.ok {
468 t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
469 }
470 }
471 }
472
473 var ipNetStringTests = []struct {
474 in *IPNet
475 out string
476 }{
477 {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/26"},
478 {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
479 {&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8::/55"},
480 {&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
481 {nil, "<nil>"},
482 }
483
484 func TestIPNetString(t *testing.T) {
485 for _, tt := range ipNetStringTests {
486 if out := tt.in.String(); out != tt.out {
487 t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
488 }
489 }
490 }
491
492 var cidrMaskTests = []struct {
493 ones int
494 bits int
495 out IPMask
496 }{
497 {0, 32, IPv4Mask(0, 0, 0, 0)},
498 {12, 32, IPv4Mask(255, 240, 0, 0)},
499 {24, 32, IPv4Mask(255, 255, 255, 0)},
500 {32, 32, IPv4Mask(255, 255, 255, 255)},
501 {0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
502 {4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
503 {48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
504 {128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
505 {33, 32, nil},
506 {32, 33, nil},
507 {-1, 128, nil},
508 {128, -1, nil},
509 }
510
511 func TestCIDRMask(t *testing.T) {
512 for _, tt := range cidrMaskTests {
513 if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) {
514 t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
515 }
516 }
517 }
518
519 var (
520 v4addr = IP{192, 168, 0, 1}
521 v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
522 v6addr = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}
523 v4mask = IPMask{255, 255, 255, 0}
524 v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
525 v6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
526 badaddr = IP{192, 168, 0}
527 badmask = IPMask{255, 255, 0}
528 v4maskzero = IPMask{0, 0, 0, 0}
529 )
530
531 var networkNumberAndMaskTests = []struct {
532 in IPNet
533 out IPNet
534 }{
535 {IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}},
536 {IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
537 {IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
538 {IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
539 {IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
540 {IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}},
541 {IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mappedv6mask}},
542 {in: IPNet{IP: v6addr, Mask: v4mask}},
543 {in: IPNet{IP: v4addr, Mask: badmask}},
544 {in: IPNet{IP: v4mappedv6addr, Mask: badmask}},
545 {in: IPNet{IP: v6addr, Mask: badmask}},
546 {in: IPNet{IP: badaddr, Mask: v4mask}},
547 {in: IPNet{IP: badaddr, Mask: v4mappedv6mask}},
548 {in: IPNet{IP: badaddr, Mask: v6mask}},
549 {in: IPNet{IP: badaddr, Mask: badmask}},
550 }
551
552 func TestNetworkNumberAndMask(t *testing.T) {
553 for _, tt := range networkNumberAndMaskTests {
554 ip, m := networkNumberAndMask(&tt.in)
555 out := &IPNet{IP: ip, Mask: m}
556 if !reflect.DeepEqual(&tt.out, out) {
557 t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out)
558 }
559 }
560 }
561
562 func TestSplitHostPort(t *testing.T) {
563 for _, tt := range []struct {
564 hostPort string
565 host string
566 port string
567 }{
568
569 {"localhost:http", "localhost", "http"},
570 {"localhost:80", "localhost", "80"},
571
572
573 {"localhost%lo0:http", "localhost%lo0", "http"},
574 {"localhost%lo0:80", "localhost%lo0", "80"},
575 {"[localhost%lo0]:http", "localhost%lo0", "http"},
576 {"[localhost%lo0]:80", "localhost%lo0", "80"},
577
578
579 {"127.0.0.1:http", "127.0.0.1", "http"},
580 {"127.0.0.1:80", "127.0.0.1", "80"},
581 {"[::1]:http", "::1", "http"},
582 {"[::1]:80", "::1", "80"},
583
584
585 {"[::1%lo0]:http", "::1%lo0", "http"},
586 {"[::1%lo0]:80", "::1%lo0", "80"},
587
588
589 {":http", "", "http"},
590 {":80", "", "80"},
591
592
593 {"golang.org:", "golang.org", ""},
594 {"127.0.0.1:", "127.0.0.1", ""},
595 {"[::1]:", "::1", ""},
596
597
598 {"golang.org:https%foo", "golang.org", "https%foo"},
599 } {
600 if host, port, err := SplitHostPort(tt.hostPort); host != tt.host || port != tt.port || err != nil {
601 t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.hostPort, host, port, err, tt.host, tt.port)
602 }
603 }
604
605 for _, tt := range []struct {
606 hostPort string
607 err string
608 }{
609 {"golang.org", "missing port in address"},
610 {"127.0.0.1", "missing port in address"},
611 {"[::1]", "missing port in address"},
612 {"[fe80::1%lo0]", "missing port in address"},
613 {"[localhost%lo0]", "missing port in address"},
614 {"localhost%lo0", "missing port in address"},
615
616 {"::1", "too many colons in address"},
617 {"fe80::1%lo0", "too many colons in address"},
618 {"fe80::1%lo0:80", "too many colons in address"},
619
620
621
622 {"[foo:bar]", "missing port in address"},
623 {"[foo:bar]baz", "missing port in address"},
624 {"[foo]bar:baz", "missing port in address"},
625
626 {"[foo]:[bar]:baz", "too many colons in address"},
627
628 {"[foo]:[bar]baz", "unexpected '[' in address"},
629 {"foo[bar]:baz", "unexpected '[' in address"},
630
631 {"foo]bar:baz", "unexpected ']' in address"},
632 } {
633 if host, port, err := SplitHostPort(tt.hostPort); err == nil {
634 t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
635 } else {
636 e := err.(*AddrError)
637 if e.Err != tt.err {
638 t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
639 }
640 if host != "" || port != "" {
641 t.Errorf("SplitHostPort(%q) = %q, %q, err; want %q, %q, err on failure", tt.hostPort, host, port, "", "")
642 }
643 }
644 }
645 }
646
647 func TestJoinHostPort(t *testing.T) {
648 for _, tt := range []struct {
649 host string
650 port string
651 hostPort string
652 }{
653
654 {"localhost", "http", "localhost:http"},
655 {"localhost", "80", "localhost:80"},
656
657
658 {"localhost%lo0", "http", "localhost%lo0:http"},
659 {"localhost%lo0", "80", "localhost%lo0:80"},
660
661
662 {"127.0.0.1", "http", "127.0.0.1:http"},
663 {"127.0.0.1", "80", "127.0.0.1:80"},
664 {"::1", "http", "[::1]:http"},
665 {"::1", "80", "[::1]:80"},
666
667
668 {"::1%lo0", "http", "[::1%lo0]:http"},
669 {"::1%lo0", "80", "[::1%lo0]:80"},
670
671
672 {"", "http", ":http"},
673 {"", "80", ":80"},
674
675
676 {"golang.org", "", "golang.org:"},
677 {"127.0.0.1", "", "127.0.0.1:"},
678 {"::1", "", "[::1]:"},
679
680
681 {"golang.org", "https%foo", "golang.org:https%foo"},
682 } {
683 if hostPort := JoinHostPort(tt.host, tt.port); hostPort != tt.hostPort {
684 t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, hostPort, tt.hostPort)
685 }
686 }
687 }
688
689 var ipAddrFamilyTests = []struct {
690 in IP
691 af4 bool
692 af6 bool
693 }{
694 {IPv4bcast, true, false},
695 {IPv4allsys, true, false},
696 {IPv4allrouter, true, false},
697 {IPv4zero, true, false},
698 {IPv4(224, 0, 0, 1), true, false},
699 {IPv4(127, 0, 0, 1), true, false},
700 {IPv4(240, 0, 0, 1), true, false},
701 {IPv6unspecified, false, true},
702 {IPv6loopback, false, true},
703 {IPv6interfacelocalallnodes, false, true},
704 {IPv6linklocalallnodes, false, true},
705 {IPv6linklocalallrouters, false, true},
706 {ParseIP("ff05::a:b:c:d"), false, true},
707 {ParseIP("fe80::1:2:3:4"), false, true},
708 {ParseIP("2001:db8::123:12:1"), false, true},
709 }
710
711 func TestIPAddrFamily(t *testing.T) {
712 for _, tt := range ipAddrFamilyTests {
713 if af := tt.in.To4() != nil; af != tt.af4 {
714 t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
715 }
716 if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.af6 {
717 t.Errorf("verifying IPv6 address family for %q = %v, want %v", tt.in, af, tt.af6)
718 }
719 }
720 }
721
722 var ipAddrScopeTests = []struct {
723 scope func(IP) bool
724 in IP
725 ok bool
726 }{
727 {IP.IsUnspecified, IPv4zero, true},
728 {IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
729 {IP.IsUnspecified, IPv6unspecified, true},
730 {IP.IsUnspecified, IPv6interfacelocalallnodes, false},
731 {IP.IsUnspecified, nil, false},
732 {IP.IsLoopback, IPv4(127, 0, 0, 1), true},
733 {IP.IsLoopback, IPv4(127, 255, 255, 254), true},
734 {IP.IsLoopback, IPv4(128, 1, 2, 3), false},
735 {IP.IsLoopback, IPv6loopback, true},
736 {IP.IsLoopback, IPv6linklocalallrouters, false},
737 {IP.IsLoopback, nil, false},
738 {IP.IsMulticast, IPv4(224, 0, 0, 0), true},
739 {IP.IsMulticast, IPv4(239, 0, 0, 0), true},
740 {IP.IsMulticast, IPv4(240, 0, 0, 0), false},
741 {IP.IsMulticast, IPv6linklocalallnodes, true},
742 {IP.IsMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
743 {IP.IsMulticast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
744 {IP.IsMulticast, nil, false},
745 {IP.IsInterfaceLocalMulticast, IPv4(224, 0, 0, 0), false},
746 {IP.IsInterfaceLocalMulticast, IPv4(0xff, 0x01, 0, 0), false},
747 {IP.IsInterfaceLocalMulticast, IPv6interfacelocalallnodes, true},
748 {IP.IsInterfaceLocalMulticast, nil, false},
749 {IP.IsLinkLocalMulticast, IPv4(224, 0, 0, 0), true},
750 {IP.IsLinkLocalMulticast, IPv4(239, 0, 0, 0), false},
751 {IP.IsLinkLocalMulticast, IPv4(0xff, 0x02, 0, 0), false},
752 {IP.IsLinkLocalMulticast, IPv6linklocalallrouters, true},
753 {IP.IsLinkLocalMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
754 {IP.IsLinkLocalMulticast, nil, false},
755 {IP.IsLinkLocalUnicast, IPv4(169, 254, 0, 0), true},
756 {IP.IsLinkLocalUnicast, IPv4(169, 255, 0, 0), false},
757 {IP.IsLinkLocalUnicast, IPv4(0xfe, 0x80, 0, 0), false},
758 {IP.IsLinkLocalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
759 {IP.IsLinkLocalUnicast, IP{0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
760 {IP.IsLinkLocalUnicast, nil, false},
761 {IP.IsGlobalUnicast, IPv4(240, 0, 0, 0), true},
762 {IP.IsGlobalUnicast, IPv4(232, 0, 0, 0), false},
763 {IP.IsGlobalUnicast, IPv4(169, 254, 0, 0), false},
764 {IP.IsGlobalUnicast, IPv4bcast, false},
765 {IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, true},
766 {IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
767 {IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
768 {IP.IsGlobalUnicast, nil, false},
769 {IP.IsPrivate, nil, false},
770 {IP.IsPrivate, IPv4(1, 1, 1, 1), false},
771 {IP.IsPrivate, IPv4(9, 255, 255, 255), false},
772 {IP.IsPrivate, IPv4(10, 0, 0, 0), true},
773 {IP.IsPrivate, IPv4(10, 255, 255, 255), true},
774 {IP.IsPrivate, IPv4(11, 0, 0, 0), false},
775 {IP.IsPrivate, IPv4(172, 15, 255, 255), false},
776 {IP.IsPrivate, IPv4(172, 16, 0, 0), true},
777 {IP.IsPrivate, IPv4(172, 16, 255, 255), true},
778 {IP.IsPrivate, IPv4(172, 23, 18, 255), true},
779 {IP.IsPrivate, IPv4(172, 31, 255, 255), true},
780 {IP.IsPrivate, IPv4(172, 31, 0, 0), true},
781 {IP.IsPrivate, IPv4(172, 32, 0, 0), false},
782 {IP.IsPrivate, IPv4(192, 167, 255, 255), false},
783 {IP.IsPrivate, IPv4(192, 168, 0, 0), true},
784 {IP.IsPrivate, IPv4(192, 168, 255, 255), true},
785 {IP.IsPrivate, IPv4(192, 169, 0, 0), false},
786 {IP.IsPrivate, IP{0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, false},
787 {IP.IsPrivate, IP{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
788 {IP.IsPrivate, IP{0xfc, 0xff, 0x12, 0, 0, 0, 0, 0x44, 0, 0, 0, 0, 0, 0, 0, 0}, true},
789 {IP.IsPrivate, IP{0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, true},
790 {IP.IsPrivate, IP{0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
791 }
792
793 func name(f any) string {
794 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
795 }
796
797 func TestIPAddrScope(t *testing.T) {
798 for _, tt := range ipAddrScopeTests {
799 if ok := tt.scope(tt.in); ok != tt.ok {
800 t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
801 }
802 ip := tt.in.To4()
803 if ip == nil {
804 continue
805 }
806 if ok := tt.scope(ip); ok != tt.ok {
807 t.Errorf("%s(%q) = %v, want %v", name(tt.scope), ip, ok, tt.ok)
808 }
809 }
810 }
811
812 func BenchmarkIPEqual(b *testing.B) {
813 b.Run("IPv4", func(b *testing.B) {
814 benchmarkIPEqual(b, IPv4len)
815 })
816 b.Run("IPv6", func(b *testing.B) {
817 benchmarkIPEqual(b, IPv6len)
818 })
819 }
820
821 func benchmarkIPEqual(b *testing.B, size int) {
822 ips := make([]IP, 1000)
823 for i := range ips {
824 ips[i] = make(IP, size)
825 rand.Read(ips[i])
826 }
827
828 for i := 0; i < b.N/2; i++ {
829 x := ips[i%len(ips)]
830 y := ips[i%len(ips)]
831 x.Equal(y)
832 }
833
834 for i := 0; i < b.N/2; i++ {
835 x := ips[i%len(ips)]
836 y := ips[(i+1)%len(ips)]
837 x.Equal(y)
838 }
839 }
840
View as plain text