Files
suaveplan/packages/security-stack/security/x509/src/pem/coverage.test.ts
2026-05-27 23:47:53 +01:00

52 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { decodePem, encodePem } from "./pem.js";
describe("PEM coverage", () => {
it("rejects empty body inside a BEGIN/END envelope", {
timeout: 5000,
}, () => {
expect(() =>
decodePem(
"-----BEGIN CERTIFICATE-----\n\n-----END CERTIFICATE-----\n"
)
).toThrow();
});
it("rejects base64 body with bad character", { timeout: 5000 }, () => {
expect(() =>
decodePem(
"-----BEGIN CERTIFICATE-----\nAAAA*BAD\n-----END CERTIFICATE-----\n"
)
).toThrow();
});
it("rejects base64 body whose length is not a multiple of 4", {
timeout: 5000,
}, () => {
expect(() =>
decodePem(
"-----BEGIN CERTIFICATE-----\nAAA\n-----END CERTIFICATE-----\n"
)
).toThrow();
});
it("rejects encodePem with a label containing lowercase chars", {
timeout: 5000,
}, () => {
expect(() =>
encodePem("bad-label", new Uint8Array([1, 2, 3]))
).toThrow();
});
it("round-trips a long bytes-array across wrapped lines", {
timeout: 5000,
}, () => {
const bytes = new Uint8Array(200).fill(0x42);
const pem = encodePem("CERTIFICATE", bytes);
const back = decodePem(pem);
expect(Array.from(back[0]?.bytes ?? new Uint8Array())).toEqual(
Array.from(bytes)
);
});
});