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 var googleaddrsipv4
= []string{
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
64 addrs
, err
:= LookupIP("www.google.com")
66 t
.Fatalf("lookup www.google.com: %v", err
)
69 for _
, addr
:= range addrs
{
70 if x
:= addr
.To4(); x
!= 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
]
90 t
.Logf("-- %s --", addr
)
91 doDial(t
, "tcp", addr
)
93 doDial(t
, "tcp4", addr
)
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.
118 t
.Skip("skipping test; ipv6 is not supported")
121 t
.Skip("test disabled; use -ipv6 to enable")
124 // Insert an actual IPv6 address for ipv6.google.com
126 addrs
, err
:= LookupIP("ipv6.google.com")
128 t
.Fatalf("lookup ipv6.google.com: %v", err
)
131 for _
, addr
:= range addrs
{
132 if x
:= addr
.To16(); x
!= 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
]
152 t
.Logf("-- %s --", addr
)
153 doDial(t
, "tcp", addr
)
154 doDial(t
, "tcp6", addr
)