Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 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 ( |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 8 | "flag"; |
| 9 | "io"; |
Russ Cox | d3a412a | 2009-06-29 15:24:23 -0700 | [diff] [blame] | 10 | "strings"; |
Russ Cox | 0d33992 | 2009-05-07 10:31:48 -0700 | [diff] [blame] | 11 | "syscall"; |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 12 | "testing"; |
| 13 | ) |
| 14 | |
| 15 | // If an IPv6 tunnel is running (see go/stubl), we can try dialing a real IPv6 address. |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 16 | var ipv6 = flag.Bool("ipv6", false, "assume ipv6 tunnel is present") |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 17 | |
Rob Pike | 55d13cf | 2009-02-15 19:58:00 -0800 | [diff] [blame] | 18 | // fd is already connected to the destination, port 80. |
| 19 | // Run an HTTP request to fetch the appropriate page. |
Russ Cox | b04ac10 | 2009-08-12 13:19:17 -0700 | [diff] [blame] | 20 | func fetchGoogle(t *testing.T, fd Conn, network, addr string) { |
Russ Cox | d3a412a | 2009-06-29 15:24:23 -0700 | [diff] [blame] | 21 | req := strings.Bytes("GET /intl/en/privacy.html HTTP/1.0\r\nHost: www.google.com\r\n\r\n"); |
Russ Cox | c93da7c7 | 2009-03-05 15:48:12 -0800 | [diff] [blame] | 22 | n, err := fd.Write(req); |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 23 | |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 24 | buf := make([]byte, 1000); |
Russ Cox | 64684cc | 2009-06-22 13:26:13 -0700 | [diff] [blame] | 25 | n, err = io.ReadFull(fd, buf); |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 26 | |
| 27 | if n < 1000 { |
Russ Cox | c93da7c7 | 2009-03-05 15:48:12 -0800 | [diff] [blame] | 28 | t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err); |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 29 | return; |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Rob Pike | 1a91b9a | 2009-01-16 11:36:44 -0800 | [diff] [blame] | 33 | func doDial(t *testing.T, network, addr string) { |
Russ Cox | b04ac10 | 2009-08-12 13:19:17 -0700 | [diff] [blame] | 34 | fd, err := Dial(network, "", addr); |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 35 | if err != nil { |
Russ Cox | b04ac10 | 2009-08-12 13:19:17 -0700 | [diff] [blame] | 36 | t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err); |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 37 | return; |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 38 | } |
Rob Pike | 1a91b9a | 2009-01-16 11:36:44 -0800 | [diff] [blame] | 39 | fetchGoogle(t, fd, network, addr); |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 40 | fd.Close(); |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 43 | var googleaddrs = []string{ |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 44 | "74.125.19.99:80", |
| 45 | "www.google.com:80", |
| 46 | "74.125.19.99:http", |
| 47 | "www.google.com:http", |
| 48 | "074.125.019.099:0080", |
| 49 | "[::ffff:74.125.19.99]:80", |
| 50 | "[::ffff:4a7d:1363]:80", |
| 51 | "[0:0:0:0:0000:ffff:74.125.19.99]:80", |
| 52 | "[0:0:0:0:000000:ffff:74.125.19.99]:80", |
| 53 | "[0:0:0:0:0:ffff::74.125.19.99]:80", |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 54 | "[2001:4860:0:2001::68]:80" // ipv6.google.com; removed if ipv6 flag not set |
| 55 | , |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 56 | } |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 57 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 58 | func TestDialGoogle(t *testing.T) { |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 59 | // If no ipv6 tunnel, don't try the last address. |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 60 | if !*ipv6 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 61 | googleaddrs[len(googleaddrs)-1] = "" |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | for i := 0; i < len(googleaddrs); i++ { |
| 65 | addr := googleaddrs[i]; |
| 66 | if addr == "" { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 67 | continue |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 68 | } |
| 69 | t.Logf("-- %s --", addr); |
Rob Pike | 1a91b9a | 2009-01-16 11:36:44 -0800 | [diff] [blame] | 70 | doDial(t, "tcp", addr); |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 71 | if addr[0] != '[' { |
Rob Pike | 1a91b9a | 2009-01-16 11:36:44 -0800 | [diff] [blame] | 72 | doDial(t, "tcp4", addr); |
Russ Cox | 0d33992 | 2009-05-07 10:31:48 -0700 | [diff] [blame] | 73 | |
| 74 | if !preferIPv4 { |
| 75 | // make sure preferIPv4 flag works. |
| 76 | preferIPv4 = true; |
| 77 | syscall.SocketDisableIPv6 = true; |
| 78 | doDial(t, "tcp4", addr); |
Russ Cox | 0d33992 | 2009-05-07 10:31:48 -0700 | [diff] [blame] | 79 | syscall.SocketDisableIPv6 = false; |
| 80 | preferIPv4 = false; |
| 81 | } |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 82 | } |
Russ Cox | b4a8b35 | 2009-11-10 17:47:38 -0800 | [diff] [blame] | 83 | |
| 84 | // Only run tcp6 if the kernel will take it. |
| 85 | if kernelSupportsIPv6() { |
| 86 | doDial(t, "tcp6", addr) |
| 87 | } |
Russ Cox | 83348f9 | 2008-12-18 15:42:39 -0800 | [diff] [blame] | 88 | } |
| 89 | } |