mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 13:41:17 -05:00
82ce15e20a
A package file's Depends can name a virtual package, such as Chrome's libasound2 on Ubuntu 24.04, which apt refuses to install by name. The capture now records, for each dependency the host has no package of, the installed packages that provide it, read from dpkg-query. The export installs those providers instead and says so in its limitations. When dpkg-query cannot say, the capture records why and the export keeps the declared name. Fixes #15682 Co-authored-by: Anurag Singh <211784907+Anurag-5ingh@users.noreply.github.com>
325 lines
10 KiB
TypeScript
325 lines
10 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { chmod, mkdir, rename, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
|
|
import { DPKG_DEB_OUTPUT } from "../domain/desktop/debianPackageFile.fixture.js";
|
|
import type { DesktopPackage } from "../domain/desktop/desktopCapture.js";
|
|
import { inspectPackageFile } from "./DebianPackageFile.js";
|
|
import { runHostQuery, type QueryOutcome } from "./HostQuery.js";
|
|
|
|
const INSTALLED: DesktopPackage = {
|
|
kind: "deb",
|
|
name: "varicad2026-en",
|
|
version: "1:2.03-1",
|
|
architecture: "amd64",
|
|
sources: [],
|
|
source_origins: [],
|
|
package_lists_present: true,
|
|
};
|
|
|
|
const BYTES = "!<arch>\ndebian-binary\n";
|
|
|
|
/** `dpkg-query -W` on a host where libgl1-mesa-glx is gone and libgl1 provides nothing extra. */
|
|
const INSTALLED_PACKAGES = {
|
|
stdout: "libc6\tinstalled\t\nlibgl1\tinstalled\t\ndpkg\tinstalled\t\n",
|
|
exitCode: 0,
|
|
} as const;
|
|
|
|
/** A package file on disk and a runner that answers dpkg-deb with `reply` and dpkg-query with `packages`, recording each call. */
|
|
const inspect = async (
|
|
reply: QueryOutcome,
|
|
installed: DesktopPackage = INSTALLED,
|
|
packages: QueryOutcome = INSTALLED_PACKAGES,
|
|
) => {
|
|
const folder = await createTestTempDirectory("desktop-deb-");
|
|
const path = join(folder, "varicad2026-en_2.03_amd64.deb");
|
|
await writeFile(path, BYTES);
|
|
const calls: (readonly string[])[] = [];
|
|
const facts = await inspectPackageFile(
|
|
path,
|
|
installed,
|
|
async (command, argv, _signal, options) => {
|
|
calls.push([command, ...argv]);
|
|
const answer = command === "dpkg-query" ? packages : reply;
|
|
return typeof answer === "object" && !options?.preserveStdoutBytes
|
|
? { stdout: answer.stdout, exitCode: answer.exitCode }
|
|
: answer;
|
|
},
|
|
undefined,
|
|
);
|
|
return { path, folder, calls, facts };
|
|
};
|
|
|
|
describe("inspectPackageFile", () => {
|
|
it("records the file's control fields, SHA-256 and size when it holds the installed version", async () => {
|
|
const { path, calls, facts } = await inspect({
|
|
stdout: DPKG_DEB_OUTPUT,
|
|
exitCode: 0,
|
|
});
|
|
|
|
expect(calls).toEqual([
|
|
[
|
|
"dpkg-deb",
|
|
"--field",
|
|
path,
|
|
"Package",
|
|
"Version",
|
|
"Architecture",
|
|
"Depends",
|
|
"Pre-Depends",
|
|
],
|
|
[
|
|
"dpkg-query",
|
|
"-W",
|
|
"-f=${Package}\\t${db:Status-Status}\\t${Provides}\\n",
|
|
],
|
|
]);
|
|
expect(facts).toEqual({
|
|
package_file: {
|
|
name: "varicad2026-en_2.03_amd64.deb",
|
|
sha256: createHash("sha256").update(BYTES).digest("hex"),
|
|
size: BYTES.length,
|
|
package: "varicad2026-en",
|
|
version: "1:2.03-1",
|
|
architecture: "amd64",
|
|
depends: expect.arrayContaining(["libgl1 | libgl1-mesa-glx"]),
|
|
pre_depends: ["dpkg (>= 1.15.6)"],
|
|
dependency_providers: {},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("records the installed packages that provide a dependency the host has no package of", async () => {
|
|
const { facts } = await inspect(
|
|
{
|
|
stdout: [
|
|
"Package: google-chrome-stable",
|
|
"Version: 1:2.03-1",
|
|
"Architecture: amd64",
|
|
"Depends: libasound2 (>= 1.0.17), libc6 (>= 2.17)",
|
|
"",
|
|
].join("\n"),
|
|
exitCode: 0,
|
|
},
|
|
{ ...INSTALLED, name: "google-chrome-stable" },
|
|
{
|
|
stdout:
|
|
"libasound2t64\tinstalled\tlibasound2 (= 1.2.11-1build2)\nlibc6\tinstalled\t\n",
|
|
exitCode: 0,
|
|
},
|
|
);
|
|
|
|
expect(facts.package_file).toMatchObject({
|
|
depends: ["libasound2 (>= 1.0.17)", "libc6 (>= 2.17)"],
|
|
dependency_providers: { libasound2: ["libasound2t64"] },
|
|
});
|
|
});
|
|
|
|
it("records why the providers are unknown when dpkg-query cannot say, keeping the file", async () => {
|
|
const reasons = [];
|
|
for (const packages of [
|
|
"missing",
|
|
{ stdout: "", exitCode: 2 },
|
|
{ stdout: "libc6 installed\n", exitCode: 0 },
|
|
] as const) {
|
|
const { facts } = await inspect(
|
|
{ stdout: DPKG_DEB_OUTPUT, exitCode: 0 },
|
|
INSTALLED,
|
|
packages,
|
|
);
|
|
expect(facts.package_file).toMatchObject({ package: "varicad2026-en" });
|
|
expect(facts.package_file?.dependency_providers).toBeUndefined();
|
|
reasons.push(facts.package_file?.dependency_providers_reason);
|
|
}
|
|
|
|
const consequence =
|
|
"so a dependency the host had no package of is installed by its own name, which apt refuses when it is virtual";
|
|
expect(reasons).toEqual([
|
|
`dpkg-query is not installed, ${consequence}`,
|
|
`dpkg-query could not list the installed packages, ${consequence}`,
|
|
`dpkg-query listed installed packages REA could not read, ${consequence}`,
|
|
]);
|
|
});
|
|
|
|
it("withholds package-file evidence for undecodable control fields", async () => {
|
|
const stdoutBuffer = Buffer.concat([
|
|
Buffer.from(DPKG_DEB_OUTPUT.trimEnd()),
|
|
Buffer.from([0xff, 0x0a]),
|
|
]);
|
|
const invalid = await inspect({
|
|
stdout: stdoutBuffer.toString("utf8"),
|
|
stdoutBuffer,
|
|
exitCode: 0,
|
|
});
|
|
expect(invalid.facts).toEqual({
|
|
package_file_reason: `dpkg-deb returned invalid UTF-8 in the control fields of ${invalid.path}`,
|
|
});
|
|
|
|
const valid = await inspect({
|
|
stdout: "",
|
|
stdoutBuffer: Buffer.from(DPKG_DEB_OUTPUT),
|
|
exitCode: 0,
|
|
});
|
|
expect(valid.facts.package_file).toMatchObject({
|
|
package: INSTALLED.name,
|
|
version: INSTALLED.version,
|
|
architecture: INSTALLED.architecture,
|
|
});
|
|
});
|
|
|
|
it("records why instead when the file holds another version", async () => {
|
|
const { path, facts } = await inspect(
|
|
{ stdout: DPKG_DEB_OUTPUT, exitCode: 0 },
|
|
{ ...INSTALLED, version: "1:2.02-1" },
|
|
);
|
|
|
|
expect(facts).toEqual({
|
|
package_file_reason: `${path} holds varicad2026-en 1:2.03-1 (amd64), not the installed varicad2026-en 1:2.02-1 (amd64)`,
|
|
});
|
|
});
|
|
|
|
it("records why when dpkg-deb is missing or cannot read the file", async () => {
|
|
const missing = await inspect("missing");
|
|
const unreadable = await inspect({ stdout: "", exitCode: 2 });
|
|
|
|
expect(missing.facts).toEqual({
|
|
package_file_reason: `dpkg-deb is not installed, so the control fields of ${missing.path} were not read`,
|
|
});
|
|
expect(unreadable.facts).toEqual({
|
|
package_file_reason: `dpkg-deb could not read ${unreadable.path} as a Debian package`,
|
|
});
|
|
});
|
|
|
|
it("matches nothing for an application dpkg did not install, or a path that is not a file", async () => {
|
|
const snap = await inspect(
|
|
{ stdout: DPKG_DEB_OUTPUT, exitCode: 0 },
|
|
{ ...INSTALLED, kind: "snap", version: null, architecture: null },
|
|
);
|
|
const folder = await createTestTempDirectory("desktop-deb-");
|
|
await mkdir(join(folder, "a.deb"));
|
|
const directory = await inspectPackageFile(
|
|
join(folder, "a.deb"),
|
|
INSTALLED,
|
|
runHostQuery,
|
|
undefined,
|
|
);
|
|
|
|
expect(snap.calls).toEqual([]);
|
|
expect(snap.facts.package_file_reason).toMatch(
|
|
/dpkg.*no installed version.*snap varicad2026-en/iu,
|
|
);
|
|
expect(directory).toEqual({
|
|
package_file_reason: `${join(folder, "a.deb")} is not a regular file`,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
|
|
"inspectPackageFile when a parent folder denies the check",
|
|
() => {
|
|
it("says the file could not be checked rather than that it is not a regular file", async () => {
|
|
const folder = await createTestTempDirectory("desktop-deb-denied-");
|
|
const blocked = join(folder, "blocked");
|
|
const path = join(blocked, "notes.deb");
|
|
await mkdir(blocked);
|
|
await writeFile(path, BYTES);
|
|
await chmod(blocked, 0o000);
|
|
try {
|
|
const facts = await inspectPackageFile(
|
|
path,
|
|
INSTALLED,
|
|
runHostQuery,
|
|
undefined,
|
|
);
|
|
|
|
expect(facts).toEqual({
|
|
package_file_reason: `${path} could not be checked, so it was not read as a file`,
|
|
});
|
|
} finally {
|
|
await chmod(blocked, 0o755);
|
|
}
|
|
});
|
|
},
|
|
);
|
|
|
|
const hasDpkgDeb =
|
|
(await runHostQuery("dpkg-deb", ["--version"], undefined)) !== "missing";
|
|
|
|
describe.skipIf(!hasDpkgDeb)("inspectPackageFile with dpkg-deb", () => {
|
|
it("reads a package dpkg-deb built", async () => {
|
|
const folder = await createTestTempDirectory("desktop-deb-build-");
|
|
await mkdir(join(folder, "root/DEBIAN"), { recursive: true });
|
|
await mkdir(join(folder, "root/usr/bin"), { recursive: true });
|
|
await writeFile(join(folder, "root/usr/bin/sample"), "#!/bin/sh\n");
|
|
await writeFile(
|
|
join(folder, "root/DEBIAN/control"),
|
|
[
|
|
"Package: rea-sample",
|
|
"Version: 2.0-1",
|
|
"Architecture: amd64",
|
|
"Maintainer: REA <rea@example.com>",
|
|
"Pre-Depends: dpkg (>= 1.15.6)",
|
|
"Depends: libc6 (>= 2.34), libgl1 | libgl1-mesa-glx, libstdc++6:any",
|
|
"Description: sample",
|
|
"",
|
|
].join("\n"),
|
|
);
|
|
const deb = join(folder, "rea-sample_2.0-1_amd64.deb");
|
|
expect(
|
|
await runHostQuery(
|
|
"dpkg-deb",
|
|
["--build", join(folder, "root"), deb],
|
|
undefined,
|
|
),
|
|
).toMatchObject({ exitCode: 0 });
|
|
|
|
const facts = await inspectPackageFile(
|
|
deb,
|
|
{ ...INSTALLED, name: "rea-sample", version: "2.0-1" },
|
|
runHostQuery,
|
|
undefined,
|
|
);
|
|
|
|
expect(facts.package_file).toMatchObject({
|
|
name: "rea-sample_2.0-1_amd64.deb",
|
|
package: "rea-sample",
|
|
version: "2.0-1",
|
|
architecture: "amd64",
|
|
depends: [
|
|
"libc6 (>= 2.34)",
|
|
"libgl1 | libgl1-mesa-glx",
|
|
"libstdc++6:any",
|
|
],
|
|
pre_depends: ["dpkg (>= 1.15.6)"],
|
|
});
|
|
expect(facts.package_file?.sha256).toMatch(/^[0-9a-f]{64}$/u);
|
|
});
|
|
});
|
|
|
|
describe("inspectPackageFile while the file changes", () => {
|
|
it("records why when the file is replaced between dpkg-deb's read and the hash", async () => {
|
|
const folder = await createTestTempDirectory("desktop-deb-swap-");
|
|
const path = join(folder, "varicad2026-en_2.03_amd64.deb");
|
|
await writeFile(path, BYTES);
|
|
|
|
const facts = await inspectPackageFile(
|
|
path,
|
|
INSTALLED,
|
|
async () => {
|
|
// Another package takes the file's place after dpkg-deb has read it.
|
|
await writeFile(join(folder, "other.deb"), `${BYTES}other\n`);
|
|
await rename(join(folder, "other.deb"), path);
|
|
return { stdout: DPKG_DEB_OUTPUT, exitCode: 0 };
|
|
},
|
|
undefined,
|
|
);
|
|
|
|
expect(facts).toEqual({
|
|
package_file_reason: `${path} changed while it was read, so its control fields and its hash could describe different files`,
|
|
});
|
|
});
|
|
});
|