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 func TestResolveGoogle(t
*testing
.T
) {
20 if testing
.Short() ||
!*testExternal
{
21 t
.Skip("skipping test to avoid external network")
24 for _
, network
:= range []string{"tcp", "tcp4", "tcp6"} {
25 addr
, err
:= ResolveTCPAddr(network
, "www.google.com:http")
27 if (network
== "tcp" || network
== "tcp4") && !supportsIPv4
{
28 t
.Logf("ipv4 is not supported: %v", err
)
29 } else if network
== "tcp6" && !supportsIPv6
{
30 t
.Logf("ipv6 is not supported: %v", err
)
32 t
.Errorf("ResolveTCPAddr failed: %v", err
)
36 if (network
== "tcp" || network
== "tcp4") && addr
.IP
.To4() == nil {
37 t
.Errorf("got %v; expected an IPv4 address on %v", addr
, network
)
38 } else if network
== "tcp6" && (addr
.IP
.To16() == nil || addr
.IP
.To4() != nil) {
39 t
.Errorf("got %v; expected an IPv6 address on %v", addr
, network
)
44 func TestDialGoogle(t
*testing
.T
) {
45 if testing
.Short() ||
!*testExternal
{
46 t
.Skip("skipping test to avoid external network")
49 d
:= &Dialer
{DualStack
: true}
50 for _
, network
:= range []string{"tcp", "tcp4", "tcp6"} {
51 if network
== "tcp" && !supportsIPv4
&& !supportsIPv6
{
52 t
.Logf("skipping test; both ipv4 and ipv6 are not supported")
54 } else if network
== "tcp4" && !supportsIPv4
{
55 t
.Logf("skipping test; ipv4 is not supported")
57 } else if network
== "tcp6" && !supportsIPv6
{
58 t
.Logf("skipping test; ipv6 is not supported")
60 } else if network
== "tcp6" && !*testIPv6
{
61 t
.Logf("test disabled; use -ipv6 to enable")
64 if c
, err
:= d
.Dial(network
, "www.google.com:http"); err
!= nil {
65 t
.Errorf("Dial failed: %v", err
)
72 // fd is already connected to the destination, port 80.
73 // Run an HTTP request to fetch the appropriate page.
74 func fetchGoogle(t
*testing
.T
, fd Conn
, network
, addr
string) {
75 req
:= []byte("GET /robots.txt HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
76 n
, err
:= fd
.Write(req
)
78 buf
:= make([]byte, 1000)
79 n
, err
= io
.ReadFull(fd
, buf
)
82 t
.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network
, addr
, err
)
87 func doDial(t
*testing
.T
, network
, addr
string) {
88 fd
, err
:= Dial(network
, addr
)
90 t
.Errorf("Dial(%q, %q, %q) = _, %v", network
, "", addr
, err
)
93 fetchGoogle(t
, fd
, network
, addr
)
97 var googleaddrsipv4
= []string{
101 "www.google.com:http",
102 "%03d.%03d.%03d.%03d:0080",
103 "[::ffff:%d.%d.%d.%d]:80",
104 "[::ffff:%02x%02x:%02x%02x]:80",
105 "[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
106 "[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
107 "[0:0:0:0::ffff:%d.%d.%d.%d]:80",
110 func TestDialGoogleIPv4(t
*testing
.T
) {
111 if testing
.Short() ||
!*testExternal
{
112 t
.Skip("skipping test to avoid external network")
115 // Insert an actual IPv4 address for google.com
117 addrs
, err
:= LookupIP("www.google.com")
119 t
.Fatalf("lookup www.google.com: %v", err
)
122 for _
, addr
:= range addrs
{
123 if x
:= addr
.To4(); x
!= nil {
129 t
.Fatalf("no IPv4 addresses for www.google.com")
132 for i
, s
:= range googleaddrsipv4
{
133 if strings
.Contains(s
, "%") {
134 googleaddrsipv4
[i
] = fmt
.Sprintf(s
, ip
[0], ip
[1], ip
[2], ip
[3])
138 for i
:= 0; i
< len(googleaddrsipv4
); i
++ {
139 addr
:= googleaddrsipv4
[i
]
143 t
.Logf("-- %s --", addr
)
144 doDial(t
, "tcp", addr
)
146 doDial(t
, "tcp4", addr
)
148 // make sure syscall.SocketDisableIPv6 flag works.
149 syscall
.SocketDisableIPv6
= true
150 doDial(t
, "tcp", addr
)
151 doDial(t
, "tcp4", addr
)
152 syscall
.SocketDisableIPv6
= false
158 var googleaddrsipv6
= []string{
159 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
160 "ipv6.google.com:80",
161 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
162 "ipv6.google.com:http",
165 func TestDialGoogleIPv6(t
*testing
.T
) {
166 if testing
.Short() ||
!*testExternal
{
167 t
.Skip("skipping test to avoid external network")
169 // Only run tcp6 if the kernel will take it.
171 t
.Skip("skipping test; ipv6 is not supported")
174 t
.Skip("test disabled; use -ipv6 to enable")
177 // Insert an actual IPv6 address for ipv6.google.com
179 addrs
, err
:= LookupIP("ipv6.google.com")
181 t
.Fatalf("lookup ipv6.google.com: %v", err
)
184 for _
, addr
:= range addrs
{
185 if x
:= addr
.To16(); x
!= nil {
191 t
.Fatalf("no IPv6 addresses for ipv6.google.com")
194 for i
, s
:= range googleaddrsipv6
{
195 if strings
.Contains(s
, "%") {
196 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])
200 for i
:= 0; i
< len(googleaddrsipv6
); i
++ {
201 addr
:= googleaddrsipv6
[i
]
205 t
.Logf("-- %s --", addr
)
206 doDial(t
, "tcp", addr
)
207 doDial(t
, "tcp6", addr
)