56 lines
5.8 KiB
TypeScript
56 lines
5.8 KiB
TypeScript
import { describe, expect, it, afterEach } from "vitest";
|
|
import { mkdtempSync, realpathSync, renameSync, mkdirSync, rmSync, statSync, chmodSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { WorkspaceFsAtV1 } from "../src/workspaces/workspace-fs-at.js";
|
|
import { CanonicalWorkspaceLockRootInput, VerifiedWorkspaceLockRootLeaseFactory } from "../src/workspaces/workspace-lock-root-lease.js";
|
|
const roots: string[] = [];
|
|
afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
|
|
function factory(sessionsRootFromValidatedInstallationConfig: string) { return new VerifiedWorkspaceLockRootLeaseFactory({ workspaceFsAt: new WorkspaceFsAtV1(), installationId: "i", sessionsRootFromValidatedInstallationConfig, serviceUid: process.getuid!(), provisionedWorkspaceMode: 0o700 }); }
|
|
|
|
describe("retained canonical workspace root", () => {
|
|
it("provisions exact identity, transfers once, and rejects a second owner", async () => {
|
|
const parent = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(parent); const f = factory(parent); const input = f.canonicalInput("abc-workspace");
|
|
const lease = await f.acquireOrProvision(input); const st = statSync(join(parent, "abc-workspace")); expect(st.uid).toBe(process.getuid!()); expect(st.mode & 0o777).toBe(0o700); expect(lease.identity.inode).toBe(BigInt(st.ino));
|
|
const transferred = lease.transfer(); expect(() => lease.transfer()).toThrow(); await transferred.close();
|
|
expect(() => f.canonicalInput("../outside")).toThrow(); expect(() => f.canonicalInput("/tmp/x")).toThrow();
|
|
});
|
|
it("fails closed when the canonical pathname is replaced after retention", async () => {
|
|
const parent = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(parent); const f = factory(parent); const lease = await f.acquireOrProvision(f.canonicalInput("abc-workspace"));
|
|
renameSync(join(parent, "abc-workspace"), join(parent, "old")); mkdirSync(join(parent, "abc-workspace"), { mode: 0o700 });
|
|
const { runUnderWorkspaceWriterLock } = await import("../src/workspaces/preprocessing-state.js"); await expect(runUnderWorkspaceWriterLock(lease, async () => undefined)).rejects.toThrow(/preprocessing/); await lease.close();
|
|
});
|
|
it("does not accept a symlink or wrong ownership/mode root", async () => {
|
|
const parent = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(parent); const other = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-other-"))); roots.push(other); symlinkSync(other, join(parent, "abc-workspace"));
|
|
const f = factory(parent); await expect(f.acquireOrProvision(f.canonicalInput("abc-workspace"))).rejects.toThrow(); rmSync(join(parent, "abc-workspace")); mkdirSync(join(parent, "abc-workspace"), { mode: 0o755 }); await expect(f.acquireOrProvision(f.canonicalInput("abc-workspace"))).rejects.toThrow();
|
|
});
|
|
it("closes every transferred root on partial ordered acquisition", async () => {
|
|
const parent = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(parent); const f = factory(parent); const a = await f.acquireOrProvision(f.canonicalInput("aaa-workspace")); const b = await f.acquireOrProvision(f.canonicalInput("bbb-workspace"));
|
|
const { runUnderOrderedWorkspaceWriterLocks } = await import("../src/workspaces/preprocessing-state.js"); await runUnderOrderedWorkspaceWriterLocks([a, b], async set => { expect(set.workspaceIds).toEqual(["aaa-workspace", "bbb-workspace"]); }); await expect(b.close()).resolves.toBeUndefined();
|
|
});
|
|
it("rejects writer pathname replacement without admitting a concurrent writer", async () => {
|
|
const parent = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(parent);
|
|
const f = factory(parent); const first = await f.acquireOrProvision(f.canonicalInput("abc-workspace"));
|
|
const second = await f.acquireOrProvision(f.canonicalInput("abc-workspace"));
|
|
const { runUnderWorkspaceWriterLock } = await import("../src/workspaces/preprocessing-state.js");
|
|
const outcome = runUnderWorkspaceWriterLock(first, async () => {
|
|
renameSync(join(parent, "abc-workspace", "writer.lock"), join(parent, "abc-workspace", "writer.lock.old"));
|
|
writeFileSync(join(parent, "abc-workspace", "writer.lock"), "", { mode: 0o600 });
|
|
await expect(runUnderWorkspaceWriterLock(second, async () => "entered")).rejects.toThrow(/preprocessing/);
|
|
return "done";
|
|
});
|
|
await expect(outcome).rejects.toThrow(/preprocessing/);
|
|
});
|
|
|
|
it("rejects forged canonical inputs even when the prototype is copied", async () => {
|
|
const parent = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(parent); const f = factory(parent);
|
|
const forged = Object.assign(Object.create(CanonicalWorkspaceLockRootInput.prototype), { workspaceId: "abc-workspace" });
|
|
await expect(f.acquireOrProvision(forged as CanonicalWorkspaceLockRootInput)).rejects.toThrow(/preprocessing/);
|
|
});
|
|
it("rejects symlink sessions roots and special permission bits", () => {
|
|
const base = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-root-"))); roots.push(base); const target = realpathSync(mkdtempSync(join(process.platform === "darwin" ? "/private/tmp" : tmpdir(), "thoth-target-"))); roots.push(target);
|
|
const link = join(base, "sessions"); symlinkSync(target, link); expect(() => factory(link)).toThrow();
|
|
chmodSync(base, 0o1700); expect(() => factory(base)).toThrow();
|
|
});
|
|
});
|