github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/conn_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  // This file implements API tests across platforms and will never have a build
     6  // tag.
     7  
     8  package net
     9  
    10  import (
    11  	"os"
    12  	"runtime"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  var connTests = []struct {
    18  	net  string
    19  	addr string
    20  }{
    21  	{"tcp", "127.0.0.1:0"},
    22  	{"unix", testUnixAddr()},
    23  	{"unixpacket", testUnixAddr()},
    24  }
    25  
    26  // someTimeout is used just to test that net.Conn implementations
    27  // don't explode when their SetFooDeadline methods are called.
    28  // It isn't actually used for testing timeouts.
    29  const someTimeout = 10 * time.Second
    30  
    31  func TestConnAndListener(t *testing.T) {
    32  	for _, tt := range connTests {
    33  		switch tt.net {
    34  		case "unix":
    35  			switch runtime.GOOS {
    36  			case "nacl", "plan9", "windows":
    37  				continue
    38  			}
    39  		case "unixpacket":
    40  			switch runtime.GOOS {
    41  			case "android", "darwin", "nacl", "openbsd", "plan9", "windows":
    42  				continue
    43  			case "freebsd": // FreeBSD 8 doesn't support unixpacket
    44  				continue
    45  			}
    46  		}
    47  
    48  		ln, err := Listen(tt.net, tt.addr)
    49  		if err != nil {
    50  			t.Fatalf("Listen failed: %v", err)
    51  		}
    52  		defer func(ln Listener, net, addr string) {
    53  			ln.Close()
    54  			switch net {
    55  			case "unix", "unixpacket":
    56  				os.Remove(addr)
    57  			}
    58  		}(ln, tt.net, tt.addr)
    59  		if ln.Addr().Network() != tt.net {
    60  			t.Fatalf("got %v; expected %v", ln.Addr().Network(), tt.net)
    61  		}
    62  
    63  		done := make(chan int)
    64  		go transponder(t, ln, done)
    65  
    66  		c, err := Dial(tt.net, ln.Addr().String())
    67  		if err != nil {
    68  			t.Fatalf("Dial failed: %v", err)
    69  		}
    70  		defer c.Close()
    71  		if c.LocalAddr().Network() != tt.net || c.LocalAddr().Network() != tt.net {
    72  			t.Fatalf("got %v->%v; expected %v->%v", c.LocalAddr().Network(), c.RemoteAddr().Network(), tt.net, tt.net)
    73  		}
    74  		c.SetDeadline(time.Now().Add(someTimeout))
    75  		c.SetReadDeadline(time.Now().Add(someTimeout))
    76  		c.SetWriteDeadline(time.Now().Add(someTimeout))
    77  
    78  		if _, err := c.Write([]byte("CONN TEST")); err != nil {
    79  			t.Fatalf("Conn.Write failed: %v", err)
    80  		}
    81  		rb := make([]byte, 128)
    82  		if _, err := c.Read(rb); err != nil {
    83  			t.Fatalf("Conn.Read failed: %v", err)
    84  		}
    85  
    86  		<-done
    87  	}
    88  }
    89  
    90  func transponder(t *testing.T, ln Listener, done chan<- int) {
    91  	defer func() { done <- 1 }()
    92  
    93  	switch ln := ln.(type) {
    94  	case *TCPListener:
    95  		ln.SetDeadline(time.Now().Add(someTimeout))
    96  	case *UnixListener:
    97  		ln.SetDeadline(time.Now().Add(someTimeout))
    98  	}
    99  	c, err := ln.Accept()
   100  	if err != nil {
   101  		t.Errorf("Listener.Accept failed: %v", err)
   102  		return
   103  	}
   104  	defer c.Close()
   105  	network := ln.Addr().Network()
   106  	if c.LocalAddr().Network() != network || c.LocalAddr().Network() != network {
   107  		t.Errorf("got %v->%v; expected %v->%v", c.LocalAddr().Network(), c.RemoteAddr().Network(), network, network)
   108  		return
   109  	}
   110  	c.SetDeadline(time.Now().Add(someTimeout))
   111  	c.SetReadDeadline(time.Now().Add(someTimeout))
   112  	c.SetWriteDeadline(time.Now().Add(someTimeout))
   113  
   114  	b := make([]byte, 128)
   115  	n, err := c.Read(b)
   116  	if err != nil {
   117  		t.Errorf("Conn.Read failed: %v", err)
   118  		return
   119  	}
   120  	if _, err := c.Write(b[:n]); err != nil {
   121  		t.Errorf("Conn.Write failed: %v", err)
   122  		return
   123  	}
   124  }