Files
CustosPublic/crates/custos-wasm/js/test.mjs
T

33 lines
1.6 KiB
JavaScript

// node test.mjs <custos_wasm.wasm> [<vault file> <password>]: the JS loader against the real build.
import fs from 'node:fs';
import { loadCustos, toBase64 } from './custos.mjs';
const [wasmPath, vaultPath, password] = process.argv.slice(2);
const c = await loadCustos(fs.readFileSync(wasmPath));
const assert = (ok, msg) => { if (!ok) { console.error('FAIL', msg); process.exit(1); } };
const bad = c.call({ op: 'share_open', blob: 'Q1NIMQA=', fragment: 'k=x' });
assert(!bad.ok && bad.code === 'Malformed', JSON.stringify(bad));
assert(c.call({ op: 'items' }).error === 'The vault is locked.', 'locked');
if (vaultPath) {
const t0 = performance.now();
const r = c.call({ op: 'open', vault: toBase64(fs.readFileSync(vaultPath)), password });
const ms = Math.round(performance.now() - t0);
assert(r.ok, r.error);
console.log(`unlocked ${r.items.length} items in ${ms} ms (WebAssembly, default KDF)`);
const item = r.items.find((i) => i.hasMainSecret);
for (const pw of [null, 'by-phone']) {
const t1 = performance.now();
const s = c.call({ op: 'share_seal', id: item.id, password: pw });
assert(s.ok, s.error);
const o = c.call({ op: 'share_open', blob: s.blob, fragment: s.fragment, password: pw });
assert(o.ok && o.payload.title === item.title, JSON.stringify(o.error));
console.log(`share ${pw ? 'with' : 'without'} password: sealed and opened in ${Math.round(performance.now() - t1)} ms`);
}
const fresh = new Set(Array.from({ length: 100 }, () => c.call({ op: 'share_seal', id: item.id }).fragment));
assert(fresh.size === 100, 'link keys repeat: random_fill is broken');
c.call({ op: 'lock' });
}
console.log('ok');