The eight Go test functions H2 added become subtests of existing tests (catalogue's state test, the bundle's state test, the views test, the process state test, the testbus bus test, the seal byte-for-byte test), and four more are folded beside them (two seal, two process watch); the two TypeScript seal tests become one, and two more TypeScript tests are folded into their neighbours. No assertion is lost. Builds: design 47 H2 §819
50 lines
2.9 KiB
TypeScript
50 lines
2.9 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
import { seal, open, sealWith, sealAnonymous, sealAnonymousWith, generateKeyPair, type Box } from "../dist/seal/index.js";
|
|
|
|
// The seals are proven against the Go package's bytes (design 47 H2, H-A-407): the fixtures go/seal's tests make and
|
|
// check on every run.
|
|
const testdata = join(import.meta.dirname, "..", "go", "seal", "testdata");
|
|
const fixture = (name: string) => JSON.parse(readFileSync(join(testdata, name), "utf8"));
|
|
|
|
// ADR 0206's box: every box Go's seal opens (the licence manager's, the agent module's TypeScript) opens here; given
|
|
// the same one-time key and iv, seal makes Go's box byte for byte, so go/seal's Open opens it; another key and a
|
|
// changed field are refused.
|
|
test("seal and open make and open go/seal's box, and sealAnonymous Go's anonymous box, byte for byte", () => {
|
|
const f = fixture("sealed-by-licence-manager.json");
|
|
for (const s of f.sealed as { plaintext: string; box: Box }[]) {
|
|
assert.equal(open(s.box, f.recipient.privateKey), s.plaintext);
|
|
}
|
|
for (const v of f.vectors as { plaintext: string; ephPrivate: string; iv: string; box: Box }[]) {
|
|
const got = sealWith(v.plaintext, f.recipient.publicKey, Buffer.from(v.ephPrivate, "base64"), Buffer.from(v.iv, "base64"));
|
|
assert.deepEqual(got, v.box);
|
|
}
|
|
const ts = fixture("sealed-by-typescript.json");
|
|
assert.equal(open(ts.box, ts.privateKey), ts.plaintext);
|
|
|
|
const a = generateKeyPair();
|
|
const b = generateKeyPair();
|
|
const box = seal("a token", a.publicKey);
|
|
assert.equal(open(box, a.privateKey), "a token");
|
|
assert.notDeepEqual(seal("a token", a.publicKey), box);
|
|
assert.throws(() => open(box, b.privateKey), /does not open with this key/);
|
|
assert.throws(() => open({ ...box, ct: Buffer.from("changed").toString("base64") }, a.privateKey), /does not open/);
|
|
assert.throws(() => open({ ...box, v: 2 }, a.privateKey), /version 2/);
|
|
|
|
// The NaCl anonymous box the vault's intake opens: given the same one-time key, sealAnonymous makes what
|
|
// golang.org/x/crypto/nacl/box's SealAnonymous made, byte for byte; a fresh one differs and is as long.
|
|
const an = fixture("sealed-anonymous-by-go.json");
|
|
assert.ok(an.vectors.length > 0);
|
|
for (const v of an.vectors as { plaintext: string; ephSecret: string; sealed: string }[]) {
|
|
assert.equal(sealAnonymousWith(v.plaintext, an.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
|
assert.equal(sealAnonymousWith(Buffer.from(v.plaintext, "utf8"), an.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
|
}
|
|
const fresh = sealAnonymous("a token", an.recipient.public);
|
|
assert.notEqual(fresh, sealAnonymous("a token", an.recipient.public));
|
|
assert.equal(Buffer.from(fresh, "base64").length, 32 + 16 + "a token".length);
|
|
assert.throws(() => sealAnonymous("x", "c2hvcnQ="), /not a raw X25519 public key/);
|
|
});
|