diff --git a/src/artifacts/MacosPkgArtifactReader.test.ts b/src/artifacts/MacosPkgArtifactReader.test.ts index 54e40693bd3..f8263910647 100644 --- a/src/artifacts/MacosPkgArtifactReader.test.ts +++ b/src/artifacts/MacosPkgArtifactReader.test.ts @@ -62,6 +62,8 @@ import { buildTocPastEofPackage, truncatedPkgHeader, buildCpioOdc, + appleDoubleHardLinkMembers, + appleDoubleResourceFork, gzipCpio, buildXar, } from "./macosPkg/MacosPkg.fixture.js"; @@ -171,11 +173,7 @@ it("resolves single-body cpio hard-link aliases and retires completed groups", ( it("keeps AppleDouble bytes out of cpio data bodies when an inode is reused", () => { const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; - // AppleDouble v2, one resource-fork descriptor and one byte of metadata. - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); const contentsOf = (members: Parameters[0]) => { const parsed = parseCpioOdc(buildCpioOdc(members)); if (!parsed.ok) throw parsed.error; @@ -184,14 +182,7 @@ it("keeps AppleDouble bytes out of cpio data bodies when an inode is reused", () ); }; - const combined = contentsOf([ - { ...linked, uid: 1, name: "ordinary", data: Buffer.from("A") }, - { ...linked, uid: 1, name: "._ordinary" }, - { ...linked, name: "._base", data: metadata }, - { ...linked, name: "./base" }, - { ...linked, name: "alias", data: Buffer.from("C") }, - { ...linked, name: "._alias" }, - ]); + const combined = contentsOf(appleDoubleHardLinkMembers(metadata)); expect(combined.get("._ordinary")?.toString()).toBe("A"); expect(combined.get("./base")?.toString()).toBe("C"); expect(combined.get("._alias")?.length).toBe(0); @@ -220,10 +211,7 @@ it("keeps AppleDouble bytes out of cpio data bodies when an inode is reused", () it("preserves an unresolved AppleDouble sidecar at a partial cpio boundary", () => { const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); const archive = buildCpioOdc([ { ...linked, name: "._original", data: metadata }, { ...linked, name: "original", data: Buffer.from("hello") }, @@ -255,10 +243,7 @@ it("resolves a maximum-size cpio hard-link group in one bounded pass", () => { }); it("resolves a large hard-link group with many trailing AppleDouble sidecars", () => { - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); const linked = { mode: 0o100644, device: 7, inode: 12, links: 2048 }; const members = [ ...Array.from({ length: 2048 }, (_, index) => ({ @@ -281,10 +266,7 @@ it("resolves a large hard-link group with many trailing AppleDouble sidecars", ( it("consumes a trailing AppleDouble sidecar so the next hard-link group resolves", () => { const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); const parsed = parseCpioOdc( buildCpioOdc([ { ...linked, name: "a1", data: Buffer.from("A") }, @@ -307,10 +289,7 @@ it("consumes a trailing AppleDouble sidecar so the next hard-link group resolves it("preserves a completed group before an ambiguous empty ._ file on a reused inode", () => { const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); const parsed = parseCpioOdc( buildCpioOdc([ { ...linked, name: "a1", data: Buffer.from("A") }, @@ -358,10 +337,7 @@ it("refuses cpio files with malformed hard-link identity fields", () => { it("retains known files and reports ambiguous reused-inode aliases as unavailable", async () => { const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); const { reader, entries, coverage } = await entriesOf( buildXar({ files: [ diff --git a/src/artifacts/macosPkg/MacosPkg.fixture.ts b/src/artifacts/macosPkg/MacosPkg.fixture.ts index 22129203605..2459e66f873 100644 --- a/src/artifacts/macosPkg/MacosPkg.fixture.ts +++ b/src/artifacts/macosPkg/MacosPkg.fixture.ts @@ -53,6 +53,27 @@ export interface CpioFixtureMember { readonly data?: Buffer; } +// AppleDouble v2, one resource-fork descriptor and one byte of metadata. +export const appleDoubleResourceFork = (): Buffer => + Buffer.from( + "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", + "hex", + ); + +export const appleDoubleHardLinkMembers = ( + metadata: Buffer, +): readonly CpioFixtureMember[] => { + const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; + return [ + { ...linked, uid: 1, name: "ordinary", data: Buffer.from("A") }, + { ...linked, uid: 1, name: "._ordinary" }, + { ...linked, name: "._base", data: metadata }, + { ...linked, name: "./base" }, + { ...linked, name: "alias", data: Buffer.from("C") }, + { ...linked, name: "._alias" }, + ]; +}; + /** Build an uncompressed odc cpio archive, including the TRAILER!!! record. */ export const buildCpioOdc = (members: readonly CpioFixtureMember[]): Buffer => { const records = [ diff --git a/tests/acceptance/macosPkgCli.test.ts b/tests/acceptance/macosPkgCli.test.ts index bd39477e389..e5458b9af84 100644 --- a/tests/acceptance/macosPkgCli.test.ts +++ b/tests/acceptance/macosPkgCli.test.ts @@ -15,6 +15,8 @@ import { import { buildProductArchive, buildXar, + appleDoubleHardLinkMembers, + appleDoubleResourceFork, gzipCpio, truncatedPkgHeader, } from "../../src/artifacts/macosPkg/MacosPkg.fixture.js"; @@ -104,26 +106,14 @@ cliTest( async ({ cli }) => { const root = await createTestTempDirectory("rea-macos-pkg-hardlinks-"); const source = join(root, "hardlinks.pkg"); - const linked = { mode: 0o100644, device: 7, inode: 12, links: 2 }; - // AppleDouble v2, one resource-fork descriptor and one byte of metadata. - const metadata = Buffer.from( - "0005160700020000" + "00".repeat(16) + "00010000000200000026000000017a", - "hex", - ); + const metadata = appleDoubleResourceFork(); await writeFile( source, buildXar({ files: [ { path: "Payload", - bytes: gzipCpio([ - { ...linked, uid: 1, name: "ordinary", data: Buffer.from("A") }, - { ...linked, uid: 1, name: "._ordinary" }, - { ...linked, name: "._base", data: metadata }, - { ...linked, name: "./base" }, - { ...linked, name: "alias", data: Buffer.from("C") }, - { ...linked, name: "._alias" }, - ]), + bytes: gzipCpio(appleDoubleHardLinkMembers(metadata)), }, ], }),