@@ -0,0 +1,189 @@
18+ "github.com/stretchr/testify/require"
21+// fakeTimeoutErr is a minimal net.Error implementation whose Timeout()
22+// returns true. It is used to verify that IsStaleSocketErr never
23+// classifies a timeout as stale.
24+type fakeTimeoutErr struct{}
26+func (fakeTimeoutErr) Error() string { return "fake timeout" }
27+func (fakeTimeoutErr) Timeout() bool { return true }
28+func (fakeTimeoutErr) Temporary() bool { return true }
30+func TestIsStaleSocketErr(t *testing.T) {
38+ {name: "nil", err: nil, want: false},
39+ {name: "ECONNREFUSED", err: syscall.ECONNREFUSED, want: true},
41+ name: "wrapped ECONNREFUSED",
42+ err: fmt.Errorf("dial: %w", syscall.ECONNREFUSED),
45+ {name: "fs.ErrNotExist", err: fs.ErrNotExist, want: true},
47+ name: "wrapped fs.ErrNotExist",
48+ err: fmt.Errorf("stat: %w", fs.ErrNotExist),
51+ {name: "timeout net.Error", err: fakeTimeoutErr{}, want: false},
52+ {name: "generic error", err: errors.New("boom"), want: false},
55+ for _, tc := range cases {
56+ t.Run(tc.name, func(t *testing.T) {
58+ require.Equal(t, tc.want, IsStaleSocketErr(tc.err))
63+func TestDefaultHost_XDGRuntimeDir(t *testing.T) {
65+ t.Setenv("XDG_RUNTIME_DIR", dir)
67+ host := DefaultHost()
69+ require.True(t, strings.HasPrefix(host, "unix://"),
70+ "DefaultHost should return a unix:// URL, got %q", host)
71+ path := strings.TrimPrefix(host, "unix://")
73+ // The composed path may exceed maxUnixSocketPathLen and fall back
74+ // to /tmp; only assert containment when it did not.
75+ if len(filepath.Join(dir, "crush.sock")) <= maxUnixSocketPathLen {
76+ require.True(t, strings.HasPrefix(path, dir),
77+ "socket path %q should live under %q", path, dir)
79+ require.True(t, strings.HasSuffix(path, ".sock"),
80+ "socket path %q should end in .sock", path)
81+ require.Contains(t, filepath.Base(path), "crush",
82+ "socket filename should contain 'crush'")
85+func TestDefaultHost_FallbackTemp(t *testing.T) {
86+ t.Setenv("XDG_RUNTIME_DIR", "")
88+ host := DefaultHost()
90+ require.True(t, strings.HasPrefix(host, "unix://"),
91+ "DefaultHost should return a unix:// URL, got %q", host)
92+ path := strings.TrimPrefix(host, "unix://")
93+ require.NotEmpty(t, path, "fallback socket path must be non-empty")
94+ require.True(t, strings.HasSuffix(path, ".sock"),
95+ "socket path %q should end in .sock", path)
96+ require.Contains(t, filepath.Base(path), "crush",
97+ "socket filename should contain 'crush'")
100+// staleSocketPath creates a deterministic stale unix socket file on
101+// disk: the socket node exists but no goroutine is accepting on it.
102+// It does so by binding a listener, disabling unlink-on-close, then
103+// closing the listener. The path is returned so the caller can probe
104+// it. A leftover file is best-effort removed via t.Cleanup.
105+func staleSocketPath(t *testing.T, path string) {
107+ ln, err := net.Listen("unix", path)
108+ require.NoError(t, err)
109+ ul, ok := ln.(*net.UnixListener)
110+ require.True(t, ok, "expected *net.UnixListener, got %T", ln)
111+ ul.SetUnlinkOnClose(false)
112+ require.NoError(t, ul.Close())
114+ // Verify it is actually stale: dialing should fail.
115+ conn, dialErr := net.DialTimeout("unix", path, 200*time.Millisecond)
118+ t.Fatalf("expected stale socket at %q to refuse connections", path)
120+ require.True(t, IsStaleSocketErr(dialErr),
121+ "expected stale-socket dial error, got %v", dialErr)
128+func TestListen_RemovesStaleSocket(t *testing.T) {
129+ // t.TempDir() yields a path that may already be near the macOS
130+ // sun_path limit; use a short filename to stay well under it.
132+ path := filepath.Join(dir, "s.sock")
134+ staleSocketPath(t, path)
136+ // Confirm the stale node is present before we call listen.
137+ _, statErr := os.Stat(path)
138+ require.NoError(t, statErr, "stale socket file should exist on disk")
140+ ln, removedStale, err := listen("unix", path)
141+ require.NoError(t, err)
142+ require.NotNil(t, ln)
143+ require.True(t, removedStale, "listen should report removedStale=true")
149+func TestListen_LiveSocketNotRemoved(t *testing.T) {
151+ path := filepath.Join(dir, "s.sock")
153+ ln1, err := net.Listen("unix", path)
154+ require.NoError(t, err)
156+ // Drain accepts so the listener stays alive and responsive without
157+ // blocking the test on a stray connection.
158+ var wg sync.WaitGroup
163+ c, err := ln1.Accept()
175+ ln2, removedStale, err := listen("unix", path)
179+ require.Error(t, err, "listen on a live socket must fail")
180+ require.False(t, removedStale,
181+ "a live socket must never be removed (got removedStale=true)")
183+ // The live socket file must still be on disk and dialable.
184+ _, statErr := os.Stat(path)
185+ require.NoError(t, statErr, "live socket file should still exist")
186+ conn, dialErr := net.DialTimeout("unix", path, 200*time.Millisecond)
187+ require.NoError(t, dialErr, "live socket should still accept dials")