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 withPlist(
13 edit: (plist: string) => string,
14 image: Uint8Array = fixture,
15): Uint8Array<ArrayBuffer> {
16 const trailer = image.subarray(image.length - 512);
17 const trailerView = new DataView(trailer.buffer, trailer.byteOffset, trailer.byteLength);
18 const plistOffset = Number(trailerView.getBigUint64(216));
19 const plistLength = Number(trailerView.getBigUint64(224));
20 const plist = edit(
21 new TextDecoder().decode(image.subarray(plistOffset, plistOffset + plistLength)),
22 );
23 const plistBytes = new TextEncoder().encode(plist);
24 const copy = new Uint8Array(plistOffset + plistBytes.length + 512);
25 copy.set(image.subarray(0, plistOffset), 0);
26 copy.set(plistBytes, plistOffset);
27 copy.set(trailer, plistOffset + plistBytes.length);
28 new DataView(copy.buffer, plistOffset + plistBytes.length, 512).setBigUint64(
29 224,
30 BigInt(plistBytes.length),
31 );
32 return copy;
33}
34
35const withPartitionName = (name: string) =>
36 withPlist((plist) => plist.replaceAll("disk image (Apple_HFS : 4)", name));
37
38/** A 4-sector partition stored as one bzip2 chunk, which the reader does not decompress. */
39function bzip2BlockTable(): string {
40 const table = new Uint8Array(204 + 2 * 40);
41 const view = new DataView(table.buffer);
42 table.set(new TextEncoder().encode("mish"), 0);
43 view.setUint32(200, 2);
44 view.setUint32(204, 0x80000006);
45 view.setBigUint64(204 + 16, 4n);
46 view.setBigUint64(204 + 32, 16n);
47 view.setUint32(244, 0xffffffff);
48 return btoa(String.fromCharCode(...table));
49}
50
51describe("listDiskImage", () => {
52 it("lists an HFS+ volume from a zlib UDIF image and reads its files", async () => {
53 const listing = await listDiskImage(bytesSource(fixture));
54
55 expect(listing.volume).toBe("Demo 1.0");
56 expect(
57 listing.entries
58 .map(({ path, directory, link }) => ({ path, directory, link }))
59 .toSorted((a, b) => a.path.localeCompare(b.path)),
60 ).toEqual([
61 { path: ".background", directory: true, link: undefined },
62 { path: ".background/readme.txt", directory: false, link: undefined },
63 { path: "Applications", directory: false, link: "/Applications" },
64 { path: "Demo.app", directory: true, link: undefined },
65 { path: "Demo.app/Contents", directory: true, link: undefined },
66 { path: "Demo.app/Contents/Info.plist", directory: false, link: undefined },
67 { path: "Demo.app/Contents/MacOS", directory: true, link: undefined },
68 { path: "Demo.app/Contents/MacOS/Demo", directory: false, link: undefined },
69 ]);
70 expect(
71 new TextDecoder().decode(await listing.readFile("Demo.app/Contents/MacOS/Demo", 1024)),
72 ).toBe("#!/bin/sh\necho demo\n");
73 });
74
75 it("skips partitions by their UDIF type, not their volume label", async () => {
76 for (const name of [
77 "ChatGPT 10 (Apple_HFS : 4)",
78 "USB Driver (Apple_HFS : 4)",
79 "Embrace 10 (Apple_HFS : 4)",
80 "OS Patches (Apple_HFS : 4)",
81 "Apple_APFS Guide (Apple_HFS : 4)",
82 "DDM Backup",
83 "GPT Backup",
84 "MBR Archive",
85 "Driver Disk",
86 "OS Patches",
87 ]) {
88 const listing = await listDiskImage(bytesSource(withPartitionName(name)));
89 expect(listing.volume).toBe("Demo 1.0");
90 }
91 for (const name of [
92 "Protective Master Boot Record (MBR : 0)",
93 "GPT Header (Primary GPT Header : 1)",
94 "Driver Descriptor Map (DDM : -1)",
95 "Macintosh (Apple_Driver43 : 2)",
96 "Patch Partition (Apple_Patches : 3)",
97 ]) {
98 await expect(listDiskImage(bytesSource(withPartitionName(name)))).rejects.toMatchObject({
99 code: "disk-image-unsupported",
100 });
101 }
102 await expect(
103 listDiskImage(bytesSource(withPartitionName("Container (Apple_APFS : 2)"))),
104 ).rejects.toMatchObject({ code: "disk-image-apfs" });
105 });
106
107 it("keeps looking for a volume after a partition it cannot read", async () => {
108 const withUnreadableFirst = (volumeName: string) =>
109 withPlist((plist) =>
110 plist
111 .replaceAll("disk image (Apple_HFS : 4)", volumeName)
112 .replace(
113 /(<key>blkx<\/key>\s*<array>)/,
114 `$1<dict><key>CFName</key><string>DDM</string><key>Data</key><data>${bzip2BlockTable()}</data></dict>`,
115 ),
116 );
117
118 const listing = await listDiskImage(bytesSource(withUnreadableFirst("disk image")));
119 expect(listing.volume).toBe("Demo 1.0");
120 await expect(
121 listDiskImage(bytesSource(withUnreadableFirst("free (Apple_Free : 4)"))),
122 ).rejects.toMatchObject({ code: "disk-image-compression-unsupported" });
123 });
124
125 it("keeps looking for a volume after an empty one", async () => {
126 // GPT image: an empty HFS+ volume, then demo.dmg's volume.
127 const image = new Uint8Array(
128 NodeFS.readFileSync(
129 new URL("../../../test/fixtures/disk-images/empty-first-volume.dmg", import.meta.url),
130 ),
131 );
132 const listing = await listDiskImage(bytesSource(image));
133 expect(listing.volume).toBe("Demo 1.0");
134 expect(listing.entries.map(({ path }) => path)).toContain("Demo.app/Contents/MacOS/Demo");
135 await expect(
136 listDiskImage(
137 bytesSource(
138 withPlist(
139 (plist) => plist.replaceAll("Demo (Apple_HFS : 2)", "Demo (Apple_Free : 2)"),
140 image,
141 ),
142 ),
143 ),
144 ).rejects.toMatchObject({
145 code: "archive-empty",
146 });
147 });
148});