Imported Upstream version 4.7.2
[platform/upstream/gcc48.git] / libgo / go / net / dialgoogle_test.go
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         "flag"
9         "fmt"
10         "io"
11         "strings"
12         "syscall"
13         "testing"
14 )
15
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")
18
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)
24
25         buf := make([]byte, 1000)
26         n, err = io.ReadFull(fd, buf)
27
28         if n < 1000 {
29                 t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err)
30                 return
31         }
32 }
33
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
39         }
40         fetchGoogle(t, fd, network, addr)
41         fd.Close()
42 }
43
44 func TestLookupCNAME(t *testing.T) {
45         if testing.Short() || !*testExternal {
46                 t.Logf("skipping test to avoid external network")
47                 return
48         }
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)
52         }
53 }
54
55 var googleaddrsipv4 = []string{
56         "%d.%d.%d.%d:80",
57         "www.google.com:80",
58         "%d.%d.%d.%d:http",
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",
66 }
67
68 func TestDialGoogleIPv4(t *testing.T) {
69         if testing.Short() || !*testExternal {
70                 t.Logf("skipping test to avoid external network")
71                 return
72         }
73
74         // Insert an actual IPv4 address for google.com
75         // into the table.
76         addrs, err := LookupIP("www.google.com")
77         if err != nil {
78                 t.Fatalf("lookup www.google.com: %v", err)
79         }
80         var ip IP
81         for _, addr := range addrs {
82                 if x := addr.To4(); x != nil {
83                         ip = x
84                         break
85                 }
86         }
87         if ip == nil {
88                 t.Fatalf("no IPv4 addresses for www.google.com")
89         }
90
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])
94                 }
95         }
96
97         for i := 0; i < len(googleaddrsipv4); i++ {
98                 addr := googleaddrsipv4[i]
99                 if addr == "" {
100                         continue
101                 }
102                 t.Logf("-- %s --", addr)
103                 doDial(t, "tcp", addr)
104                 if addr[0] != '[' {
105                         doDial(t, "tcp4", addr)
106                         if supportsIPv6 {
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
112                         }
113                 }
114         }
115 }
116
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",
122 }
123
124 func TestDialGoogleIPv6(t *testing.T) {
125         if testing.Short() || !*testExternal {
126                 t.Logf("skipping test to avoid external network")
127                 return
128         }
129         // Only run tcp6 if the kernel will take it.
130         if !*testIPv6 || !supportsIPv6 {
131                 return
132         }
133
134         // Insert an actual IPv6 address for ipv6.google.com
135         // into the table.
136         addrs, err := LookupIP("ipv6.google.com")
137         if err != nil {
138                 t.Fatalf("lookup ipv6.google.com: %v", err)
139         }
140         var ip IP
141         for _, addr := range addrs {
142                 if x := addr.To16(); x != nil {
143                         ip = x
144                         break
145                 }
146         }
147         if ip == nil {
148                 t.Fatalf("no IPv6 addresses for ipv6.google.com")
149         }
150
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])
154                 }
155         }
156
157         for i := 0; i < len(googleaddrsipv6); i++ {
158                 addr := googleaddrsipv6[i]
159                 if addr == "" {
160                         continue
161                 }
162                 t.Logf("-- %s --", addr)
163                 doDial(t, "tcp", addr)
164                 doDial(t, "tcp6", addr)
165         }
166 }