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.
16 // If an IPv6 tunnel is running, we can try dialing a real IPv6 address.
17 var testIPv6 = flag.Bool("ipv6", false, "assume ipv6 tunnel is present")
19 // fd is already connected to the destination, port 80.
20 // Run an HTTP request to fetch the appropriate page.
21 func fetchGoogle(t *testing.T, fd Conn, network, addr string) {
22 req := []byte("GET /robots.txt HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
23 n, err := fd.Write(req)
25 buf := make([]byte, 1000)
26 n, err = io.ReadFull(fd, buf)
29 t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err)
34 func doDial(t *testing.T, network, addr string) {
35 fd, err := Dial(network, addr)
37 t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err)
40 fetchGoogle(t, fd, network, addr)
44 func TestLookupCNAME(t *testing.T) {
45 if testing.Short() || !*testExternal {
46 t.Logf("skipping test to avoid external network")
49 cname, err := LookupCNAME("www.google.com")
50 if !strings.HasSuffix(cname, ".l.google.com.") || err != nil {
51 t.Errorf(`LookupCNAME("www.google.com.") = %q, %v, want "*.l.google.com.", nil`, cname, err)
55 var googleaddrsipv4 = []string{
59 "www.google.com:http",
60 "%03d.%03d.%03d.%03d:0080",
61 "[::ffff:%d.%d.%d.%d]:80",
62 "[::ffff:%02x%02x:%02x%02x]:80",
63 "[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
64 "[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
65 "[0:0:0:0:0:ffff::%d.%d.%d.%d]:80",
68 func TestDialGoogleIPv4(t *testing.T) {
69 if testing.Short() || !*testExternal {
70 t.Logf("skipping test to avoid external network")
74 // Insert an actual IPv4 address for google.com
76 addrs, err := LookupIP("www.google.com")
78 t.Fatalf("lookup www.google.com: %v", err)
81 for _, addr := range addrs {
82 if x := addr.To4(); x != nil {
88 t.Fatalf("no IPv4 addresses for www.google.com")
91 for i, s := range googleaddrsipv4 {
92 if strings.Contains(s, "%") {
93 googleaddrsipv4[i] = fmt.Sprintf(s, ip[0], ip[1], ip[2], ip[3])
97 for i := 0; i < len(googleaddrsipv4); i++ {
98 addr := googleaddrsipv4[i]
102 t.Logf("-- %s --", addr)
103 doDial(t, "tcp", addr)
105 doDial(t, "tcp4", addr)
107 // make sure syscall.SocketDisableIPv6 flag works.
108 syscall.SocketDisableIPv6 = true
109 doDial(t, "tcp", addr)
110 doDial(t, "tcp4", addr)
111 syscall.SocketDisableIPv6 = false
117 var googleaddrsipv6 = []string{
118 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
119 "ipv6.google.com:80",
120 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
121 "ipv6.google.com:http",
124 func TestDialGoogleIPv6(t *testing.T) {
125 if testing.Short() || !*testExternal {
126 t.Logf("skipping test to avoid external network")
129 // Only run tcp6 if the kernel will take it.
130 if !*testIPv6 || !supportsIPv6 {
134 // Insert an actual IPv6 address for ipv6.google.com
136 addrs, err := LookupIP("ipv6.google.com")
138 t.Fatalf("lookup ipv6.google.com: %v", err)
141 for _, addr := range addrs {
142 if x := addr.To16(); x != nil {
148 t.Fatalf("no IPv6 addresses for ipv6.google.com")
151 for i, s := range googleaddrsipv6 {
152 if strings.Contains(s, "%") {
153 googleaddrsipv6[i] = fmt.Sprintf(s, ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15])
157 for i := 0; i < len(googleaddrsipv6); i++ {
158 addr := googleaddrsipv6[i]
162 t.Logf("-- %s --", addr)
163 doDial(t, "tcp", addr)
164 doDial(t, "tcp6", addr)