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

View as plain text