Source file src/net/unixsock_windows_test.go

     1  // Copyright 2018 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  //go:build windows
     6  
     7  package net
     8  
     9  import (
    10  	"internal/syscall/windows"
    11  	"os"
    12  	"reflect"
    13  	"syscall"
    14  	"testing"
    15  )
    16  
    17  func TestUnixConnLocalWindows(t *testing.T) {
    18  	if !windows.SupportUnixSocket() {
    19  		t.Skip("unix test")
    20  	}
    21  	handler := func(ls *localServer, ln Listener) {}
    22  	for _, laddr := range []string{"", testUnixAddr(t)} {
    23  		laddr := laddr
    24  		taddr := testUnixAddr(t)
    25  		ta, err := ResolveUnixAddr("unix", taddr)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		ln, err := ListenUnix("unix", ta)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  		ls := (&streamListener{Listener: ln}).newLocalServer()
    34  		defer ls.teardown()
    35  		if err := ls.buildup(handler); err != nil {
    36  			t.Fatal(err)
    37  		}
    38  
    39  		la, err := ResolveUnixAddr("unix", laddr)
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  		c, err := DialUnix("unix", la, ta)
    44  		if err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		defer func() {
    48  			c.Close()
    49  			if la != nil {
    50  				defer os.Remove(laddr)
    51  			}
    52  		}()
    53  		if _, err := c.Write([]byte("UNIXCONN LOCAL AND REMOTE NAME TEST")); err != nil {
    54  			t.Fatal(err)
    55  		}
    56  
    57  		if laddr == "" {
    58  			laddr = "@"
    59  		}
    60  		var connAddrs = [3]struct{ got, want Addr }{
    61  			{ln.Addr(), ta},
    62  			{c.LocalAddr(), &UnixAddr{Name: laddr, Net: "unix"}},
    63  			{c.RemoteAddr(), ta},
    64  		}
    65  		for _, ca := range connAddrs {
    66  			if !reflect.DeepEqual(ca.got, ca.want) {
    67  				t.Fatalf("got %#v, expected %#v", ca.got, ca.want)
    68  			}
    69  		}
    70  	}
    71  }
    72  
    73  func TestUnixAbstractLongNameNulStart(t *testing.T) {
    74  	if !windows.SupportUnixSocket() {
    75  		t.Skip("unix test")
    76  	}
    77  
    78  	// Create an abstract socket name that starts with a null byte ("\x00")
    79  	// whose length is the maximum of RawSockaddrUnix Path len
    80  	paddedAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path))
    81  	copy(paddedAddr, "\x00abstract_test")
    82  
    83  	la, err := ResolveUnixAddr("unix", string(paddedAddr))
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	c, err := ListenUnix("unix", la)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	c.Close()
    92  }
    93  
    94  func TestModeSocket(t *testing.T) {
    95  	if !windows.SupportUnixSocket() {
    96  		t.Skip("unix test")
    97  	}
    98  
    99  	addr := testUnixAddr(t)
   100  
   101  	l, err := Listen("unix", addr)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	defer l.Close()
   106  
   107  	stat, err := os.Stat(addr)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	mode := stat.Mode()
   113  	if mode&os.ModeSocket == 0 {
   114  		t.Fatalf("%v should have ModeSocket", mode)
   115  	}
   116  }
   117  

View as plain text