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 TestUnixAbstractLongNameNulStart(t *testing.T) {
    53  	// Create an abstract socket name that starts with a null byte ("\x00")
    54  	// whose length is the maximum of RawSockaddrUnix Path len
    55  	paddedAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path))
    56  	copy(paddedAddr, "\x00abstract_test")
    57  
    58  	la, err := ResolveUnixAddr("unix", string(paddedAddr))
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	c, err := ListenUnix("unix", la)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	c.Close()
    67  }
    68  
    69  func TestUnixgramLinuxAbstractLongName(t *testing.T) {
    70  	if !testableNetwork("unixgram") {
    71  		t.Skip("abstract unix socket long name test")
    72  	}
    73  
    74  	// Create an abstract socket name whose length is exactly
    75  	// the maximum RawSockkaddrUnix Path len
    76  	rsu := syscall.RawSockaddrUnix{}
    77  	addrBytes := make([]byte, len(rsu.Path))
    78  	copy(addrBytes, "@abstract_test")
    79  	addr := string(addrBytes)
    80  
    81  	la, err := ResolveUnixAddr("unixgram", addr)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	c, err := ListenUnixgram("unixgram", la)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	defer c.Close()
    90  
    91  	off := make(chan bool)
    92  	data := [5]byte{1, 2, 3, 4, 5}
    93  	go func() {
    94  		defer func() { off <- true }()
    95  		s, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0)
    96  		if err != nil {
    97  			t.Error(err)
    98  			return
    99  		}
   100  		defer syscall.Close(s)
   101  		rsa := &syscall.SockaddrUnix{Name: addr}
   102  		if err := syscall.Sendto(s, data[:], 0, rsa); err != nil {
   103  			t.Error(err)
   104  			return
   105  		}
   106  	}()
   107  
   108  	<-off
   109  	b := make([]byte, 64)
   110  	c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
   111  	n, from, err := c.ReadFrom(b)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	if from != nil {
   116  		t.Fatalf("unexpected peer address: %v", from)
   117  	}
   118  	if !bytes.Equal(b[:n], data[:]) {
   119  		t.Fatalf("got %v; want %v", b[:n], data[:])
   120  	}
   121  }
   122  

View as plain text