diff --git a/pkgs/mcpx/internal/cli/stopall_test.go b/pkgs/mcpx/internal/cli/stopall_test.go index c15266bada..c2e609e510 100644 --- a/pkgs/mcpx/internal/cli/stopall_test.go +++ b/pkgs/mcpx/internal/cli/stopall_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/dezren39/mcpx/internal/daemon" + "github.com/dezren39/mcpx/internal/testsupport" ) // writeDaemonRecord plants a daemon record for a live process (the test's @@ -38,7 +39,11 @@ func writeDaemonRecord(t *testing.T, dir, socket, configPath string) { // default directory. func customStateApp(t *testing.T) (a *App, defaultDir string) { t.Helper() - xdg := t.TempDir() + // The default dir holds the daemon socket the stop-all test binds, so + // it must come from a directory short enough for a unix socket path + // however deep TMPDIR is (#434) -- t.TempDir() under a nix sandbox + // build's TMPDIR exceeds Darwin's 104-byte sockaddr_un limit. + xdg := testsupport.SocketDir(t) t.Setenv("XDG_STATE_HOME", xdg) t.Setenv("XDG_CACHE_HOME", filepath.Join(xdg, "cache")) t.Setenv("MCPX_STATE_DIR", "") diff --git a/pkgs/mcpx/internal/sdnotify/sdnotify_test.go b/pkgs/mcpx/internal/sdnotify/sdnotify_test.go index 208d65ee43..c84898eb04 100644 --- a/pkgs/mcpx/internal/sdnotify/sdnotify_test.go +++ b/pkgs/mcpx/internal/sdnotify/sdnotify_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "testing" "time" + + "github.com/dezren39/mcpx/internal/testsupport" ) func listen(t *testing.T, name string) *net.UnixConn { @@ -38,7 +40,9 @@ func TestSendIsANoOpOutsideSystemd(t *testing.T) { } func TestReadyAndMainPIDReachTheSocket(t *testing.T) { - sock := filepath.Join(t.TempDir(), "notify.sock") + // Same short-directory requirement as every directly bound socket: + // t.TempDir() under a long TMPDIR exceeds the sockaddr_un limit (#434). + sock := filepath.Join(testsupport.SocketDir(t), "notify.sock") conn := listen(t, sock) t.Setenv("NOTIFY_SOCKET", sock) diff --git a/pkgs/mcpx/internal/testsupport/tempdir.go b/pkgs/mcpx/internal/testsupport/tempdir.go index 2063d909a8..368636e346 100644 --- a/pkgs/mcpx/internal/testsupport/tempdir.go +++ b/pkgs/mcpx/internal/testsupport/tempdir.go @@ -38,6 +38,33 @@ func TempDir(t testing.TB) string { return Resolved(t, dir) } +// SocketDir is a scratch directory short enough to bind a unix socket in, +// however deep the ambient TMPDIR is. +// +// t.TempDir() nests under TMPDIR as //001, and a nix +// sandbox build's TMPDIR is long and deeply nested itself, so a socket path +// built on it easily exceeds Darwin's 104-byte sockaddr_un limit (100 usable, +// the ceiling daemon.maxSocketPath uses) and bind fails with an opaque +// EINVAL -- in a test whose subject has nothing to do with socket paths +// (#434). The product's own sockets dodge this via daemon.socketPath's +// hashed fallback; a test that binds a socket directly has no such layer, +// so it needs the short directory itself. Like TempDir's out-of-home +// fallback above, the directory is created directly under /tmp, resolved, +// and fails the test if a daemon.sock inside it would not fit. +func SocketDir(t testing.TB) string { + t.Helper() + dir, err := os.MkdirTemp("/tmp", "mcpx-sock-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.RemoveAll(dir) }) + dir = Resolved(t, dir) + if n := len(filepath.Join(dir, "daemon.sock")); n > 100 { + t.Fatalf("socket directory %q leaves a %d-byte daemon.sock path, over the 100-byte unix socket ceiling", dir, n) + } + return dir +} + func within(path, dir string) bool { rel, err := filepath.Rel(dir, path) return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) diff --git a/pkgs/mcpx/internal/testsupport/tempdir_test.go b/pkgs/mcpx/internal/testsupport/tempdir_test.go index 2837a7674b..4b64c437d7 100644 --- a/pkgs/mcpx/internal/testsupport/tempdir_test.go +++ b/pkgs/mcpx/internal/testsupport/tempdir_test.go @@ -38,3 +38,23 @@ func TestTempDirIsNeverInsideHome(t *testing.T) { t.Errorf("TempDir must still be a usable directory: %v", err) } } + +// SocketDir must stay short even when TMPDIR is a long, deeply nested +// directory (a nix sandbox build's is): the whole point is that a socket +// bound inside it fits Darwin's 104-byte sockaddr_un limit (#434). +func TestSocketDirStaysShortUnderALongTmpDir(t *testing.T) { + long := filepath.Join(os.TempDir(), strings.Repeat("a-very-long-directory-name/", 6)) + t.Setenv("TMPDIR", long) + t.Setenv("GOTMPDIR", long) + + dir := testsupport.SocketDir(t) + if strings.HasPrefix(dir, long) { + t.Errorf("SocketDir returned %q, inside the long TMPDIR %q", dir, long) + } + if n := len(filepath.Join(dir, "daemon.sock")); n > 100 { + t.Errorf("a daemon.sock inside SocketDir is %d bytes, over the 100-byte unix socket ceiling", n) + } + if st, err := os.Stat(dir); err != nil || !st.IsDir() { + t.Errorf("SocketDir must be a usable directory: %v", err) + } +}