// node --test modules/*/test.mjs (after `cargo wasm`; needs ../atlibs: its JS host and `cargo host`) // Byte-equal to what js/user-data.js wrote (snapshots/), on both engines. import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { test } from "node:test"; import { runModuleRust } from "../../../atlibs/rust/host/conformance.mjs"; import { runModule } from "../../../atlibs/javascript/src/run-module/index.js"; const mod = { ...JSON.parse(readFileSync(new URL("module.json", import.meta.url), "utf8")), module: readFileSync(new URL("../../target/wasm32-unknown-unknown/release/iroh_user_data.wasm", import.meta.url)) }; const snapshot = (name) => readFileSync(new URL(`snapshots/${name}.cloud-config`, import.meta.url), "utf8"); const KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExample me@host"; const ctx = { wasm: { run: async ({ module, entry, input }) => { const x = new WebAssembly.Instance(new WebAssembly.Module(module), {}).exports; const data = new TextEncoder().encode(JSON.stringify(input)); const ptr = x.alloc(data.length); new Uint8Array(x.memory.buffer, ptr, data.length).set(data); const packed = BigInt.asUintN(64, x[entry](ptr, data.length)); return JSON.parse(new TextDecoder().decode(new Uint8Array(x.memory.buffer, Number(packed >> 32n), Number(packed & 0xffffffffn)))); } } }; for (const [engine, run] of [["js", runModule], ["rust", runModuleRust]]) { const ud = async (input) => { const ran = await run(ctx, mod, "iroh_user_data", input, async () => assert.fail("no calls"), { wasm: mod.module }); if (ran.error) throw new Error(ran.error); return ran.result.user_data; }; test(`iroh_user_data (${engine}): the snapshots, ssh only, more files, bad input`, async () => { assert.equal(await ud({ ssh_key: KEY }), snapshot("iroh")); assert.equal(await ud({ ssh_key: KEY + "\n", dumbpipe_version: "0.38.0" }), snapshot("iroh-0.38.0")); const doc = (u) => JSON.parse(u.slice("#cloud-config\n".length)); const ssh = doc(await ud({ ssh_key: KEY, dumbpipe: false })); assert.deepEqual(ssh.write_files.map((f) => f.path), ["/root/.ssh/authorized_keys", "/etc/ssh/sshd_config.d/10-iroh.conf"]); const more = { path: "/etc/x", owner: "root:root", permissions: "0644", content: "x\n" }; assert.deepEqual(doc(await ud({ ssh_key: KEY, write_files: [more] })).write_files.at(-1), more); await assert.rejects(ud({}), /ssh_key/); await assert.rejects(ud({ ssh_key: "nope" }), /ssh_key/); await assert.rejects(ud({ ssh_key: `${KEY}\nssh-rsa AAAA x` }), /ssh_key/); await assert.rejects(ud({ ssh_key: KEY, dumbpipe_version: "1; rm -rf /" }), /dumbpipe_version/); }); }