You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

34 lines
1.1 KiB

import { describe, expect, test } from "bun:test";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { assertDiskFileIsAllowedRasterImage, IMAGE_MAGIC_MISMATCH_MESSAGE } from "./image-magic-bytes";
describe("assertDiskFileIsAllowedRasterImage", () => {
test("rejects non-image bytes", () => {
const dir = mkdtempSync(join(tmpdir(), "img-magic-"));
try {
const p = join(dir, "x.bin");
writeFileSync(p, Buffer.from("not an image"));
expect(() => assertDiskFileIsAllowedRasterImage(p)).toThrow(IMAGE_MAGIC_MISMATCH_MESSAGE);
} finally {
rmSync(dir, { recursive: true });
}
});
test("accepts jpeg magic prefix", () => {
const dir = mkdtempSync(join(tmpdir(), "img-magic-"));
try {
const p = join(dir, "x.jpg");
const buf = Buffer.alloc(20, 0);
buf[0] = 0xff;
buf[1] = 0xd8;
buf[2] = 0xff;
buf[3] = 0xe0;
writeFileSync(p, buf);
expect(() => assertDiskFileIsAllowedRasterImage(p)).not.toThrow();
} finally {
rmSync(dir, { recursive: true });
}
});
});