Skip to content

Commit 4b3d8d1

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent b55cbbb commit 4b3d8d1

File tree

7 files changed

+68
-53
lines changed

7 files changed

+68
-53
lines changed

src/net/mockserver_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ import (
1010
"errors"
1111
"fmt"
1212
"os"
13+
"path/filepath"
1314
"sync"
1415
"testing"
1516
"time"
1617
)
1718

18-
// testUnixAddr uses os.CreateTemp to get a name that is unique.
19-
func testUnixAddr() string {
20-
f, err := os.CreateTemp("", "go-nettest")
19+
// testUnixAddr uses os.MkdirTemp to get a name that is unique.
20+
func testUnixAddr(t testing.TB) string {
21+
// Pass an empty pattern to get a directory name that is as short as possible.
22+
// If we end up with a name longer than the sun_path field in the sockaddr_un
23+
// struct, we won't be able to make the syscall to open the socket.
24+
d, err := os.MkdirTemp("", "")
2125
if err != nil {
22-
panic(err)
26+
t.Fatal(err)
2327
}
24-
addr := f.Name()
25-
f.Close()
26-
os.Remove(addr)
27-
return addr
28+
t.Cleanup(func() {
29+
if err := os.RemoveAll(d); err != nil {
30+
t.Error(err)
31+
}
32+
})
33+
return filepath.Join(d, "sock")
2834
}
2935

3036
func newLocalListener(t testing.TB, network string) Listener {
@@ -59,7 +65,7 @@ func newLocalListener(t testing.TB, network string) Listener {
5965
return listen("tcp6", "[::1]:0")
6066
}
6167
case "unix", "unixpacket":
62-
return listen(network, testUnixAddr())
68+
return listen(network, testUnixAddr(t))
6369
}
6470

6571
t.Helper()
@@ -327,7 +333,7 @@ func newLocalPacketListener(t testing.TB, network string) PacketConn {
327333
return listenPacket("udp6", "[::1]:0")
328334
}
329335
case "unixgram":
330-
return listenPacket(network, testUnixAddr())
336+
return listenPacket(network, testUnixAddr(t))
331337
}
332338

333339
t.Helper()

src/net/packetconn_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
2727
return []byte("PACKETCONN TEST"), nil
2828
}
2929

30-
var packetConnTests = []struct {
31-
net string
32-
addr1 string
33-
addr2 string
34-
}{
35-
{"udp", "127.0.0.1:0", "127.0.0.1:0"},
36-
{"unixgram", testUnixAddr(), testUnixAddr()},
37-
}
38-
3930
func TestPacketConn(t *testing.T) {
31+
var packetConnTests = []struct {
32+
net string
33+
addr1 string
34+
addr2 string
35+
}{
36+
{"udp", "127.0.0.1:0", "127.0.0.1:0"},
37+
{"unixgram", testUnixAddr(t), testUnixAddr(t)},
38+
}
39+
4040
closer := func(c PacketConn, net, addr1, addr2 string) {
4141
c.Close()
4242
switch net {
@@ -85,6 +85,15 @@ func TestPacketConn(t *testing.T) {
8585
}
8686

8787
func TestConnAndPacketConn(t *testing.T) {
88+
var packetConnTests = []struct {
89+
net string
90+
addr1 string
91+
addr2 string
92+
}{
93+
{"udp", "127.0.0.1:0", "127.0.0.1:0"},
94+
{"unixgram", testUnixAddr(t), testUnixAddr(t)},
95+
}
96+
8897
closer := func(c PacketConn, net, addr1, addr2 string) {
8998
c.Close()
9099
switch net {

src/net/protoconn_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func TestUnixListenerSpecificMethods(t *testing.T) {
204204
t.Skip("unix test")
205205
}
206206

207-
addr := testUnixAddr()
207+
addr := testUnixAddr(t)
208208
la, err := ResolveUnixAddr("unix", addr)
209209
if err != nil {
210210
t.Fatal(err)
@@ -245,7 +245,7 @@ func TestUnixConnSpecificMethods(t *testing.T) {
245245
t.Skip("unixgram test")
246246
}
247247

248-
addr1, addr2, addr3 := testUnixAddr(), testUnixAddr(), testUnixAddr()
248+
addr1, addr2, addr3 := testUnixAddr(t), testUnixAddr(t), testUnixAddr(t)
249249

250250
a1, err := ResolveUnixAddr("unixgram", addr1)
251251
if err != nil {

src/net/server_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ func TestTCPServer(t *testing.T) {
122122
}
123123
}
124124

125-
var unixAndUnixpacketServerTests = []struct {
126-
network, address string
127-
}{
128-
{"unix", testUnixAddr()},
129-
{"unix", "@nettest/go/unix"},
130-
131-
{"unixpacket", testUnixAddr()},
132-
{"unixpacket", "@nettest/go/unixpacket"},
133-
}
134-
135125
// TestUnixAndUnixpacketServer tests concurrent accept-read-write
136126
// servers
137127
func TestUnixAndUnixpacketServer(t *testing.T) {
128+
var unixAndUnixpacketServerTests = []struct {
129+
network, address string
130+
}{
131+
{"unix", testUnixAddr(t)},
132+
{"unix", "@nettest/go/unix"},
133+
134+
{"unixpacket", testUnixAddr(t)},
135+
{"unixpacket", "@nettest/go/unixpacket"},
136+
}
137+
138138
const N = 3
139139

140140
for i, tt := range unixAndUnixpacketServerTests {
@@ -313,18 +313,18 @@ func TestUDPServer(t *testing.T) {
313313
}
314314
}
315315

316-
var unixgramServerTests = []struct {
317-
saddr string // server endpoint
318-
caddr string // client endpoint
319-
dial bool // test with Dial
320-
}{
321-
{saddr: testUnixAddr(), caddr: testUnixAddr()},
322-
{saddr: testUnixAddr(), caddr: testUnixAddr(), dial: true},
323-
324-
{saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
325-
}
326-
327316
func TestUnixgramServer(t *testing.T) {
317+
var unixgramServerTests = []struct {
318+
saddr string // server endpoint
319+
caddr string // client endpoint
320+
dial bool // test with Dial
321+
}{
322+
{saddr: testUnixAddr(t), caddr: testUnixAddr(t)},
323+
{saddr: testUnixAddr(t), caddr: testUnixAddr(t), dial: true},
324+
325+
{saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
326+
}
327+
328328
for i, tt := range unixgramServerTests {
329329
if !testableListenArgs("unixgram", tt.saddr, "") {
330330
t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)

src/net/splice_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func testSpliceNoUnixpacket(t *testing.T) {
213213
}
214214

215215
func testSpliceNoUnixgram(t *testing.T) {
216-
addr, err := ResolveUnixAddr("unixgram", testUnixAddr())
216+
addr, err := ResolveUnixAddr("unixgram", testUnixAddr(t))
217217
if err != nil {
218218
t.Fatal(err)
219219
}

src/net/unixsock_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestReadUnixgramWithUnnamedSocket(t *testing.T) {
2525
testenv.SkipFlaky(t, 15157)
2626
}
2727

28-
addr := testUnixAddr()
28+
addr := testUnixAddr(t)
2929
la, err := ResolveUnixAddr("unixgram", addr)
3030
if err != nil {
3131
t.Fatal(err)
@@ -168,7 +168,7 @@ func TestUnixgramWrite(t *testing.T) {
168168
t.Skip("unixgram test")
169169
}
170170

171-
addr := testUnixAddr()
171+
addr := testUnixAddr(t)
172172
laddr, err := ResolveUnixAddr("unixgram", addr)
173173
if err != nil {
174174
t.Fatal(err)
@@ -213,7 +213,7 @@ func testUnixgramWriteConn(t *testing.T, raddr *UnixAddr) {
213213
}
214214

215215
func testUnixgramWritePacketConn(t *testing.T, raddr *UnixAddr) {
216-
addr := testUnixAddr()
216+
addr := testUnixAddr(t)
217217
c, err := ListenPacket("unixgram", addr)
218218
if err != nil {
219219
t.Fatal(err)
@@ -242,9 +242,9 @@ func TestUnixConnLocalAndRemoteNames(t *testing.T) {
242242
}
243243

244244
handler := func(ls *localServer, ln Listener) {}
245-
for _, laddr := range []string{"", testUnixAddr()} {
245+
for _, laddr := range []string{"", testUnixAddr(t)} {
246246
laddr := laddr
247-
taddr := testUnixAddr()
247+
taddr := testUnixAddr(t)
248248
ta, err := ResolveUnixAddr("unix", taddr)
249249
if err != nil {
250250
t.Fatal(err)
@@ -301,9 +301,9 @@ func TestUnixgramConnLocalAndRemoteNames(t *testing.T) {
301301
t.Skip("unixgram test")
302302
}
303303

304-
for _, laddr := range []string{"", testUnixAddr()} {
304+
for _, laddr := range []string{"", testUnixAddr(t)} {
305305
laddr := laddr
306-
taddr := testUnixAddr()
306+
taddr := testUnixAddr(t)
307307
ta, err := ResolveUnixAddr("unixgram", taddr)
308308
if err != nil {
309309
t.Fatal(err)
@@ -359,7 +359,7 @@ func TestUnixUnlink(t *testing.T) {
359359
if !testableNetwork("unix") {
360360
t.Skip("unix test")
361361
}
362-
name := testUnixAddr()
362+
name := testUnixAddr(t)
363363

364364
listen := func(t *testing.T) *UnixListener {
365365
l, err := Listen("unix", name)

src/net/unixsock_windows_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func TestUnixConnLocalWindows(t *testing.T) {
4545
}
4646

4747
handler := func(ls *localServer, ln Listener) {}
48-
for _, laddr := range []string{"", testUnixAddr()} {
48+
for _, laddr := range []string{"", testUnixAddr(t)} {
4949
laddr := laddr
50-
taddr := testUnixAddr()
50+
taddr := testUnixAddr(t)
5151
ta, err := ResolveUnixAddr("unix", taddr)
5252
if err != nil {
5353
t.Fatal(err)

0 commit comments

Comments
 (0)