1
0
mirror of https://github.com/golang/go synced 2024-11-12 05:50:21 -07:00
go/test/dialgoogle.go
Russ Cox 9350ef4eea add network listening & tests
R=r,presotto
OCL=15410
CL=15440
2008-09-17 13:49:23 -07:00

97 lines
2.0 KiB
Go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// $G $F.go && $L $F.$A && ./$A.out
package main
import (
"net";
"flag";
"os";
"syscall"
)
// If an IPv6 tunnel is running (see go/stubl), we can try dialing a real IPv6 address.
var ipv6 = false
var ipv6_flag = flag.Bool("ipv6", false, &ipv6, "assume ipv6 tunnel is present")
func StringToBuf(s string) *[]byte
{
l := len(s);
b := new([]byte, l);
for i := 0; i < l; i++ {
b[i] = s[i];
}
return b;
}
// fd is already connected to www.google.com port 80.
// Run an HTTP request to fetch the main page.
func FetchGoogle(fd net.Conn) {
req := StringToBuf("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
n, errno := fd.Write(req);
buf := new([1000]byte);
n, errno = fd.Read(buf);
fd.Close();
if n < 1000 {
panic("short http read");
}
}
func TestDial(network, addr string) {
fd, err := net.Dial(network, "", addr)
if err != nil {
panic("net.Dial ", network, " ", addr, ": ", err.String())
}
FetchGoogle(fd)
}
func TestDialTCP(network, addr string) {
fd, err := net.DialTCP(network, "", addr)
if err != nil {
panic("net.DialTCP ", network, " ", addr, ": ", err.String())
}
FetchGoogle(fd)
}
var addrs = []string {
"74.125.19.99:80",
"074.125.019.099:0080",
"[::ffff:74.125.19.99]:80",
"[::ffff:4a7d:1363]:80",
"[0:0:0:0:0000:ffff:74.125.19.99]:80",
"[0:0:0:0:000000:ffff:74.125.19.99]:80",
"[0:0:0:0:0:ffff::74.125.19.99]:80",
"[2001:4860:0:2001::68]:80" // ipv6.google.com; removed if ipv6 flag not set
}
func main()
{
flag.Parse()
// If no ipv6 tunnel, don't try the last address.
if !ipv6 {
addrs[len(addrs)-1] = ""
}
for i := 0; i < len(addrs); i++ {
addr := addrs[i]
if addr == "" {
continue
}
// print(addr, "\n");
TestDial("tcp", addr);
TestDialTCP("tcp", addr)
if addr[0] != '[' {
TestDial("tcp4", addr);
TestDialTCP("tcp4", addr)
}
TestDial("tcp6", addr);
TestDialTCP("tcp6", addr)
}
}