Files
suaveplan/packages/security-stack/security/tpm-struct/src/extra-data/extra-data.test.ts
2026-05-27 23:47:53 +01:00

50 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { extraDataMatches } from "./extra-data.js";
function bytewise(a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) return false;
let acc = 0;
for (let i = 0; i < a.length; i++)
acc |= (a[i] as number) ^ (b[i] as number);
return acc === 0;
}
describe("extraDataMatches", () => {
it("returns true for equal buffers", { timeout: 5000 }, () => {
const a = new Uint8Array([1, 2, 3, 4]);
const b = new Uint8Array([1, 2, 3, 4]);
expect(extraDataMatches(a, b, bytewise)).toBe(true);
});
it("returns false for differing content (same length)", {
timeout: 5000,
}, () => {
const a = new Uint8Array([1, 2, 3, 4]);
const b = new Uint8Array([1, 2, 3, 5]);
expect(extraDataMatches(a, b, bytewise)).toBe(false);
});
it("returns false on length mismatch without calling equal", {
timeout: 5000,
}, () => {
let called = false;
const fn = () => {
called = true;
return true;
};
const out = extraDataMatches(
new Uint8Array([1, 2, 3]),
new Uint8Array([1, 2]),
fn
);
expect(out).toBe(false);
expect(called).toBe(false);
});
it("returns true on empty buffers", { timeout: 5000 }, () => {
expect(
extraDataMatches(new Uint8Array(0), new Uint8Array(0), bytewise)
).toBe(true);
});
});