mail-address-semantics/impl/js/test/signing.test.mjs

28 lines
1,016 B
JavaScript
Raw Normal View History

// mas-modes-v0.1-spec/conformance/auth.json, consumed unchanged (CONTRACT.md section 7).
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { signature, verify, AuthError } from "../src/signing.mjs";
const fx = JSON.parse(readFileSync(new URL("../../../mas-modes-v0.1-spec/conformance/auth.json", import.meta.url)));
for (const v of fx.vectors) {
test(`${v.id} signature`, () => {
assert.equal(signature(Buffer.from(v.key_hex, "hex"), v.timestamp, Buffer.from(v.body, "utf8")), v.signature);
});
}
for (const c of fx.verify) {
test(`${c.id} ${c.why}`, () => {
const keys = Object.fromEntries(Object.entries(c.keys).map(([p, k]) => [p, Buffer.from(k, "hex")]));
let got;
try {
got = verify(c.headers, Buffer.from(c.body, "utf8"), keys, c.now);
} catch (e) {
assert.ok(e instanceof AuthError);
got = "error:" + e.code;
}
assert.equal(got, "peer" in c ? c.peer : "error:" + c.error);
});
}