Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkgs/mcpx/internal/cli/stopall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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", "")
Expand Down
6 changes: 5 additions & 1 deletion pkgs/mcpx/internal/sdnotify/sdnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"testing"
"time"

"github.com/dezren39/mcpx/internal/testsupport"
)

func listen(t *testing.T, name string) *net.UnixConn {
Expand Down Expand Up @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions pkgs/mcpx/internal/testsupport/tempdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tmp>/<TestName><random>/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))
Expand Down
20 changes: 20 additions & 0 deletions pkgs/mcpx/internal/testsupport/tempdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading