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/registry"
    11  	"os"
    12  	"reflect"
    13  	"runtime"
    14  	"strconv"
    15  	"testing"
    16  )
    17  
    18  func isBuild17063() bool {
    19  	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.READ)
    20  	if err != nil {
    21  		return false
    22  	}
    23  	defer k.Close()
    24  
    25  	s, _, err := k.GetStringValue("CurrentBuild")
    26  	if err != nil {
    27  		return false
    28  	}
    29  	ver, err := strconv.Atoi(s)
    30  	if err != nil {
    31  		return false
    32  	}
    33  	return ver >= 17063
    34  }
    35  
    36  func TestUnixConnLocalWindows(t *testing.T) {
    37  	switch runtime.GOARCH {
    38  	case "386":
    39  		t.Skip("not supported on windows/386, see golang.org/issue/27943")
    40  	case "arm":
    41  		t.Skip("not supported on windows/arm, see golang.org/issue/28061")
    42  	}
    43  	if !isBuild17063() {
    44  		t.Skip("unix test")
    45  	}
    46  
    47  	handler := func(ls *localServer, ln Listener) {}
    48  	for _, laddr := range []string{"", testUnixAddr(t)} {
    49  		laddr := laddr
    50  		taddr := testUnixAddr(t)
    51  		ta, err := ResolveUnixAddr("unix", taddr)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		ln, err := ListenUnix("unix", ta)
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  		ls := (&streamListener{Listener: ln}).newLocalServer()
    60  		defer ls.teardown()
    61  		if err := ls.buildup(handler); err != nil {
    62  			t.Fatal(err)
    63  		}
    64  
    65  		la, err := ResolveUnixAddr("unix", laddr)
    66  		if err != nil {
    67  			t.Fatal(err)
    68  		}
    69  		c, err := DialUnix("unix", la, ta)
    70  		if err != nil {
    71  			t.Fatal(err)
    72  		}
    73  		defer func() {
    74  			c.Close()
    75  			if la != nil {
    76  				defer os.Remove(laddr)
    77  			}
    78  		}()
    79  		if _, err := c.Write([]byte("UNIXCONN LOCAL AND REMOTE NAME TEST")); err != nil {
    80  			t.Fatal(err)
    81  		}
    82  
    83  		if laddr == "" {
    84  			laddr = "@"
    85  		}
    86  		var connAddrs = [3]struct{ got, want Addr }{
    87  			{ln.Addr(), ta},
    88  			{c.LocalAddr(), &UnixAddr{Name: laddr, Net: "unix"}},
    89  			{c.RemoteAddr(), ta},
    90  		}
    91  		for _, ca := range connAddrs {
    92  			if !reflect.DeepEqual(ca.got, ca.want) {
    93  				t.Fatalf("got %#v, expected %#v", ca.got, ca.want)
    94  			}
    95  		}
    96  	}
    97  }
    98  

View as plain text