github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/net/external_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  	"fmt"
     9  	"io"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestResolveGoogle(t *testing.T) {
    15  	if testing.Short() || !*testExternal {
    16  		t.Skip("avoid external network")
    17  	}
    18  	if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 {
    19  		t.Skip("both IPv4 and IPv6 are required")
    20  	}
    21  
    22  	for _, network := range []string{"tcp", "tcp4", "tcp6"} {
    23  		addr, err := ResolveTCPAddr(network, "www.google.com:http")
    24  		if err != nil {
    25  			t.Error(err)
    26  			continue
    27  		}
    28  		switch {
    29  		case network == "tcp" && addr.IP.To4() == nil:
    30  			fallthrough
    31  		case network == "tcp4" && addr.IP.To4() == nil:
    32  			t.Errorf("got %v; want an IPv4 address on %s", addr, network)
    33  		case network == "tcp6" && (addr.IP.To16() == nil || addr.IP.To4() != nil):
    34  			t.Errorf("got %v; want an IPv6 address on %s", addr, network)
    35  		}
    36  	}
    37  }
    38  
    39  var dialGoogleTests = []struct {
    40  	dial               func(string, string) (Conn, error)
    41  	unreachableNetwork string
    42  	networks           []string
    43  	addrs              []string
    44  }{
    45  	{
    46  		dial:     (&Dialer{DualStack: true}).Dial,
    47  		networks: []string{"tcp", "tcp4", "tcp6"},
    48  		addrs:    []string{"www.google.com:http"},
    49  	},
    50  	{
    51  		dial:               Dial,
    52  		unreachableNetwork: "tcp6",
    53  		networks:           []string{"tcp", "tcp4"},
    54  	},
    55  	{
    56  		dial:               Dial,
    57  		unreachableNetwork: "tcp4",
    58  		networks:           []string{"tcp", "tcp6"},
    59  	},
    60  }
    61  
    62  func TestDialGoogle(t *testing.T) {
    63  	if testing.Short() || !*testExternal {
    64  		t.Skip("avoid external network")
    65  	}
    66  	if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 {
    67  		t.Skip("both IPv4 and IPv6 are required")
    68  	}
    69  
    70  	var err error
    71  	dialGoogleTests[1].addrs, dialGoogleTests[2].addrs, err = googleLiteralAddrs()
    72  	if err != nil {
    73  		t.Error(err)
    74  	}
    75  	for _, tt := range dialGoogleTests {
    76  		for _, network := range tt.networks {
    77  			disableSocketConnect(tt.unreachableNetwork)
    78  			for _, addr := range tt.addrs {
    79  				if err := fetchGoogle(tt.dial, network, addr); err != nil {
    80  					t.Error(err)
    81  				}
    82  			}
    83  			enableSocketConnect()
    84  		}
    85  	}
    86  }
    87  
    88  var (
    89  	literalAddrs4 = [...]string{
    90  		"%d.%d.%d.%d:80",
    91  		"www.google.com:80",
    92  		"%d.%d.%d.%d:http",
    93  		"www.google.com:http",
    94  		"%03d.%03d.%03d.%03d:0080",
    95  		"[::ffff:%d.%d.%d.%d]:80",
    96  		"[::ffff:%02x%02x:%02x%02x]:80",
    97  		"[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
    98  		"[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
    99  		"[0:0:0:0::ffff:%d.%d.%d.%d]:80",
   100  	}
   101  	literalAddrs6 = [...]string{
   102  		"[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
   103  		"ipv6.google.com:80",
   104  		"[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
   105  		"ipv6.google.com:http",
   106  	}
   107  )
   108  
   109  func googleLiteralAddrs() (lits4, lits6 []string, err error) {
   110  	ips, err := LookupIP("www.google.com")
   111  	if err != nil {
   112  		return nil, nil, err
   113  	}
   114  	if len(ips) == 0 {
   115  		return nil, nil, nil
   116  	}
   117  	var ip4, ip6 IP
   118  	for _, ip := range ips {
   119  		if ip4 == nil && ip.To4() != nil {
   120  			ip4 = ip.To4()
   121  		}
   122  		if ip6 == nil && ip.To16() != nil && ip.To4() == nil {
   123  			ip6 = ip.To16()
   124  		}
   125  		if ip4 != nil && ip6 != nil {
   126  			break
   127  		}
   128  	}
   129  	if ip4 != nil {
   130  		for i, lit4 := range literalAddrs4 {
   131  			if strings.Contains(lit4, "%") {
   132  				literalAddrs4[i] = fmt.Sprintf(lit4, ip4[0], ip4[1], ip4[2], ip4[3])
   133  			}
   134  		}
   135  		lits4 = literalAddrs4[:]
   136  	}
   137  	if ip6 != nil {
   138  		for i, lit6 := range literalAddrs6 {
   139  			if strings.Contains(lit6, "%") {
   140  				literalAddrs6[i] = fmt.Sprintf(lit6, ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6], ip6[7], ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13], ip6[14], ip6[15])
   141  			}
   142  		}
   143  		lits6 = literalAddrs6[:]
   144  	}
   145  	return
   146  }
   147  
   148  func fetchGoogle(dial func(string, string) (Conn, error), network, address string) error {
   149  	c, err := dial(network, address)
   150  	if err != nil {
   151  		return err
   152  	}
   153  	defer c.Close()
   154  	req := []byte("GET /robots.txt HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
   155  	if _, err := c.Write(req); err != nil {
   156  		return err
   157  	}
   158  	b := make([]byte, 1000)
   159  	n, err := io.ReadFull(c, b)
   160  	if err != nil {
   161  		return err
   162  	}
   163  	if n < 1000 {
   164  		return fmt.Errorf("short read from %s:%s->%s", network, c.RemoteAddr(), c.LocalAddr())
   165  	}
   166  	return nil
   167  }