2022-03-23 14:41:12 +00:00
|
|
|
import { createSandbox } from "./environment/node_sandbox";
|
|
|
|
import { expect, test } from "@jest/globals";
|
|
|
|
import { System } from "./system";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
test("Run a Node sandbox", async () => {
|
2022-03-23 14:41:12 +00:00
|
|
|
let system = new System("server");
|
2022-03-25 11:03:06 +00:00
|
|
|
system.registerSyscalls("", [], {
|
|
|
|
addNumbers: (ctx, a, b) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
return a + b;
|
|
|
|
},
|
|
|
|
failingSyscall: () => {
|
|
|
|
throw new Error("#fail");
|
|
|
|
},
|
|
|
|
});
|
2022-03-25 11:03:06 +00:00
|
|
|
system.registerSyscalls("", ["restricted"], {
|
|
|
|
restrictedSyscall: () => {
|
|
|
|
return "restricted";
|
|
|
|
},
|
|
|
|
});
|
|
|
|
system.registerSyscalls("", ["dangerous"], {
|
|
|
|
dangerousSyscall: () => {
|
|
|
|
return "yay";
|
|
|
|
},
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
let plug = await system.load(
|
|
|
|
"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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()`,
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
hooks: {
|
|
|
|
events: {},
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 11:03:06 +00:00
|
|
|
createSandbox
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|
|
|
|
expect(await plug.invoke("addTen", [10])).toBe(20);
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
expect(await plug.invoke("addNumbersSyscall", [10, i])).toBe(10 + i);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await plug.invoke("errorOut", []);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (e: any) {
|
|
|
|
expect(e.message).toBe("BOOM");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await plug.invoke("errorOutSys", []);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (e: any) {
|
|
|
|
expect(e.message).toBe("#fail");
|
|
|
|
}
|
2022-03-25 11:03:06 +00:00
|
|
|
try {
|
|
|
|
await plug.invoke("restrictedTest", []);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (e: any) {
|
|
|
|
expect(e.message).toBe(
|
|
|
|
"Missing permission 'restricted' for syscall restrictedSyscall"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
expect(await plug.invoke("dangerousTest", [])).toBe("yay");
|
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
await system.unloadAll();
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|