Source file src/net/unixsock_linux_test.go

     1  // Copyright 2017 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  package net
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"syscall"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestUnixgramAutobind(t *testing.T) {
    16  	laddr := &UnixAddr{Name: "", Net: "unixgram"}
    17  	c1, err := ListenUnixgram("unixgram", laddr)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	defer c1.Close()
    22  
    23  	// retrieve the autobind address
    24  	autoAddr := c1.LocalAddr().(*UnixAddr)
    25  	if len(autoAddr.Name) <= 1 {
    26  		t.Fatalf("invalid autobind address: %v", autoAddr)
    27  	}
    28  	if autoAddr.Name[0] != '@' {
    29  		t.Fatalf("invalid autobind address: %v", autoAddr)
    30  	}
    31  
    32  	c2, err := DialUnix("unixgram", nil, autoAddr)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer c2.Close()
    37  
    38  	if !reflect.DeepEqual(c1.LocalAddr(), c2.RemoteAddr()) {
    39  		t.Fatalf("expected autobind address %v, got %v", c1.LocalAddr(), c2.RemoteAddr())
    40  	}
    41  }
    42  
    43  func TestUnixAutobindClose(t *testing.T) {
    44  	laddr := &UnixAddr{Name: "", Net: "unix"}
    45  	ln, err := ListenUnix("unix", laddr)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	ln.Close()
    50  }
    51  
    52  func TestUnixgramLinuxAbstractLongName(t *testing.T) {
    53  	if !testableNetwork("unixgram") {
    54  		t.Skip("abstract unix socket long name test")
    55  	}
    56  
    57  	// Create an abstract socket name whose length is exactly
    58  	// the maximum RawSockkaddrUnix Path len
    59  	rsu := syscall.RawSockaddrUnix{}
    60  	addrBytes := make([]byte, len(rsu.Path))
    61  	copy(addrBytes, "@abstract_test")
    62  	addr := string(addrBytes)
    63  
    64  	la, err := ResolveUnixAddr("unixgram", addr)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	c, err := ListenUnixgram("unixgram", la)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	defer c.Close()
    73  
    74  	off := make(chan bool)
    75  	data := [5]byte{1, 2, 3, 4, 5}
    76  	go func() {
    77  		defer func() { off <- true }()
    78  		s, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0)
    79  		if err != nil {
    80  			t.Error(err)
    81  			return
    82  		}
    83  		defer syscall.Close(s)
    84  		rsa := &syscall.SockaddrUnix{Name: addr}
    85  		if err := syscall.Sendto(s, data[:], 0, rsa); err != nil {
    86  			t.Error(err)
    87  			return
    88  		}
    89  	}()
    90  
    91  	<-off
    92  	b := make([]byte, 64)
    93  	c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
    94  	n, from, err := c.ReadFrom(b)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	if from != nil {
    99  		t.Fatalf("unexpected peer address: %v", from)
   100  	}
   101  	if !bytes.Equal(b[:n], data[:]) {
   102  		t.Fatalf("got %v; want %v", b[:n], data[:])
   103  	}
   104  }
   105  

View as plain text