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