github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/net/dialgoogle_test.go (about)

     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  func TestResolveGoogle(t *testing.T) {
    20  	if testing.Short() || !*testExternal {
    21  		t.Skip("skipping test to avoid external network")
    22  	}
    23  
    24  	for _, network := range []string{"tcp", "tcp4", "tcp6"} {
    25  		addr, err := ResolveTCPAddr(network, "www.google.com:http")
    26  		if err != nil {
    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)
    31  			} else {
    32  				t.Errorf("ResolveTCPAddr failed: %v", err)
    33  			}
    34  			continue
    35  		}
    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)
    40  		}
    41  	}
    42  }
    43  
    44  func TestDialGoogle(t *testing.T) {
    45  	if testing.Short() || !*testExternal {
    46  		t.Skip("skipping test to avoid external network")
    47  	}
    48  
    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")
    53  			continue
    54  		} else if network == "tcp4" && !supportsIPv4 {
    55  			t.Logf("skipping test; ipv4 is not supported")
    56  			continue
    57  		} else if network == "tcp6" && !supportsIPv6 {
    58  			t.Logf("skipping test; ipv6 is not supported")
    59  			continue
    60  		} else if network == "tcp6" && !*testIPv6 {
    61  			t.Logf("test disabled; use -ipv6 to enable")
    62  			continue
    63  		}
    64  		if c, err := d.Dial(network, "www.google.com:http"); err != nil {
    65  			t.Errorf("Dial failed: %v", err)
    66  		} else {
    67  			c.Close()
    68  		}
    69  	}
    70  }
    71  
    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)
    77  
    78  	buf := make([]byte, 1000)
    79  	n, err = io.ReadFull(fd, buf)
    80  
    81  	if n < 1000 {
    82  		t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err)
    83  		return
    84  	}
    85  }
    86  
    87  func doDial(t *testing.T, network, addr string) {
    88  	fd, err := Dial(network, addr)
    89  	if err != nil {
    90  		t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err)
    91  		return
    92  	}
    93  	fetchGoogle(t, fd, network, addr)
    94  	fd.Close()
    95  }
    96  
    97  var googleaddrsipv4 = []string{
    98  	"%d.%d.%d.%d:80",
    99  	"www.google.com:80",
   100  	"%d.%d.%d.%d:http",
   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",
   108  }
   109  
   110  func TestDialGoogleIPv4(t *testing.T) {
   111  	if testing.Short() || !*testExternal {
   112  		t.Skip("skipping test to avoid external network")
   113  	}
   114  
   115  	// Insert an actual IPv4 address for google.com
   116  	// into the table.
   117  	addrs, err := LookupIP("www.google.com")
   118  	if err != nil {
   119  		t.Fatalf("lookup www.google.com: %v", err)
   120  	}
   121  	var ip IP
   122  	for _, addr := range addrs {
   123  		if x := addr.To4(); x != nil {
   124  			ip = x
   125  			break
   126  		}
   127  	}
   128  	if ip == nil {
   129  		t.Fatalf("no IPv4 addresses for www.google.com")
   130  	}
   131  
   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])
   135  		}
   136  	}
   137  
   138  	for i := 0; i < len(googleaddrsipv4); i++ {
   139  		addr := googleaddrsipv4[i]
   140  		if addr == "" {
   141  			continue
   142  		}
   143  		t.Logf("-- %s --", addr)
   144  		doDial(t, "tcp", addr)
   145  		if addr[0] != '[' {
   146  			doDial(t, "tcp4", addr)
   147  			if supportsIPv6 {
   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
   153  			}
   154  		}
   155  	}
   156  }
   157  
   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",
   163  }
   164  
   165  func TestDialGoogleIPv6(t *testing.T) {
   166  	if testing.Short() || !*testExternal {
   167  		t.Skip("skipping test to avoid external network")
   168  	}
   169  	// Only run tcp6 if the kernel will take it.
   170  	if !supportsIPv6 {
   171  		t.Skip("skipping test; ipv6 is not supported")
   172  	}
   173  	if !*testIPv6 {
   174  		t.Skip("test disabled; use -ipv6 to enable")
   175  	}
   176  
   177  	// Insert an actual IPv6 address for ipv6.google.com
   178  	// into the table.
   179  	addrs, err := LookupIP("ipv6.google.com")
   180  	if err != nil {
   181  		t.Fatalf("lookup ipv6.google.com: %v", err)
   182  	}
   183  	var ip IP
   184  	for _, addr := range addrs {
   185  		if x := addr.To16(); x != nil {
   186  			ip = x
   187  			break
   188  		}
   189  	}
   190  	if ip == nil {
   191  		t.Fatalf("no IPv6 addresses for ipv6.google.com")
   192  	}
   193  
   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])
   197  		}
   198  	}
   199  
   200  	for i := 0; i < len(googleaddrsipv6); i++ {
   201  		addr := googleaddrsipv6[i]
   202  		if addr == "" {
   203  			continue
   204  		}
   205  		t.Logf("-- %s --", addr)
   206  		doDial(t, "tcp", addr)
   207  		doDial(t, "tcp6", addr)
   208  	}
   209  }