Source file src/net/packetconn_test.go

     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  // This file implements API tests across platforms and will never have a build
     6  // tag.
     7  
     8  //go:build !js
     9  
    10  package net
    11  
    12  import (
    13  	"os"
    14  	"testing"
    15  )
    16  
    17  // The full stack test cases for IPConn have been moved to the
    18  // following:
    19  //	golang.org/x/net/ipv4
    20  //	golang.org/x/net/ipv6
    21  //	golang.org/x/net/icmp
    22  
    23  func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
    24  	if !testableNetwork(network) {
    25  		return nil, func() { t.Logf("skipping %s test", network) }
    26  	}
    27  	return []byte("PACKETCONN TEST"), nil
    28  }
    29  
    30  func TestPacketConn(t *testing.T) {
    31  	var packetConnTests = []struct {
    32  		net   string
    33  		addr1 string
    34  		addr2 string
    35  	}{
    36  		{"udp", "127.0.0.1:0", "127.0.0.1:0"},
    37  		{"unixgram", testUnixAddr(t), testUnixAddr(t)},
    38  	}
    39  
    40  	closer := func(c PacketConn, net, addr1, addr2 string) {
    41  		c.Close()
    42  		switch net {
    43  		case "unixgram":
    44  			os.Remove(addr1)
    45  			os.Remove(addr2)
    46  		}
    47  	}
    48  
    49  	for _, tt := range packetConnTests {
    50  		wb, skipOrFatalFn := packetConnTestData(t, tt.net)
    51  		if skipOrFatalFn != nil {
    52  			skipOrFatalFn()
    53  			continue
    54  		}
    55  
    56  		c1, err := ListenPacket(tt.net, tt.addr1)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		defer closer(c1, tt.net, tt.addr1, tt.addr2)
    61  		c1.LocalAddr()
    62  
    63  		c2, err := ListenPacket(tt.net, tt.addr2)
    64  		if err != nil {
    65  			t.Fatal(err)
    66  		}
    67  		defer closer(c2, tt.net, tt.addr1, tt.addr2)
    68  		c2.LocalAddr()
    69  		rb2 := make([]byte, 128)
    70  
    71  		if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
    72  			t.Fatal(err)
    73  		}
    74  		if _, _, err := c2.ReadFrom(rb2); err != nil {
    75  			t.Fatal(err)
    76  		}
    77  		if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
    78  			t.Fatal(err)
    79  		}
    80  		rb1 := make([]byte, 128)
    81  		if _, _, err := c1.ReadFrom(rb1); err != nil {
    82  			t.Fatal(err)
    83  		}
    84  	}
    85  }
    86  
    87  func TestConnAndPacketConn(t *testing.T) {
    88  	var packetConnTests = []struct {
    89  		net   string
    90  		addr1 string
    91  		addr2 string
    92  	}{
    93  		{"udp", "127.0.0.1:0", "127.0.0.1:0"},
    94  		{"unixgram", testUnixAddr(t), testUnixAddr(t)},
    95  	}
    96  
    97  	closer := func(c PacketConn, net, addr1, addr2 string) {
    98  		c.Close()
    99  		switch net {
   100  		case "unixgram":
   101  			os.Remove(addr1)
   102  			os.Remove(addr2)
   103  		}
   104  	}
   105  
   106  	for _, tt := range packetConnTests {
   107  		var wb []byte
   108  		wb, skipOrFatalFn := packetConnTestData(t, tt.net)
   109  		if skipOrFatalFn != nil {
   110  			skipOrFatalFn()
   111  			continue
   112  		}
   113  
   114  		c1, err := ListenPacket(tt.net, tt.addr1)
   115  		if err != nil {
   116  			t.Fatal(err)
   117  		}
   118  		defer closer(c1, tt.net, tt.addr1, tt.addr2)
   119  		c1.LocalAddr()
   120  
   121  		c2, err := Dial(tt.net, c1.LocalAddr().String())
   122  		if err != nil {
   123  			t.Fatal(err)
   124  		}
   125  		defer c2.Close()
   126  		c2.LocalAddr()
   127  		c2.RemoteAddr()
   128  
   129  		if _, err := c2.Write(wb); err != nil {
   130  			t.Fatal(err)
   131  		}
   132  		rb1 := make([]byte, 128)
   133  		if _, _, err := c1.ReadFrom(rb1); err != nil {
   134  			t.Fatal(err)
   135  		}
   136  		var dst Addr
   137  		switch tt.net {
   138  		case "unixgram":
   139  			continue
   140  		default:
   141  			dst = c2.LocalAddr()
   142  		}
   143  		if _, err := c1.WriteTo(wb, dst); err != nil {
   144  			t.Fatal(err)
   145  		}
   146  		rb2 := make([]byte, 128)
   147  		if _, err := c2.Read(rb2); err != nil {
   148  			t.Fatal(err)
   149  		}
   150  	}
   151  }
   152  

View as plain text