Source file src/net/unixsock_windows_test.go

Documentation: net

     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  // +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()} {
    49  		laddr := laddr
    50  		taddr := testUnixAddr()
    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, err := (&streamListener{Listener: ln}).newLocalServer()
    60  		if err != nil {
    61  			t.Fatal(err)
    62  		}
    63  		defer ls.teardown()
    64  		if err := ls.buildup(handler); err != nil {
    65  			t.Fatal(err)
    66  		}
    67  
    68  		la, err := ResolveUnixAddr("unix", laddr)
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  		c, err := DialUnix("unix", la, ta)
    73  		if err != nil {
    74  			t.Fatal(err)
    75  		}
    76  		defer func() {
    77  			c.Close()
    78  			if la != nil {
    79  				defer os.Remove(laddr)
    80  			}
    81  		}()
    82  		if _, err := c.Write([]byte("UNIXCONN LOCAL AND REMOTE NAME TEST")); err != nil {
    83  			t.Fatal(err)
    84  		}
    85  
    86  		if laddr == "" {
    87  			laddr = "@"
    88  		}
    89  		var connAddrs = [3]struct{ got, want Addr }{
    90  			{ln.Addr(), ta},
    91  			{c.LocalAddr(), &UnixAddr{Name: laddr, Net: "unix"}},
    92  			{c.RemoteAddr(), ta},
    93  		}
    94  		for _, ca := range connAddrs {
    95  			if !reflect.DeepEqual(ca.got, ca.want) {
    96  				t.Fatalf("got %#v, expected %#v", ca.got, ca.want)
    97  			}
    98  		}
    99  	}
   100  }
   101  

View as plain text