Files
reverse-engineering-anything/tests/boundary/firmware/firmwareReportFiles.test.ts
T
morluto b87b318ae7 refactor(wasm): leave WABT cancellation to the shared owners (#10796)
* fix(pcap): report a refused tool launch as an unavailable tool

When a capture read's tool launch was refused with EPERM, pcap reported it
as unreadable tool output (AnalysisProtocolError) instead of an unavailable
tool. Pcap also never told "not executable" apart from "missing".

Add classifyLaunchFailure in src/process as the one owner of which spawn
errnos mean missing (ENOENT, ENOTDIR) and which mean present but not
executable (EACCES, EPERM, ENOEXEC, EISDIR). Pcap, apktool and gdb used three
different errno lists; all three now use it.

* refactor(process): classify owned command outcomes through one owner

Add ProviderOutcome, read once from the supervisor's facts, and classifyProviderOutcome, the one mapping from an outcome to a typed error. Migrate pcap and the shared artifact producer command failure (TShark, sigrok). Add the rea/provider-failure-classifier lint rule with a shrink-only baseline of per-site (cause: unknown) => AnalysisError functions.

* refactor(process): classify Java and adb launch refusals with the shared owner (#10598)

The Ghidra and JADX Java probes and adb's version probe now take
missing-versus-not-executable from classifyLaunchFailure instead of their
own errno checks. ELOOP and ENAMETOOLONG join the owner's missing set, so
adb keeps its existing classification and both Java probes now treat a
non-directory path component as a missing runtime.

* fix(pcap): preserve command launch attribution

* test(pcap): expect an unknown failing stage when a limited capture tool exits unsuccessfully

* refactor(process): classify owned command outcomes through one owner

Add ProviderOutcome, read once from the supervisor's facts, and classifyProviderOutcome, the one mapping from an outcome to a typed error. Migrate pcap and the shared artifact producer command failure (TShark, sigrok). Add the rea/provider-failure-classifier lint rule with a shrink-only baseline of per-site (cause: unknown) => AnalysisError functions.

* test(lint): cover baselined files in the provider-failure classifier rule

* fix(apktool): report the decoder JVM's heap exhaustion as memory, not a target refusal

The supervisor now watches for a managed runtime's out-of-memory report while output streams (JVM java.lang.OutOfMemoryError, V8 'JavaScript heap out of memory'), so a retention budget cannot hide it. ProviderOutcome gains a runtime-exhausted kind and classifyProviderOutcome maps it once. Apktool decode opts in and moves to the shared classifier; Ghidra's single-purpose output token becomes the JVM runtime option and its area matching reuses the shared table.

* fix(process): keep the runtime's out-of-memory report on the memory error it supports

* fix(pcap): attribute a limited capture's exit through the prlimit wrapper

* fix(process): keep a shortened out-of-memory line marked as shortened

* fix(process): identify the refused executable for every launcher shape

Name the executable on errnos Node throws synchronously from spawn
(ENOEXEC, ENOTDIR, ...) and read the Windows native launcher's
executable-launch constraint, so pcap attributes those refusals to the
selected tool or limiter instead of an unknown stage.

* test(pcap): prove synchronous launch naming with ENOTDIR on every POSIX host

* fix(apktool): advise the JVM limit that governs the reported memory area

* fix(ghidra): read the supervisor's parsed OOM area instead of reparsing lines

* fix(sqlite,capture): read V8 heap exhaustion from streamed output, not retained stderr (#10724)

The SQLite worker and the Node capture decoders now declare runtime: "v8", so the supervisor observes V8's heap report as output streams. Both classifiers read the runtime-exhausted outcome instead of matching retained stderr, so a report beyond the retention budget, or one whose process then overflowed its output budget, is still reported as memory exhaustion.

* refactor(evm): classify EVMole worker outcomes through the shared provider classifier (#10716)

* refactor(evm): classify EVMole worker outcomes through the shared provider classifier

The EVMole worker's cancellation, deadline, output budget and CPU/file-size signals now go through classifyProviderOutcome; only the prlimit launch refusal and the reserved EFBIG exit remain in its dialect. The worker is opted into V8 exhaustion scanning so a heap report names its fixed 256 MiB heap. providerOutcomeOf now reads a launch refusal only from a spawn errno, so a private-storage or carrier read errno is no longer reported as a limiter launch failure.

* fix(evm): keep a stream failure's exit status and update Linux worker expectations

* refactor(process): read launch refusals through the launcher identity owner

* refactor(wasm): classify WABT command outcomes through the shared provider classifier (#10727)

* refactor(wasm): classify WABT command outcomes through the shared provider classifier

WABT's cancellation, command deadline, output budget, and inherited-limit signals now go through classifyProviderOutcome; its dialect keeps only the configuration-probe and launch-refusal unavailability and the unclassified-exit output error. A tool ended by SIGXCPU/SIGXFSZ or the memory limit is now a resource constraint instead of an output error, and a refused launch in the producer phase is reported as unavailable. The shared timeout context gains the analysis setting that selected the deadline, and the output-limit context may name its exceeded budget.

* fix(wasm): keep setup guidance when a WABT version probe's output cannot be collected

* refactor(providers): classify raw provider failures through one owner

* refactor(process): build classified cancellations with cancelledBy and the carried caller reason

* chore(lint): count runOwned's close-failure mapper from main in the classifier baseline

* fix(process): carry the runtime's exhaustion report on every memory error that claims it

SQLite and historical-capture memory errors recognised a streamed V8 heap report but dropped it, so a report outside retained output reached callers without its source line or omission flags. One projection, runtimeExhaustionReport, now owns that shape for the shared classifier and both consumers. Also lowers the EVMole and WABT cancellation-constructor baselines.

* fix(process): keep the provider's recovery for a native-thread exhaustion report

The shared classifier replaced the provider-supplied recovery with generic capacity advice whenever the JVM reported it could not create a native thread, dropping Apktool's launcher guidance and flipping retryable to true. The provider's recovery now wins; the capacity advice remains the fallback for providers that supply none.

* refactor(pwntools): classify pwntools failures through the shared provider classifier (#10782)

* refactor(pwntools): classify pwntools failures through the shared provider classifier

pwntools' per-site classifier is replaced by pwntoolsFailureDialect for classifyProviderFailure. Command outcomes go through classifyProviderOutcome, launch refusals through launcherRefusalOf instead of a 'spawn ' syscall prefix check, the input limit through the file dialect's fileSize, and the cleanup report through commandCleanup. The shared read cancellation now carries the reader's partial observation, and resource-signal reports accept provider detail.

* fix(pwntools): report a failed command's output once and expect resource_constraint for overflow

The shared outcome owner already attaches the command's output as capturedOutput, so the pwntools adapter fallback no longer copies the same text into diagnostics.stdout, diagnostics.stderr and diagnostics.captured_output; diagnostics keep the exit facts. Update the overflow expectations the dialect migration missed (layout provider cleanup and over-budget rows, pwntools cleanup) to resource_constraint.

* refactor(wasm): leave WABT cancellation to the shared owners

WabtFailures matched any Error named AbortError as a cancellation. No WABT path produces one: reads, path checks and owned commands report cancellation as ArtifactReaderFailure or OwnedCommandFailure, which classifyProviderFailure and classifyProviderOutcome already classify with the caller's reason. The provider's two signal checks now build the cancellation with cancelledBy. Lowers the AnalysisCancelledError constructor baseline for both WABT files to zero.

* fix(wasm): report a cancelled snapshot write as the caller's cancellation

The private snapshot write passed the caller's signal to Node's writeFile, whose abort envelope reached the dialect's unclassified output failure once the name-based AbortError guess was removed. The write now turns the envelope carrying its own signal's reason into cancelledBy at the producer.

* refactor(firmware): classify firmware failures through the shared provider classifier (#10810)

Firmware producers now report their own deadline expiry: the start check, the output inventory and launcher admission throw the phase timeout where they observe the abort, and a read the phase signal cancelled reports it through the dialect's interruption. The classifier no longer rereads a cancellation's reason as a timeout. A new readCleanup hook keeps firmware's cleanup report shape.
2026-11-15 17:42:49 +08:00

593 lines
20 KiB
TypeScript

import { access, chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import {
inventoryFirmwareOutput,
FirmwareFileAccess,
} from "../../../src/firmware/FirmwareFiles.js";
import { readFirmwareCommandReport } from "../../../src/firmware/FirmwareCommand.js";
import { ArtifactReaderFailure } from "../../../src/artifacts/ArtifactReader.js";
import { AnalysisCleanupLease } from "../../../src/artifacts/AnalysisCleanupLease.js";
import { firmwareFailureDialect } from "../../../src/firmware/FirmwareFailures.js";
import { classifyProviderFailure } from "../../../src/providers/classifyProviderFailure.js";
import { FIRMWARE_LIMITS } from "../../../src/firmware/FirmwareRelease.js";
import { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
import { AnalysisResourceConstraintError } from "../../../src/domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { SafeOutputTree } from "../../../src/artifacts/SafeOutputTree.js";
import {
normalizeBinwalkReport,
normalizeUnblobReport,
} from "../../../src/firmware/FirmwareReports.js";
import {
firmwareResultSchemas,
firmwareRequestSchema,
} from "../../../src/domain/firmware/firmwareAnalysis.js";
import { createHash } from "node:crypto";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
/** Classify a raw firmware failure the way the firmware producers do. */
const firmwareFailure = (
cause: unknown,
context: Parameters<typeof firmwareFailureDialect>[0],
) => classifyProviderFailure(cause, firmwareFailureDialect(context));
it.each([
["invalid leading byte", [0xff]],
["incomplete sequence", [0xe2, 0x82]],
["overlong sequence", [0xc0, 0xaf]],
["encoded surrogate", [0xed, 0xa0, 0x80]],
])(
"rejects complete JSON containing %s without repairing producer bytes",
async (_name, bytes) => {
const root = await createTestTempDirectory("rea-firmware-report-");
const path = join(root, "report.json");
await writeFile(
path,
Buffer.concat([
Buffer.from('{"description":"'),
Buffer.from(bytes),
Buffer.from('"}'),
]),
);
await expect(
new FirmwareFileAccess().report(path, "inspect_firmware_regions"),
).rejects.toMatchObject({
_tag: "AnalysisOutputError",
reason: "Tool report is not valid complete JSON",
});
},
);
it.each([
["missing", "Report was not created", async () => {}],
["empty", "Report is empty", (path: string) => writeFile(path, "")],
[
"invalid_json",
"Tool report is not valid complete JSON",
(path: string) => writeFile(path, '[{"Analysis":'),
],
[
"oversized",
`Report exceeded ${FIRMWARE_LIMITS.reportBytes} bytes`,
(path: string) =>
writeFile(path, Buffer.alloc(FIRMWARE_LIMITS.reportBytes + 1, 0x20)),
],
[
"not_regular_file",
"Report is not a regular file",
(path: string) => mkdir(path),
],
] as const)(
"keeps report state %s after a successful exit distinct from an empty scan",
async (state, reason, prepare) => {
const root = await createTestTempDirectory("rea-firmware-report-");
const path = join(root, "report.json");
await prepare(path);
const failure: unknown = await readFirmwareCommandReport({
path,
engine: "binwalk",
operation: "inspect_firmware_regions",
execution: {
stdout: { text: "", bytes: 0, observedBytes: 0 },
stderr: { text: "", bytes: 0, observedBytes: 0 },
exit_code: 0,
signal: null,
command: "/opt/binwalk",
args: ["--quiet", "--log", path, "/work/firmware.bin"],
resource_limiter: "/usr/bin/prlimit",
},
files: new FirmwareFileAccess(),
}).catch((cause: unknown) => cause);
if (!(failure instanceof AnalysisError))
throw new Error("Expected a typed report failure");
const projected = projectAnalysisError(failure);
expect(projected).toMatchObject({
code: "unreadable_output",
retryable: false,
message: expect.stringContaining("not an empty or complete result"),
details: {
reason: `binwalk exited 0 without a usable report: ${reason}`,
diagnostics: {
report_state: state,
report_path: path,
command: "/opt/binwalk",
args: ["--quiet", "--log", path, "/work/firmware.bin"],
resource_limiter: "/usr/bin/prlimit",
exit_code: 0,
signal: null,
stdout_bytes: 0,
stderr_bytes: 0,
},
},
});
// Generic doctor advice is not presented as a repair for a missing report.
expect(projected.message).not.toContain("rea doctor");
// Recorded arguments name a removed private workspace; recovery must not
// present them as a replayable command.
expect(projected.remediation.action).toContain("diagnostics.report_state");
expect(projected.remediation.action).toContain("cannot be replayed as-is");
if (state === "oversized") {
expect(projected.remediation.action).toContain("no runtime override");
expect(projected.remediation.action).toMatch(
/unchanged request.*fails/iu,
);
expect(projected.remediation.action).not.toContain(
"Rerun the same REA request",
);
expect(projected.message).toContain("no runtime override");
expect(projected.message).not.toContain("Rerun the same REA request");
} else {
expect(projected.remediation.action).toContain(
"replacing every private path",
);
expect(projected.remediation.action).toContain(
"Rerun the same REA request",
);
expect(projected.remediation.action).not.toMatch(
/unchanged request.*fails/iu,
);
}
},
);
it("preserves valid Unicode and a literal replacement character in producer JSON", async () => {
const root = await createTestTempDirectory("rea-firmware-report-");
const path = join(root, "report.json");
const report = { description: "固件 🧩 \ufffd", path: "/输入/firmware.bin" };
await writeFile(path, JSON.stringify(report));
expect(
await new FirmwareFileAccess().report(path, "inspect_firmware_regions"),
).toEqual(report);
});
it("orders extracted firmware paths by code point independently of host locale", async () => {
const root = await createTestTempDirectory("rea-firmware-inventory-");
await mkdir(join(root, "Lib"));
for (const path of ["a.bin", "z.bin", "\u00e4.bin", "B.bin", "Lib/x.bin"])
await writeFile(join(root, path), "");
const entries = await inventoryFirmwareOutput(
root,
1024,
16,
Number.MAX_SAFE_INTEGER,
"inspect_firmware_regions",
);
// Locale collation would interleave case and place "ä" by language rules.
expect(entries.map(({ relativePath }) => relativePath)).toEqual([
"B.bin",
"Lib/x.bin",
"a.bin",
"z.bin",
"\u00e4.bin",
]);
});
it.skipIf(process.platform !== "linux")(
"refuses raw filenames that cannot round-trip through UTF-8 instead of omitting their bytes",
async () => {
const root = await createTestTempDirectory("rea-firmware-raw-name-");
await writeFile(
Buffer.concat([
Buffer.from(`${root}/bad`),
Buffer.from([0xff]),
Buffer.from(".bin"),
]),
"123",
);
await writeFile(join(root, "good.bin"), "1234");
await expect(
inventoryFirmwareOutput(
root,
4,
10,
Number.MAX_SAFE_INTEGER,
"extract_firmware",
),
).rejects.toMatchObject({
_tag: "AnalysisOutputError",
diagnostics: { raw_name_hex: "626164ff2e62696e" },
});
},
);
it.each([
{ bytes: 1, files: 10, expected: "max_output_bytes" },
{ bytes: 10, files: 1, expected: "max_output_files" },
])(
"reports the selected $expected staging budget as a deterministic resource refusal",
async ({ bytes, files, expected }) => {
const root = await createTestTempDirectory("rea-firmware-staging-");
await writeFile(join(root, "a"), "1");
await writeFile(join(root, "b"), "2");
await expect(
inventoryFirmwareOutput(
root,
bytes,
files,
Number.MAX_SAFE_INTEGER,
"extract_firmware",
),
).rejects.toMatchObject({
_tag: "AnalysisResourceConstraintError",
resource: "capacity",
reportedLimits: { [expected]: 1 },
retryable: false,
remediationAction: expect.stringContaining(expected),
});
},
);
it("refuses a staged file that reaches the producer's per-file limit", async () => {
const root = await createTestTempDirectory("rea-firmware-file-limit-");
await writeFile(join(root, "below"), "123");
await expect(
inventoryFirmwareOutput(root, 1024, 10, 4, "extract_firmware"),
).resolves.toEqual([expect.objectContaining({ size: 3 })]);
await writeFile(join(root, "reached"), "1234");
const failure: unknown = await inventoryFirmwareOutput(
root,
1024,
10,
4,
"extract_firmware",
).then(
() => undefined,
(cause: unknown) => cause,
);
expect(failure).toMatchObject({
_tag: "AnalysisResourceConstraintError",
resource: "file-size",
reportedLimits: { file_size_bytes: 4 },
retryable: false,
});
if (!(failure instanceof AnalysisResourceConstraintError)) throw failure;
expect(failure.remediationAction).toMatch(
/inherited.*4-byte file-size limit.*RLIMIT_FSIZE/iu,
);
expect(failure.remediationAction).toMatch(
/raise the file-size limit.*ulimit -f/iu,
);
expect(failure.remediationAction).toMatch(
/analysis_timeout_ms does not raise it/iu,
);
expect(failure.remediationAction).toMatch(/unchanged request fails/iu);
});
it("attributes missing sources to the selected path without relabeling snapshot destination failure", async () => {
const root = await createTestTempDirectory("rea-firmware-source-");
const files = new FirmwareFileAccess();
const request = (path: string) =>
firmwareRequestSchema.parse({
operation: "inspect_firmware_regions",
input: { path },
});
await expect(
files.snapshot(
request(join(root, "absent")),
root,
new AbortController().signal,
),
).rejects.toMatchObject({
_tag: "AnalysisInputError",
issues: [{ path: ["path"], reason: "invalid_value" }],
});
const source = join(root, "source");
await writeFile(source, "firmware");
await expect(
files.snapshot(
request(source),
join(root, "missing-workspace"),
new AbortController().signal,
),
).rejects.toMatchObject({ code: "ENOENT" });
await files.close();
});
it("does not activate an abort-bound source during awaited destination setup", async () => {
const root = await createTestTempDirectory("rea-firmware-publish-abort-");
const source = join(root, "source");
const bytes = Buffer.from("firmware");
await writeFile(source, bytes);
const files = new FirmwareFileAccess();
const tree = await SafeOutputTree.create(join(root, "published"));
const controller = new AbortController();
const reason = { stop: "during destination setup" };
const write = tree.write.bind(tree);
const spy = vi.spyOn(tree, "write").mockImplementation(async (...args) => {
controller.abort(reason);
// Yield a turn while the source has no consumer, reproducing the handoff.
await new Promise<void>((resolve) => setImmediate(resolve));
return write(...args);
});
try {
await expect(
files.publish(
{
path: source,
relativePath: "nested/file",
kind: "file",
size: bytes.length,
linkTarget: null,
},
createHash("sha256").update(bytes).digest("hex"),
tree,
controller.signal,
),
).rejects.toMatchObject({ reason: "cancelled", cause: reason });
} finally {
spy.mockRestore();
await tree.rollback();
await files.close();
}
await expect(access(join(root, "published"))).rejects.toMatchObject({
code: "ENOENT",
});
expect(Buffer.compare(await readFile(source), bytes)).toBe(0);
});
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"keeps denied source access distinct from invalid paths and producer failures",
async () => {
const root = await createTestTempDirectory("rea-firmware-source-denial-");
const source = join(root, "input");
await writeFile(source, "firmware", { mode: 0o000 });
const files = new FirmwareFileAccess();
try {
await expect(
files.snapshot(
firmwareRequestSchema.parse({
operation: "inspect_firmware_regions",
input: { path: source },
}),
root,
new AbortController().signal,
),
).rejects.toMatchObject({
_tag: "AnalysisAccessDeniedError",
path: source,
systemCode: "EACCES",
});
} finally {
await chmod(source, 0o600);
await files.close();
}
},
);
it("retains unfamiliar Unblob report members from JSON files through public diagnostics", async () => {
const root = await createTestTempDirectory("rea-firmware-report-metadata-");
const inputPath = join(root, "input.bin");
const reportPath = join(root, "report.json");
const sha256 = "a".repeat(64);
const unfamiliar: unknown = JSON.parse(
'{"__typename__":"FutureReport","__proto__":{"source":"local","empty":null},"constructor":false,"prototype":0,"nested":{"__proto__":"retained"}}',
);
const record = {
task: { path: inputPath, depth: 0, blob_id: "", is_multi_file: false },
reports: [
{ __typename__: "HashReport", sha256 },
{
__typename__: "StatReport",
path: inputPath,
size: 4,
is_dir: false,
is_file: true,
is_link: false,
link_target: null,
},
unfamiliar,
],
subtasks: [],
};
await writeFile(reportPath, JSON.stringify([record]));
const files = new FirmwareFileAccess();
try {
const raw = await files.report(reportPath, "extract_firmware");
const normalized = normalizeUnblobReport(raw, {
inputPath,
outputRoot: join(root, "output"),
sha256,
length: 4,
offset: 0,
maxDepth: 3,
});
const diagnostics =
firmwareResultSchemas.extract_firmware.shape.diagnostics.parse(
normalized.diagnostics,
);
expect(JSON.stringify(diagnostics)).toBe(
JSON.stringify([{ input_path: "$input", report: unfamiliar }]),
);
expect(normalized.coverage).toBe("partial");
for (const malformed of [{}, { __typename__: null }, { __typename__: 1 }]) {
expect(() =>
normalizeUnblobReport(
[{ ...record, reports: [...record.reports.slice(0, 2), malformed] }],
{
inputPath,
outputRoot: join(root, "output"),
sha256,
length: 4,
offset: 0,
maxDepth: 3,
},
),
).toThrow();
}
} finally {
await files.close();
}
});
it("rejects Binwalk extraction activity under every own JSON member", async () => {
const root = await createTestTempDirectory(
"rea-firmware-report-extractions-",
);
const reportPath = join(root, "report.json");
const inputPath = join(root, "input.bin");
const files = new FirmwareFileAccess();
try {
for (const extractions of [
"{}",
'{"__proto__":null}',
'{"ordinary":false}',
]) {
await writeFile(
reportPath,
`[{"Analysis":{"file_path":${JSON.stringify(inputPath)},"file_map":[],"extractions":${extractions}}}]`,
);
const raw = await files.report(reportPath, "inspect_firmware_regions");
const normalize = () => normalizeBinwalkReport(raw, inputPath, 4);
if (extractions === "{}") expect(normalize()).toEqual([]);
else expect(normalize).toThrow("Expected one inspection-only report");
}
} finally {
await files.close();
}
});
it.each([false, true])(
"retains completed command output after report read failure (cleanup pending: %s)",
async (pending) => {
const lease = new AnalysisCleanupLease(async () => {});
const reader = pending
? ArtifactReaderFailure.withCleanup(
new Error("report read failed"),
{ reason: "close failed", resources: ["/work/report"] },
undefined,
[lease],
)
: new ArtifactReaderFailure("io", "report read failed");
const files = new FirmwareFileAccess();
vi.spyOn(files, "report").mockRejectedValue(reader);
const cause: unknown = await readFirmwareCommandReport({
path: "/work/report",
engine: "binwalk",
operation: "inspect_firmware_regions",
files,
execution: {
stdout: { text: "found output", bytes: 12, observedBytes: 12 },
stderr: { text: "warning", bytes: 7, observedBytes: 7 },
exit_code: 0,
signal: null,
command: "/opt/binwalk",
args: [],
resource_limiter: "/usr/bin/prlimit",
},
}).catch((failure: unknown) => failure);
const failure = firmwareFailure(cause, {
engine: "binwalk",
operation: "inspect_firmware_regions",
});
expect(
projectAnalysisError(failure).details?.captured_output,
).toMatchObject({
stdout: "found output",
stderr: "warning",
stdout_bytes: 12,
stderr_bytes: 7,
exit_code: 0,
});
if (pending) {
expect(failure.cleanupIncomplete).toBe(true);
await lease.release();
expect(failure.cleanupIncomplete).toBe(false);
}
await files.close();
},
);
it("retains the analysis timeout and command output while report cleanup is pending and after retry", async () => {
const deadlineSignal = new AbortController();
const reason = new Error("analysis deadline");
deadlineSignal.abort(reason);
const lease = new AnalysisCleanupLease(async () => {});
const reader = ArtifactReaderFailure.withCleanup(
new ArtifactReaderFailure("cancelled", "deadline interrupted report read", {
cause: reason,
}),
{ reason: "close failed", resources: ["/work/report"] },
{
kind: "file-snapshot-hash",
path: "/work/report",
sha256: "a".repeat(64),
bytes: 12,
},
[lease],
);
const files = new FirmwareFileAccess();
vi.spyOn(files, "report").mockRejectedValue(reader);
const failure: unknown = await readFirmwareCommandReport({
path: "/work/report",
engine: "binwalk",
operation: "inspect_firmware_regions",
files,
deadline: {
phase: "analysis",
timeoutMs: 42,
signal: deadlineSignal.signal,
expiresAtMs: 0,
},
execution: {
stdout: { text: "found output", bytes: 12, observedBytes: 12 },
stderr: { text: "warning", bytes: 7, observedBytes: 7 },
exit_code: 0,
signal: null,
command: "/opt/binwalk",
args: [],
resource_limiter: "/usr/bin/prlimit",
},
}).catch((cause: unknown) => cause);
if (!(failure instanceof AnalysisError))
throw new Error("Expected typed report failure");
expect(failure.cleanupIncomplete).toBe(true);
expect(failure.partialObservation).toMatchObject({
kind: "file-snapshot-hash",
path: "/work/report",
});
expect(projectAnalysisError(failure).details?.captured_output).toMatchObject({
stdout: "found output",
stderr: "warning",
exit_code: 0,
});
await lease.release();
expect(failure.cleanupIncomplete).toBe(false);
expect(
projectAnalysisError(failure).details?.partial_observation,
).toMatchObject({ kind: "file-snapshot-hash", path: "/work/report" });
expect(projectAnalysisError(failure)).toMatchObject({
code: "provider_timeout",
details: {
timeout_ms: 42,
captured_output: {
stdout: "found output",
stderr: "warning",
exit_code: 0,
},
},
});
await files.close();
});