Skip to content

Commit 1ff92f6

Browse files
committed
test(state): reproduce every disappearing SQLite sidecar
1 parent 576717f commit 1ff92f6

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

‎src/state/openclaw-state-db.permissions.test.ts‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const chmodFailHook = vi.hoisted(() => ({
1313
error: undefined as Error | undefined,
1414
calls: 0,
1515
failProbe: true,
16-
failTargetSuffix: undefined as string | undefined,
16+
removeTargetSuffix: undefined as string | undefined,
1717
targets: [] as string[],
1818
}));
1919

@@ -22,11 +22,15 @@ vi.mock("node:fs", async (importOriginal) => {
2222
const chmodSync: typeof actual.chmodSync = ((target: unknown, mode: unknown) => {
2323
chmodFailHook.calls += 1;
2424
chmodFailHook.targets.push(String(target));
25+
if (
26+
chmodFailHook.removeTargetSuffix &&
27+
String(target).endsWith(chmodFailHook.removeTargetSuffix)
28+
) {
29+
// Remove the file after existsSync reaches chmod to reproduce the exact race.
30+
actual.unlinkSync(String(target));
31+
}
2532
const isProbe = String(target).includes(".openclaw-chmod-probe-");
26-
const matchesTarget =
27-
chmodFailHook.failTargetSuffix === undefined ||
28-
String(target).endsWith(chmodFailHook.failTargetSuffix);
29-
if (chmodFailHook.error && matchesTarget && (chmodFailHook.failProbe || !isProbe)) {
33+
if (chmodFailHook.error && (chmodFailHook.failProbe || !isProbe)) {
3034
throw chmodFailHook.error;
3135
}
3236
return (actual.chmodSync as (...args: unknown[]) => unknown)(target, mode);
@@ -59,7 +63,7 @@ describe("state database permission hardening without chmod support", () => {
5963
chmodFailHook.error = undefined;
6064
chmodFailHook.calls = 0;
6165
chmodFailHook.failProbe = true;
62-
chmodFailHook.failTargetSuffix = undefined;
66+
chmodFailHook.removeTargetSuffix = undefined;
6367
chmodFailHook.targets = [];
6468
closeOpenClawStateDatabaseForTest();
6569
if (stateDir) {
@@ -143,26 +147,24 @@ describe("state database permission hardening without chmod support", () => {
143147
);
144148
});
145149

146-
it("opens when a sidecar is deleted between the existence check and the chmod", () => {
147-
// SQLite creates and removes -wal/-shm as connections open and checkpoint,
148-
// so a concurrent opener can delete a sidecar after it is seen to exist.
149-
// ENOENT then means there is nothing left to harden, not a fatal fault.
150-
stateDir = fs.mkdtempSync(join(tmpdir(), "openclaw-state-chmod-"));
151-
const options = { env: { OPENCLAW_STATE_DIR: stateDir } };
152-
openOpenClawStateDatabase(options);
153-
closeOpenClawStateDatabaseForTest();
154-
// Present at the existence check so hardening reaches the chmod call.
155-
const shmPath = join(stateDir, "state", "openclaw.sqlite-shm");
156-
fs.writeFileSync(shmPath, "");
157-
chmodFailHook.error = chmodError("ENOENT");
158-
chmodFailHook.failTargetSuffix = "-shm";
150+
it.each(["-wal", "-shm", "-journal"])(
151+
"opens when the %s sidecar disappears before chmod",
152+
(suffix) => {
153+
stateDir = fs.mkdtempSync(join(tmpdir(), "openclaw-state-chmod-"));
154+
const options = { env: { OPENCLAW_STATE_DIR: stateDir } };
155+
openOpenClawStateDatabase(options);
156+
closeOpenClawStateDatabaseForTest();
157+
const sidecarPath = join(stateDir, "state", `openclaw.sqlite${suffix}`);
158+
fs.writeFileSync(sidecarPath, "");
159+
chmodFailHook.removeTargetSuffix = suffix;
159160

160-
const database = openOpenClawStateDatabase(options);
161+
const database = openOpenClawStateDatabase(options);
161162

162-
expect(database.db.isOpen).toBe(true);
163-
// Guards against a vacuous pass if the sidecar were skipped before chmod.
164-
expect(chmodFailHook.targets.some((target) => target.endsWith("-shm"))).toBe(true);
165-
});
163+
expect(database.db.isOpen).toBe(true);
164+
expect(fs.existsSync(sidecarPath)).toBe(false);
165+
expect(chmodFailHook.targets).toContain(sidecarPath);
166+
},
167+
);
166168

167169
it("rethrows when the main database vanishes between the existence check and the chmod", () => {
168170
// resolveSqliteDatabaseFilePaths lists the unsuffixed database first. Losing
@@ -172,9 +174,7 @@ describe("state database permission hardening without chmod support", () => {
172174
const options = { env: { OPENCLAW_STATE_DIR: stateDir } };
173175
openOpenClawStateDatabase(options);
174176
closeOpenClawStateDatabaseForTest();
175-
chmodFailHook.error = chmodError("ENOENT");
176-
chmodFailHook.failTargetSuffix = "openclaw.sqlite";
177-
chmodFailHook.failProbe = false;
177+
chmodFailHook.removeTargetSuffix = "openclaw.sqlite";
178178

179179
expect(() => openOpenClawStateDatabase(options)).toThrow(/ENOENT/);
180180
// Guards against a vacuous pass if the main file were skipped before chmod.

0 commit comments

Comments
 (0)