File size: 5,677 Bytes
3d7a2d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | //go:build !windows
package server
import (
"errors"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// fakeTimeoutErr is a minimal net.Error implementation whose Timeout()
// returns true. It is used to verify that IsStaleSocketErr never
// classifies a timeout as stale.
type fakeTimeoutErr struct{}
func (fakeTimeoutErr) Error() string { return "fake timeout" }
func (fakeTimeoutErr) Timeout() bool { return true }
func (fakeTimeoutErr) Temporary() bool { return true }
func TestIsStaleSocketErr(t *testing.T) {
t.Parallel()
cases := []struct {
name string
err error
want bool
}{
{name: "nil", err: nil, want: false},
{name: "ECONNREFUSED", err: syscall.ECONNREFUSED, want: true},
{
name: "wrapped ECONNREFUSED",
err: fmt.Errorf("dial: %w", syscall.ECONNREFUSED),
want: true,
},
{name: "fs.ErrNotExist", err: fs.ErrNotExist, want: true},
{
name: "wrapped fs.ErrNotExist",
err: fmt.Errorf("stat: %w", fs.ErrNotExist),
want: true,
},
{name: "timeout net.Error", err: fakeTimeoutErr{}, want: false},
{name: "generic error", err: errors.New("boom"), want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tc.want, IsStaleSocketErr(tc.err))
})
}
}
func TestDefaultHost_XDGRuntimeDir(t *testing.T) {
dir := t.TempDir()
t.Setenv("XDG_RUNTIME_DIR", dir)
host := DefaultHost()
require.True(t, strings.HasPrefix(host, "unix://"),
"DefaultHost should return a unix:// URL, got %q", host)
path := strings.TrimPrefix(host, "unix://")
// The composed path may exceed maxUnixSocketPathLen and fall back
// to /tmp; only assert containment when it did not. Recompose the
// path under dir (rather than checking the returned path length,
// which is short again after a /tmp fallback) to decide whether a
// fallback happened. The socket is named crush-<uid>.sock.
composed := filepath.Join(dir, filepath.Base(path))
if len(composed) <= maxUnixSocketPathLen {
require.True(t, strings.HasPrefix(path, dir),
"socket path %q should live under %q", path, dir)
}
require.True(t, strings.HasSuffix(path, ".sock"),
"socket path %q should end in .sock", path)
require.Contains(t, filepath.Base(path), "crush",
"socket filename should contain 'crush'")
}
func TestDefaultHost_FallbackTemp(t *testing.T) {
t.Setenv("XDG_RUNTIME_DIR", "")
host := DefaultHost()
require.True(t, strings.HasPrefix(host, "unix://"),
"DefaultHost should return a unix:// URL, got %q", host)
path := strings.TrimPrefix(host, "unix://")
require.NotEmpty(t, path, "fallback socket path must be non-empty")
require.True(t, strings.HasSuffix(path, ".sock"),
"socket path %q should end in .sock", path)
require.Contains(t, filepath.Base(path), "crush",
"socket filename should contain 'crush'")
}
// staleSocketPath creates a deterministic stale unix socket file on
// disk: the socket node exists but no goroutine is accepting on it.
// It does so by binding a listener, disabling unlink-on-close, then
// closing the listener. The path is returned so the caller can probe
// it. A leftover file is best-effort removed via t.Cleanup.
func staleSocketPath(t *testing.T, path string) {
t.Helper()
ln, err := net.Listen("unix", path) //nolint:noctx
require.NoError(t, err)
ul, ok := ln.(*net.UnixListener)
require.True(t, ok, "expected *net.UnixListener, got %T", ln)
ul.SetUnlinkOnClose(false)
require.NoError(t, ul.Close())
// Verify it is actually stale: dialing should fail.
conn, dialErr := net.DialTimeout("unix", path, 200*time.Millisecond) //nolint:noctx
if dialErr == nil {
conn.Close()
t.Fatalf("expected stale socket at %q to refuse connections", path)
}
require.True(t, IsStaleSocketErr(dialErr),
"expected stale-socket dial error, got %v", dialErr)
t.Cleanup(func() {
_ = os.Remove(path)
})
}
func TestListen_RemovesStaleSocket(t *testing.T) {
// t.TempDir() yields a path that may already be near the macOS
// sun_path limit; use a short filename to stay well under it.
dir := t.TempDir()
path := filepath.Join(dir, "s.sock")
staleSocketPath(t, path)
// Confirm the stale node is present before we call listen.
_, statErr := os.Stat(path)
require.NoError(t, statErr, "stale socket file should exist on disk")
ln, removedStale, err := listen("unix", path)
require.NoError(t, err)
require.NotNil(t, ln)
require.True(t, removedStale, "listen should report removedStale=true")
t.Cleanup(func() {
_ = ln.Close()
})
}
func TestListen_LiveSocketNotRemoved(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "s.sock")
ln1, err := net.Listen("unix", path) //nolint:noctx
require.NoError(t, err)
// Drain accepts so the listener stays alive and responsive without
// blocking the test on a stray connection.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
c, err := ln1.Accept()
if err != nil {
return
}
_ = c.Close()
}
}()
t.Cleanup(func() {
_ = ln1.Close()
wg.Wait()
})
ln2, removedStale, err := listen("unix", path)
if ln2 != nil {
_ = ln2.Close()
}
require.Error(t, err, "listen on a live socket must fail")
require.False(t, removedStale,
"a live socket must never be removed (got removedStale=true)")
// The live socket file must still be on disk and dialable.
_, statErr := os.Stat(path)
require.NoError(t, statErr, "live socket file should still exist")
conn, dialErr := net.DialTimeout("unix", path, 200*time.Millisecond) //nolint:noctx
require.NoError(t, dialErr, "live socket should still accept dials")
_ = conn.Close()
}
|