diff options
author | Ian Lance Taylor <iant@golang.org> | 2018-09-24 21:46:21 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2018-09-24 21:46:21 +0000 |
commit | dd931d9b48647e898dc80927c532ae93cc09e192 (patch) | |
tree | 71be2295cd79b8a182f6130611658db8628772d5 /libgo/go/net/lookup_test.go | |
parent | 779d8a5ad09b01428726ea5a0e6c87bd9ac3c0e4 (diff) | |
download | gcc-dd931d9b48647e898dc80927c532ae93cc09e192.zip gcc-dd931d9b48647e898dc80927c532ae93cc09e192.tar.gz gcc-dd931d9b48647e898dc80927c532ae93cc09e192.tar.bz2 |
libgo: update to Go 1.11
Reviewed-on: https://go-review.googlesource.com/136435
gotools/:
* Makefile.am (mostlyclean-local): Run chmod on check-go-dir to
make sure it is writable.
(check-go-tools): Likewise.
(check-vet): Copy internal/objabi to check-vet-dir.
* Makefile.in: Rebuild.
From-SVN: r264546
Diffstat (limited to 'libgo/go/net/lookup_test.go')
-rw-r--r-- | libgo/go/net/lookup_test.go | 288 |
1 files changed, 240 insertions, 48 deletions
diff --git a/libgo/go/net/lookup_test.go b/libgo/go/net/lookup_test.go index 24787cc..5c66dfa 100644 --- a/libgo/go/net/lookup_test.go +++ b/libgo/go/net/lookup_test.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build !js + package net import ( @@ -13,6 +15,7 @@ import ( "runtime" "sort" "strings" + "sync" "testing" "time" ) @@ -60,19 +63,34 @@ var lookupGoogleSRVTests = []struct { }, } +var backoffDuration = [...]time.Duration{time.Second, 5 * time.Second, 30 * time.Second} + func TestLookupGoogleSRV(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) + t.Parallel() + mustHaveExternalNetwork(t) + + if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { + t.Skip("no resolv.conf on iOS") } if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") } - for _, tt := range lookupGoogleSRVTests { + attempts := 0 + for i := 0; i < len(lookupGoogleSRVTests); i++ { + tt := lookupGoogleSRVTests[i] cname, srvs, err := LookupSRV(tt.service, tt.proto, tt.name) if err != nil { testenv.SkipFlakyNet(t) + if attempts < len(backoffDuration) { + dur := backoffDuration[attempts] + t.Logf("backoff %v after failure %v\n", dur, err) + time.Sleep(dur) + attempts++ + i-- + continue + } t.Fatal(err) } if len(srvs) == 0 { @@ -97,19 +115,31 @@ var lookupGmailMXTests = []struct { } func TestLookupGmailMX(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) + t.Parallel() + mustHaveExternalNetwork(t) + + if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { + t.Skip("no resolv.conf on iOS") } if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") } - defer dnsWaitGroup.Wait() - - for _, tt := range lookupGmailMXTests { + attempts := 0 + for i := 0; i < len(lookupGmailMXTests); i++ { + tt := lookupGmailMXTests[i] mxs, err := LookupMX(tt.name) if err != nil { + testenv.SkipFlakyNet(t) + if attempts < len(backoffDuration) { + dur := backoffDuration[attempts] + t.Logf("backoff %v after failure %v\n", dur, err) + time.Sleep(dur) + attempts++ + i-- + continue + } t.Fatal(err) } if len(mxs) == 0 { @@ -131,20 +161,31 @@ var lookupGmailNSTests = []struct { } func TestLookupGmailNS(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) + t.Parallel() + mustHaveExternalNetwork(t) + + if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { + t.Skip("no resolv.conf on iOS") } if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") } - defer dnsWaitGroup.Wait() - - for _, tt := range lookupGmailNSTests { + attempts := 0 + for i := 0; i < len(lookupGmailNSTests); i++ { + tt := lookupGmailNSTests[i] nss, err := LookupNS(tt.name) if err != nil { testenv.SkipFlakyNet(t) + if attempts < len(backoffDuration) { + dur := backoffDuration[attempts] + t.Logf("backoff %v after failure %v\n", dur, err) + time.Sleep(dur) + attempts++ + i-- + continue + } t.Fatal(err) } if len(nss) == 0 { @@ -166,19 +207,31 @@ var lookupGmailTXTTests = []struct { } func TestLookupGmailTXT(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) + t.Parallel() + mustHaveExternalNetwork(t) + + if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { + t.Skip("no resolv.conf on iOS") } if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") } - defer dnsWaitGroup.Wait() - - for _, tt := range lookupGmailTXTTests { + attempts := 0 + for i := 0; i < len(lookupGmailTXTTests); i++ { + tt := lookupGmailTXTTests[i] txts, err := LookupTXT(tt.name) if err != nil { + testenv.SkipFlakyNet(t) + if attempts < len(backoffDuration) { + dur := backoffDuration[attempts] + t.Logf("backoff %v after failure %v\n", dur, err) + time.Sleep(dur) + attempts++ + i-- + continue + } t.Fatal(err) } if len(txts) == 0 { @@ -203,9 +256,7 @@ var lookupGooglePublicDNSAddrTests = []struct { } func TestLookupGooglePublicDNSAddr(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) if !supportsIPv4() || !supportsIPv6() || !*testIPv4 || !*testIPv6 { t.Skip("both IPv4 and IPv6 are required") @@ -255,6 +306,32 @@ func TestLookupIPv6LinkLocalAddr(t *testing.T) { } } +func TestLookupIPv6LinkLocalAddrWithZone(t *testing.T) { + if !supportsIPv6() || !*testIPv6 { + t.Skip("IPv6 is required") + } + + ipaddrs, err := DefaultResolver.LookupIPAddr(context.Background(), "fe80::1%lo0") + if err != nil { + t.Error(err) + } + for _, addr := range ipaddrs { + if e, a := "lo0", addr.Zone; e != a { + t.Errorf("wrong zone: want %q, got %q", e, a) + } + } + + addrs, err := DefaultResolver.LookupHost(context.Background(), "fe80::1%lo0") + if err != nil { + t.Error(err) + } + for _, addr := range addrs { + if e, a := "fe80::1%lo0", addr; e != a { + t.Errorf("wrong host: want %q got %q", e, a) + } + } +} + var lookupCNAMETests = []struct { name, cname string }{ @@ -264,9 +341,7 @@ var lookupCNAMETests = []struct { } func TestLookupCNAME(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") @@ -274,9 +349,20 @@ func TestLookupCNAME(t *testing.T) { defer dnsWaitGroup.Wait() - for _, tt := range lookupCNAMETests { + attempts := 0 + for i := 0; i < len(lookupCNAMETests); i++ { + tt := lookupCNAMETests[i] cname, err := LookupCNAME(tt.name) if err != nil { + testenv.SkipFlakyNet(t) + if attempts < len(backoffDuration) { + dur := backoffDuration[attempts] + t.Logf("backoff %v after failure %v\n", dur, err) + time.Sleep(dur) + attempts++ + i-- + continue + } t.Fatal(err) } if !strings.HasSuffix(cname, tt.cname) { @@ -293,9 +379,7 @@ var lookupGoogleHostTests = []struct { } func TestLookupGoogleHost(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") @@ -320,12 +404,8 @@ func TestLookupGoogleHost(t *testing.T) { } func TestLookupLongTXT(t *testing.T) { - if runtime.GOOS == "plan9" { - t.Skip("skipping on plan9; see https://golang.org/issue/22857") - } - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + testenv.SkipFlaky(t, 22857) + mustHaveExternalNetwork(t) defer dnsWaitGroup.Wait() @@ -351,9 +431,7 @@ var lookupGoogleIPTests = []struct { } func TestLookupGoogleIP(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") @@ -499,9 +577,7 @@ func TestLookupDotsWithLocalSource(t *testing.T) { t.Skip("IPv4 is required") } - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) defer dnsWaitGroup.Wait() @@ -542,14 +618,16 @@ func TestLookupDotsWithLocalSource(t *testing.T) { } func TestLookupDotsWithRemoteSource(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) if !supportsIPv4() || !*testIPv4 { t.Skip("IPv4 is required") } + if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { + t.Skip("no resolv.conf on iOS") + } + defer dnsWaitGroup.Wait() if fixup := forceGoDNS(); fixup != nil { @@ -664,10 +742,10 @@ func srvString(srvs []*SRV) string { } func TestLookupPort(t *testing.T) { - // See http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml + // See https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml // // Please be careful about adding new test cases. - // There are platforms having incomplete mappings for + // There are platforms which have incomplete mappings for // restricted resource access and security reasons. type test struct { network string @@ -793,9 +871,7 @@ func TestLookupNonLDH(t *testing.T) { } func TestLookupContextCancel(t *testing.T) { - if testenv.Builder() == "" { - testenv.MustHaveExternalNetwork(t) - } + mustHaveExternalNetwork(t) if runtime.GOOS == "nacl" { t.Skip("skip on nacl") } @@ -816,3 +892,119 @@ func TestLookupContextCancel(t *testing.T) { t.Fatal(err) } } + +// Issue 24330: treat the nil *Resolver like a zero value. Verify nothing +// crashes if nil is used. +func TestNilResolverLookup(t *testing.T) { + mustHaveExternalNetwork(t) + if runtime.GOOS == "nacl" { + t.Skip("skip on nacl") + } + var r *Resolver = nil + ctx := context.Background() + + // Don't care about the results, just that nothing panics: + r.LookupAddr(ctx, "8.8.8.8") + r.LookupCNAME(ctx, "google.com") + r.LookupHost(ctx, "google.com") + r.LookupIPAddr(ctx, "google.com") + r.LookupMX(ctx, "gmail.com") + r.LookupNS(ctx, "google.com") + r.LookupPort(ctx, "tcp", "smtp") + r.LookupSRV(ctx, "service", "proto", "name") + r.LookupTXT(ctx, "gmail.com") +} + +// TestLookupHostCancel verifies that lookup works even after many +// canceled lookups (see golang.org/issue/24178 for details). +func TestLookupHostCancel(t *testing.T) { + mustHaveExternalNetwork(t) + if runtime.GOOS == "nacl" { + t.Skip("skip on nacl") + } + + const ( + google = "www.google.com" + invalidDomain = "nonexistentdomain.golang.org" + n = 600 // this needs to be larger than threadLimit size + ) + + _, err := LookupHost(google) + if err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + for i := 0; i < n; i++ { + addr, err := DefaultResolver.LookupHost(ctx, invalidDomain) + if err == nil { + t.Fatalf("LookupHost(%q): returns %v, but should fail", invalidDomain, addr) + } + if !strings.Contains(err.Error(), "canceled") { + t.Fatalf("LookupHost(%q): failed with unexpected error: %v", invalidDomain, err) + } + time.Sleep(time.Millisecond * 1) + } + + _, err = LookupHost(google) + if err != nil { + t.Fatal(err) + } +} + +type lookupCustomResolver struct { + *Resolver + mu sync.RWMutex + dialed bool +} + +func (lcr *lookupCustomResolver) dial() func(ctx context.Context, network, address string) (Conn, error) { + return func(ctx context.Context, network, address string) (Conn, error) { + lcr.mu.Lock() + lcr.dialed = true + lcr.mu.Unlock() + return Dial(network, address) + } +} + +// TestConcurrentPreferGoResolversDial tests that multiple resolvers with the +// PreferGo option used concurrently are all dialed properly. +func TestConcurrentPreferGoResolversDial(t *testing.T) { + // The windows implementation of the resolver does not use the Dial + // function. + if runtime.GOOS == "windows" { + t.Skip("skip on windows") + } + + testenv.MustHaveExternalNetwork(t) + testenv.SkipFlakyNet(t) + + defer dnsWaitGroup.Wait() + + resolvers := make([]*lookupCustomResolver, 2) + for i := range resolvers { + cs := lookupCustomResolver{Resolver: &Resolver{PreferGo: true}} + cs.Dial = cs.dial() + resolvers[i] = &cs + } + + var wg sync.WaitGroup + wg.Add(len(resolvers)) + for i, resolver := range resolvers { + go func(r *Resolver, index int) { + defer wg.Done() + _, err := r.LookupIPAddr(context.Background(), "google.com") + if err != nil { + t.Fatalf("lookup failed for resolver %d: %q", index, err) + } + }(resolver.Resolver, i) + } + wg.Wait() + + for i, resolver := range resolvers { + if !resolver.dialed { + t.Errorf("custom resolver %d not dialed during lookup", i) + } + } +} |