refactor: share Android and firmware provider factories (#826)
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
|
||||
import { createAndroidAnalysisProvider } from "../../src/composition/android.js";
|
||||
import { createFirmwareAnalysisProvider } from "../../src/composition/firmware.js";
|
||||
import { createServer } from "../../src/server/createServer.js";
|
||||
import { createTestBinarySession } from "../fixtures/binarySession.js";
|
||||
import { writeOrderedZip } from "../fixtures/artifactEntryOrder.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
const connect = async (environment: NodeJS.ProcessEnv) => {
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("Target-free analysis must not acquire a deep provider");
|
||||
});
|
||||
const server = createServer(session, session, {
|
||||
androidAnalysis: createAndroidAnalysisProvider(environment),
|
||||
firmwareAnalysis: createFirmwareAnalysisProvider(environment),
|
||||
});
|
||||
const client = new Client({ name: "composition-parity", version: "1" });
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
return client;
|
||||
};
|
||||
|
||||
cliTest.skipIf(process.platform === "win32")(
|
||||
"preserves the selected APK configuration through the compiled CLI and MCP factories",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-android-composition-");
|
||||
const apk = join(root, "fixture.apk");
|
||||
await writeOrderedZip(apk, ["AndroidManifest.xml", "classes.dex"]);
|
||||
const environment = {
|
||||
REA_LOG_LEVEL: "silent",
|
||||
JAVA_HOME: "relative-fixture-jdk",
|
||||
};
|
||||
const cliResult = await cli.run({
|
||||
arguments: ["inspect-android-package", apk, "--json"],
|
||||
environment,
|
||||
timeoutMs: 10_000,
|
||||
});
|
||||
expect(cliResult.exitCode).toBe(1);
|
||||
expect(cliResult.json).toMatchObject({ code: "capability_unavailable" });
|
||||
const client = await connect(environment);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_android_package",
|
||||
arguments: { path: apk },
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
expect(response.structuredContent).toEqual({ error: cliResult.json });
|
||||
expect(JSON.stringify(response.structuredContent)).toContain("JAVA_HOME");
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.skipIf(process.platform !== "linux")(
|
||||
"preserves the missing firmware command reason through both caller paths",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-firmware-composition-");
|
||||
const path = join(root, "input.bin");
|
||||
await writeFile(path, "firmware fixture");
|
||||
const environment = { REA_LOG_LEVEL: "silent" };
|
||||
const cliResult = await cli.run({
|
||||
arguments: ["inspect-firmware-regions", path, "--json"],
|
||||
environment,
|
||||
timeoutMs: 10_000,
|
||||
});
|
||||
expect(cliResult.exitCode).toBe(1);
|
||||
expect(cliResult.json).toMatchObject({ code: "capability_unavailable" });
|
||||
const client = await connect(environment);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_firmware_regions",
|
||||
arguments: { path },
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
expect(response.structuredContent).toEqual({ error: cliResult.json });
|
||||
expect(JSON.stringify(response.structuredContent)).toContain(
|
||||
"REA_BINWALK_COMMAND",
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -11,6 +11,23 @@ import {
|
||||
|
||||
const it = test.skipIf(process.platform === "win32");
|
||||
|
||||
it("does not carry a factory instance's cleanup failure into another instance", async () => {
|
||||
const failed = await setup("cleanup-failure");
|
||||
const healthy = await setup();
|
||||
const failure = await failed.service.execute("inspect_android_package", {
|
||||
path: failed.apk,
|
||||
});
|
||||
expect(failure).toMatchObject({
|
||||
ok: false,
|
||||
error: { cleanupIncomplete: true },
|
||||
});
|
||||
const result = await healthy.service.execute("inspect_android_package", {
|
||||
path: healthy.apk,
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
await verifyCleanup(healthy.launches);
|
||||
});
|
||||
|
||||
it("retains APK identity, original input, raw producer data and normalized observations", async () => {
|
||||
const { service, apk, launches } = await setup();
|
||||
const outcome = await service.execute("inspect_android_package", {
|
||||
|
||||
@@ -13,6 +13,23 @@ import { toCallToolResult } from "../../../src/server/toolResult.js";
|
||||
|
||||
const it = test.skipIf(process.platform !== "linux");
|
||||
|
||||
it("keeps cleanup-failure state local to the selected factory instance", async () => {
|
||||
const failed = await firmwareFixture("cleanup-failure");
|
||||
const healthy = await firmwareFixture();
|
||||
const failure = await failed.service.execute("inspect_firmware_regions", {
|
||||
path: failed.path,
|
||||
});
|
||||
expect(failure).toMatchObject({
|
||||
ok: false,
|
||||
error: { cleanupIncomplete: true },
|
||||
});
|
||||
const result = await healthy.service.execute("inspect_firmware_regions", {
|
||||
path: healthy.path,
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
await assertFirmwareCleanup(healthy.launches);
|
||||
});
|
||||
|
||||
it("binds region observations to exact bytes and preserves provider-reported uncertainty", async () => {
|
||||
const fixture = await firmwareFixture();
|
||||
const result = await fixture.service.execute("inspect_firmware_regions", {
|
||||
|
||||
@@ -11,10 +11,17 @@ describe("incremental module import boundaries", () => {
|
||||
["src/domain/probe.ts", "../browser/Provider.js", false],
|
||||
["src/contracts/probe.ts", "../android/Provider.js", false],
|
||||
["src/contracts/probe.ts", "../cli.js", false],
|
||||
["src/contracts/probe.ts", "../composition/android.js", false],
|
||||
["src/domain/probe.test.ts", "../composition/firmware.js", false],
|
||||
["src/application/probe.ts", "../android/JadxProvider.js", false],
|
||||
["src/application/probe.ts", "../composition/android.js", false],
|
||||
["src/server/probe.ts", "../firmware/FirmwareProvider.js", false],
|
||||
["src/domain/probe.ts", "./result.js", true],
|
||||
["src/contracts/probe.ts", "../domain/result.js", true],
|
||||
["src/application/probe.ts", "../domain/result.js", true],
|
||||
["src/composition/probe.ts", "../ghidra/Provider.js", true],
|
||||
["src/composition/probe.ts", "../android/JadxProvider.js", true],
|
||||
["src/composition/probe.ts", "../firmware/FirmwareProvider.js", true],
|
||||
])("checks %s importing %s", async (file, dependency, allowed) => {
|
||||
const temporary = await createTestTempDirectory("rea-module-boundary-");
|
||||
const config = join(temporary, ".oxlintrc.json");
|
||||
|
||||
Vendored
+2
-2
@@ -3,7 +3,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { AndroidAnalysisService } from "../../../src/application/AndroidAnalysisService.js";
|
||||
import { JadxProvider } from "../../../src/android/JadxProvider.js";
|
||||
import { createAndroidAnalysisProvider } from "../../../src/composition/android.js";
|
||||
import {
|
||||
ProviderProcessSupervisor,
|
||||
spawnOwnedProviderProcess,
|
||||
@@ -26,7 +26,7 @@ export const createJadxProtocolFixture = async (mode = "normal") => {
|
||||
cwd: string | undefined;
|
||||
pid: number | undefined;
|
||||
}[] = [];
|
||||
const provider = new JadxProvider(
|
||||
const provider = createAndroidAnalysisProvider(
|
||||
{ ...process.env, REA_JADX_MCP_JAR: jar },
|
||||
async (options) => {
|
||||
const spawned = await spawnOwnedProviderProcess({
|
||||
|
||||
Vendored
+2
-2
@@ -2,7 +2,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { access, rm, writeFile } from "node:fs/promises";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { FirmwareProvider } from "../../../src/firmware/FirmwareProvider.js";
|
||||
import { createFirmwareAnalysisProvider } from "../../../src/composition/firmware.js";
|
||||
import { FirmwareAnalysisService } from "../../../src/application/FirmwareAnalysisService.js";
|
||||
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
||||
import { createTestTempDirectory } from "../temporaryDirectory.js";
|
||||
@@ -18,7 +18,7 @@ export const firmwareFixture = async (mode = "normal") => {
|
||||
pid: number | undefined;
|
||||
args: readonly string[];
|
||||
}[] = [];
|
||||
const provider = new FirmwareProvider(
|
||||
const provider = createFirmwareAnalysisProvider(
|
||||
{
|
||||
...process.env,
|
||||
REA_BINWALK_COMMAND: "/usr/bin/true",
|
||||
|
||||
Reference in New Issue
Block a user