1package swarm
2
3import (
4 "os"
5 "path/filepath"
6 "runtime"
7 "strings"
8 "testing"
9)
10
11func TestInboxSocketPathUsesRuntimeDirectory(t *testing.T) {
12 if runtime.GOOS == "windows" {
13 t.Skip("swarm inbox transport uses Unix-domain sockets")
14 }
15
16 runtimeDir := shortSocketDir(t)
17 stateRoot := filepath.Join(t.TempDir(), "state")
18 t.Setenv("XDG_RUNTIME_DIR", runtimeDir)
19
20 path, err := inboxSocketPath(stateRoot, "agent-123")
21 if err != nil {
22 t.Fatalf("inboxSocketPath: %v", err)
23 }
24 if !strings.HasPrefix(path, runtimeDir+string(filepath.Separator)) {
25 t.Fatalf("path = %q; want it below runtime dir %q", path, runtimeDir)
26 }
27 if strings.HasPrefix(path, stateRoot+string(filepath.Separator)) {
28 t.Fatalf("path = %q; transient socket must not be below state root %q", path, stateRoot)
29 }
30}
31
32func TestInboxSocketPathFallsBackFromUnusableRuntimeDirectory(t *testing.T) {
33 if runtime.GOOS == "windows" {
34 t.Skip("swarm inbox transport uses Unix-domain sockets")
35 }
36
37 badRuntime := filepath.Join(shortSocketDir(t), "not-a-directory")
38 if err := os.WriteFile(badRuntime, []byte("x"), 0o600); err != nil {
39 t.Fatal(err)
40 }
41 tempDir := shortSocketDir(t)
42 t.Setenv("XDG_RUNTIME_DIR", badRuntime)
43 t.Setenv("TMPDIR", tempDir)
44
45 path, err := inboxSocketPath(t.TempDir(), "agent-456")
46 if err != nil {
47 t.Fatalf("inboxSocketPath: %v", err)
48 }
49 if !strings.HasPrefix(path, tempDir+string(filepath.Separator)) {
50 t.Fatalf("path = %q; want fallback below %q", path, tempDir)
51 }
52}
53
54func TestInboxSocketPathShortensLongAgentID(t *testing.T) {
55 if runtime.GOOS == "windows" {
56 t.Skip("swarm inbox transport uses Unix-domain sockets")
57 }
58
59 t.Setenv("XDG_RUNTIME_DIR", shortSocketDir(t))
60 path, err := inboxSocketPath(t.TempDir(), strings.Repeat("agent", 100))
61 if err != nil {
62 t.Fatalf("inboxSocketPath: %v", err)
63 }
64 if len(path) > maxUnixSocketPath {
65 t.Fatalf("socket path length = %d; want <= %d: %q", len(path), maxUnixSocketPath, path)
66 }
67}