...
Run Format

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

View as plain text