]> Cypherpunks repositories - gostls13.git/commitdiff
net: fix the series of TestLookup and external tests
authorMikio Hara <mikioh.mikioh@gmail.com>
Wed, 13 May 2015 03:44:45 +0000 (12:44 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Wed, 20 May 2015 07:54:37 +0000 (07:54 +0000)
On Windows, we need to make sure that the node under test has external
connectivity.

Fixes #10795.

Change-Id: I99f2336180c7b56474fa90a4a6cdd5a6c4dd3805
Reviewed-on: https://go-review.googlesource.com/10006
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/net/external_test.go
src/net/lookup_test.go
src/net/main_test.go

index 20611ff420e71bd1df3ca635fa5322bfdb2fba0e..d5ff2be20a3cbc050d83aedeb769eae44186c1e4 100644 (file)
@@ -15,33 +15,23 @@ func TestResolveGoogle(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-       if !supportsIPv4 && !supportsIPv6 {
-               t.Skip("ipv4 and ipv6 are not supported")
+       if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 {
+               t.Skip("both IPv4 and IPv6 are required")
        }
 
        for _, network := range []string{"tcp", "tcp4", "tcp6"} {
                addr, err := ResolveTCPAddr(network, "www.google.com:http")
                if err != nil {
-                       switch {
-                       case network == "tcp" && !supportsIPv4:
-                               fallthrough
-                       case network == "tcp4" && !supportsIPv4:
-                               t.Logf("skipping test; ipv4 is not supported: %v", err)
-                       case network == "tcp6" && !supportsIPv6:
-                               t.Logf("skipping test; ipv6 is not supported: %v", err)
-                       default:
-                               t.Error(err)
-                       }
+                       t.Error(err)
                        continue
                }
-
                switch {
                case network == "tcp" && addr.IP.To4() == nil:
                        fallthrough
                case network == "tcp4" && addr.IP.To4() == nil:
-                       t.Errorf("got %v; want an ipv4 address on %s", addr, network)
+                       t.Errorf("got %v; want an IPv4 address on %s", addr, network)
                case network == "tcp6" && (addr.IP.To16() == nil || addr.IP.To4() != nil):
-                       t.Errorf("got %v; want an ipv6 address on %s", addr, network)
+                       t.Errorf("got %v; want an IPv6 address on %s", addr, network)
                }
        }
 }
@@ -73,8 +63,8 @@ func TestDialGoogle(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-       if !supportsIPv4 && !supportsIPv6 {
-               t.Skip("ipv4 and ipv6 are not supported")
+       if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 {
+               t.Skip("both IPv4 and IPv6 are required")
        }
 
        var err error
@@ -84,25 +74,6 @@ func TestDialGoogle(t *testing.T) {
        }
        for _, tt := range dialGoogleTests {
                for _, network := range tt.networks {
-                       switch {
-                       case network == "tcp4" && !supportsIPv4:
-                               t.Log("skipping test; ipv4 is not supported")
-                               continue
-                       case network == "tcp4" && !*testIPv4:
-                               fallthrough
-                       case tt.unreachableNetwork == "tcp6" && !*testIPv4:
-                               t.Log("disabled; use -ipv4 to enable")
-                               continue
-                       case network == "tcp6" && !supportsIPv6:
-                               t.Log("skipping test; ipv6 is not supported")
-                               continue
-                       case network == "tcp6" && !*testIPv6:
-                               fallthrough
-                       case tt.unreachableNetwork == "tcp4" && !*testIPv6:
-                               t.Log("disabled; use -ipv6 to enable")
-                               continue
-                       }
-
                        disableSocketConnect(tt.unreachableNetwork)
                        for _, addr := range tt.addrs {
                                if err := fetchGoogle(tt.dial, network, addr); err != nil {
index 1f36184d55a88b779c4e5ab8c6cec45553d3d7a9..064bc0b9f16c76e0572d576c39778e20b487612c 100644 (file)
@@ -23,17 +23,34 @@ func lookupLocalhost(fn func(string) ([]IPAddr, error), host string) ([]IPAddr,
        }
 }
 
+// The Lookup APIs use various sources such as local database, DNS or
+// mDNS, and may use platform-dependent DNS stub resolver if possible.
+// The APIs accept any of forms for a query; host name in various
+// encodings, UTF-8 encoded net name, domain name, FQDN or absolute
+// FQDN, but the result would be one of the forms and it depends on
+// the circumstances.
+
 var lookupGoogleSRVTests = []struct {
        service, proto, name string
        cname, target        string
 }{
        {
                "xmpp-server", "tcp", "google.com",
-               ".google.com", ".google.com",
+               "google.com", "google.com",
+       },
+       {
+               "xmpp-server", "tcp", "google.com.",
+               "google.com", "google.com",
+       },
+
+       // non-standard back door
+       {
+               "", "", "_xmpp-server._tcp.google.com",
+               "google.com", "google.com",
        },
        {
-               "", "", "_xmpp-server._tcp.google.com", // non-standard back door
-               ".google.com", ".google.com",
+               "", "", "_xmpp-server._tcp.google.com.",
+               "google.com", "google.com",
        },
 }
 
@@ -41,6 +58,9 @@ func TestLookupGoogleSRV(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
+       }
 
        for _, tt := range lookupGoogleSRVTests {
                cname, srvs, err := LookupSRV(tt.service, tt.proto, tt.name)
@@ -50,88 +70,126 @@ func TestLookupGoogleSRV(t *testing.T) {
                if len(srvs) == 0 {
                        t.Error("got no record")
                }
-               if !strings.Contains(cname, tt.cname) {
-                       t.Errorf("got %q; want %q", cname, tt.cname)
+               if !strings.HasSuffix(cname, tt.cname) && !strings.HasSuffix(cname, tt.cname+".") {
+                       t.Errorf("got %s; want %s", cname, tt.cname)
                }
                for _, srv := range srvs {
-                       if !strings.Contains(srv.Target, tt.target) {
-                               t.Errorf("got %v; want a record containing %q", srv, tt.target)
+                       if !strings.HasSuffix(srv.Target, tt.target) && !strings.HasSuffix(srv.Target, tt.target+".") {
+                               t.Errorf("got %v; want a record containing %s", srv, tt.target)
                        }
                }
        }
 }
 
+var lookupGmailMXTests = []struct {
+       name, host string
+}{
+       {"gmail.com", "google.com"},
+       {"gmail.com.", "google.com"},
+}
+
 func TestLookupGmailMX(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-
-       mxs, err := LookupMX("gmail.com")
-       if err != nil {
-               t.Fatal(err)
-       }
-       if len(mxs) == 0 {
-               t.Error("got no record")
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
        }
-       for _, mx := range mxs {
-               if !strings.Contains(mx.Host, ".google.com") {
-                       t.Errorf("got %v; want a record containing .google.com.", mx)
+
+       for _, tt := range lookupGmailMXTests {
+               mxs, err := LookupMX(tt.name)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if len(mxs) == 0 {
+                       t.Error("got no record")
+               }
+               for _, mx := range mxs {
+                       if !strings.HasSuffix(mx.Host, tt.host) && !strings.HasSuffix(mx.Host, tt.host+".") {
+                               t.Errorf("got %v; want a record containing %s", mx, tt.host)
+                       }
                }
        }
 }
 
+var lookupGmailNSTests = []struct {
+       name, host string
+}{
+       {"gmail.com", "google.com"},
+       {"gmail.com.", "google.com"},
+}
+
 func TestLookupGmailNS(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-
-       nss, err := LookupNS("gmail.com")
-       if err != nil {
-               t.Fatal(err)
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
        }
-       if len(nss) == 0 {
-               t.Error("got no record")
-       }
-       for _, ns := range nss {
-               if !strings.Contains(ns.Host, ".google.com") {
-                       t.Errorf("got %v; want a record containing .google.com.", ns)
+
+       for _, tt := range lookupGmailNSTests {
+               nss, err := LookupNS(tt.name)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if len(nss) == 0 {
+                       t.Error("got no record")
+               }
+               for _, ns := range nss {
+                       if !strings.HasSuffix(ns.Host, tt.host) && !strings.HasSuffix(ns.Host, tt.host+".") {
+                               t.Errorf("got %v; want a record containing %s", ns, tt.host)
+                       }
                }
        }
 }
 
+var lookupGmailTXTTests = []struct {
+       name, txt, host string
+}{
+       {"gmail.com", "spf", "google.com"},
+       {"gmail.com.", "spf", "google.com"},
+}
+
 func TestLookupGmailTXT(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-
-       txts, err := LookupTXT("gmail.com")
-       if err != nil {
-               t.Fatal(err)
-       }
-       if len(txts) == 0 {
-               t.Error("got no record")
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
        }
-       for _, txt := range txts {
-               if !strings.Contains(txt, "spf") {
-                       t.Errorf("got %q; want a spf record", txt)
+
+       for _, tt := range lookupGmailTXTTests {
+               txts, err := LookupTXT(tt.name)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if len(txts) == 0 {
+                       t.Error("got no record")
+               }
+               for _, txt := range txts {
+                       if !strings.Contains(txt, tt.txt) || (!strings.HasSuffix(txt, tt.host) && !strings.HasSuffix(txt, tt.host+".")) {
+                               t.Errorf("got %s; want a record containing %s, %s", txt, tt.txt, tt.host)
+                       }
                }
        }
 }
 
 var lookupGooglePublicDNSAddrs = []struct {
-       addr string
-       name string
+       addr, name string
 }{
-       {"8.8.8.8", ".google.com."},
-       {"8.8.4.4", ".google.com."},
-       {"2001:4860:4860::8888", ".google.com."},
-       {"2001:4860:4860::8844", ".google.com."},
+       {"8.8.8.8", ".google.com"},
+       {"8.8.4.4", ".google.com"},
+       {"2001:4860:4860::8888", ".google.com"},
+       {"2001:4860:4860::8844", ".google.com"},
 }
 
 func TestLookupGooglePublicDNSAddr(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
+       if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 {
+               t.Skip("both IPv4 and IPv6 are required")
+       }
 
        for _, tt := range lookupGooglePublicDNSAddrs {
                names, err := LookupAddr(tt.addr)
@@ -142,61 +200,97 @@ func TestLookupGooglePublicDNSAddr(t *testing.T) {
                        t.Error("got no record")
                }
                for _, name := range names {
-                       if !strings.HasSuffix(name, tt.name) {
-                               t.Errorf("got %q; want a record containing %q", name, tt.name)
+                       if !strings.HasSuffix(name, tt.name) && !strings.HasSuffix(name, tt.name+".") {
+                               t.Errorf("got %s; want a record containing %s", name, tt.name)
                        }
                }
        }
 }
 
+var lookupIANACNAMETests = []struct {
+       name, cname string
+}{
+       {"www.iana.org", "icann.org"},
+       {"www.iana.org.", "icann.org"},
+}
+
 func TestLookupIANACNAME(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-
-       cname, err := LookupCNAME("www.iana.org")
-       if err != nil {
-               t.Fatal(err)
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
        }
-       if !strings.HasSuffix(cname, ".icann.org.") {
-               t.Errorf("got %q; want a record containing .icann.org.", cname)
+
+       for _, tt := range lookupIANACNAMETests {
+               cname, err := LookupCNAME(tt.name)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if !strings.HasSuffix(cname, tt.cname) && !strings.HasSuffix(cname, tt.cname+".") {
+                       t.Errorf("got %s; want a record containing %s", cname, tt.cname)
+               }
        }
 }
 
+var lookupGoogleHostTests = []struct {
+       name string
+}{
+       {"google.com"},
+       {"google.com."},
+}
+
 func TestLookupGoogleHost(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-
-       addrs, err := LookupHost("google.com")
-       if err != nil {
-               t.Fatal(err)
-       }
-       if len(addrs) == 0 {
-               t.Error("got no record")
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
        }
-       for _, addr := range addrs {
-               if ParseIP(addr) == nil {
-                       t.Errorf("got %q; want a literal ip address", addr)
+
+       for _, tt := range lookupGoogleHostTests {
+               addrs, err := LookupHost(tt.name)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if len(addrs) == 0 {
+                       t.Error("got no record")
+               }
+               for _, addr := range addrs {
+                       if ParseIP(addr) == nil {
+                               t.Errorf("got %q; want a literal IP address", addr)
+                       }
                }
        }
 }
 
+var lookupGoogleIPTests = []struct {
+       name string
+}{
+       {"google.com"},
+       {"google.com."},
+}
+
 func TestLookupGoogleIP(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Skip("avoid external network")
        }
-
-       ips, err := LookupIP("google.com")
-       if err != nil {
-               t.Fatal(err)
-       }
-       if len(ips) == 0 {
-               t.Error("got no record")
+       if !supportsIPv4 || !*testIPv4 {
+               t.Skip("IPv4 is required")
        }
-       for _, ip := range ips {
-               if ip.To4() == nil && ip.To16() == nil {
-                       t.Errorf("got %v; want an ip address", ip)
+
+       for _, tt := range lookupGoogleIPTests {
+               ips, err := LookupIP(tt.name)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if len(ips) == 0 {
+                       t.Error("got no record")
+               }
+               for _, ip := range ips {
+                       if ip.To4() == nil && ip.To16() == nil {
+                               t.Errorf("got %v; want an IP address", ip)
+                       }
                }
        }
 }
index 5e2f3da0e6a4c0cdc5c3befda6997fd7407a7fb0..ceec08911ed8b481217542da8e714676e3eec496 100644 (file)
@@ -30,10 +30,16 @@ var (
 
        // If external IPv4 connectivity exists, we can try dialing
        // non-node/interface local scope IPv4 addresses.
+       // On Windows, Lookup APIs may not return IPv4-related
+       // resource records when a node has no external IPv4
+       // connectivity.
        testIPv4 = flag.Bool("ipv4", true, "assume external IPv4 connectivity exists")
 
        // If external IPv6 connectivity exists, we can try dialing
        // non-node/interface local scope IPv6 addresses.
+       // On Windows, Lookup APIs may not return IPv6-related
+       // resource records when a node has no external IPv6
+       // connectivity.
        testIPv6 = flag.Bool("ipv6", false, "assume external IPv6 connectivity exists")
 )