|
| 1 | +// System systemd ownership tests cover loaded, installed, and unverifiable states. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +const state = vi.hoisted(() => ({ |
| 5 | + systemctl: { stdout: "not-found\n", stderr: "", code: 0 }, |
| 6 | + managerUnitPath: { |
| 7 | + stdout: |
| 8 | + "/etc/systemd/system /run/systemd/system /usr/local/lib/systemd/system /usr/lib/systemd/system\n", |
| 9 | + stderr: "", |
| 10 | + code: 0, |
| 11 | + }, |
| 12 | + paths: new Set<string>(), |
| 13 | + pathErrors: new Map<string, string>(), |
| 14 | +})); |
| 15 | + |
| 16 | +function fsError(code: string, target: string): NodeJS.ErrnoException { |
| 17 | + return Object.assign(new Error(`${code}: ${target}`), { code }); |
| 18 | +} |
| 19 | + |
| 20 | +vi.mock("node:fs/promises", () => { |
| 21 | + const mocked = { |
| 22 | + lstat: vi.fn(async (target: string) => { |
| 23 | + const code = state.pathErrors.get(target); |
| 24 | + if (code) { |
| 25 | + throw fsError(code, target); |
| 26 | + } |
| 27 | + if (!state.paths.has(target)) { |
| 28 | + throw fsError("ENOENT", target); |
| 29 | + } |
| 30 | + return {}; |
| 31 | + }), |
| 32 | + }; |
| 33 | + return { ...mocked, default: mocked }; |
| 34 | +}); |
| 35 | + |
| 36 | +const execFileUtf8 = vi.hoisted(() => |
| 37 | + vi.fn(async (_command: string, args: string[]) => |
| 38 | + args.includes("--property=UnitPath") ? state.managerUnitPath : state.systemctl, |
| 39 | + ), |
| 40 | +); |
| 41 | +vi.mock("./exec-file.js", () => ({ execFileUtf8 })); |
| 42 | + |
| 43 | +import { assertNoSystemSystemdOwnership } from "./systemd-system.js"; |
| 44 | + |
| 45 | +describe("system systemd ownership", () => { |
| 46 | + const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + vi.clearAllMocks(); |
| 50 | + state.systemctl = { stdout: "not-found\n", stderr: "", code: 0 }; |
| 51 | + state.managerUnitPath = { |
| 52 | + stdout: |
| 53 | + "/etc/systemd/system /run/systemd/system /usr/local/lib/systemd/system /usr/lib/systemd/system\n", |
| 54 | + stderr: "", |
| 55 | + code: 0, |
| 56 | + }; |
| 57 | + state.paths.clear(); |
| 58 | + state.pathErrors.clear(); |
| 59 | + execFileUtf8.mockReset(); |
| 60 | + execFileUtf8.mockImplementation(async (_command: string, args: string[]) => |
| 61 | + args.includes("--property=UnitPath") ? state.managerUnitPath : state.systemctl, |
| 62 | + ); |
| 63 | + if (originalPlatformDescriptor) { |
| 64 | + Object.defineProperty(process, "platform", { |
| 65 | + ...originalPlatformDescriptor, |
| 66 | + value: "linux", |
| 67 | + }); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + if (originalPlatformDescriptor) { |
| 73 | + Object.defineProperty(process, "platform", originalPlatformDescriptor); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it("reports a unit loaded by the system manager", async () => { |
| 78 | + state.systemctl = { stdout: "loaded\n", stderr: "", code: 0 }; |
| 79 | + |
| 80 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 81 | + ownership: { status: "loaded", unitName: "openclaw-gateway.service" }, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it.each([ |
| 86 | + "/etc/systemd/system/openclaw-gateway.service", |
| 87 | + "/run/systemd/system/openclaw-gateway.service", |
| 88 | + "/usr/local/lib/systemd/system/openclaw-gateway.service", |
| 89 | + ])("detects a custom same-name system unit at %s", async (unitPath) => { |
| 90 | + state.paths.add(unitPath); |
| 91 | + |
| 92 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 93 | + ownership: { |
| 94 | + status: "installed", |
| 95 | + unitName: "openclaw-gateway.service", |
| 96 | + unitPath, |
| 97 | + }, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("ignores differently named profile and custom units", async () => { |
| 102 | + state.paths.add("/etc/systemd/system/openclaw-gateway-rescue.service"); |
| 103 | + state.paths.add("/etc/systemd/system/vendor-openclaw.service"); |
| 104 | + |
| 105 | + await expect( |
| 106 | + assertNoSystemSystemdOwnership("openclaw-gateway-primary.service"), |
| 107 | + ).resolves.toBeUndefined(); |
| 108 | + expect(execFileUtf8).toHaveBeenCalledTimes(3); |
| 109 | + }); |
| 110 | + |
| 111 | + it("fails closed when the system manager cannot be queried", async () => { |
| 112 | + state.systemctl = { |
| 113 | + stdout: "", |
| 114 | + stderr: "Failed to connect to bus: Permission denied", |
| 115 | + code: 1, |
| 116 | + }; |
| 117 | + |
| 118 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 119 | + ownership: { |
| 120 | + status: "unverifiable", |
| 121 | + unitName: "openclaw-gateway.service", |
| 122 | + operation: "systemctl", |
| 123 | + detail: "Failed to connect to bus: Permission denied", |
| 124 | + }, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does not mistake a missing system bus for a missing unit", async () => { |
| 129 | + state.systemctl = { |
| 130 | + stdout: "", |
| 131 | + stderr: "Failed to connect to bus: No such file or directory", |
| 132 | + code: 1, |
| 133 | + }; |
| 134 | + |
| 135 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 136 | + ownership: { |
| 137 | + status: "unverifiable", |
| 138 | + operation: "systemctl", |
| 139 | + detail: "Failed to connect to bus: No such file or directory", |
| 140 | + }, |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it("fails closed when an exact system path cannot be inspected", async () => { |
| 145 | + const unitPath = "/etc/systemd/system/openclaw-gateway.service"; |
| 146 | + state.pathErrors.set(unitPath, "EACCES"); |
| 147 | + |
| 148 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 149 | + ownership: { |
| 150 | + status: "unverifiable", |
| 151 | + unitName: "openclaw-gateway.service", |
| 152 | + operation: "filesystem", |
| 153 | + detail: `${unitPath}: EACCES: ${unitPath}`, |
| 154 | + }, |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it("fails closed when manager unit load paths cannot be enumerated", async () => { |
| 159 | + state.managerUnitPath = { |
| 160 | + stdout: "", |
| 161 | + stderr: "manager UnitPath unavailable", |
| 162 | + code: 1, |
| 163 | + }; |
| 164 | + |
| 165 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 166 | + ownership: { |
| 167 | + status: "unverifiable", |
| 168 | + operation: "systemctl", |
| 169 | + detail: "manager UnitPath unavailable", |
| 170 | + }, |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + it("rechecks the system manager after a negative filesystem snapshot", async () => { |
| 175 | + let systemctlCalls = 0; |
| 176 | + execFileUtf8.mockImplementation(async (_command: string, args: string[]) => { |
| 177 | + if (args.includes("--property=UnitPath")) { |
| 178 | + return state.managerUnitPath; |
| 179 | + } |
| 180 | + systemctlCalls += 1; |
| 181 | + return systemctlCalls === 1 |
| 182 | + ? { stdout: "not-found\n", stderr: "", code: 0 } |
| 183 | + : { stdout: "loaded\n", stderr: "", code: 0 }; |
| 184 | + }); |
| 185 | + |
| 186 | + await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({ |
| 187 | + ownership: { status: "loaded", unitName: "openclaw-gateway.service" }, |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + it.each([ |
| 192 | + { uid: 1000, prefix: "sudo " }, |
| 193 | + { uid: 0, prefix: "" }, |
| 194 | + ])("renders actionable ownership recovery for uid $uid", async ({ uid, prefix }) => { |
| 195 | + const existingGeteuid = Object.getOwnPropertyDescriptor(process, "geteuid"); |
| 196 | + Object.defineProperty(process, "geteuid", { |
| 197 | + configurable: true, |
| 198 | + value: () => uid, |
| 199 | + }); |
| 200 | + state.paths.add("/etc/systemd/system/openclaw-gateway.service"); |
| 201 | + |
| 202 | + try { |
| 203 | + const error = await assertNoSystemSystemdOwnership("openclaw-gateway.service").catch( |
| 204 | + (caught: unknown) => caught, |
| 205 | + ); |
| 206 | + |
| 207 | + expect(error).toMatchObject({ |
| 208 | + name: "SystemSystemdOwnershipError", |
| 209 | + code: "SYSTEM_SYSTEMD_OWNERSHIP", |
| 210 | + ownership: { status: "installed" }, |
| 211 | + }); |
| 212 | + expect(String(error)).toContain("--force does not override system ownership"); |
| 213 | + expect(String(error)).toContain(`${prefix}systemctl disable --now openclaw-gateway.service`); |
| 214 | + expect(String(error)).toContain(`${prefix}rm /etc/systemd/system/openclaw-gateway.service`); |
| 215 | + } finally { |
| 216 | + if (existingGeteuid) { |
| 217 | + Object.defineProperty(process, "geteuid", existingGeteuid); |
| 218 | + } else { |
| 219 | + Reflect.deleteProperty(process, "geteuid"); |
| 220 | + } |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + it("does not recommend deleting package- or generator-owned units", async () => { |
| 225 | + state.paths.add("/usr/lib/systemd/system/openclaw-gateway.service"); |
| 226 | + |
| 227 | + const error = await assertNoSystemSystemdOwnership("openclaw-gateway.service").catch( |
| 228 | + (caught: unknown) => caught, |
| 229 | + ); |
| 230 | + |
| 231 | + expect(String(error)).toContain("uninstall or reconfigure the package, generator"); |
| 232 | + expect(String(error)).not.toContain("rm /usr/lib/systemd/system/openclaw-gateway.service"); |
| 233 | + }); |
| 234 | +}); |
0 commit comments