...

Source file src/net/iprawsock_test.go

Documentation: net

		 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  //go:build !js
		 6  // +build !js
		 7  
		 8  package net
		 9  
		10  import (
		11  	"reflect"
		12  	"testing"
		13  )
		14  
		15  // The full stack test cases for IPConn have been moved to the
		16  // following:
		17  //	golang.org/x/net/ipv4
		18  //	golang.org/x/net/ipv6
		19  //	golang.org/x/net/icmp
		20  
		21  type resolveIPAddrTest struct {
		22  	network			 string
		23  	litAddrOrName string
		24  	addr					*IPAddr
		25  	err					 error
		26  }
		27  
		28  var resolveIPAddrTests = []resolveIPAddrTest{
		29  	{"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
		30  	{"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
		31  	{"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
		32  
		33  	{"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
		34  	{"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
		35  	{"ip6:ipv6-icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
		36  	{"ip6:IPv6-ICMP", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
		37  
		38  	{"ip", "::1%en0", &IPAddr{IP: ParseIP("::1"), Zone: "en0"}, nil},
		39  	{"ip6", "::1%911", &IPAddr{IP: ParseIP("::1"), Zone: "911"}, nil},
		40  
		41  	{"", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, // Go 1.0 behavior
		42  	{"", "::1", &IPAddr{IP: ParseIP("::1")}, nil},					 // Go 1.0 behavior
		43  
		44  	{"ip4:icmp", "", &IPAddr{}, nil},
		45  
		46  	{"l2tp", "127.0.0.1", nil, UnknownNetworkError("l2tp")},
		47  	{"l2tp:gre", "127.0.0.1", nil, UnknownNetworkError("l2tp:gre")},
		48  	{"tcp", "1.2.3.4:123", nil, UnknownNetworkError("tcp")},
		49  
		50  	{"ip4", "2001:db8::1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "2001:db8::1"}},
		51  	{"ip4:icmp", "2001:db8::1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "2001:db8::1"}},
		52  	{"ip6", "127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "127.0.0.1"}},
		53  	{"ip6", "::ffff:127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "::ffff:127.0.0.1"}},
		54  	{"ip6:ipv6-icmp", "127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "127.0.0.1"}},
		55  	{"ip6:ipv6-icmp", "::ffff:127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "::ffff:127.0.0.1"}},
		56  }
		57  
		58  func TestResolveIPAddr(t *testing.T) {
		59  	if !testableNetwork("ip+nopriv") {
		60  		t.Skip("ip+nopriv test")
		61  	}
		62  
		63  	origTestHookLookupIP := testHookLookupIP
		64  	defer func() { testHookLookupIP = origTestHookLookupIP }()
		65  	testHookLookupIP = lookupLocalhost
		66  
		67  	for _, tt := range resolveIPAddrTests {
		68  		addr, err := ResolveIPAddr(tt.network, tt.litAddrOrName)
		69  		if !reflect.DeepEqual(addr, tt.addr) || !reflect.DeepEqual(err, tt.err) {
		70  			t.Errorf("ResolveIPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr, err, tt.addr, tt.err)
		71  			continue
		72  		}
		73  		if err == nil {
		74  			addr2, err := ResolveIPAddr(addr.Network(), addr.String())
		75  			if !reflect.DeepEqual(addr2, tt.addr) || err != tt.err {
		76  				t.Errorf("(%q, %q): ResolveIPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr.Network(), addr.String(), addr2, err, tt.addr, tt.err)
		77  			}
		78  		}
		79  	}
		80  }
		81  
		82  var ipConnLocalNameTests = []struct {
		83  	net	 string
		84  	laddr *IPAddr
		85  }{
		86  	{"ip4:icmp", &IPAddr{IP: IPv4(127, 0, 0, 1)}},
		87  	{"ip4:icmp", &IPAddr{}},
		88  	{"ip4:icmp", nil},
		89  }
		90  
		91  func TestIPConnLocalName(t *testing.T) {
		92  	for _, tt := range ipConnLocalNameTests {
		93  		if !testableNetwork(tt.net) {
		94  			t.Logf("skipping %s test", tt.net)
		95  			continue
		96  		}
		97  		c, err := ListenIP(tt.net, tt.laddr)
		98  		if err != nil {
		99  			t.Fatal(err)
	 100  		}
	 101  		defer c.Close()
	 102  		if la := c.LocalAddr(); la == nil {
	 103  			t.Fatal("should not fail")
	 104  		}
	 105  	}
	 106  }
	 107  
	 108  func TestIPConnRemoteName(t *testing.T) {
	 109  	if !testableNetwork("ip:tcp") {
	 110  		t.Skip("ip:tcp test")
	 111  	}
	 112  
	 113  	raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
	 114  	c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
	 115  	if err != nil {
	 116  		t.Fatal(err)
	 117  	}
	 118  	defer c.Close()
	 119  	if !reflect.DeepEqual(raddr, c.RemoteAddr()) {
	 120  		t.Fatalf("got %#v; want %#v", c.RemoteAddr(), raddr)
	 121  	}
	 122  }
	 123  
	 124  func TestDialListenIPArgs(t *testing.T) {
	 125  	type test struct {
	 126  		argLists	 [][2]string
	 127  		shouldFail bool
	 128  	}
	 129  	tests := []test{
	 130  		{
	 131  			argLists: [][2]string{
	 132  				{"ip", "127.0.0.1"},
	 133  				{"ip:", "127.0.0.1"},
	 134  				{"ip::", "127.0.0.1"},
	 135  				{"ip", "::1"},
	 136  				{"ip:", "::1"},
	 137  				{"ip::", "::1"},
	 138  				{"ip4", "127.0.0.1"},
	 139  				{"ip4:", "127.0.0.1"},
	 140  				{"ip4::", "127.0.0.1"},
	 141  				{"ip6", "::1"},
	 142  				{"ip6:", "::1"},
	 143  				{"ip6::", "::1"},
	 144  			},
	 145  			shouldFail: true,
	 146  		},
	 147  	}
	 148  	if testableNetwork("ip") {
	 149  		priv := test{shouldFail: false}
	 150  		for _, tt := range []struct {
	 151  			network, address string
	 152  			args						 [2]string
	 153  		}{
	 154  			{"ip4:47", "127.0.0.1", [2]string{"ip4:47", "127.0.0.1"}},
	 155  			{"ip6:47", "::1", [2]string{"ip6:47", "::1"}},
	 156  		} {
	 157  			c, err := ListenPacket(tt.network, tt.address)
	 158  			if err != nil {
	 159  				continue
	 160  			}
	 161  			c.Close()
	 162  			priv.argLists = append(priv.argLists, tt.args)
	 163  		}
	 164  		if len(priv.argLists) > 0 {
	 165  			tests = append(tests, priv)
	 166  		}
	 167  	}
	 168  
	 169  	for _, tt := range tests {
	 170  		for _, args := range tt.argLists {
	 171  			_, err := Dial(args[0], args[1])
	 172  			if tt.shouldFail != (err != nil) {
	 173  				t.Errorf("Dial(%q, %q) = %v; want (err != nil) is %t", args[0], args[1], err, tt.shouldFail)
	 174  			}
	 175  			_, err = ListenPacket(args[0], args[1])
	 176  			if tt.shouldFail != (err != nil) {
	 177  				t.Errorf("ListenPacket(%q, %q) = %v; want (err != nil) is %t", args[0], args[1], err, tt.shouldFail)
	 178  			}
	 179  			a, err := ResolveIPAddr("ip", args[1])
	 180  			if err != nil {
	 181  				t.Errorf("ResolveIPAddr(\"ip\", %q) = %v", args[1], err)
	 182  				continue
	 183  			}
	 184  			_, err = DialIP(args[0], nil, a)
	 185  			if tt.shouldFail != (err != nil) {
	 186  				t.Errorf("DialIP(%q, %v) = %v; want (err != nil) is %t", args[0], a, err, tt.shouldFail)
	 187  			}
	 188  			_, err = ListenIP(args[0], a)
	 189  			if tt.shouldFail != (err != nil) {
	 190  				t.Errorf("ListenIP(%q, %v) = %v; want (err != nil) is %t", args[0], a, err, tt.shouldFail)
	 191  			}
	 192  		}
	 193  	}
	 194  }
	 195  

View as plain text