github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/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:0:ffff::%d.%d.%d.%d]:80",
   108  }
   109  
   110  func TestDNSThreadLimit(t *testing.T) {
   111  	if testing.Short() || !*testExternal {
   112  		t.Skip("skipping test to avoid external network")
   113  	}
   114  
   115  	const N = 10000
   116  	c := make(chan int, N)
   117  	for i := 0; i < N; i++ {
   118  		go func(i int) {
   119  			LookupIP(fmt.Sprintf("%d.net-test.golang.org", i))
   120  			c <- 1
   121  		}(i)
   122  	}
   123  	// Don't bother waiting for the stragglers; stop at 0.9 N.
   124  	for i := 0; i < N*9/10; i++ {
   125  		if i%100 == 0 {
   126  			//println("TestDNSThreadLimit:", i)
   127  		}
   128  		<-c
   129  	}
   130  
   131  	// If we're still here, it worked.
   132  }
   133  
   134  func TestDialGoogleIPv4(t *testing.T) {
   135  	if testing.Short() || !*testExternal {
   136  		t.Skip("skipping test to avoid external network")
   137  	}
   138  
   139  	// Insert an actual IPv4 address for google.com
   140  	// into the table.
   141  	addrs, err := LookupIP("www.google.com")
   142  	if err != nil {
   143  		t.Fatalf("lookup www.google.com: %v", err)
   144  	}
   145  	var ip IP
   146  	for _, addr := range addrs {
   147  		if x := addr.To4(); x != nil {
   148  			ip = x
   149  			break
   150  		}
   151  	}
   152  	if ip == nil {
   153  		t.Fatalf("no IPv4 addresses for www.google.com")
   154  	}
   155  
   156  	for i, s := range googleaddrsipv4 {
   157  		if strings.Contains(s, "%") {
   158  			googleaddrsipv4[i] = fmt.Sprintf(s, ip[0], ip[1], ip[2], ip[3])
   159  		}
   160  	}
   161  
   162  	for i := 0; i < len(googleaddrsipv4); i++ {
   163  		addr := googleaddrsipv4[i]
   164  		if addr == "" {
   165  			continue
   166  		}
   167  		t.Logf("-- %s --", addr)
   168  		doDial(t, "tcp", addr)
   169  		if addr[0] != '[' {
   170  			doDial(t, "tcp4", addr)
   171  			if supportsIPv6 {
   172  				// make sure syscall.SocketDisableIPv6 flag works.
   173  				syscall.SocketDisableIPv6 = true
   174  				doDial(t, "tcp", addr)
   175  				doDial(t, "tcp4", addr)
   176  				syscall.SocketDisableIPv6 = false
   177  			}
   178  		}
   179  	}
   180  }
   181  
   182  var googleaddrsipv6 = []string{
   183  	"[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
   184  	"ipv6.google.com:80",
   185  	"[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
   186  	"ipv6.google.com:http",
   187  }
   188  
   189  func TestDialGoogleIPv6(t *testing.T) {
   190  	if testing.Short() || !*testExternal {
   191  		t.Skip("skipping test to avoid external network")
   192  	}
   193  	// Only run tcp6 if the kernel will take it.
   194  	if !supportsIPv6 {
   195  		t.Skip("skipping test; ipv6 is not supported")
   196  	}
   197  	if !*testIPv6 {
   198  		t.Skip("test disabled; use -ipv6 to enable")
   199  	}
   200  
   201  	// Insert an actual IPv6 address for ipv6.google.com
   202  	// into the table.
   203  	addrs, err := LookupIP("ipv6.google.com")
   204  	if err != nil {
   205  		t.Fatalf("lookup ipv6.google.com: %v", err)
   206  	}
   207  	var ip IP
   208  	for _, addr := range addrs {
   209  		if x := addr.To16(); x != nil {
   210  			ip = x
   211  			break
   212  		}
   213  	}
   214  	if ip == nil {
   215  		t.Fatalf("no IPv6 addresses for ipv6.google.com")
   216  	}
   217  
   218  	for i, s := range googleaddrsipv6 {
   219  		if strings.Contains(s, "%") {
   220  			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])
   221  		}
   222  	}
   223  
   224  	for i := 0; i < len(googleaddrsipv6); i++ {
   225  		addr := googleaddrsipv6[i]
   226  		if addr == "" {
   227  			continue
   228  		}
   229  		t.Logf("-- %s --", addr)
   230  		doDial(t, "tcp", addr)
   231  		doDial(t, "tcp6", addr)
   232  	}
   233  }