Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - a plain test that reads one binary fixture.
2import * as NodeFS from "node:fs";
3import { describe, expect, it } from "vite-plus/test";
4
5import { bytesSource, listDiskImage } from "./diskImage";
6
7// `hdiutil create -fs HFS+ -format UDZO`: Demo.app, an Applications symlink and a dot folder.
8const fixture = new Uint8Array(
9 NodeFS.readFileSync(new URL("../../../test/fixtures/disk-images/demo.dmg", import.meta.url)),
10);
11
12function withPartitionName(name: string): Uint8Array<ArrayBuffer> {
13 const trailer = fixture.subarray(fixture.length - 512);
14 const trailerView = new DataView(trailer.buffer, trailer.byteOffset, trailer.byteLength);
15 const plistOffset = Number(trailerView.getBigUint64(216));
16 const plistLength = Number(trailerView.getBigUint64(224));
17 const plist = new TextDecoder()
18 .decode(fixture.subarray(plistOffset, plistOffset + plistLength))
19 .replaceAll("disk image (Apple_HFS : 4)", name);
20 const plistBytes = new TextEncoder().encode(plist);
21 const copy = new Uint8Array(plistOffset + plistBytes.length + 512);
22 copy.set(fixture.subarray(0, plistOffset), 0);
23 copy.set(plistBytes, plistOffset);
24 copy.set(trailer, plistOffset + plistBytes.length);
25 new DataView(copy.buffer, plistOffset + plistBytes.length, 512).setBigUint64(
26 224,
27 BigInt(plistBytes.length),
28 );
29 return copy;
30}
31
32describe("listDiskImage", () => {
33 it("lists an HFS+ volume from a zlib UDIF image and reads its files", async () => {
34 const listing = await listDiskImage(bytesSource(fixture));
35
36 expect(listing.volume).toBe("Demo 1.0");
37 expect(
38 listing.entries
39 .map(({ path, directory, link }) => ({ path, directory, link }))
40 .toSorted((a, b) => a.path.localeCompare(b.path)),
41 ).toEqual([
42 { path: ".background", directory: true, link: undefined },
43 { path: ".background/readme.txt", directory: false, link: undefined },
44 { path: "Applications", directory: false, link: "/Applications" },
45 { path: "Demo.app", directory: true, link: undefined },
46 { path: "Demo.app/Contents", directory: true, link: undefined },
47 { path: "Demo.app/Contents/Info.plist", directory: false, link: undefined },
48 { path: "Demo.app/Contents/MacOS", directory: true, link: undefined },
49 { path: "Demo.app/Contents/MacOS/Demo", directory: false, link: undefined },
50 ]);
51 expect(
52 new TextDecoder().decode(await listing.readFile("Demo.app/Contents/MacOS/Demo", 1024)),
53 ).toBe("#!/bin/sh\necho demo\n");
54 });
55
56 it("skips partitions by their UDIF type, not their volume label", async () => {
57 for (const name of [
58 "ChatGPT 10 (Apple_HFS : 4)",
59 "USB Driver (Apple_HFS : 4)",
60 "Embrace 10 (Apple_HFS : 4)",
61 "OS Patches (Apple_HFS : 4)",
62 "Apple_APFS Guide (Apple_HFS : 4)",
63 ]) {
64 const listing = await listDiskImage(bytesSource(withPartitionName(name)));
65 expect(listing.volume).toBe("Demo 1.0");
66 }
67 for (const name of [
68 "Protective Master Boot Record (MBR : 0)",
69 "GPT Header (Primary GPT Header : 1)",
70 "Driver Descriptor Map (DDM : -1)",
71 "Macintosh (Apple_Driver43 : 2)",
72 "Patch Partition (Apple_Patches : 3)",
73 ]) {
74 await expect(listDiskImage(bytesSource(withPartitionName(name)))).rejects.toMatchObject({
75 code: "disk-image-unsupported",
76 });
77 }
78 await expect(
79 listDiskImage(bytesSource(withPartitionName("Container (Apple_APFS : 2)"))),
80 ).rejects.toMatchObject({ code: "disk-image-apfs" });
81 });
82});