github.com/dorkamotorka/go/src@v0.0.0-20230614113921-187095f0e316/net/udpsock_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  //go:build !js && !wasip1
     6  
     7  package net
     8  
     9  import (
    10  	"errors"
    11  	"internal/testenv"
    12  	"net/netip"
    13  	"os"
    14  	"reflect"
    15  	"runtime"
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  func BenchmarkUDP6LinkLocalUnicast(b *testing.B) {
    21  	testHookUninstaller.Do(uninstallTestHooks)
    22  
    23  	if !supportsIPv6() {
    24  		b.Skip("IPv6 is not supported")
    25  	}
    26  	ifi := loopbackInterface()
    27  	if ifi == nil {
    28  		b.Skip("loopback interface not found")
    29  	}
    30  	lla := ipv6LinkLocalUnicastAddr(ifi)
    31  	if lla == "" {
    32  		b.Skip("IPv6 link-local unicast address not found")
    33  	}
    34  
    35  	c1, err := ListenPacket("udp6", JoinHostPort(lla+"%"+ifi.Name, "0"))
    36  	if err != nil {
    37  		b.Fatal(err)
    38  	}
    39  	defer c1.Close()
    40  	c2, err := ListenPacket("udp6", JoinHostPort(lla+"%"+ifi.Name, "0"))
    41  	if err != nil {
    42  		b.Fatal(err)
    43  	}
    44  	defer c2.Close()
    45  
    46  	var buf [1]byte
    47  	for i := 0; i < b.N; i++ {
    48  		if _, err := c1.WriteTo(buf[:], c2.LocalAddr()); err != nil {
    49  			b.Fatal(err)
    50  		}
    51  		if _, _, err := c2.ReadFrom(buf[:]); err != nil {
    52  			b.Fatal(err)
    53  		}
    54  	}
    55  }
    56  
    57  type resolveUDPAddrTest struct {
    58  	network       string
    59  	litAddrOrName string
    60  	addr          *UDPAddr
    61  	err           error
    62  }
    63  
    64  var resolveUDPAddrTests = []resolveUDPAddrTest{
    65  	{"udp", "127.0.0.1:0", &UDPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil},
    66  	{"udp4", "127.0.0.1:65535", &UDPAddr{IP: IPv4(127, 0, 0, 1), Port: 65535}, nil},
    67  
    68  	{"udp", "[::1]:0", &UDPAddr{IP: ParseIP("::1"), Port: 0}, nil},
    69  	{"udp6", "[::1]:65535", &UDPAddr{IP: ParseIP("::1"), Port: 65535}, nil},
    70  
    71  	{"udp", "[::1%en0]:1", &UDPAddr{IP: ParseIP("::1"), Port: 1, Zone: "en0"}, nil},
    72  	{"udp6", "[::1%911]:2", &UDPAddr{IP: ParseIP("::1"), Port: 2, Zone: "911"}, nil},
    73  
    74  	{"", "127.0.0.1:0", &UDPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil}, // Go 1.0 behavior
    75  	{"", "[::1]:0", &UDPAddr{IP: ParseIP("::1"), Port: 0}, nil},         // Go 1.0 behavior
    76  
    77  	{"udp", ":12345", &UDPAddr{Port: 12345}, nil},
    78  
    79  	{"http", "127.0.0.1:0", nil, UnknownNetworkError("http")},
    80  
    81  	{"udp", "127.0.0.1:domain", &UDPAddr{IP: ParseIP("127.0.0.1"), Port: 53}, nil},
    82  	{"udp", "[::ffff:127.0.0.1]:domain", &UDPAddr{IP: ParseIP("::ffff:127.0.0.1"), Port: 53}, nil},
    83  	{"udp", "[2001:db8::1]:domain", &UDPAddr{IP: ParseIP("2001:db8::1"), Port: 53}, nil},
    84  	{"udp4", "127.0.0.1:domain", &UDPAddr{IP: ParseIP("127.0.0.1"), Port: 53}, nil},
    85  	{"udp4", "[::ffff:127.0.0.1]:domain", &UDPAddr{IP: ParseIP("127.0.0.1"), Port: 53}, nil},
    86  	{"udp6", "[2001:db8::1]:domain", &UDPAddr{IP: ParseIP("2001:db8::1"), Port: 53}, nil},
    87  
    88  	{"udp4", "[2001:db8::1]:domain", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "2001:db8::1"}},
    89  	{"udp6", "127.0.0.1:domain", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "127.0.0.1"}},
    90  	{"udp6", "[::ffff:127.0.0.1]:domain", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "::ffff:127.0.0.1"}},
    91  }
    92  
    93  func TestResolveUDPAddr(t *testing.T) {
    94  	origTestHookLookupIP := testHookLookupIP
    95  	defer func() { testHookLookupIP = origTestHookLookupIP }()
    96  	testHookLookupIP = lookupLocalhost
    97  
    98  	for _, tt := range resolveUDPAddrTests {
    99  		addr, err := ResolveUDPAddr(tt.network, tt.litAddrOrName)
   100  		if !reflect.DeepEqual(addr, tt.addr) || !reflect.DeepEqual(err, tt.err) {
   101  			t.Errorf("ResolveUDPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr, err, tt.addr, tt.err)
   102  			continue
   103  		}
   104  		if err == nil {
   105  			addr2, err := ResolveUDPAddr(addr.Network(), addr.String())
   106  			if !reflect.DeepEqual(addr2, tt.addr) || err != tt.err {
   107  				t.Errorf("(%q, %q): ResolveUDPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr.Network(), addr.String(), addr2, err, tt.addr, tt.err)
   108  			}
   109  		}
   110  	}
   111  }
   112  
   113  func TestWriteToUDP(t *testing.T) {
   114  	switch runtime.GOOS {
   115  	case "plan9":
   116  		t.Skipf("not supported on %s", runtime.GOOS)
   117  	}
   118  
   119  	c, err := ListenPacket("udp", "127.0.0.1:0")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	defer c.Close()
   124  
   125  	testWriteToConn(t, c.LocalAddr().String())
   126  	testWriteToPacketConn(t, c.LocalAddr().String())
   127  }
   128  
   129  func testWriteToConn(t *testing.T, raddr string) {
   130  	c, err := Dial("udp", raddr)
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	defer c.Close()
   135  
   136  	ra, err := ResolveUDPAddr("udp", raddr)
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  
   141  	b := []byte("CONNECTED-MODE SOCKET")
   142  	_, err = c.(*UDPConn).WriteToUDP(b, ra)
   143  	if err == nil {
   144  		t.Fatal("should fail")
   145  	}
   146  	if err != nil && err.(*OpError).Err != ErrWriteToConnected {
   147  		t.Fatalf("should fail as ErrWriteToConnected: %v", err)
   148  	}
   149  	_, err = c.(*UDPConn).WriteTo(b, ra)
   150  	if err == nil {
   151  		t.Fatal("should fail")
   152  	}
   153  	if err != nil && err.(*OpError).Err != ErrWriteToConnected {
   154  		t.Fatalf("should fail as ErrWriteToConnected: %v", err)
   155  	}
   156  	_, err = c.Write(b)
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, ra)
   161  	if err == nil {
   162  		t.Fatal("should fail")
   163  	}
   164  	if err != nil && err.(*OpError).Err != ErrWriteToConnected {
   165  		t.Fatalf("should fail as ErrWriteToConnected: %v", err)
   166  	}
   167  	_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, nil)
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  }
   172  
   173  func testWriteToPacketConn(t *testing.T, raddr string) {
   174  	c, err := ListenPacket("udp", "127.0.0.1:0")
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  	defer c.Close()
   179  
   180  	ra, err := ResolveUDPAddr("udp", raddr)
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  
   185  	b := []byte("UNCONNECTED-MODE SOCKET")
   186  	_, err = c.(*UDPConn).WriteToUDP(b, ra)
   187  	if err != nil {
   188  		t.Fatal(err)
   189  	}
   190  	_, err = c.WriteTo(b, ra)
   191  	if err != nil {
   192  		t.Fatal(err)
   193  	}
   194  	_, err = c.(*UDPConn).Write(b)
   195  	if err == nil {
   196  		t.Fatal("should fail")
   197  	}
   198  	_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, nil)
   199  	if err == nil {
   200  		t.Fatal("should fail")
   201  	}
   202  	if err != nil && err.(*OpError).Err != errMissingAddress {
   203  		t.Fatalf("should fail as errMissingAddress: %v", err)
   204  	}
   205  	_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, ra)
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  }
   210  
   211  var udpConnLocalNameTests = []struct {
   212  	net   string
   213  	laddr *UDPAddr
   214  }{
   215  	{"udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)}},
   216  	{"udp4", &UDPAddr{}},
   217  	{"udp4", nil},
   218  }
   219  
   220  func TestUDPConnLocalName(t *testing.T) {
   221  	testenv.MustHaveExternalNetwork(t)
   222  
   223  	for _, tt := range udpConnLocalNameTests {
   224  		c, err := ListenUDP(tt.net, tt.laddr)
   225  		if err != nil {
   226  			t.Fatal(err)
   227  		}
   228  		defer c.Close()
   229  		la := c.LocalAddr()
   230  		if a, ok := la.(*UDPAddr); !ok || a.Port == 0 {
   231  			t.Fatalf("got %v; expected a proper address with non-zero port number", la)
   232  		}
   233  	}
   234  }
   235  
   236  func TestUDPConnLocalAndRemoteNames(t *testing.T) {
   237  	for _, laddr := range []string{"", "127.0.0.1:0"} {
   238  		c1, err := ListenPacket("udp", "127.0.0.1:0")
   239  		if err != nil {
   240  			t.Fatal(err)
   241  		}
   242  		defer c1.Close()
   243  
   244  		var la *UDPAddr
   245  		if laddr != "" {
   246  			var err error
   247  			if la, err = ResolveUDPAddr("udp", laddr); err != nil {
   248  				t.Fatal(err)
   249  			}
   250  		}
   251  		c2, err := DialUDP("udp", la, c1.LocalAddr().(*UDPAddr))
   252  		if err != nil {
   253  			t.Fatal(err)
   254  		}
   255  		defer c2.Close()
   256  
   257  		var connAddrs = [4]struct {
   258  			got Addr
   259  			ok  bool
   260  		}{
   261  			{c1.LocalAddr(), true},
   262  			{c1.(*UDPConn).RemoteAddr(), false},
   263  			{c2.LocalAddr(), true},
   264  			{c2.RemoteAddr(), true},
   265  		}
   266  		for _, ca := range connAddrs {
   267  			if a, ok := ca.got.(*UDPAddr); ok != ca.ok || ok && a.Port == 0 {
   268  				t.Fatalf("got %v; expected a proper address with non-zero port number", ca.got)
   269  			}
   270  		}
   271  	}
   272  }
   273  
   274  func TestIPv6LinkLocalUnicastUDP(t *testing.T) {
   275  	testenv.MustHaveExternalNetwork(t)
   276  
   277  	if !supportsIPv6() {
   278  		t.Skip("IPv6 is not supported")
   279  	}
   280  
   281  	for i, tt := range ipv6LinkLocalUnicastUDPTests {
   282  		c1, err := ListenPacket(tt.network, tt.address)
   283  		if err != nil {
   284  			// It might return "LookupHost returned no
   285  			// suitable address" error on some platforms.
   286  			t.Log(err)
   287  			continue
   288  		}
   289  		ls := (&packetListener{PacketConn: c1}).newLocalServer()
   290  		defer ls.teardown()
   291  		ch := make(chan error, 1)
   292  		handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, ch) }
   293  		if err := ls.buildup(handler); err != nil {
   294  			t.Fatal(err)
   295  		}
   296  		if la, ok := c1.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
   297  			t.Fatalf("got %v; expected a proper address with zone identifier", la)
   298  		}
   299  
   300  		c2, err := Dial(tt.network, ls.PacketConn.LocalAddr().String())
   301  		if err != nil {
   302  			t.Fatal(err)
   303  		}
   304  		defer c2.Close()
   305  		if la, ok := c2.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
   306  			t.Fatalf("got %v; expected a proper address with zone identifier", la)
   307  		}
   308  		if ra, ok := c2.RemoteAddr().(*UDPAddr); !ok || !tt.nameLookup && ra.Zone == "" {
   309  			t.Fatalf("got %v; expected a proper address with zone identifier", ra)
   310  		}
   311  
   312  		if _, err := c2.Write([]byte("UDP OVER IPV6 LINKLOCAL TEST")); err != nil {
   313  			t.Fatal(err)
   314  		}
   315  		b := make([]byte, 32)
   316  		if _, err := c2.Read(b); err != nil {
   317  			t.Fatal(err)
   318  		}
   319  
   320  		for err := range ch {
   321  			t.Errorf("#%d: %v", i, err)
   322  		}
   323  	}
   324  }
   325  
   326  func TestUDPZeroBytePayload(t *testing.T) {
   327  	switch runtime.GOOS {
   328  	case "plan9":
   329  		t.Skipf("not supported on %s", runtime.GOOS)
   330  	case "darwin", "ios":
   331  		testenv.SkipFlaky(t, 29225)
   332  	}
   333  
   334  	c := newLocalPacketListener(t, "udp")
   335  	defer c.Close()
   336  
   337  	for _, genericRead := range []bool{false, true} {
   338  		n, err := c.WriteTo(nil, c.LocalAddr())
   339  		if err != nil {
   340  			t.Fatal(err)
   341  		}
   342  		if n != 0 {
   343  			t.Errorf("got %d; want 0", n)
   344  		}
   345  		c.SetReadDeadline(time.Now().Add(30 * time.Second))
   346  		var b [1]byte
   347  		var name string
   348  		if genericRead {
   349  			_, err = c.(Conn).Read(b[:])
   350  			name = "Read"
   351  		} else {
   352  			_, _, err = c.ReadFrom(b[:])
   353  			name = "ReadFrom"
   354  		}
   355  		if err != nil {
   356  			t.Errorf("%s of zero byte packet failed: %v", name, err)
   357  		}
   358  	}
   359  }
   360  
   361  func TestUDPZeroByteBuffer(t *testing.T) {
   362  	switch runtime.GOOS {
   363  	case "plan9":
   364  		t.Skipf("not supported on %s", runtime.GOOS)
   365  	}
   366  
   367  	c := newLocalPacketListener(t, "udp")
   368  	defer c.Close()
   369  
   370  	b := []byte("UDP ZERO BYTE BUFFER TEST")
   371  	for _, genericRead := range []bool{false, true} {
   372  		n, err := c.WriteTo(b, c.LocalAddr())
   373  		if err != nil {
   374  			t.Fatal(err)
   375  		}
   376  		if n != len(b) {
   377  			t.Errorf("got %d; want %d", n, len(b))
   378  		}
   379  		c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
   380  		if genericRead {
   381  			_, err = c.(Conn).Read(nil)
   382  		} else {
   383  			_, _, err = c.ReadFrom(nil)
   384  		}
   385  		switch err {
   386  		case nil: // ReadFrom succeeds
   387  		default: // Read may timeout, it depends on the platform
   388  			if nerr, ok := err.(Error); (!ok || !nerr.Timeout()) && runtime.GOOS != "windows" { // Windows returns WSAEMSGSIZE
   389  				t.Fatal(err)
   390  			}
   391  		}
   392  	}
   393  }
   394  
   395  func TestUDPReadSizeError(t *testing.T) {
   396  	switch runtime.GOOS {
   397  	case "plan9":
   398  		t.Skipf("not supported on %s", runtime.GOOS)
   399  	}
   400  
   401  	c1 := newLocalPacketListener(t, "udp")
   402  	defer c1.Close()
   403  
   404  	c2, err := Dial("udp", c1.LocalAddr().String())
   405  	if err != nil {
   406  		t.Fatal(err)
   407  	}
   408  	defer c2.Close()
   409  
   410  	b1 := []byte("READ SIZE ERROR TEST")
   411  	for _, genericRead := range []bool{false, true} {
   412  		n, err := c2.Write(b1)
   413  		if err != nil {
   414  			t.Fatal(err)
   415  		}
   416  		if n != len(b1) {
   417  			t.Errorf("got %d; want %d", n, len(b1))
   418  		}
   419  		b2 := make([]byte, len(b1)-1)
   420  		if genericRead {
   421  			n, err = c1.(Conn).Read(b2)
   422  		} else {
   423  			n, _, err = c1.ReadFrom(b2)
   424  		}
   425  		if err != nil && runtime.GOOS != "windows" { // Windows returns WSAEMSGSIZE
   426  			t.Fatal(err)
   427  		}
   428  		if n != len(b1)-1 {
   429  			t.Fatalf("got %d; want %d", n, len(b1)-1)
   430  		}
   431  	}
   432  }
   433  
   434  // TestUDPReadTimeout verifies that ReadFromUDP with timeout returns an error
   435  // without data or an address.
   436  func TestUDPReadTimeout(t *testing.T) {
   437  	la, err := ResolveUDPAddr("udp4", "127.0.0.1:0")
   438  	if err != nil {
   439  		t.Fatal(err)
   440  	}
   441  	c, err := ListenUDP("udp4", la)
   442  	if err != nil {
   443  		t.Fatal(err)
   444  	}
   445  	defer c.Close()
   446  
   447  	c.SetDeadline(time.Now())
   448  	b := make([]byte, 1)
   449  	n, addr, err := c.ReadFromUDP(b)
   450  	if !errors.Is(err, os.ErrDeadlineExceeded) {
   451  		t.Errorf("ReadFromUDP got err %v want os.ErrDeadlineExceeded", err)
   452  	}
   453  	if n != 0 {
   454  		t.Errorf("ReadFromUDP got n %d want 0", n)
   455  	}
   456  	if addr != nil {
   457  		t.Errorf("ReadFromUDP got addr %+#v want nil", addr)
   458  	}
   459  }
   460  
   461  func TestAllocs(t *testing.T) {
   462  	switch runtime.GOOS {
   463  	case "plan9":
   464  		// Plan9 wasn't optimized.
   465  		t.Skipf("skipping on %v", runtime.GOOS)
   466  	}
   467  	// Optimizations are required to remove the allocs.
   468  	testenv.SkipIfOptimizationOff(t)
   469  
   470  	conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)})
   471  	if err != nil {
   472  		t.Fatal(err)
   473  	}
   474  	defer conn.Close()
   475  	addr := conn.LocalAddr()
   476  	addrPort := addr.(*UDPAddr).AddrPort()
   477  	buf := make([]byte, 8)
   478  
   479  	allocs := testing.AllocsPerRun(1000, func() {
   480  		_, _, err := conn.WriteMsgUDPAddrPort(buf, nil, addrPort)
   481  		if err != nil {
   482  			t.Fatal(err)
   483  		}
   484  		_, _, _, _, err = conn.ReadMsgUDPAddrPort(buf, nil)
   485  		if err != nil {
   486  			t.Fatal(err)
   487  		}
   488  	})
   489  	if got := int(allocs); got != 0 {
   490  		t.Errorf("WriteMsgUDPAddrPort/ReadMsgUDPAddrPort allocated %d objects", got)
   491  	}
   492  
   493  	allocs = testing.AllocsPerRun(1000, func() {
   494  		_, err := conn.WriteToUDPAddrPort(buf, addrPort)
   495  		if err != nil {
   496  			t.Fatal(err)
   497  		}
   498  		_, _, err = conn.ReadFromUDPAddrPort(buf)
   499  		if err != nil {
   500  			t.Fatal(err)
   501  		}
   502  	})
   503  	if got := int(allocs); got != 0 {
   504  		t.Errorf("WriteToUDPAddrPort/ReadFromUDPAddrPort allocated %d objects", got)
   505  	}
   506  
   507  	allocs = testing.AllocsPerRun(1000, func() {
   508  		_, err := conn.WriteTo(buf, addr)
   509  		if err != nil {
   510  			t.Fatal(err)
   511  		}
   512  		_, _, err = conn.ReadFromUDP(buf)
   513  		if err != nil {
   514  			t.Fatal(err)
   515  		}
   516  	})
   517  	if got := int(allocs); got != 1 {
   518  		t.Errorf("WriteTo/ReadFromUDP allocated %d objects", got)
   519  	}
   520  }
   521  
   522  func BenchmarkReadWriteMsgUDPAddrPort(b *testing.B) {
   523  	conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)})
   524  	if err != nil {
   525  		b.Fatal(err)
   526  	}
   527  	defer conn.Close()
   528  	addr := conn.LocalAddr().(*UDPAddr).AddrPort()
   529  	buf := make([]byte, 8)
   530  	b.ResetTimer()
   531  	b.ReportAllocs()
   532  	for i := 0; i < b.N; i++ {
   533  		_, _, err := conn.WriteMsgUDPAddrPort(buf, nil, addr)
   534  		if err != nil {
   535  			b.Fatal(err)
   536  		}
   537  		_, _, _, _, err = conn.ReadMsgUDPAddrPort(buf, nil)
   538  		if err != nil {
   539  			b.Fatal(err)
   540  		}
   541  	}
   542  }
   543  
   544  func BenchmarkWriteToReadFromUDP(b *testing.B) {
   545  	conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)})
   546  	if err != nil {
   547  		b.Fatal(err)
   548  	}
   549  	defer conn.Close()
   550  	addr := conn.LocalAddr()
   551  	buf := make([]byte, 8)
   552  	b.ResetTimer()
   553  	b.ReportAllocs()
   554  	for i := 0; i < b.N; i++ {
   555  		_, err := conn.WriteTo(buf, addr)
   556  		if err != nil {
   557  			b.Fatal(err)
   558  		}
   559  		_, _, err = conn.ReadFromUDP(buf)
   560  		if err != nil {
   561  			b.Fatal(err)
   562  		}
   563  	}
   564  }
   565  
   566  func BenchmarkWriteToReadFromUDPAddrPort(b *testing.B) {
   567  	conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)})
   568  	if err != nil {
   569  		b.Fatal(err)
   570  	}
   571  	defer conn.Close()
   572  	addr := conn.LocalAddr().(*UDPAddr).AddrPort()
   573  	buf := make([]byte, 8)
   574  	b.ResetTimer()
   575  	b.ReportAllocs()
   576  	for i := 0; i < b.N; i++ {
   577  		_, err := conn.WriteToUDPAddrPort(buf, addr)
   578  		if err != nil {
   579  			b.Fatal(err)
   580  		}
   581  		_, _, err = conn.ReadFromUDPAddrPort(buf)
   582  		if err != nil {
   583  			b.Fatal(err)
   584  		}
   585  	}
   586  }
   587  
   588  func TestUDPIPVersionReadMsg(t *testing.T) {
   589  	switch runtime.GOOS {
   590  	case "plan9":
   591  		t.Skipf("skipping on %v", runtime.GOOS)
   592  	}
   593  	conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)})
   594  	if err != nil {
   595  		t.Fatal(err)
   596  	}
   597  	defer conn.Close()
   598  	daddr := conn.LocalAddr().(*UDPAddr).AddrPort()
   599  	buf := make([]byte, 8)
   600  	_, err = conn.WriteToUDPAddrPort(buf, daddr)
   601  	if err != nil {
   602  		t.Fatal(err)
   603  	}
   604  	_, _, _, saddr, err := conn.ReadMsgUDPAddrPort(buf, nil)
   605  	if err != nil {
   606  		t.Fatal(err)
   607  	}
   608  	if !saddr.Addr().Is4() {
   609  		t.Error("returned AddrPort is not IPv4")
   610  	}
   611  	_, err = conn.WriteToUDPAddrPort(buf, daddr)
   612  	if err != nil {
   613  		t.Fatal(err)
   614  	}
   615  	_, _, _, soldaddr, err := conn.ReadMsgUDP(buf, nil)
   616  	if err != nil {
   617  		t.Fatal(err)
   618  	}
   619  	if len(soldaddr.IP) != 4 {
   620  		t.Error("returned UDPAddr is not IPv4")
   621  	}
   622  }
   623  
   624  // TestIPv6WriteMsgUDPAddrPortTargetAddrIPVersion verifies that
   625  // WriteMsgUDPAddrPort accepts IPv4, IPv4-mapped IPv6, and IPv6 target addresses
   626  // on a UDPConn listening on "::".
   627  func TestIPv6WriteMsgUDPAddrPortTargetAddrIPVersion(t *testing.T) {
   628  	if !supportsIPv6() {
   629  		t.Skip("IPv6 is not supported")
   630  	}
   631  
   632  	switch runtime.GOOS {
   633  	case "dragonfly", "openbsd":
   634  		// DragonflyBSD's IPv6 sockets are always IPv6-only, according to the man page:
   635  		// https://www.dragonflybsd.org/cgi/web-man?command=ip6 (search for IPV6_V6ONLY).
   636  		// OpenBSD's IPv6 sockets are always IPv6-only, according to the man page:
   637  		// https://man.openbsd.org/ip6#IPV6_V6ONLY
   638  		t.Skipf("skipping on %v", runtime.GOOS)
   639  	}
   640  
   641  	conn, err := ListenUDP("udp", nil)
   642  	if err != nil {
   643  		t.Fatal(err)
   644  	}
   645  	defer conn.Close()
   646  
   647  	daddr4 := netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), 12345)
   648  	daddr4in6 := netip.AddrPortFrom(netip.MustParseAddr("::ffff:127.0.0.1"), 12345)
   649  	daddr6 := netip.AddrPortFrom(netip.MustParseAddr("::1"), 12345)
   650  	buf := make([]byte, 8)
   651  
   652  	_, _, err = conn.WriteMsgUDPAddrPort(buf, nil, daddr4)
   653  	if err != nil {
   654  		t.Fatal(err)
   655  	}
   656  
   657  	_, _, err = conn.WriteMsgUDPAddrPort(buf, nil, daddr4in6)
   658  	if err != nil {
   659  		t.Fatal(err)
   660  	}
   661  
   662  	_, _, err = conn.WriteMsgUDPAddrPort(buf, nil, daddr6)
   663  	if err != nil {
   664  		t.Fatal(err)
   665  	}
   666  }