]> Cypherpunks repositories - gostls13.git/commitdiff
net: create unix sockets in unique directories
authorBryan C. Mills <bcmills@google.com>
Thu, 9 Dec 2021 16:55:20 +0000 (11:55 -0500)
committerBryan Mills <bcmills@google.com>
Mon, 13 Dec 2021 16:42:31 +0000 (16:42 +0000)
This change applies the same transformation as in CL 366774,
but to the net package.

testUnixAddr was using os.CreateTemp to obtain a unique socket path,
but then calling os.Remove on that path immediately. Since the
existence of the file is what guarantees its uniqueness, that could
occasionally result in testUnixAddr returning the same path for two
calls, causing the tests using those paths to fail — especially if
they are the same test or are run in parallel.

Instead, we now create a unique, short temp directory for each call,
and use a path within that directory for the socket address.

For #34611

Change-Id: I8e13b606abce2479a0305f7aeecf5d54c449a032
Reviewed-on: https://go-review.googlesource.com/c/go/+/370694
Trust: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/mockserver_test.go
src/net/packetconn_test.go
src/net/protoconn_test.go
src/net/server_test.go
src/net/splice_test.go
src/net/unixsock_test.go
src/net/unixsock_windows_test.go

index 0868871b7b3c21a6d94c6690ebccd3a063e56fc9..186bd330b2c20933228fba0fb025c2a23b13de28 100644 (file)
@@ -10,21 +10,27 @@ import (
        "errors"
        "fmt"
        "os"
+       "path/filepath"
        "sync"
        "testing"
        "time"
 )
 
-// testUnixAddr uses os.CreateTemp to get a name that is unique.
-func testUnixAddr() string {
-       f, err := os.CreateTemp("", "go-nettest")
+// testUnixAddr uses os.MkdirTemp to get a name that is unique.
+func testUnixAddr(t testing.TB) string {
+       // Pass an empty pattern to get a directory name that is as short as possible.
+       // If we end up with a name longer than the sun_path field in the sockaddr_un
+       // struct, we won't be able to make the syscall to open the socket.
+       d, err := os.MkdirTemp("", "")
        if err != nil {
-               panic(err)
+               t.Fatal(err)
        }
-       addr := f.Name()
-       f.Close()
-       os.Remove(addr)
-       return addr
+       t.Cleanup(func() {
+               if err := os.RemoveAll(d); err != nil {
+                       t.Error(err)
+               }
+       })
+       return filepath.Join(d, "sock")
 }
 
 func newLocalListener(t testing.TB, network string) Listener {
@@ -59,7 +65,7 @@ func newLocalListener(t testing.TB, network string) Listener {
                        return listen("tcp6", "[::1]:0")
                }
        case "unix", "unixpacket":
-               return listen(network, testUnixAddr())
+               return listen(network, testUnixAddr(t))
        }
 
        t.Helper()
@@ -327,7 +333,7 @@ func newLocalPacketListener(t testing.TB, network string) PacketConn {
                        return listenPacket("udp6", "[::1]:0")
                }
        case "unixgram":
-               return listenPacket(network, testUnixAddr())
+               return listenPacket(network, testUnixAddr(t))
        }
 
        t.Helper()
index 487912efab961566079dd0f19e3679dc33f5f2ef..fa160df5f50a1da8f5e36f69007f162e238912d8 100644 (file)
@@ -27,16 +27,16 @@ func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
        return []byte("PACKETCONN TEST"), nil
 }
 
-var packetConnTests = []struct {
-       net   string
-       addr1 string
-       addr2 string
-}{
-       {"udp", "127.0.0.1:0", "127.0.0.1:0"},
-       {"unixgram", testUnixAddr(), testUnixAddr()},
-}
-
 func TestPacketConn(t *testing.T) {
+       var packetConnTests = []struct {
+               net   string
+               addr1 string
+               addr2 string
+       }{
+               {"udp", "127.0.0.1:0", "127.0.0.1:0"},
+               {"unixgram", testUnixAddr(t), testUnixAddr(t)},
+       }
+
        closer := func(c PacketConn, net, addr1, addr2 string) {
                c.Close()
                switch net {
@@ -85,6 +85,15 @@ func TestPacketConn(t *testing.T) {
 }
 
 func TestConnAndPacketConn(t *testing.T) {
+       var packetConnTests = []struct {
+               net   string
+               addr1 string
+               addr2 string
+       }{
+               {"udp", "127.0.0.1:0", "127.0.0.1:0"},
+               {"unixgram", testUnixAddr(t), testUnixAddr(t)},
+       }
+
        closer := func(c PacketConn, net, addr1, addr2 string) {
                c.Close()
                switch net {
index baf3ac6679018605261fef17171a147412edbfd1..e4198a3a051a6a8d7518953f94e25d2fc5545884 100644 (file)
@@ -204,7 +204,7 @@ func TestUnixListenerSpecificMethods(t *testing.T) {
                t.Skip("unix test")
        }
 
-       addr := testUnixAddr()
+       addr := testUnixAddr(t)
        la, err := ResolveUnixAddr("unix", addr)
        if err != nil {
                t.Fatal(err)
@@ -245,7 +245,7 @@ func TestUnixConnSpecificMethods(t *testing.T) {
                t.Skip("unixgram test")
        }
 
-       addr1, addr2, addr3 := testUnixAddr(), testUnixAddr(), testUnixAddr()
+       addr1, addr2, addr3 := testUnixAddr(t), testUnixAddr(t), testUnixAddr(t)
 
        a1, err := ResolveUnixAddr("unixgram", addr1)
        if err != nil {
index be12c1a12d7cb4c065309dcca05932373eb8d0ae..6796d7993e9c5bf8ca372995782be0b2474aebfa 100644 (file)
@@ -122,19 +122,19 @@ func TestTCPServer(t *testing.T) {
        }
 }
 
-var unixAndUnixpacketServerTests = []struct {
-       network, address string
-}{
-       {"unix", testUnixAddr()},
-       {"unix", "@nettest/go/unix"},
-
-       {"unixpacket", testUnixAddr()},
-       {"unixpacket", "@nettest/go/unixpacket"},
-}
-
 // TestUnixAndUnixpacketServer tests concurrent accept-read-write
 // servers
 func TestUnixAndUnixpacketServer(t *testing.T) {
+       var unixAndUnixpacketServerTests = []struct {
+               network, address string
+       }{
+               {"unix", testUnixAddr(t)},
+               {"unix", "@nettest/go/unix"},
+
+               {"unixpacket", testUnixAddr(t)},
+               {"unixpacket", "@nettest/go/unixpacket"},
+       }
+
        const N = 3
 
        for i, tt := range unixAndUnixpacketServerTests {
@@ -313,18 +313,18 @@ func TestUDPServer(t *testing.T) {
        }
 }
 
-var unixgramServerTests = []struct {
-       saddr string // server endpoint
-       caddr string // client endpoint
-       dial  bool   // test with Dial
-}{
-       {saddr: testUnixAddr(), caddr: testUnixAddr()},
-       {saddr: testUnixAddr(), caddr: testUnixAddr(), dial: true},
-
-       {saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
-}
-
 func TestUnixgramServer(t *testing.T) {
+       var unixgramServerTests = []struct {
+               saddr string // server endpoint
+               caddr string // client endpoint
+               dial  bool   // test with Dial
+       }{
+               {saddr: testUnixAddr(t), caddr: testUnixAddr(t)},
+               {saddr: testUnixAddr(t), caddr: testUnixAddr(t), dial: true},
+
+               {saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
+       }
+
        for i, tt := range unixgramServerTests {
                if !testableListenArgs("unixgram", tt.saddr, "") {
                        t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)
index 38d51451b63a04d7f9f4f703780b85e6c7745f26..fa14c95eb715335a96d7fccd117d6d9afde01b83 100644 (file)
@@ -213,7 +213,7 @@ func testSpliceNoUnixpacket(t *testing.T) {
 }
 
 func testSpliceNoUnixgram(t *testing.T) {
-       addr, err := ResolveUnixAddr("unixgram", testUnixAddr())
+       addr, err := ResolveUnixAddr("unixgram", testUnixAddr(t))
        if err != nil {
                t.Fatal(err)
        }
index 5ad20a0151665ba3aeced105b27180da8528ef27..2fc9580cafb68f88108cf287443f563c7d94519b 100644 (file)
@@ -25,7 +25,7 @@ func TestReadUnixgramWithUnnamedSocket(t *testing.T) {
                testenv.SkipFlaky(t, 15157)
        }
 
-       addr := testUnixAddr()
+       addr := testUnixAddr(t)
        la, err := ResolveUnixAddr("unixgram", addr)
        if err != nil {
                t.Fatal(err)
@@ -168,7 +168,7 @@ func TestUnixgramWrite(t *testing.T) {
                t.Skip("unixgram test")
        }
 
-       addr := testUnixAddr()
+       addr := testUnixAddr(t)
        laddr, err := ResolveUnixAddr("unixgram", addr)
        if err != nil {
                t.Fatal(err)
@@ -213,7 +213,7 @@ func testUnixgramWriteConn(t *testing.T, raddr *UnixAddr) {
 }
 
 func testUnixgramWritePacketConn(t *testing.T, raddr *UnixAddr) {
-       addr := testUnixAddr()
+       addr := testUnixAddr(t)
        c, err := ListenPacket("unixgram", addr)
        if err != nil {
                t.Fatal(err)
@@ -242,9 +242,9 @@ func TestUnixConnLocalAndRemoteNames(t *testing.T) {
        }
 
        handler := func(ls *localServer, ln Listener) {}
-       for _, laddr := range []string{"", testUnixAddr()} {
+       for _, laddr := range []string{"", testUnixAddr(t)} {
                laddr := laddr
-               taddr := testUnixAddr()
+               taddr := testUnixAddr(t)
                ta, err := ResolveUnixAddr("unix", taddr)
                if err != nil {
                        t.Fatal(err)
@@ -301,9 +301,9 @@ func TestUnixgramConnLocalAndRemoteNames(t *testing.T) {
                t.Skip("unixgram test")
        }
 
-       for _, laddr := range []string{"", testUnixAddr()} {
+       for _, laddr := range []string{"", testUnixAddr(t)} {
                laddr := laddr
-               taddr := testUnixAddr()
+               taddr := testUnixAddr(t)
                ta, err := ResolveUnixAddr("unixgram", taddr)
                if err != nil {
                        t.Fatal(err)
@@ -359,7 +359,7 @@ func TestUnixUnlink(t *testing.T) {
        if !testableNetwork("unix") {
                t.Skip("unix test")
        }
-       name := testUnixAddr()
+       name := testUnixAddr(t)
 
        listen := func(t *testing.T) *UnixListener {
                l, err := Listen("unix", name)
index e847a20de00f09b86df05f193c6cdddfc836c0d3..d541d89f78c1e875dca9c1b90578aeca985968e6 100644 (file)
@@ -45,9 +45,9 @@ func TestUnixConnLocalWindows(t *testing.T) {
        }
 
        handler := func(ls *localServer, ln Listener) {}
-       for _, laddr := range []string{"", testUnixAddr()} {
+       for _, laddr := range []string{"", testUnixAddr(t)} {
                laddr := laddr
-               taddr := testUnixAddr()
+               taddr := testUnixAddr(t)
                ta, err := ResolveUnixAddr("unix", taddr)
                if err != nil {
                        t.Fatal(err)