1 // Copyright 2016 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.
18 // testUnixAddr uses ioutil.TempFile to get a name that is unique.
19 // It also uses /tmp directory in case it is prohibited to create UNIX
21 func testUnixAddr() string {
22 f
, err
:= ioutil
.TempFile("", "go-nettest")
32 // testableNetwork reports whether network is testable on the current
33 // platform configuration.
34 // This is based on logic from standard library's net/platform_test.go.
35 func testableNetwork(network
string) bool {
39 case "android", "nacl", "plan9", "windows":
42 if runtime
.GOOS
== "darwin" && (runtime
.GOARCH
== "arm" || runtime
.GOARCH
== "arm64") {
47 case "android", "darwin", "nacl", "plan9", "windows", "freebsd":
54 func newLocalListener(network
string) (net
.Listener
, error
) {
57 ln
, err
:= net
.Listen("tcp", "127.0.0.1:0")
59 ln
, err
= net
.Listen("tcp6", "[::1]:0")
62 case "unix", "unixpacket":
63 return net
.Listen(network
, testUnixAddr())
65 return nil, fmt
.Errorf("%s is not supported", network
)
68 func TestTestConn(t
*testing
.T
) {
69 tests
:= []struct{ name
, network
string }{
72 {"UnixPacketPipe", "unixpacket"},
75 for _
, tt
:= range tests
{
76 t
.Run(tt
.name
, func(t
*testing
.T
) {
77 if !testableNetwork(tt
.network
) {
78 t
.Skipf("not supported on %s", runtime
.GOOS
)
81 mp
:= func() (c1
, c2 net
.Conn
, stop
func(), err error
) {
82 ln
, err
:= newLocalListener(tt
.network
)
84 return nil, nil, nil, err
87 // Start a connection between two endpoints.
89 done
:= make(chan bool)
91 c2
, err2
= ln
.Accept()
94 c1
, err1
= net
.Dial(ln
.Addr().Network(), ln
.Addr().String())
106 case "unix", "unixpacket":
107 os
.Remove(ln
.Addr().String())
114 return nil, nil, nil, err1
117 return nil, nil, nil, err2
119 return c1
, c2
, stop
, nil