2022-10-12 09:47:13 +00:00
|
|
|
import { createSandbox } from "./environments/deno_sandbox.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { System } from "./system.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2022-11-19 15:05:37 +00:00
|
|
|
} from "https://deno.land/std@0.165.0/testing/asserts.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
|
|
|
Deno.test("Run a deno sandbox", async () => {
|
|
|
|
const system = new System("server");
|
2022-04-03 16:42:12 +00:00
|
|
|
system.registerSyscalls([], {
|
2022-10-10 12:50:21 +00:00
|
|
|
addNumbers: (_ctx, a, b) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
return a + b;
|
|
|
|
},
|
|
|
|
failingSyscall: () => {
|
|
|
|
throw new Error("#fail");
|
|
|
|
},
|
|
|
|
});
|
2022-04-03 16:42:12 +00:00
|
|
|
system.registerSyscalls(["restricted"], {
|
2022-03-25 11:03:06 +00:00
|
|
|
restrictedSyscall: () => {
|
|
|
|
return "restricted";
|
|
|
|
},
|
|
|
|
});
|
2022-04-03 16:42:12 +00:00
|
|
|
system.registerSyscalls(["dangerous"], {
|
2022-03-25 11:03:06 +00:00
|
|
|
dangerousSyscall: () => {
|
|
|
|
return "yay";
|
|
|
|
},
|
|
|
|
});
|
2022-10-10 12:50:21 +00:00
|
|
|
const plug = await system.load(
|
2022-03-20 08:56:28 +00:00
|
|
|
{
|
2022-04-26 17:04:36 +00:00
|
|
|
name: "test",
|
2022-03-25 11:03:06 +00:00
|
|
|
requiredPermissions: ["dangerous"],
|
2022-03-20 08:56:28 +00:00
|
|
|
functions: {
|
|
|
|
addTen: {
|
|
|
|
code: `(() => {
|
|
|
|
return {
|
|
|
|
default: (n) => {
|
|
|
|
return n + 10;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
2022-10-28 14:17:40 +00:00
|
|
|
redirectTest: {
|
|
|
|
redirect: "addTen",
|
|
|
|
},
|
|
|
|
redirectTest2: {
|
|
|
|
redirect: "test.addTen",
|
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
addNumbersSyscall: {
|
|
|
|
code: `(() => {
|
|
|
|
return {
|
|
|
|
default: async (a, b) => {
|
2022-03-24 09:48:56 +00:00
|
|
|
return await self.syscall("addNumbers", a, b);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
|
|
|
errorOut: {
|
|
|
|
code: `(() => {
|
|
|
|
return {
|
|
|
|
default: () => {
|
|
|
|
throw Error("BOOM");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
|
|
|
errorOutSys: {
|
|
|
|
code: `(() => {
|
|
|
|
return {
|
|
|
|
default: async () => {
|
2022-03-24 09:48:56 +00:00
|
|
|
await self.syscall("failingSyscall");
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
2022-03-25 11:03:06 +00:00
|
|
|
restrictedTest: {
|
|
|
|
code: `(() => {
|
|
|
|
return {
|
|
|
|
default: async () => {
|
|
|
|
await self.syscall("restrictedSyscall");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
|
|
|
dangerousTest: {
|
|
|
|
code: `(() => {
|
|
|
|
return {
|
|
|
|
default: async () => {
|
|
|
|
return await self.syscall("dangerousSyscall");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
},
|
2022-10-10 12:50:21 +00:00
|
|
|
createSandbox,
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|
2022-10-10 12:50:21 +00:00
|
|
|
assertEquals(await plug.invoke("addTen", [10]), 20);
|
2022-10-28 14:17:40 +00:00
|
|
|
assertEquals(await plug.invoke("redirectTest", [10]), 20);
|
|
|
|
assertEquals(await plug.invoke("redirectTest2", [10]), 20);
|
2022-03-20 08:56:28 +00:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-10-10 12:50:21 +00:00
|
|
|
assertEquals(await plug.invoke("addNumbersSyscall", [10, i]), 10 + i);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
await plug.invoke("errorOut", []);
|
2022-10-10 12:50:21 +00:00
|
|
|
assert(false);
|
2022-03-20 08:56:28 +00:00
|
|
|
} catch (e: any) {
|
2022-10-10 12:50:21 +00:00
|
|
|
assert(e.message.indexOf("BOOM") !== -1);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
await plug.invoke("errorOutSys", []);
|
2022-10-10 12:50:21 +00:00
|
|
|
assert(false);
|
2022-03-20 08:56:28 +00:00
|
|
|
} catch (e: any) {
|
2022-10-10 12:50:21 +00:00
|
|
|
assert(e.message.indexOf("#fail") !== -1);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-03-25 11:03:06 +00:00
|
|
|
try {
|
|
|
|
await plug.invoke("restrictedTest", []);
|
2022-10-10 12:50:21 +00:00
|
|
|
assert(false);
|
2022-03-25 11:03:06 +00:00
|
|
|
} catch (e: any) {
|
2022-10-10 12:50:21 +00:00
|
|
|
assert(
|
|
|
|
e.message.indexOf(
|
|
|
|
"Missing permission 'restricted' for syscall restrictedSyscall",
|
|
|
|
) !== -1,
|
2022-03-25 11:03:06 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
assertEquals(await plug.invoke("dangerousTest", []), "yay");
|
|
|
|
|
|
|
|
await system.unloadAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
import { bundle as plugOsBundle } from "./bin/plugos-bundle.ts";
|
|
|
|
import { esbuild } from "./compile.ts";
|
2023-01-01 19:28:25 +00:00
|
|
|
import { urlToPathname } from "./util.ts";
|
2022-10-13 13:16:18 +00:00
|
|
|
|
2023-01-01 19:28:25 +00:00
|
|
|
const __dirname = urlToPathname(new URL(".", import.meta.url));
|
2022-10-10 12:50:21 +00:00
|
|
|
|
|
|
|
Deno.test("Preload dependencies", async () => {
|
|
|
|
const globalModules = await plugOsBundle(
|
|
|
|
`${__dirname}../plugs/global.plug.yaml`,
|
|
|
|
);
|
|
|
|
const testPlugManifest = await plugOsBundle(
|
|
|
|
`${__dirname}test.plug.yaml`,
|
2022-10-13 13:16:18 +00:00
|
|
|
{
|
|
|
|
imports: [globalModules],
|
|
|
|
},
|
2022-10-10 12:50:21 +00:00
|
|
|
);
|
|
|
|
esbuild.stop();
|
|
|
|
|
|
|
|
const system = new System("server");
|
|
|
|
system.on({
|
2022-11-01 08:57:20 +00:00
|
|
|
sandboxInitialized: async (sandbox) => {
|
2022-10-10 12:50:21 +00:00
|
|
|
for (
|
|
|
|
const [modName, code] of Object.entries(globalModules.dependencies!)
|
|
|
|
) {
|
2022-11-01 08:57:20 +00:00
|
|
|
await sandbox.loadDependency(modName, code as string);
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load test module
|
|
|
|
console.log("Loading test module");
|
|
|
|
const testPlug = await system.load(
|
|
|
|
testPlugManifest,
|
|
|
|
createSandbox,
|
|
|
|
);
|
|
|
|
console.log("Running");
|
|
|
|
|
|
|
|
const result = await testPlug.invoke("boot", []);
|
|
|
|
|
|
|
|
console.log("Result", result);
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
await system.unloadAll();
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|