1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:54:45 -07:00

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>
This commit is contained in:
Bryan C. Mills 2021-12-09 11:55:20 -05:00 committed by Bryan Mills
parent b55cbbb9e7
commit 4b3d8d1a39
7 changed files with 68 additions and 53 deletions

View 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()

View 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 {

View 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 {

View 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)

View 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)
}

View 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)

View 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)