...
Run Format

Source file src/net/packetconn_test.go

Documentation: net

     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  // +build !js
     9  
    10  package net
    11  
    12  import (
    13  	"os"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  // The full stack test cases for IPConn have been moved to the
    19  // following:
    20  //	golang.org/x/net/ipv4
    21  //	golang.org/x/net/ipv6
    22  //	golang.org/x/net/icmp
    23  
    24  func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
    25  	if !testableNetwork(network) {
    26  		return nil, func() { t.Logf("skipping %s test", network) }
    27  	}
    28  	return []byte("PACKETCONN TEST"), nil
    29  }
    30  
    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(), testUnixAddr()},
    38  }
    39  
    40  func TestPacketConn(t *testing.T) {
    41  	closer := func(c PacketConn, net, addr1, addr2 string) {
    42  		c.Close()
    43  		switch net {
    44  		case "unixgram":
    45  			os.Remove(addr1)
    46  			os.Remove(addr2)
    47  		}
    48  	}
    49  
    50  	for _, tt := range packetConnTests {
    51  		wb, skipOrFatalFn := packetConnTestData(t, tt.net)
    52  		if skipOrFatalFn != nil {
    53  			skipOrFatalFn()
    54  			continue
    55  		}
    56  
    57  		c1, err := ListenPacket(tt.net, tt.addr1)
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		defer closer(c1, tt.net, tt.addr1, tt.addr2)
    62  		c1.LocalAddr()
    63  		c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
    64  		c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
    65  		c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
    66  
    67  		c2, err := ListenPacket(tt.net, tt.addr2)
    68  		if err != nil {
    69  			t.Fatal(err)
    70  		}
    71  		defer closer(c2, tt.net, tt.addr1, tt.addr2)
    72  		c2.LocalAddr()
    73  		c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
    74  		c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
    75  		c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
    76  		rb2 := make([]byte, 128)
    77  
    78  		if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
    79  			t.Fatal(err)
    80  		}
    81  		if _, _, err := c2.ReadFrom(rb2); err != nil {
    82  			t.Fatal(err)
    83  		}
    84  		if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		rb1 := make([]byte, 128)
    88  		if _, _, err := c1.ReadFrom(rb1); err != nil {
    89  			t.Fatal(err)
    90  		}
    91  	}
    92  }
    93  
    94  func TestConnAndPacketConn(t *testing.T) {
    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  		c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
   119  		c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
   120  		c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
   121  
   122  		c2, err := Dial(tt.net, c1.LocalAddr().String())
   123  		if err != nil {
   124  			t.Fatal(err)
   125  		}
   126  		defer c2.Close()
   127  		c2.LocalAddr()
   128  		c2.RemoteAddr()
   129  		c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
   130  		c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
   131  		c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
   132  
   133  		if _, err := c2.Write(wb); err != nil {
   134  			t.Fatal(err)
   135  		}
   136  		rb1 := make([]byte, 128)
   137  		if _, _, err := c1.ReadFrom(rb1); err != nil {
   138  			t.Fatal(err)
   139  		}
   140  		var dst Addr
   141  		switch tt.net {
   142  		case "unixgram":
   143  			continue
   144  		default:
   145  			dst = c2.LocalAddr()
   146  		}
   147  		if _, err := c1.WriteTo(wb, dst); err != nil {
   148  			t.Fatal(err)
   149  		}
   150  		rb2 := make([]byte, 128)
   151  		if _, err := c2.Read(rb2); err != nil {
   152  			t.Fatal(err)
   153  		}
   154  	}
   155  }
   156  

View as plain text