Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
376 changes: 376 additions & 0 deletions packages/code/src/checkpoint-directory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,376 @@
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import {
chmod,
mkdir,
mkdtemp,
readdir,
rename,
rm,
stat,
symlink,
writeFile,
} from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';
import test from 'node:test';
import type { TestContext } from 'node:test';

import { withCheckpointDirectory } from './checkpoint-directory.js';
import { ProcessLockBusyError } from './process-lock.js';

const linux = process.platform === 'linux';
const options = { skip: !linux, timeout: 10_000 };
const configuredRoot = process.env.CHECKPOINT_TEST_ROOT;
const base = configuredRoot ?? homedir();
const unsupported = process.env.CHECKPOINT_TEST_UNSUPPORTED_ROOT;
const admit = async () => () => {};
const unexpected = async (): Promise<never> =>
assert.fail('Unexpected callback');

async function fixture(t: TestContext, root = base): Promise<string> {
const path = await mkdtemp(join(root, '.checkpoint-reservation-'));
t.after(() => rm(path, { recursive: true, force: true }));
return path;
}

const unsupportedFilesystem =
/^Checkpoint directory requires a local ext4 or XFS filesystem$/;

async function supportedFixture(t: TestContext): Promise<string | undefined> {
const path = await fixture(t);
try {
await assertReusable(path);
return path;
} catch (error) {
if (
configuredRoot ||
!(error instanceof Error) ||
!unsupportedFilesystem.test(error.message)
)
throw error;
t.skip(
'Default fixture filesystem does not support checkpoint reservations'
);
}
}

async function assertReusable(path: string): Promise<void> {
assert.equal(
await withCheckpointDirectory(path, admit, async fd => {
const pinned = await stat(fd, { bigint: true });
const current = await stat(path, { bigint: true });
assert.equal(pinned.dev, current.dev);
assert.equal(pinned.ino, current.ino);
return 'reserved';
}),
'reserved'
);
}

test(
'checkpoint reservation refuses unsupported platforms',
{ skip: linux },
async () => {
await assert.rejects(
withCheckpointDirectory('/unused', unexpected, unexpected),
/requires Linux/
);
}
);

test(
'checkpoint reservation bounds canonical absolute path input',
options,
async () => {
for (const path of [
'',
'/',
'relative',
'/a//b',
'/a/',
'/./a',
'/a/../b',
'/a\0b',
'/\ud800',
'/' + 'a'.repeat(4096),
'/' + 'é'.repeat(2048),
'/' + 'é'.repeat(128),
'/' + Array(65).fill('a').join('/'),
]) {
await assert.rejects(
withCheckpointDirectory(path, unexpected, unexpected),
/Invalid checkpoint directory path/
);
}
}
);

test(
'checkpoint reservation rejects untrusted entries without creating or changing them',
options,
async t => {
const root = await fixture(t);
const store = join(root, 'store');
await assert.rejects(
withCheckpointDirectory(store, unexpected, unexpected),
{ code: 'ENOENT' }
);
await writeFile(store, 'fixture', { mode: 0o700 });
await assert.rejects(
withCheckpointDirectory(store, unexpected, unexpected),
{ code: 'ENOTDIR' }
);
await rm(store);
await mkdir(store, { mode: 0o700 });
const alias = join(root, 'alias');
await symlink(store, alias);
await assert.rejects(
withCheckpointDirectory(alias, unexpected, unexpected),
{ code: 'ENOTDIR' }
);
const ancestor = join(root, 'parent');
await symlink(root, ancestor);
await assert.rejects(
withCheckpointDirectory(
join(ancestor, 'store'),
unexpected,
unexpected
),
{ code: 'ENOTDIR' }
);
for (const mode of [0o750, 0o2700]) {
await chmod(store, mode);
await assert.rejects(
withCheckpointDirectory(store, unexpected, unexpected),
/mode 0700/
);
assert.equal((await stat(store)).mode & 0o7777, mode);
}
await chmod(store, 0o700);
for (const mode of [0o720, 0o1777]) {
await chmod(root, mode);
await assert.rejects(
withCheckpointDirectory(store, unexpected, unexpected),
/Untrusted checkpoint directory ancestor/
);
}
await chmod(root, 0o700);
assert.deepEqual(await readdir(store), []);
}
);

test(
'checkpoint reservation rejects a non-local-ext4/XFS fixture before the verifier',
{
...options,
skip: !linux,
},
async t => {
const path = await fixture(t, unsupported);
let admitted = false;
try {
await withCheckpointDirectory(path, admit, async () => {
admitted = true;
});
} catch (error) {
assert.match(
String(error instanceof Error ? error.message : error),
unsupportedFilesystem
);
return;
}
assert.equal(admitted, true);
if (unsupported)
assert.fail('Configured unsupported fixture was admitted');
t.skip(
'Default fixture is supported; set CHECKPOINT_TEST_UNSUPPORTED_ROOT to test rejection'
);
}
);

test(
'checkpoint reservation unwinds verifier, operation and release failures',
options,
async t => {
const path = await supportedFixture(t);
if (!path) return;
const failure = new Error('synthetic failure');
await assert.rejects(
withCheckpointDirectory(
path,
async () => {
await assert.rejects(
withCheckpointDirectory(path, unexpected, unexpected),
ProcessLockBusyError
);
throw failure;
},
unexpected
),
error => error === failure
);
await assertReusable(path);

let borrowed: string | undefined;
let released = false;
await assert.rejects(
withCheckpointDirectory(
path,
async directory => {
const current = await stat(path, { bigint: true });
assert.equal(directory.path, path);
assert.equal(directory.dev, current.dev);
assert.equal(directory.ino, current.ino);
assert.match(directory.mountId, /^\d+$/);
return async () => {
await assert.rejects(
withCheckpointDirectory(
path,
unexpected,
unexpected
),
ProcessLockBusyError
);
released = true;
};
},
async fd => {
borrowed = fd;
throw failure;
}
),
error => error === failure
);
assert.ok(released);
assert.ok(borrowed);
await assert.rejects(stat(borrowed), { code: 'ENOENT' });
await assertReusable(path);

await assert.rejects(
withCheckpointDirectory(
path,
async () => () => {
throw failure;
},
async () => {}
),
error => error === failure
);
await assertReusable(path);
}
);

test(
'checkpoint reservation revalidates the pinned directory after isolation and releases pins on mismatch',
options,
async t => {
const root = await supportedFixture(t);
if (!root) return;
const path = join(root, 'store');
const moved = join(root, 'moved');
await mkdir(path, { mode: 0o700 });
let released = false;
await assert.rejects(
withCheckpointDirectory(
path,
async () => {
await rename(path, moved);
await mkdir(path, { mode: 0o700 });
return () => {
released = true;
};
},
unexpected
),
/changed during reservation/
);
assert.ok(released);
await assertReusable(moved);
await assertReusable(path);

released = false;
await assert.rejects(
withCheckpointDirectory(
path,
async () => {
await chmod(path, 0o750);
return () => {
released = true;
};
},
unexpected
),
/mode 0700/
);
assert.ok(released);
await chmod(path, 0o700);
await assertReusable(path);

released = false;
await assert.rejects(
withCheckpointDirectory(
path,
async () => {
await chmod(root, 0o720);
return () => {
released = true;
};
},
unexpected
),
/Untrusted checkpoint directory ancestor/
);
assert.ok(released);
await chmod(root, 0o700);
await assertReusable(path);
}
);

test(
'checkpoint reservation contends across processes during the async operation and recovers after process exit',
options,
async t => {
const path = await supportedFixture(t);
if (!path) return;
const url = new URL('./checkpoint-directory.js', import.meta.url).href;
const child = spawn(
process.execPath,
[
'--input-type=module',
'-e',
`
import { setTimeout as delay } from 'node:timers/promises';
import { withCheckpointDirectory } from ${JSON.stringify(url)};
await withCheckpointDirectory(process.argv[1], async () => () => {}, async () => {
process.send('reserved');
await delay(30_000);
});
`,
path,
],
{ stdio: ['ignore', 'ignore', 'inherit', 'ipc'] }
);
const closed = once(child, 'close');
t.after(async () => {
child.kill('SIGKILL');
await closed;
});
const [ready] = await Promise.race([
once(child, 'message', { signal: t.signal }),
closed.then(([code, signal]) =>
assert.fail(`Owner exited before reserving: ${code}/${signal}`)
),
]);
assert.equal(ready, 'reserved');
await assert.rejects(
withCheckpointDirectory(path, unexpected, unexpected),
ProcessLockBusyError
);
child.kill('SIGKILL');
assert.deepEqual(await closed, [null, 'SIGKILL']);
await assertReusable(path);
assert.deepEqual(await readdir(path), []);
}
);
Loading
Loading