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

View as plain text