github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/net/lookup_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 package net 6 7 import ( 8 "bytes" 9 "context" 10 "fmt" 11 "internal/testenv" 12 "reflect" 13 "runtime" 14 "sort" 15 "strings" 16 "testing" 17 "time" 18 ) 19 20 func lookupLocalhost(ctx context.Context, fn func(context.Context, string) ([]IPAddr, error), host string) ([]IPAddr, error) { 21 switch host { 22 case "localhost": 23 return []IPAddr{ 24 {IP: IPv4(127, 0, 0, 1)}, 25 {IP: IPv6loopback}, 26 }, nil 27 default: 28 return fn(ctx, host) 29 } 30 } 31 32 // The Lookup APIs use various sources such as local database, DNS or 33 // mDNS, and may use platform-dependent DNS stub resolver if possible. 34 // The APIs accept any of forms for a query; host name in various 35 // encodings, UTF-8 encoded net name, domain name, FQDN or absolute 36 // FQDN, but the result would be one of the forms and it depends on 37 // the circumstances. 38 39 var lookupGoogleSRVTests = []struct { 40 service, proto, name string 41 cname, target string 42 }{ 43 { 44 "xmpp-server", "tcp", "google.com", 45 "google.com.", "google.com.", 46 }, 47 { 48 "xmpp-server", "tcp", "google.com.", 49 "google.com.", "google.com.", 50 }, 51 52 // non-standard back door 53 { 54 "", "", "_xmpp-server._tcp.google.com", 55 "google.com.", "google.com.", 56 }, 57 { 58 "", "", "_xmpp-server._tcp.google.com.", 59 "google.com.", "google.com.", 60 }, 61 } 62 63 func TestLookupGoogleSRV(t *testing.T) { 64 if testenv.Builder() == "" { 65 testenv.MustHaveExternalNetwork(t) 66 } 67 68 if !supportsIPv4() || !*testIPv4 { 69 t.Skip("IPv4 is required") 70 } 71 72 for _, tt := range lookupGoogleSRVTests { 73 cname, srvs, err := LookupSRV(tt.service, tt.proto, tt.name) 74 if err != nil { 75 testenv.SkipFlakyNet(t) 76 t.Fatal(err) 77 } 78 if len(srvs) == 0 { 79 t.Error("got no record") 80 } 81 if !strings.HasSuffix(cname, tt.cname) { 82 t.Errorf("got %s; want %s", cname, tt.cname) 83 } 84 for _, srv := range srvs { 85 if !strings.HasSuffix(srv.Target, tt.target) { 86 t.Errorf("got %v; want a record containing %s", srv, tt.target) 87 } 88 } 89 } 90 } 91 92 var lookupGmailMXTests = []struct { 93 name, host string 94 }{ 95 {"gmail.com", "google.com."}, 96 {"gmail.com.", "google.com."}, 97 } 98 99 func TestLookupGmailMX(t *testing.T) { 100 if testenv.Builder() == "" { 101 testenv.MustHaveExternalNetwork(t) 102 } 103 104 if !supportsIPv4() || !*testIPv4 { 105 t.Skip("IPv4 is required") 106 } 107 108 defer dnsWaitGroup.Wait() 109 110 for _, tt := range lookupGmailMXTests { 111 mxs, err := LookupMX(tt.name) 112 if err != nil { 113 t.Fatal(err) 114 } 115 if len(mxs) == 0 { 116 t.Error("got no record") 117 } 118 for _, mx := range mxs { 119 if !strings.HasSuffix(mx.Host, tt.host) { 120 t.Errorf("got %v; want a record containing %s", mx, tt.host) 121 } 122 } 123 } 124 } 125 126 var lookupGmailNSTests = []struct { 127 name, host string 128 }{ 129 {"gmail.com", "google.com."}, 130 {"gmail.com.", "google.com."}, 131 } 132 133 func TestLookupGmailNS(t *testing.T) { 134 if testenv.Builder() == "" { 135 testenv.MustHaveExternalNetwork(t) 136 } 137 138 if !supportsIPv4() || !*testIPv4 { 139 t.Skip("IPv4 is required") 140 } 141 142 defer dnsWaitGroup.Wait() 143 144 for _, tt := range lookupGmailNSTests { 145 nss, err := LookupNS(tt.name) 146 if err != nil { 147 testenv.SkipFlakyNet(t) 148 t.Fatal(err) 149 } 150 if len(nss) == 0 { 151 t.Error("got no record") 152 } 153 for _, ns := range nss { 154 if !strings.HasSuffix(ns.Host, tt.host) { 155 t.Errorf("got %v; want a record containing %s", ns, tt.host) 156 } 157 } 158 } 159 } 160 161 var lookupGmailTXTTests = []struct { 162 name, txt, host string 163 }{ 164 {"gmail.com", "spf", "google.com"}, 165 {"gmail.com.", "spf", "google.com"}, 166 } 167 168 func TestLookupGmailTXT(t *testing.T) { 169 if testenv.Builder() == "" { 170 testenv.MustHaveExternalNetwork(t) 171 } 172 173 if !supportsIPv4() || !*testIPv4 { 174 t.Skip("IPv4 is required") 175 } 176 177 defer dnsWaitGroup.Wait() 178 179 for _, tt := range lookupGmailTXTTests { 180 txts, err := LookupTXT(tt.name) 181 if err != nil { 182 t.Fatal(err) 183 } 184 if len(txts) == 0 { 185 t.Error("got no record") 186 } 187 found := false 188 for _, txt := range txts { 189 if strings.Contains(txt, tt.txt) && (strings.HasSuffix(txt, tt.host) || strings.HasSuffix(txt, tt.host+".")) { 190 found = true 191 break 192 } 193 } 194 if !found { 195 t.Errorf("got %v; want a record containing %s, %s", txts, tt.txt, tt.host) 196 } 197 } 198 } 199 200 var lookupGooglePublicDNSAddrTests = []struct { 201 addr, name string 202 }{ 203 {"8.8.8.8", ".google.com."}, 204 {"8.8.4.4", ".google.com."}, 205 206 {"2001:4860:4860::8888", ".google.com."}, 207 {"2001:4860:4860::8844", ".google.com."}, 208 } 209 210 func TestLookupGooglePublicDNSAddr(t *testing.T) { 211 if testenv.Builder() == "" { 212 testenv.MustHaveExternalNetwork(t) 213 } 214 215 if !supportsIPv4() || !supportsIPv6() || !*testIPv4 || !*testIPv6 { 216 t.Skip("both IPv4 and IPv6 are required") 217 } 218 219 defer dnsWaitGroup.Wait() 220 221 for _, tt := range lookupGooglePublicDNSAddrTests { 222 names, err := LookupAddr(tt.addr) 223 if err != nil { 224 t.Fatal(err) 225 } 226 if len(names) == 0 { 227 t.Error("got no record") 228 } 229 for _, name := range names { 230 if !strings.HasSuffix(name, tt.name) { 231 t.Errorf("got %s; want a record containing %s", name, tt.name) 232 } 233 } 234 } 235 } 236 237 func TestLookupIPv6LinkLocalAddr(t *testing.T) { 238 if !supportsIPv6() || !*testIPv6 { 239 t.Skip("IPv6 is required") 240 } 241 242 defer dnsWaitGroup.Wait() 243 244 addrs, err := LookupHost("localhost") 245 if err != nil { 246 t.Fatal(err) 247 } 248 found := false 249 for _, addr := range addrs { 250 if addr == "fe80::1%lo0" { 251 found = true 252 break 253 } 254 } 255 if !found { 256 t.Skipf("not supported on %s", runtime.GOOS) 257 } 258 if _, err := LookupAddr("fe80::1%lo0"); err != nil { 259 t.Error(err) 260 } 261 } 262 263 var lookupCNAMETests = []struct { 264 name, cname string 265 }{ 266 {"www.iana.org", "icann.org."}, 267 {"www.iana.org.", "icann.org."}, 268 {"www.google.com", "google.com."}, 269 } 270 271 func TestLookupCNAME(t *testing.T) { 272 if testenv.Builder() == "" { 273 testenv.MustHaveExternalNetwork(t) 274 } 275 276 if !supportsIPv4() || !*testIPv4 { 277 t.Skip("IPv4 is required") 278 } 279 280 defer dnsWaitGroup.Wait() 281 282 for _, tt := range lookupCNAMETests { 283 cname, err := LookupCNAME(tt.name) 284 if err != nil { 285 t.Fatal(err) 286 } 287 if !strings.HasSuffix(cname, tt.cname) { 288 t.Errorf("got %s; want a record containing %s", cname, tt.cname) 289 } 290 } 291 } 292 293 var lookupGoogleHostTests = []struct { 294 name string 295 }{ 296 {"google.com"}, 297 {"google.com."}, 298 } 299 300 func TestLookupGoogleHost(t *testing.T) { 301 if testenv.Builder() == "" { 302 testenv.MustHaveExternalNetwork(t) 303 } 304 305 if !supportsIPv4() || !*testIPv4 { 306 t.Skip("IPv4 is required") 307 } 308 309 defer dnsWaitGroup.Wait() 310 311 for _, tt := range lookupGoogleHostTests { 312 addrs, err := LookupHost(tt.name) 313 if err != nil { 314 t.Fatal(err) 315 } 316 if len(addrs) == 0 { 317 t.Error("got no record") 318 } 319 for _, addr := range addrs { 320 if ParseIP(addr) == nil { 321 t.Errorf("got %q; want a literal IP address", addr) 322 } 323 } 324 } 325 } 326 327 func TestLookupLongTXT(t *testing.T) { 328 if runtime.GOOS == "plan9" { 329 t.Skip("skipping on plan9; see https://golang.org/issue/22857") 330 } 331 if testenv.Builder() == "" { 332 testenv.MustHaveExternalNetwork(t) 333 } 334 335 defer dnsWaitGroup.Wait() 336 337 txts, err := LookupTXT("golang.rsc.io") 338 if err != nil { 339 t.Fatal(err) 340 } 341 sort.Strings(txts) 342 want := []string{ 343 strings.Repeat("abcdefghijklmnopqrstuvwxyABCDEFGHJIKLMNOPQRSTUVWXY", 10), 344 "gophers rule", 345 } 346 if !reflect.DeepEqual(txts, want) { 347 t.Fatalf("LookupTXT golang.rsc.io incorrect\nhave %q\nwant %q", txts, want) 348 } 349 } 350 351 var lookupGoogleIPTests = []struct { 352 name string 353 }{ 354 {"google.com"}, 355 {"google.com."}, 356 } 357 358 func TestLookupGoogleIP(t *testing.T) { 359 if testenv.Builder() == "" { 360 testenv.MustHaveExternalNetwork(t) 361 } 362 363 if !supportsIPv4() || !*testIPv4 { 364 t.Skip("IPv4 is required") 365 } 366 367 defer dnsWaitGroup.Wait() 368 369 for _, tt := range lookupGoogleIPTests { 370 ips, err := LookupIP(tt.name) 371 if err != nil { 372 t.Fatal(err) 373 } 374 if len(ips) == 0 { 375 t.Error("got no record") 376 } 377 for _, ip := range ips { 378 if ip.To4() == nil && ip.To16() == nil { 379 t.Errorf("got %v; want an IP address", ip) 380 } 381 } 382 } 383 } 384 385 var revAddrTests = []struct { 386 Addr string 387 Reverse string 388 ErrPrefix string 389 }{ 390 {"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""}, 391 {"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""}, 392 {"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""}, 393 {"::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", ""}, 394 {"1::", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.ip6.arpa.", ""}, 395 {"1234:567::89a:bcde", "e.d.c.b.a.9.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.6.5.0.4.3.2.1.ip6.arpa.", ""}, 396 {"1234:567:fefe:bcbc:adad:9e4a:89a:bcde", "e.d.c.b.a.9.8.0.a.4.e.9.d.a.d.a.c.b.c.b.e.f.e.f.7.6.5.0.4.3.2.1.ip6.arpa.", ""}, 397 {"1.2.3", "", "unrecognized address"}, 398 {"1.2.3.4.5", "", "unrecognized address"}, 399 {"1234:567:bcbca::89a:bcde", "", "unrecognized address"}, 400 {"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"}, 401 } 402 403 func TestReverseAddress(t *testing.T) { 404 defer dnsWaitGroup.Wait() 405 for i, tt := range revAddrTests { 406 a, err := reverseaddr(tt.Addr) 407 if len(tt.ErrPrefix) > 0 && err == nil { 408 t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix) 409 continue 410 } 411 if len(tt.ErrPrefix) == 0 && err != nil { 412 t.Errorf("#%d: expected <nil>, got %q (error)", i, err) 413 } 414 if err != nil && err.(*DNSError).Err != tt.ErrPrefix { 415 t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, err.(*DNSError).Err) 416 } 417 if a != tt.Reverse { 418 t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a) 419 } 420 } 421 } 422 423 func TestDNSFlood(t *testing.T) { 424 if !*testDNSFlood { 425 t.Skip("test disabled; use -dnsflood to enable") 426 } 427 428 defer dnsWaitGroup.Wait() 429 430 var N = 5000 431 if runtime.GOOS == "darwin" { 432 // On Darwin this test consumes kernel threads much 433 // than other platforms for some reason. 434 // When we monitor the number of allocated Ms by 435 // observing on runtime.newm calls, we can see that it 436 // easily reaches the per process ceiling 437 // kern.num_threads when CGO_ENABLED=1 and 438 // GODEBUG=netdns=go. 439 N = 500 440 } 441 442 const timeout = 3 * time.Second 443 ctxHalfTimeout, cancel := context.WithTimeout(context.Background(), timeout/2) 444 defer cancel() 445 ctxTimeout, cancel := context.WithTimeout(context.Background(), timeout) 446 defer cancel() 447 448 c := make(chan error, 2*N) 449 for i := 0; i < N; i++ { 450 name := fmt.Sprintf("%d.net-test.golang.org", i) 451 go func() { 452 _, err := DefaultResolver.LookupIPAddr(ctxHalfTimeout, name) 453 c <- err 454 }() 455 go func() { 456 _, err := DefaultResolver.LookupIPAddr(ctxTimeout, name) 457 c <- err 458 }() 459 } 460 qstats := struct { 461 succeeded, failed int 462 timeout, temporary, other int 463 unknown int 464 }{} 465 deadline := time.After(timeout + time.Second) 466 for i := 0; i < 2*N; i++ { 467 select { 468 case <-deadline: 469 t.Fatal("deadline exceeded") 470 case err := <-c: 471 switch err := err.(type) { 472 case nil: 473 qstats.succeeded++ 474 case Error: 475 qstats.failed++ 476 if err.Timeout() { 477 qstats.timeout++ 478 } 479 if err.Temporary() { 480 qstats.temporary++ 481 } 482 if !err.Timeout() && !err.Temporary() { 483 qstats.other++ 484 } 485 default: 486 qstats.failed++ 487 qstats.unknown++ 488 } 489 } 490 } 491 492 // A high volume of DNS queries for sub-domain of golang.org 493 // would be coordinated by authoritative or recursive server, 494 // or stub resolver which implements query-response rate 495 // limitation, so we can expect some query successes and more 496 // failures including timeout, temporary and other here. 497 // As a rule, unknown must not be shown but it might possibly 498 // happen due to issue 4856 for now. 499 t.Logf("%v succeeded, %v failed (%v timeout, %v temporary, %v other, %v unknown)", qstats.succeeded, qstats.failed, qstats.timeout, qstats.temporary, qstats.other, qstats.unknown) 500 } 501 502 func TestLookupDotsWithLocalSource(t *testing.T) { 503 if !supportsIPv4() || !*testIPv4 { 504 t.Skip("IPv4 is required") 505 } 506 507 if testenv.Builder() == "" { 508 testenv.MustHaveExternalNetwork(t) 509 } 510 511 defer dnsWaitGroup.Wait() 512 513 for i, fn := range []func() func(){forceGoDNS, forceCgoDNS} { 514 fixup := fn() 515 if fixup == nil { 516 continue 517 } 518 names, err := LookupAddr("127.0.0.1") 519 fixup() 520 if err != nil { 521 t.Logf("#%d: %v", i, err) 522 continue 523 } 524 mode := "netgo" 525 if i == 1 { 526 mode = "netcgo" 527 } 528 loop: 529 for i, name := range names { 530 if strings.Index(name, ".") == len(name)-1 { // "localhost" not "localhost." 531 for j := range names { 532 if j == i { 533 continue 534 } 535 if names[j] == name[:len(name)-1] { 536 // It's OK if we find the name without the dot, 537 // as some systems say 127.0.0.1 localhost localhost. 538 continue loop 539 } 540 } 541 t.Errorf("%s: got %s; want %s", mode, name, name[:len(name)-1]) 542 } else if strings.Contains(name, ".") && !strings.HasSuffix(name, ".") { // "localhost.localdomain." not "localhost.localdomain" 543 t.Errorf("%s: got %s; want name ending with trailing dot", mode, name) 544 } 545 } 546 } 547 } 548 549 func TestLookupDotsWithRemoteSource(t *testing.T) { 550 if testenv.Builder() == "" { 551 testenv.MustHaveExternalNetwork(t) 552 } 553 554 if !supportsIPv4() || !*testIPv4 { 555 t.Skip("IPv4 is required") 556 } 557 558 defer dnsWaitGroup.Wait() 559 560 if fixup := forceGoDNS(); fixup != nil { 561 testDots(t, "go") 562 fixup() 563 } 564 if fixup := forceCgoDNS(); fixup != nil { 565 testDots(t, "cgo") 566 fixup() 567 } 568 } 569 570 func testDots(t *testing.T, mode string) { 571 names, err := LookupAddr("8.8.8.8") // Google dns server 572 if err != nil { 573 testenv.SkipFlakyNet(t) 574 t.Errorf("LookupAddr(8.8.8.8): %v (mode=%v)", err, mode) 575 } else { 576 for _, name := range names { 577 if !strings.HasSuffix(name, ".google.com.") { 578 t.Errorf("LookupAddr(8.8.8.8) = %v, want names ending in .google.com. with trailing dot (mode=%v)", names, mode) 579 break 580 } 581 } 582 } 583 584 cname, err := LookupCNAME("www.mit.edu") 585 if err != nil { 586 testenv.SkipFlakyNet(t) 587 t.Errorf("LookupCNAME(www.mit.edu, mode=%v): %v", mode, err) 588 } else if !strings.HasSuffix(cname, ".") { 589 t.Errorf("LookupCNAME(www.mit.edu) = %v, want cname ending in . with trailing dot (mode=%v)", cname, mode) 590 } 591 592 mxs, err := LookupMX("google.com") 593 if err != nil { 594 testenv.SkipFlakyNet(t) 595 t.Errorf("LookupMX(google.com): %v (mode=%v)", err, mode) 596 } else { 597 for _, mx := range mxs { 598 if !strings.HasSuffix(mx.Host, ".google.com.") { 599 t.Errorf("LookupMX(google.com) = %v, want names ending in .google.com. with trailing dot (mode=%v)", mxString(mxs), mode) 600 break 601 } 602 } 603 } 604 605 nss, err := LookupNS("google.com") 606 if err != nil { 607 testenv.SkipFlakyNet(t) 608 t.Errorf("LookupNS(google.com): %v (mode=%v)", err, mode) 609 } else { 610 for _, ns := range nss { 611 if !strings.HasSuffix(ns.Host, ".google.com.") { 612 t.Errorf("LookupNS(google.com) = %v, want names ending in .google.com. with trailing dot (mode=%v)", nsString(nss), mode) 613 break 614 } 615 } 616 } 617 618 cname, srvs, err := LookupSRV("xmpp-server", "tcp", "google.com") 619 if err != nil { 620 testenv.SkipFlakyNet(t) 621 t.Errorf("LookupSRV(xmpp-server, tcp, google.com): %v (mode=%v)", err, mode) 622 } else { 623 if !strings.HasSuffix(cname, ".google.com.") { 624 t.Errorf("LookupSRV(xmpp-server, tcp, google.com) returned cname=%v, want name ending in .google.com. with trailing dot (mode=%v)", cname, mode) 625 } 626 for _, srv := range srvs { 627 if !strings.HasSuffix(srv.Target, ".google.com.") { 628 t.Errorf("LookupSRV(xmpp-server, tcp, google.com) returned addrs=%v, want names ending in .google.com. with trailing dot (mode=%v)", srvString(srvs), mode) 629 break 630 } 631 } 632 } 633 } 634 635 func mxString(mxs []*MX) string { 636 var buf bytes.Buffer 637 sep := "" 638 fmt.Fprintf(&buf, "[") 639 for _, mx := range mxs { 640 fmt.Fprintf(&buf, "%s%s:%d", sep, mx.Host, mx.Pref) 641 sep = " " 642 } 643 fmt.Fprintf(&buf, "]") 644 return buf.String() 645 } 646 647 func nsString(nss []*NS) string { 648 var buf bytes.Buffer 649 sep := "" 650 fmt.Fprintf(&buf, "[") 651 for _, ns := range nss { 652 fmt.Fprintf(&buf, "%s%s", sep, ns.Host) 653 sep = " " 654 } 655 fmt.Fprintf(&buf, "]") 656 return buf.String() 657 } 658 659 func srvString(srvs []*SRV) string { 660 var buf bytes.Buffer 661 sep := "" 662 fmt.Fprintf(&buf, "[") 663 for _, srv := range srvs { 664 fmt.Fprintf(&buf, "%s%s:%d:%d:%d", sep, srv.Target, srv.Port, srv.Priority, srv.Weight) 665 sep = " " 666 } 667 fmt.Fprintf(&buf, "]") 668 return buf.String() 669 } 670 671 func TestLookupPort(t *testing.T) { 672 // See http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml 673 // 674 // Please be careful about adding new test cases. 675 // There are platforms having incomplete mappings for 676 // restricted resource access and security reasons. 677 type test struct { 678 network string 679 name string 680 port int 681 ok bool 682 } 683 var tests = []test{ 684 {"tcp", "0", 0, true}, 685 {"udp", "0", 0, true}, 686 {"udp", "domain", 53, true}, 687 688 {"--badnet--", "zzz", 0, false}, 689 {"tcp", "--badport--", 0, false}, 690 {"tcp", "-1", 0, false}, 691 {"tcp", "65536", 0, false}, 692 {"udp", "-1", 0, false}, 693 {"udp", "65536", 0, false}, 694 {"tcp", "123456789", 0, false}, 695 696 // Issue 13610: LookupPort("tcp", "") 697 {"tcp", "", 0, true}, 698 {"tcp4", "", 0, true}, 699 {"tcp6", "", 0, true}, 700 {"udp", "", 0, true}, 701 {"udp4", "", 0, true}, 702 {"udp6", "", 0, true}, 703 } 704 705 switch runtime.GOOS { 706 case "android": 707 if netGo { 708 t.Skipf("not supported on %s without cgo; see golang.org/issues/14576", runtime.GOOS) 709 } 710 default: 711 tests = append(tests, test{"tcp", "http", 80, true}) 712 } 713 714 for _, tt := range tests { 715 port, err := LookupPort(tt.network, tt.name) 716 if port != tt.port || (err == nil) != tt.ok { 717 t.Errorf("LookupPort(%q, %q) = %d, %v; want %d, error=%t", tt.network, tt.name, port, err, tt.port, !tt.ok) 718 } 719 if err != nil { 720 if perr := parseLookupPortError(err); perr != nil { 721 t.Error(perr) 722 } 723 } 724 } 725 } 726 727 // Like TestLookupPort but with minimal tests that should always pass 728 // because the answers are baked-in to the net package. 729 func TestLookupPort_Minimal(t *testing.T) { 730 type test struct { 731 network string 732 name string 733 port int 734 } 735 var tests = []test{ 736 {"tcp", "http", 80}, 737 {"tcp", "HTTP", 80}, // case shouldn't matter 738 {"tcp", "https", 443}, 739 {"tcp", "ssh", 22}, 740 {"tcp", "gopher", 70}, 741 {"tcp4", "http", 80}, 742 {"tcp6", "http", 80}, 743 } 744 745 for _, tt := range tests { 746 port, err := LookupPort(tt.network, tt.name) 747 if port != tt.port || err != nil { 748 t.Errorf("LookupPort(%q, %q) = %d, %v; want %d, error=nil", tt.network, tt.name, port, err, tt.port) 749 } 750 } 751 } 752 753 func TestLookupProtocol_Minimal(t *testing.T) { 754 type test struct { 755 name string 756 want int 757 } 758 var tests = []test{ 759 {"tcp", 6}, 760 {"TcP", 6}, // case shouldn't matter 761 {"icmp", 1}, 762 {"igmp", 2}, 763 {"udp", 17}, 764 {"ipv6-icmp", 58}, 765 } 766 767 for _, tt := range tests { 768 got, err := lookupProtocol(context.Background(), tt.name) 769 if got != tt.want || err != nil { 770 t.Errorf("LookupProtocol(%q) = %d, %v; want %d, error=nil", tt.name, got, err, tt.want) 771 } 772 } 773 774 } 775 776 func TestLookupNonLDH(t *testing.T) { 777 if runtime.GOOS == "nacl" { 778 t.Skip("skip on nacl") 779 } 780 781 defer dnsWaitGroup.Wait() 782 783 if fixup := forceGoDNS(); fixup != nil { 784 defer fixup() 785 } 786 787 // "LDH" stands for letters, digits, and hyphens and is the usual 788 // description of standard DNS names. 789 // This test is checking that other kinds of names are reported 790 // as not found, not reported as invalid names. 791 addrs, err := LookupHost("!!!.###.bogus..domain.") 792 if err == nil { 793 t.Fatalf("lookup succeeded: %v", addrs) 794 } 795 if !strings.HasSuffix(err.Error(), errNoSuchHost.Error()) { 796 t.Fatalf("lookup error = %v, want %v", err, errNoSuchHost) 797 } 798 } 799 800 func TestLookupContextCancel(t *testing.T) { 801 if testenv.Builder() == "" { 802 testenv.MustHaveExternalNetwork(t) 803 } 804 if runtime.GOOS == "nacl" { 805 t.Skip("skip on nacl") 806 } 807 808 defer dnsWaitGroup.Wait() 809 810 ctx, ctxCancel := context.WithCancel(context.Background()) 811 ctxCancel() 812 _, err := DefaultResolver.LookupIPAddr(ctx, "google.com") 813 if err != errCanceled { 814 testenv.SkipFlakyNet(t) 815 t.Fatal(err) 816 } 817 ctx = context.Background() 818 _, err = DefaultResolver.LookupIPAddr(ctx, "google.com") 819 if err != nil { 820 testenv.SkipFlakyNet(t) 821 t.Fatal(err) 822 } 823 }