35 lines
1.5 KiB
JavaScript
35 lines
1.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { execFileSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import test from "node:test";
|
|
import { crc32, restorePacket } from "../web/codec.mjs";
|
|
|
|
const fixtures = JSON.parse(execFileSync(process.env.PYTHON || "python", [fileURLToPath(new URL("./codec_fixtures.py", import.meta.url))], { encoding: "utf8" }));
|
|
const fresh = fixture => Uint8Array.from(Buffer.from(fixture.packet, "base64")).buffer;
|
|
|
|
test("CRC32 matches the standard check vector", () => {
|
|
assert.equal(crc32(new TextEncoder().encode("123456789")), 0xcbf43926);
|
|
});
|
|
|
|
test("Python packets restore byte-for-byte for RGB, RGBA, grayscale, PNG and JPEG", () => {
|
|
for (const fixture of fixtures) {
|
|
const restored = restorePacket(fresh(fixture));
|
|
assert.deepEqual(Buffer.from(restored.bytes), Buffer.from(fixture.plain, "base64"));
|
|
assert.deepEqual([restored.width, restored.height], [fixture.width, fixture.height]);
|
|
}
|
|
});
|
|
|
|
test("corrupt, truncated, unsupported and oversized packets fail explicitly", () => {
|
|
const valid = fixtures[0];
|
|
assert.throws(() => restorePacket(fresh(valid).slice(0, 23)));
|
|
assert.throws(() => restorePacket(fresh(valid).slice(0, -1)));
|
|
for (const offset of [0, 4, 5, 6, 7, 16, 20, 24]) {
|
|
const buffer = fresh(valid);
|
|
new Uint8Array(buffer)[offset] ^= 0xff;
|
|
assert.throws(() => restorePacket(buffer), `mutation at ${offset}`);
|
|
}
|
|
const tooBig = fresh(valid);
|
|
new DataView(tooBig).setUint32(8, 16_777_216);
|
|
assert.throws(() => restorePacket(tooBig));
|
|
});
|