2024-03-16 22:29:24 +08:00
|
|
|
import { createSandbox } from "../../lib/plugos/sandboxes/deno_worker_sandbox.ts";
|
|
|
|
import { System } from "../../lib/plugos/system.ts";
|
2024-10-10 18:52:28 +08:00
|
|
|
import { assertEquals } from "@std/assert";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { compileManifest } from "../compile.ts";
|
|
|
|
import * as esbuild from "esbuild";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { SysCallMapping } from "../../lib/plugos/system.ts";
|
2024-11-14 04:08:24 +08:00
|
|
|
import { fileURLToPath } from "node:url";
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-01-28 21:13:37 +08:00
|
|
|
Deno.test("Run a deno sandbox", {
|
|
|
|
sanitizeResources: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
}, async () => {
|
2022-10-10 20:50:21 +08:00
|
|
|
const system = new System("server");
|
2022-04-04 00:42:12 +08:00
|
|
|
system.registerSyscalls([], {
|
2022-10-10 20:50:21 +08:00
|
|
|
addNumbers: (_ctx, a, b) => {
|
2022-03-20 16:56:28 +08:00
|
|
|
return a + b;
|
|
|
|
},
|
|
|
|
failingSyscall: () => {
|
|
|
|
throw new Error("#fail");
|
|
|
|
},
|
2024-01-15 23:43:12 +08:00
|
|
|
} as SysCallMapping);
|
2022-04-04 00:42:12 +08:00
|
|
|
system.registerSyscalls(["restricted"], {
|
2022-03-25 19:03:06 +08:00
|
|
|
restrictedSyscall: () => {
|
|
|
|
return "restricted";
|
|
|
|
},
|
|
|
|
});
|
2022-04-04 00:42:12 +08:00
|
|
|
system.registerSyscalls(["dangerous"], {
|
2022-03-25 19:03:06 +08:00
|
|
|
dangerousSyscall: () => {
|
|
|
|
return "yay";
|
|
|
|
},
|
|
|
|
});
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
2022-10-13 21:16:18 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
const workerPath = await compileManifest(
|
2024-11-14 04:08:24 +08:00
|
|
|
fileURLToPath(new URL("test_runtime.plug.yaml", import.meta.url)),
|
2023-05-24 02:53:53 +08:00
|
|
|
tempDir,
|
2024-07-30 20:26:02 +08:00
|
|
|
{
|
2024-11-14 04:08:24 +08:00
|
|
|
configPath: fileURLToPath(new URL("../../deno.json", import.meta.url)),
|
2024-07-30 20:26:02 +08:00
|
|
|
},
|
2022-10-10 20:50:21 +08:00
|
|
|
);
|
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
const plug = await system.load(
|
2023-12-07 01:44:48 +08:00
|
|
|
"test",
|
2024-01-15 23:43:12 +08:00
|
|
|
createSandbox(new URL(`file://${workerPath}`)),
|
2022-10-10 20:50:21 +08:00
|
|
|
);
|
|
|
|
|
2024-01-14 20:38:39 +08:00
|
|
|
assertEquals({
|
|
|
|
addedNumbers: 3,
|
|
|
|
yamlMessage: "hello: world\n",
|
|
|
|
}, await plug.invoke("boot", []));
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-01-14 20:38:39 +08:00
|
|
|
await system.unloadAll();
|
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
|
|
|
|
|
|
|
esbuild.stop();
|
2022-03-20 16:56:28 +08:00
|
|
|
});
|