aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2021-12-09 11:55:20 -0500
committerBryan Mills <bcmills@google.com>2021-12-13 16:42:31 +0000
commit4b3d8d1a390a51ea6a1b3f66ef9d56ef7203bbe7 (patch)
treefaa6991261baf219df06070126015395847bb1aa /src/net
parentb55cbbb9e76969d67fbc6e264a584ad18c2f95fa (diff)
downloadgo-4b3d8d1a390a51ea6a1b3f66ef9d56ef7203bbe7.tar.gz
go-4b3d8d1a390a51ea6a1b3f66ef9d56ef7203bbe7.zip
net: create unix sockets in unique directories
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>
Diffstat (limited to 'src/net')
-rw-r--r--src/net/mockserver_test.go26
-rw-r--r--src/net/packetconn_test.go27
-rw-r--r--src/net/protoconn_test.go4
-rw-r--r--src/net/server_test.go42
-rw-r--r--src/net/splice_test.go2
-rw-r--r--src/net/unixsock_test.go16
-rw-r--r--src/net/unixsock_windows_test.go4
7 files changed, 68 insertions, 53 deletions
diff --git a/src/net/mockserver_test.go b/src/net/mockserver_test.go
index 0868871b7b..186bd330b2 100644
--- a/src/net/mockserver_test.go
+++ b/src/net/mockserver_test.go
@@ -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()
diff --git a/src/net/packetconn_test.go b/src/net/packetconn_test.go
index 487912efab..fa160df5f5 100644
--- a/src/net/packetconn_test.go
+++ b/src/net/packetconn_test.go
@@ -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 {
diff --git a/src/net/protoconn_test.go b/src/net/protoconn_test.go
index baf3ac6679..e4198a3a05 100644
--- a/src/net/protoconn_test.go
+++ b/src/net/protoconn_test.go
@@ -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 {
diff --git a/src/net/server_test.go b/src/net/server_test.go
index be12c1a12d..6796d7993e 100644
--- a/src/net/server_test.go
+++ b/src/net/server_test.go
@@ -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)
diff --git a/src/net/splice_test.go b/src/net/splice_test.go
index 38d51451b6..fa14c95eb7 100644
--- a/src/net/splice_test.go
+++ b/src/net/splice_test.go
@@ -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)
}
diff --git a/src/net/unixsock_test.go b/src/net/unixsock_test.go
index 5ad20a0151..2fc9580caf 100644
--- a/src/net/unixsock_test.go
+++ b/src/net/unixsock_test.go
@@ -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)
diff --git a/src/net/unixsock_windows_test.go b/src/net/unixsock_windows_test.go
index e847a20de0..d541d89f78 100644
--- a/src/net/unixsock_windows_test.go
+++ b/src/net/unixsock_windows_test.go
@@ -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)