github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/net/nettest/conntest_test.go (about) 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. 4 5 //go:build go1.8 6 // +build go1.8 7 8 package nettest 9 10 import ( 11 "net" 12 "os" 13 "runtime" 14 "testing" 15 16 "github.com/icodeface/tls/internal/x/net/internal/nettest" 17 ) 18 19 func TestTestConn(t *testing.T) { 20 tests := []struct{ name, network string }{ 21 {"TCP", "tcp"}, 22 {"UnixPipe", "unix"}, 23 {"UnixPacketPipe", "unixpacket"}, 24 } 25 26 for _, tt := range tests { 27 t.Run(tt.name, func(t *testing.T) { 28 if !nettest.TestableNetwork(tt.network) { 29 t.Skipf("not supported on %s", runtime.GOOS) 30 } 31 32 mp := func() (c1, c2 net.Conn, stop func(), err error) { 33 ln, err := nettest.NewLocalListener(tt.network) 34 if err != nil { 35 return nil, nil, nil, err 36 } 37 38 // Start a connection between two endpoints. 39 var err1, err2 error 40 done := make(chan bool) 41 go func() { 42 c2, err2 = ln.Accept() 43 close(done) 44 }() 45 c1, err1 = net.Dial(ln.Addr().Network(), ln.Addr().String()) 46 <-done 47 48 stop = func() { 49 if err1 == nil { 50 c1.Close() 51 } 52 if err2 == nil { 53 c2.Close() 54 } 55 ln.Close() 56 switch tt.network { 57 case "unix", "unixpacket": 58 os.Remove(ln.Addr().String()) 59 } 60 } 61 62 switch { 63 case err1 != nil: 64 stop() 65 return nil, nil, nil, err1 66 case err2 != nil: 67 stop() 68 return nil, nil, nil, err2 69 default: 70 return c1, c2, stop, nil 71 } 72 } 73 74 TestConn(t, mp) 75 }) 76 } 77 }