Merged revisions 195034,195219,195245,195357,195374,195428,195599,195673,195809 via...
[official-gcc.git] / main / libgo / go / net / dialgoogle_test.go
blob73a94f5bf1c1fe0d961fcd2aa7b9c67b4bd65836
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.
5 package net
7 import (
8 "flag"
9 "fmt"
10 "io"
11 "strings"
12 "syscall"
13 "testing"
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)
28 if n < 1000 {
29 t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err)
30 return
34 func doDial(t *testing.T, network, addr string) {
35 fd, err := Dial(network, addr)
36 if err != nil {
37 t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err)
38 return
40 fetchGoogle(t, fd, network, addr)
41 fd.Close()
44 var googleaddrsipv4 = []string{
45 "%d.%d.%d.%d:80",
46 "www.google.com:80",
47 "%d.%d.%d.%d:http",
48 "www.google.com:http",
49 "%03d.%03d.%03d.%03d:0080",
50 "[::ffff:%d.%d.%d.%d]:80",
51 "[::ffff:%02x%02x:%02x%02x]:80",
52 "[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
53 "[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
54 "[0:0:0:0:0:ffff::%d.%d.%d.%d]:80",
57 func TestDialGoogleIPv4(t *testing.T) {
58 if testing.Short() || !*testExternal {
59 t.Skip("skipping test to avoid external network")
62 // Insert an actual IPv4 address for google.com
63 // into the table.
64 addrs, err := LookupIP("www.google.com")
65 if err != nil {
66 t.Fatalf("lookup www.google.com: %v", err)
68 var ip IP
69 for _, addr := range addrs {
70 if x := addr.To4(); x != nil {
71 ip = x
72 break
75 if ip == nil {
76 t.Fatalf("no IPv4 addresses for www.google.com")
79 for i, s := range googleaddrsipv4 {
80 if strings.Contains(s, "%") {
81 googleaddrsipv4[i] = fmt.Sprintf(s, ip[0], ip[1], ip[2], ip[3])
85 for i := 0; i < len(googleaddrsipv4); i++ {
86 addr := googleaddrsipv4[i]
87 if addr == "" {
88 continue
90 t.Logf("-- %s --", addr)
91 doDial(t, "tcp", addr)
92 if addr[0] != '[' {
93 doDial(t, "tcp4", addr)
94 if supportsIPv6 {
95 // make sure syscall.SocketDisableIPv6 flag works.
96 syscall.SocketDisableIPv6 = true
97 doDial(t, "tcp", addr)
98 doDial(t, "tcp4", addr)
99 syscall.SocketDisableIPv6 = false
105 var googleaddrsipv6 = []string{
106 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
107 "ipv6.google.com:80",
108 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
109 "ipv6.google.com:http",
112 func TestDialGoogleIPv6(t *testing.T) {
113 if testing.Short() || !*testExternal {
114 t.Skip("skipping test to avoid external network")
116 // Only run tcp6 if the kernel will take it.
117 if !supportsIPv6 {
118 t.Skip("skipping test; ipv6 is not supported")
120 if !*testIPv6 {
121 t.Skip("test disabled; use -ipv6 to enable")
124 // Insert an actual IPv6 address for ipv6.google.com
125 // into the table.
126 addrs, err := LookupIP("ipv6.google.com")
127 if err != nil {
128 t.Fatalf("lookup ipv6.google.com: %v", err)
130 var ip IP
131 for _, addr := range addrs {
132 if x := addr.To16(); x != nil {
133 ip = x
134 break
137 if ip == nil {
138 t.Fatalf("no IPv6 addresses for ipv6.google.com")
141 for i, s := range googleaddrsipv6 {
142 if strings.Contains(s, "%") {
143 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])
147 for i := 0; i < len(googleaddrsipv6); i++ {
148 addr := googleaddrsipv6[i]
149 if addr == "" {
150 continue
152 t.Logf("-- %s --", addr)
153 doDial(t, "tcp", addr)
154 doDial(t, "tcp6", addr)