import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { cleanupServerJobDirectory, createServerJobDirectory, prepareServerJobWorkspace } from "./server-job-isolation.mjs"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const reportPath = path.join(root, "tests/golden/M13-04B/server-job-workspace-report.json"); const manifestPath = path.join(root, "tests/golden/M13-04B/manifest.json"); const temporary = await fs.mkdtemp(path.join(os.tmpdir(), "m13-04b-check-")); const hashFile = async (file) => crypto.createHash("sha256").update(await fs.readFile(file)).digest("hex"); try { const job = await createServerJobDirectory(temporary, "server:workspace"); const workspace = await prepareServerJobWorkspace(job, new Uint8Array([1, 2, 3, 4])); assert.equal((await fs.stat(workspace.sourceDirectory)).mode & 0o777, 0o555); assert.equal((await fs.stat(workspace.sourcePath)).mode & 0o777, 0o444); assert.equal((await fs.stat(workspace.outputDirectory)).mode & 0o777, 0o700); assert.notEqual(path.dirname(workspace.sourcePath), workspace.outputDirectory); let sourceWrite = "ACCEPTED"; try { await fs.writeFile(workspace.sourcePath, new Uint8Array([9])); } catch (error) { sourceWrite = error?.code ?? "ERROR"; } assert.equal(sourceWrite, "EACCES"); const outputPath = path.join(workspace.outputDirectory, "result.bin"); await fs.writeFile(outputPath, new Uint8Array([7, 8])); assert.deepEqual([...await fs.readFile(outputPath)], [7, 8]); await cleanupServerJobDirectory(job); await assert.rejects(fs.stat(workspace.sourcePath), { code: "ENOENT" }); await assert.rejects(fs.stat(outputPath), { code: "ENOENT" }); const report = { schemaVersion: 1, task: "M13-04B", operation: "SERVER_JOB_SOURCE_READONLY_OUTPUT_ISOLATION", runtime: "NODE_SERVER_FILESYSTEM", sourceDirectoryMode: "0555", sourceFileMode: "0444", outputDirectoryMode: "0700", sourceWrite: "EACCES", outputWrite: "OK", sourceOutputDistinct: true, cleanupNoResidual: true, execution: "DISABLED", nextTask: "M13-04C" }; if (process.env.UPDATE_M13_04B_REPORT === "1") { await fs.mkdir(path.dirname(reportPath), { recursive: true }); await fs.writeFile(reportPath, `${JSON.stringify(report, null, 2)}\n`); } assert.deepEqual(JSON.parse(await fs.readFile(reportPath, "utf8")), report); const manifest = JSON.parse(await fs.readFile(manifestPath, "utf8")); assert.deepEqual({ schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-04B", parentTask: "M13-04A", nextTask: "M13-04C" }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(await hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`); process.stdout.write(`server-job-workspace-ok sourceDir=0555 sourceFile=0444 sourceWrite=EACCES outputDir=0700 outputWrite=OK distinct=1 residual=0 next=${manifest.nextTask}\n`); } finally { await fs.rm(temporary, { recursive: true, force: true }); }