Home | History | Annotate | Download | only in 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 
     48 func TestResolveIPAddr(t *testing.T) {
     49 	if !testableNetwork("ip+nopriv") {
     50 		t.Skip("ip+nopriv test")
     51 	}
     52 
     53 	origTestHookLookupIP := testHookLookupIP
     54 	defer func() { testHookLookupIP = origTestHookLookupIP }()
     55 	testHookLookupIP = lookupLocalhost
     56 
     57 	for i, tt := range resolveIPAddrTests {
     58 		addr, err := ResolveIPAddr(tt.network, tt.litAddrOrName)
     59 		if err != tt.err {
     60 			t.Errorf("#%d: %v", i, err)
     61 		} else if !reflect.DeepEqual(addr, tt.addr) {
     62 			t.Errorf("#%d: got %#v; want %#v", i, addr, tt.addr)
     63 		}
     64 		if err != nil {
     65 			continue
     66 		}
     67 		rtaddr, err := ResolveIPAddr(addr.Network(), addr.String())
     68 		if err != nil {
     69 			t.Errorf("#%d: %v", i, err)
     70 		} else if !reflect.DeepEqual(rtaddr, addr) {
     71 			t.Errorf("#%d: got %#v; want %#v", i, rtaddr, addr)
     72 		}
     73 	}
     74 }
     75 
     76 var ipConnLocalNameTests = []struct {
     77 	net   string
     78 	laddr *IPAddr
     79 }{
     80 	{"ip4:icmp", &IPAddr{IP: IPv4(127, 0, 0, 1)}},
     81 	{"ip4:icmp", &IPAddr{}},
     82 	{"ip4:icmp", nil},
     83 }
     84 
     85 func TestIPConnLocalName(t *testing.T) {
     86 	for _, tt := range ipConnLocalNameTests {
     87 		if !testableNetwork(tt.net) {
     88 			t.Logf("skipping %s test", tt.net)
     89 			continue
     90 		}
     91 		c, err := ListenIP(tt.net, tt.laddr)
     92 		if err != nil {
     93 			t.Fatal(err)
     94 		}
     95 		defer c.Close()
     96 		if la := c.LocalAddr(); la == nil {
     97 			t.Fatal("should not fail")
     98 		}
     99 	}
    100 }
    101 
    102 func TestIPConnRemoteName(t *testing.T) {
    103 	if !testableNetwork("ip:tcp") {
    104 		t.Skip("ip:tcp test")
    105 	}
    106 
    107 	raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
    108 	c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
    109 	if err != nil {
    110 		t.Fatal(err)
    111 	}
    112 	defer c.Close()
    113 	if !reflect.DeepEqual(raddr, c.RemoteAddr()) {
    114 		t.Fatalf("got %#v; want %#v", c.RemoteAddr(), raddr)
    115 	}
    116 }
    117