github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/udp_test.go (about)

     1  // Copyright 2012 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  	"runtime"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestResolveUDPAddr(t *testing.T) {
    16  	for _, tt := range resolveTCPAddrTests {
    17  		net := strings.Replace(tt.net, "tcp", "udp", -1)
    18  		addr, err := ResolveUDPAddr(net, tt.litAddrOrName)
    19  		if err != tt.err {
    20  			t.Fatalf("ResolveUDPAddr(%q, %q) failed: %v", net, tt.litAddrOrName, err)
    21  		}
    22  		if !reflect.DeepEqual(addr, (*UDPAddr)(tt.addr)) {
    23  			t.Fatalf("ResolveUDPAddr(%q, %q) = %#v, want %#v", net, tt.litAddrOrName, addr, tt.addr)
    24  		}
    25  		if err == nil {
    26  			str := addr.String()
    27  			addr1, err := ResolveUDPAddr(net, str)
    28  			if err != nil {
    29  				t.Fatalf("ResolveUDPAddr(%q, %q) [from %q]: %v", net, str, tt.litAddrOrName, err)
    30  			}
    31  			if !reflect.DeepEqual(addr1, addr) {
    32  				t.Fatalf("ResolveUDPAddr(%q, %q) [from %q] = %#v, want %#v", net, str, tt.litAddrOrName, addr1, addr)
    33  			}
    34  		}
    35  	}
    36  }
    37  
    38  func TestReadFromUDP(t *testing.T) {
    39  	switch runtime.GOOS {
    40  	case "nacl", "plan9":
    41  		t.Skipf("skipping test on %q, see issue 8916", runtime.GOOS)
    42  	}
    43  
    44  	ra, err := ResolveUDPAddr("udp", "127.0.0.1:7")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	la, err := ResolveUDPAddr("udp", "127.0.0.1:0")
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	c, err := ListenUDP("udp", la)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	defer c.Close()
    59  
    60  	_, err = c.WriteToUDP([]byte("a"), ra)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	err = c.SetDeadline(time.Now().Add(100 * time.Millisecond))
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	b := make([]byte, 1)
    70  	_, _, err = c.ReadFromUDP(b)
    71  	if err == nil {
    72  		t.Fatal("ReadFromUDP should fail")
    73  	} else if !isTimeout(err) {
    74  		t.Fatal(err)
    75  	}
    76  }
    77  
    78  func TestWriteToUDP(t *testing.T) {
    79  	switch runtime.GOOS {
    80  	case "plan9":
    81  		t.Skipf("skipping test on %q", runtime.GOOS)
    82  	}
    83  
    84  	l, err := ListenPacket("udp", "127.0.0.1:0")
    85  	if err != nil {
    86  		t.Fatalf("Listen failed: %v", err)
    87  	}
    88  	defer l.Close()
    89  
    90  	testWriteToConn(t, l.LocalAddr().String())
    91  	testWriteToPacketConn(t, l.LocalAddr().String())
    92  }
    93  
    94  func testWriteToConn(t *testing.T, raddr string) {
    95  	c, err := Dial("udp", raddr)
    96  	if err != nil {
    97  		t.Fatalf("Dial failed: %v", err)
    98  	}
    99  	defer c.Close()
   100  
   101  	ra, err := ResolveUDPAddr("udp", raddr)
   102  	if err != nil {
   103  		t.Fatalf("ResolveUDPAddr failed: %v", err)
   104  	}
   105  
   106  	_, err = c.(*UDPConn).WriteToUDP([]byte("Connection-oriented mode socket"), ra)
   107  	if err == nil {
   108  		t.Fatal("WriteToUDP should fail")
   109  	}
   110  	if err != nil && err.(*OpError).Err != ErrWriteToConnected {
   111  		t.Fatalf("WriteToUDP should fail as ErrWriteToConnected: %v", err)
   112  	}
   113  
   114  	_, err = c.(*UDPConn).WriteTo([]byte("Connection-oriented mode socket"), ra)
   115  	if err == nil {
   116  		t.Fatal("WriteTo should fail")
   117  	}
   118  	if err != nil && err.(*OpError).Err != ErrWriteToConnected {
   119  		t.Fatalf("WriteTo should fail as ErrWriteToConnected: %v", err)
   120  	}
   121  
   122  	_, err = c.Write([]byte("Connection-oriented mode socket"))
   123  	if err != nil {
   124  		t.Fatalf("Write failed: %v", err)
   125  	}
   126  }
   127  
   128  func testWriteToPacketConn(t *testing.T, raddr string) {
   129  	c, err := ListenPacket("udp", "127.0.0.1:0")
   130  	if err != nil {
   131  		t.Fatalf("ListenPacket failed: %v", err)
   132  	}
   133  	defer c.Close()
   134  
   135  	ra, err := ResolveUDPAddr("udp", raddr)
   136  	if err != nil {
   137  		t.Fatalf("ResolveUDPAddr failed: %v", err)
   138  	}
   139  
   140  	_, err = c.(*UDPConn).WriteToUDP([]byte("Connection-less mode socket"), ra)
   141  	if err != nil {
   142  		t.Fatalf("WriteToUDP failed: %v", err)
   143  	}
   144  
   145  	_, err = c.WriteTo([]byte("Connection-less mode socket"), ra)
   146  	if err != nil {
   147  		t.Fatalf("WriteTo failed: %v", err)
   148  	}
   149  
   150  	_, err = c.(*UDPConn).Write([]byte("Connection-less mode socket"))
   151  	if err == nil {
   152  		t.Fatal("Write should fail")
   153  	}
   154  }
   155  
   156  var udpConnLocalNameTests = []struct {
   157  	net   string
   158  	laddr *UDPAddr
   159  }{
   160  	{"udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)}},
   161  	{"udp4", &UDPAddr{}},
   162  	{"udp4", nil},
   163  }
   164  
   165  func TestUDPConnLocalName(t *testing.T) {
   166  	if testing.Short() || !*testExternal {
   167  		t.Skip("skipping test to avoid external network")
   168  	}
   169  
   170  	for _, tt := range udpConnLocalNameTests {
   171  		c, err := ListenUDP(tt.net, tt.laddr)
   172  		if err != nil {
   173  			t.Fatalf("ListenUDP failed: %v", err)
   174  		}
   175  		defer c.Close()
   176  		la := c.LocalAddr()
   177  		if a, ok := la.(*UDPAddr); !ok || a.Port == 0 {
   178  			t.Fatalf("got %v; expected a proper address with non-zero port number", la)
   179  		}
   180  	}
   181  }
   182  
   183  func TestUDPConnLocalAndRemoteNames(t *testing.T) {
   184  	for _, laddr := range []string{"", "127.0.0.1:0"} {
   185  		c1, err := ListenPacket("udp", "127.0.0.1:0")
   186  		if err != nil {
   187  			t.Fatalf("ListenUDP failed: %v", err)
   188  		}
   189  		defer c1.Close()
   190  
   191  		var la *UDPAddr
   192  		if laddr != "" {
   193  			var err error
   194  			if la, err = ResolveUDPAddr("udp", laddr); err != nil {
   195  				t.Fatalf("ResolveUDPAddr failed: %v", err)
   196  			}
   197  		}
   198  		c2, err := DialUDP("udp", la, c1.LocalAddr().(*UDPAddr))
   199  		if err != nil {
   200  			t.Fatalf("DialUDP failed: %v", err)
   201  		}
   202  		defer c2.Close()
   203  
   204  		var connAddrs = [4]struct {
   205  			got Addr
   206  			ok  bool
   207  		}{
   208  			{c1.LocalAddr(), true},
   209  			{c1.(*UDPConn).RemoteAddr(), false},
   210  			{c2.LocalAddr(), true},
   211  			{c2.RemoteAddr(), true},
   212  		}
   213  		for _, ca := range connAddrs {
   214  			if a, ok := ca.got.(*UDPAddr); ok != ca.ok || ok && a.Port == 0 {
   215  				t.Fatalf("got %v; expected a proper address with non-zero port number", ca.got)
   216  			}
   217  		}
   218  	}
   219  }
   220  
   221  func TestIPv6LinkLocalUnicastUDP(t *testing.T) {
   222  	if testing.Short() || !*testExternal {
   223  		t.Skip("skipping test to avoid external network")
   224  	}
   225  	if !supportsIPv6 {
   226  		t.Skip("ipv6 is not supported")
   227  	}
   228  	ifi := loopbackInterface()
   229  	if ifi == nil {
   230  		t.Skip("loopback interface not found")
   231  	}
   232  	laddr := ipv6LinkLocalUnicastAddr(ifi)
   233  	if laddr == "" {
   234  		t.Skip("ipv6 unicast address on loopback not found")
   235  	}
   236  
   237  	type test struct {
   238  		net, addr  string
   239  		nameLookup bool
   240  	}
   241  	var tests = []test{
   242  		{"udp", "[" + laddr + "%" + ifi.Name + "]:0", false},
   243  		{"udp6", "[" + laddr + "%" + ifi.Name + "]:0", false},
   244  	}
   245  	// The first udp test fails on DragonFly - see issue 7473.
   246  	if runtime.GOOS == "dragonfly" {
   247  		tests = tests[1:]
   248  	}
   249  	switch runtime.GOOS {
   250  	case "darwin", "dragonfly", "freebsd", "openbsd", "netbsd":
   251  		tests = append(tests, []test{
   252  			{"udp", "[localhost%" + ifi.Name + "]:0", true},
   253  			{"udp6", "[localhost%" + ifi.Name + "]:0", true},
   254  		}...)
   255  	case "linux":
   256  		tests = append(tests, []test{
   257  			{"udp", "[ip6-localhost%" + ifi.Name + "]:0", true},
   258  			{"udp6", "[ip6-localhost%" + ifi.Name + "]:0", true},
   259  		}...)
   260  	}
   261  	for _, tt := range tests {
   262  		c1, err := ListenPacket(tt.net, tt.addr)
   263  		if err != nil {
   264  			// It might return "LookupHost returned no
   265  			// suitable address" error on some platforms.
   266  			t.Logf("ListenPacket failed: %v", err)
   267  			continue
   268  		}
   269  		defer c1.Close()
   270  		if la, ok := c1.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
   271  			t.Fatalf("got %v; expected a proper address with zone identifier", la)
   272  		}
   273  
   274  		c2, err := Dial(tt.net, c1.LocalAddr().String())
   275  		if err != nil {
   276  			t.Fatalf("Dial failed: %v", err)
   277  		}
   278  		defer c2.Close()
   279  		if la, ok := c2.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
   280  			t.Fatalf("got %v; expected a proper address with zone identifier", la)
   281  		}
   282  		if ra, ok := c2.RemoteAddr().(*UDPAddr); !ok || !tt.nameLookup && ra.Zone == "" {
   283  			t.Fatalf("got %v; expected a proper address with zone identifier", ra)
   284  		}
   285  
   286  		if _, err := c2.Write([]byte("UDP OVER IPV6 LINKLOCAL TEST")); err != nil {
   287  			t.Fatalf("Conn.Write failed: %v", err)
   288  		}
   289  		b := make([]byte, 32)
   290  		if _, from, err := c1.ReadFrom(b); err != nil {
   291  			t.Fatalf("PacketConn.ReadFrom failed: %v", err)
   292  		} else {
   293  			if ra, ok := from.(*UDPAddr); !ok || !tt.nameLookup && ra.Zone == "" {
   294  				t.Fatalf("got %v; expected a proper address with zone identifier", ra)
   295  			}
   296  		}
   297  	}
   298  }