mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* test(lifecycle): cover browser-transport handshakes and cleanup retry Close six provider lifecycle gaps: Electron CDP handshake, cleanup and retry; V8 inspector handshake; web-module-trace handshake and retry. * test(lifecycle): assert retained cleanup finishes before the next acquisition Hold the retried browser and Electron transport close and check that no replacement launch or connect starts until it completes. Track gated upgrade sockets in the fake CDP browser so close() destroys them. * test(lifecycle): release held retry gates and join gated work on failure Teardown for the resolver and Electron retry tests releases the held close and awaits the gated request even when an assertion fails while it is held. * test(lifecycle): cover worker and tool launch, worker handshake, and decoder completion (#11512) * test(lifecycle): cover worker and tool launch, worker handshake, and decoder completion Close six provider lifecycle gaps: SQLite and JavaScript worker launch, JavaScript worker handshake, assetutil launch, and qlog and mitmproxy decoding through the owned decoder child. * test(lifecycle): run the worker relaunch in a built project and check exits before close The relaunch starts the built worker entry, so it moves to tests/boundary/javascript. The handshake tests now check that the stand-in child has exited when analyze() returns, before close() runs. * test(lifecycle): cover in-process cancellation and PE certificate completion (#11524) * test(lifecycle): cover in-process cancellation and PE certificate completion Close six provider lifecycle gaps: caller cancellation for the managed, Harmony, JavaScript workflow, runtime reconciliation and V8 code cache providers, and PE certificate completion (an existing CLI/MCP regression). Two Apple application gaps remain (#11509). * test(lifecycle): compare the whole retained V8 code cache observation * test(javascript): hold the startup deadline clock until the stand-in worker is running * test(javascript): restore real timers in a void teardown * test(lifecycle): join the held Electron request on failure, refuse unmodeled Inspector commands, drop a wording assertion * fix(apple): run assetutil under owned process supervision and retain unproven cleanup (#11586) * fix(process): report a cancellation or deadline during refused final cleanup as that interruption * fix(apple): prepare ownership before the assetutil deadline, keep per-stream allowances, join interrupted-cleanup tests * fix(artifacts): classify assetutil launch failures through the shared launch owner After #11615, owned-command launch failures are one launch-failed outcome classified by the shared owner. The assetutil dialect keeps only its unavailable report (launchUnavailable) and names its provider; host capacity and E2BIG become resource constraints. * fix(artifacts): keep a completed assetutil run's output on its cleanup-only report When assetutil exits 0 but its process cleanup is unproven, the cleanup error is the primary report. retainOwnedCommandCleanup now accepts the completed run's captured output for such a caller, so CLI and MCP keep the retained output beside the cleanup owner.
1249 lines
44 KiB
TypeScript
1249 lines
44 KiB
TypeScript
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
|
|
import { describe, expect, it } from "vitest";
|
|
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
|
|
|
|
import { projectManagedApplicationGraphEvidence } from "../../../src/application/managed/ManagedApplicationGraphService.js";
|
|
import {
|
|
managedApplicationGraphReferenceInputSchema,
|
|
managedApplicationGraphResultSchema,
|
|
} from "../../../src/domain/managed/managedApplicationGraph.js";
|
|
import { MANAGED_APPLICATION_GRAPH_EXAMPLE } from "../../../src/contracts/managed/managedWorkflowExamples.js";
|
|
import { traceApplicationFeatureEvidence } from "../../../src/application/javascript/JavaScriptApplicationWorkflowService.js";
|
|
import { MANAGED_STATIC_PROVIDER } from "../../../src/domain/investigationProviders.js";
|
|
import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { parseJavaScriptApplicationGraph } from "../../../src/domain/javascript/javascriptApplicationGraph.js";
|
|
import {
|
|
managedMemberInspectionSchema,
|
|
managedNativeBoundaryInspectionSchema,
|
|
} from "../../../src/domain/managed/managedArtifact.js";
|
|
import { inspectManagedArtifactBytes } from "../../../src/dotnet/ManagedArtifactInspector.js";
|
|
import { inspectManagedMembersBytes } from "../../../src/dotnet/ManagedMemberInspector.js";
|
|
import { inspectManagedNativeBoundariesBytes } from "../../../src/dotnet/ManagedNativeBoundaryInspector.js";
|
|
import {
|
|
buildManagedPeFixture,
|
|
managedPeFixtureTarget,
|
|
} from "../../../src/dotnet/ManagedPe.fixture.js";
|
|
|
|
/** Tiny-format CIL method body. */
|
|
const tinyIl = (il: readonly number[]): Buffer =>
|
|
Buffer.from([(il.length << 2) | 2, ...il]);
|
|
|
|
const memberGraph = (
|
|
bytes: Buffer,
|
|
options: Parameters<typeof inspectManagedMembersBytes>[2] = {},
|
|
) => {
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedCalls.exe");
|
|
const members = inspectManagedMembersBytes(bytes, binary, options);
|
|
const memberEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: members,
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
});
|
|
const projection = projectManagedApplicationGraphEvidence({
|
|
managed_members: memberEvidence,
|
|
});
|
|
if (!projection.ok) throw new Error(JSON.stringify(projection.error));
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projection.value).normalized_result,
|
|
);
|
|
const graph = parseJavaScriptApplicationGraph(result.graph);
|
|
return { members, memberEvidence, result, graph };
|
|
};
|
|
|
|
const createManagedInteropEvidence = () => {
|
|
const bytes = buildManagedPeFixture({
|
|
pinvoke: {
|
|
moduleName: "user32.dll",
|
|
importName: "MessageBoxW",
|
|
mappingFlags: 0x0345,
|
|
},
|
|
});
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
|
|
const managedArtifact = inspectManagedArtifactBytes(bytes, binary);
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const boundaries = inspectManagedNativeBoundariesBytes(bytes, binary);
|
|
|
|
return {
|
|
artifactEvidence: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_artifact",
|
|
parameters: {},
|
|
result: managedArtifact,
|
|
rawResult: null,
|
|
limitations: managedArtifact.limitations,
|
|
locations: [{ kind: "artifact-path", path: binary.path }],
|
|
}),
|
|
memberEvidence: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: members,
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
locations: [{ kind: "artifact-path", path: binary.path }],
|
|
}),
|
|
boundaryEvidence: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_native_boundaries",
|
|
parameters: {},
|
|
result: boundaries,
|
|
rawResult: null,
|
|
limitations: boundaries.limitations,
|
|
locations: [{ kind: "artifact-path", path: binary.path }],
|
|
}),
|
|
};
|
|
};
|
|
|
|
describe("managed application graph MethodDef scope", () => {
|
|
it("preserves MethodDef page omissions from decoded member Evidence", () => {
|
|
const bytes = buildManagedPeFixture({
|
|
ilBody: tinyIl([0x2a]),
|
|
methodRowCount: 3,
|
|
});
|
|
const full = memberGraph(bytes);
|
|
expect(full.members.method_rows).toEqual({ offset: 0, count: 3, total: 3 });
|
|
expect(full.result.coverage.status).toBe("complete-within-inputs");
|
|
for (const options of [
|
|
{ methodLimit: 0 },
|
|
{ methodOffset: 1, methodLimit: 1 },
|
|
{ methodOffset: 3 },
|
|
]) {
|
|
const page = memberGraph(bytes, options);
|
|
expect(page.members.coverage.state).toBe("complete");
|
|
expect(page.result.summary.methods).toBe(page.members.method_rows!.count);
|
|
expect(page.result.coverage.status).toBe("partial");
|
|
expect(page.result.limitations).toContain(
|
|
`Managed member Evidence examined MethodDef rows at offset ${page.members.method_rows!.offset} with count ${page.members.method_rows!.count} of 3 total rows; methods outside that interval and their relations are omitted, not absent.`,
|
|
);
|
|
}
|
|
});
|
|
|
|
it("keeps an unexamined MethodDef scope unknown", () => {
|
|
const bytes = buildManagedPeFixture({ ilBody: tinyIl([0x2a]) });
|
|
const binary = managedPeFixtureTarget(bytes);
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const evidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: { ...members, method_rows: null },
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
});
|
|
const projection = projectManagedApplicationGraphEvidence({
|
|
managed_members: evidence,
|
|
});
|
|
if (!projection.ok) throw new Error(JSON.stringify(projection.error));
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projection.value).normalized_result,
|
|
);
|
|
expect(result.coverage.status).toBe("partial");
|
|
expect(result.summary.methods).toBe(1);
|
|
expect(result.limitations).toContainEqual(
|
|
expect.stringMatching(
|
|
/MethodDef row scope.*methods.*relations.*unknown/iu,
|
|
),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("managed application graph cancellation", () => {
|
|
it("stops resolving retained managed sources when the caller cancels mid-request", () => {
|
|
const members = MANAGED_APPLICATION_GRAPH_EXAMPLE.managed_members;
|
|
const controller = new AbortController();
|
|
const reason = { request: "cancel managed graph projection" };
|
|
const looked: string[] = [];
|
|
const result = projectManagedApplicationGraphEvidence(
|
|
{
|
|
managed_members: {
|
|
kind: "retained-evidence",
|
|
evidence_id: members.evidence_id,
|
|
},
|
|
// Never read: cancellation is observed before this lookup.
|
|
managed_native_boundaries: {
|
|
kind: "retained-evidence",
|
|
evidence_id: `ev_${"b".repeat(64)}`,
|
|
},
|
|
},
|
|
(id) => {
|
|
looked.push(id);
|
|
controller.abort(reason);
|
|
return id === members.evidence_id ? members : undefined;
|
|
},
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(looked).toEqual([members.evidence_id]);
|
|
});
|
|
});
|
|
|
|
describe("managed application graph projection", () => {
|
|
it("projects managed metadata and native declarations into authenticated graph Evidence", () => {
|
|
const { artifactEvidence, memberEvidence, boundaryEvidence } =
|
|
createManagedInteropEvidence();
|
|
|
|
const evidence = projectManagedApplicationGraphEvidence({
|
|
managed_artifact: artifactEvidence,
|
|
managed_members: memberEvidence,
|
|
managed_native_boundaries: boundaryEvidence,
|
|
});
|
|
|
|
expect(
|
|
evidence.ok,
|
|
evidence.ok
|
|
? undefined
|
|
: `${JSON.stringify(evidence.error)} cause=${String(evidence.error.cause)}`,
|
|
).toBe(true);
|
|
if (!evidence.ok) throw new Error("projection failed");
|
|
const parsed = parseEvidence(evidence.value);
|
|
expect(parsed).toMatchObject({
|
|
operation: "project_managed_application_graph",
|
|
predicate_type: "rea.managed-application-graph",
|
|
provider: { id: "rea-dotnet-workflows" },
|
|
confidence: "inferred",
|
|
authority: "analyst-inference",
|
|
evidence_links: [
|
|
artifactEvidence.evidence_id,
|
|
memberEvidence.evidence_id,
|
|
boundaryEvidence.evidence_id,
|
|
],
|
|
});
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parsed.normalized_result,
|
|
);
|
|
const graph = parseJavaScriptApplicationGraph(result.graph);
|
|
expect(result.summary).toMatchObject({
|
|
assemblies: 1,
|
|
modules: 1,
|
|
types: 1,
|
|
methods: 1,
|
|
fields: 1,
|
|
pinvoke_imports: 1,
|
|
});
|
|
expect(graph.nodes.map(({ kind }) => kind)).toEqual(
|
|
expect.arrayContaining([
|
|
"artifact",
|
|
"managed-assembly",
|
|
"managed-module",
|
|
"managed-type",
|
|
"managed-method",
|
|
"managed-field",
|
|
"managed-pinvoke-import",
|
|
]),
|
|
);
|
|
const allEvidenceIds = [
|
|
artifactEvidence.evidence_id,
|
|
memberEvidence.evidence_id,
|
|
boundaryEvidence.evidence_id,
|
|
].sort();
|
|
const observedEvidenceIds = (kind: string): readonly string[] => {
|
|
const observation = graph.nodes.find((node) => node.kind === kind)
|
|
?.observations[0];
|
|
if (observation === undefined)
|
|
throw new Error(`Expected ${kind} observation`);
|
|
return observation.evidence.evidence_ids;
|
|
};
|
|
expect(observedEvidenceIds("artifact")).toEqual(allEvidenceIds);
|
|
expect(observedEvidenceIds("managed-assembly")).toEqual([
|
|
artifactEvidence.evidence_id,
|
|
]);
|
|
expect(observedEvidenceIds("managed-module")).toEqual([
|
|
artifactEvidence.evidence_id,
|
|
]);
|
|
for (const kind of ["managed-type", "managed-method", "managed-field"])
|
|
expect(observedEvidenceIds(kind)).toEqual([memberEvidence.evidence_id]);
|
|
expect(observedEvidenceIds("managed-pinvoke-import")).toEqual([
|
|
boundaryEvidence.evidence_id,
|
|
]);
|
|
const containmentEdges = graph.edges.filter(
|
|
({ relation }) => relation === "contains",
|
|
);
|
|
expect(containmentEdges.length).toBeGreaterThan(0);
|
|
for (const edge of containmentEdges)
|
|
expect(edge.evidence.evidence_ids).toEqual(allEvidenceIds);
|
|
const method = graph.nodes.find(
|
|
({ kind, observations }) =>
|
|
kind === "managed-method" &&
|
|
observations[0]?.label === "Fixture.Program.Main",
|
|
);
|
|
expect(method).toBeDefined();
|
|
const importEdge = graph.edges.find(
|
|
({ source_node_id, relation, target_node_id }) =>
|
|
source_node_id === method?.node_id &&
|
|
relation === "imports" &&
|
|
graph.nodes.find(({ node_id: id }) => id === target_node_id)?.kind ===
|
|
"managed-pinvoke-import",
|
|
);
|
|
expect(importEdge).toBeDefined();
|
|
expect(importEdge?.evidence.evidence_ids).toEqual(
|
|
[memberEvidence.evidence_id, boundaryEvidence.evidence_id].sort(),
|
|
);
|
|
expect(
|
|
graph.nodes.flatMap(({ observations }) =>
|
|
observations.map(({ evidence }) => evidence.authority),
|
|
),
|
|
).toContain("managed-static-analysis");
|
|
|
|
const trace = traceApplicationFeatureEvidence({
|
|
application: parsed,
|
|
native_observations: [],
|
|
seed: {
|
|
kind: "string",
|
|
value: "MessageBoxW",
|
|
match: "exact",
|
|
case_sensitive: true,
|
|
},
|
|
direction: "incoming",
|
|
});
|
|
expect(trace.ok, trace.ok ? undefined : JSON.stringify(trace.error)).toBe(
|
|
true,
|
|
);
|
|
if (!trace.ok) throw new Error("trace failed");
|
|
expect(trace.value.result).toMatchObject({
|
|
source_evidence_id: parsed.evidence_id,
|
|
summary: { matched_seeds: 1 },
|
|
});
|
|
});
|
|
});
|
|
|
|
it("preserves only a managed graph's matching subject in feature traces", () => {
|
|
const { artifactEvidence } = createManagedInteropEvidence();
|
|
const selectedArtifact = parseEvidence({
|
|
...artifactEvidence,
|
|
subject: { ...artifactEvidence.subject, name: "selected managed image" },
|
|
});
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_artifact: selectedArtifact,
|
|
});
|
|
if (!projected.ok) throw projected.error;
|
|
const parsed = parseEvidence(projected.value);
|
|
expect(parsed.subject).toEqual(selectedArtifact.subject);
|
|
const query = {
|
|
seed: {
|
|
kind: "string" as const,
|
|
value: "MessageBoxW",
|
|
match: "exact" as const,
|
|
case_sensitive: true,
|
|
},
|
|
direction: "incoming" as const,
|
|
};
|
|
const trace = traceApplicationFeatureEvidence({
|
|
application: parsed,
|
|
...query,
|
|
});
|
|
if (!trace.ok) throw trace.error;
|
|
expect(trace.value.evidence.subject).toEqual(selectedArtifact.subject);
|
|
const foreign = createEvidence(
|
|
{ path: "/other.exe", sha256: "f".repeat(64), format: "pe" },
|
|
parsed.provider,
|
|
{
|
|
operation: parsed.operation,
|
|
predicateType: parsed.predicate_type,
|
|
parameters: parsed.parameters,
|
|
result: parsed.normalized_result,
|
|
confidence: "inferred",
|
|
authority: "analyst-inference",
|
|
evidenceLinks: parsed.evidence_links,
|
|
},
|
|
);
|
|
const foreignTrace = traceApplicationFeatureEvidence({
|
|
application: foreign,
|
|
...query,
|
|
});
|
|
if (!foreignTrace.ok) throw foreignTrace.error;
|
|
expect(foreignTrace.value.evidence.subject.identity.kind).toBe("unknown");
|
|
const contradictory = createEvidence(
|
|
{ path: "/other.exe", sha256: "f".repeat(64), format: "pe" },
|
|
parsed.provider,
|
|
{
|
|
operation: parsed.operation,
|
|
predicateType: parsed.predicate_type,
|
|
parameters: parsed.parameters,
|
|
result: {
|
|
...(parsed.normalized_result as Record<string, unknown>),
|
|
root_artifact_sha256: "f".repeat(64),
|
|
},
|
|
confidence: "inferred",
|
|
authority: "analyst-inference",
|
|
evidenceLinks: parsed.evidence_links,
|
|
},
|
|
);
|
|
const contradictoryTrace = traceApplicationFeatureEvidence({
|
|
application: contradictory,
|
|
...query,
|
|
});
|
|
expect(contradictoryTrace).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisInputError",
|
|
issues: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
path: ["application", "normalized_result", "root_artifact_sha256"],
|
|
}),
|
|
]),
|
|
},
|
|
});
|
|
});
|
|
|
|
it("names the graph after a later file subject when the artifact subject is unknown", () => {
|
|
const { artifactEvidence, memberEvidence } = createManagedInteropEvidence();
|
|
const unboundArtifact = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
artifactEvidence.provider,
|
|
{
|
|
operation: artifactEvidence.operation,
|
|
parameters: artifactEvidence.parameters,
|
|
result: artifactEvidence.normalized_result,
|
|
rawResult: null,
|
|
limitations: artifactEvidence.limitations,
|
|
},
|
|
);
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_artifact: unboundArtifact,
|
|
managed_members: memberEvidence,
|
|
});
|
|
if (!projected.ok) throw projected.error;
|
|
expect(parseEvidence(projected.value).subject).toEqual(
|
|
memberEvidence.subject,
|
|
);
|
|
});
|
|
|
|
describe("managed application graph request", () => {
|
|
it("accepts any nonempty set of inline sources and rejects duplicates", () => {
|
|
const evidence = MANAGED_APPLICATION_GRAPH_EXAMPLE.managed_members;
|
|
expect(
|
|
managedApplicationGraphReferenceInputSchema.safeParse({
|
|
managed_members: evidence,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
managedApplicationGraphReferenceInputSchema.safeParse({}).success,
|
|
).toBe(false);
|
|
expect(
|
|
managedApplicationGraphReferenceInputSchema.safeParse({
|
|
managed_members: evidence,
|
|
managed_native_boundaries: evidence,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("managed application graph coverage", () => {
|
|
it("rejects managed Evidence whose artifact digest differs from its subject", () => {
|
|
const bytes = buildManagedPeFixture();
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/Mismatch.dll");
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const mismatched = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
artifact: { ...members.artifact, sha256: "b".repeat(64) },
|
|
identity_scope: {
|
|
...members.identity_scope,
|
|
requires_artifact_sha256: "b".repeat(64),
|
|
},
|
|
},
|
|
});
|
|
const rejected = projectManagedApplicationGraphEvidence({
|
|
managed_members: mismatched,
|
|
});
|
|
if (rejected.ok) throw new Error("mismatched managed Evidence accepted");
|
|
expect(JSON.stringify(rejected.error)).toContain(
|
|
`Managed Evidence inspect_managed_members (${mismatched.evidence_id}) subject SHA-256 ${binary.sha256} does not match normalized artifact SHA-256 ${"b".repeat(64)}`,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("managed application graph identity facts", () => {
|
|
it.each([
|
|
["managed_members", "memberEvidence"],
|
|
["managed_native_boundaries", "boundaryEvidence"],
|
|
] as const)(
|
|
"binds a module from %s to its supplying Evidence",
|
|
(inputKey, evidenceKey) => {
|
|
const source = createManagedInteropEvidence()[evidenceKey];
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
[inputKey]: source,
|
|
});
|
|
if (!projected.ok) throw projected.error;
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projected.value).normalized_result,
|
|
);
|
|
const graph = parseJavaScriptApplicationGraph(result.graph);
|
|
expect(result.summary).toMatchObject({ assemblies: 0, modules: 1 });
|
|
expect(
|
|
graph.nodes.find(({ kind }) => kind === "managed-module")
|
|
?.observations[0],
|
|
).toMatchObject({
|
|
evidence: {
|
|
extractor: { operation: source.operation },
|
|
evidence_ids: [source.evidence_id],
|
|
},
|
|
});
|
|
expect(result.evidence_links).toEqual([source.evidence_id]);
|
|
expect(result.limitations).toContainEqual(
|
|
expect.stringMatching(
|
|
/artifact Evidence.*not supplied.*assembly identity.*absent/iu,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
it("reports no assembly or module when the inspector observes no identity rows", () => {
|
|
const bytes = buildManagedPeFixture({
|
|
moduleRowCount: 0,
|
|
assemblyRowCount: 0,
|
|
});
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/NoIdentityRows.exe");
|
|
const inspection = inspectManagedArtifactBytes(bytes, binary);
|
|
expect(inspection).toMatchObject({ module: null, assembly: null });
|
|
const artifactEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_artifact",
|
|
parameters: {},
|
|
result: inspection,
|
|
rawResult: null,
|
|
limitations: inspection.limitations,
|
|
locations: [{ kind: "artifact-path", path: binary.path }],
|
|
});
|
|
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_artifact: artifactEvidence,
|
|
});
|
|
if (!projected.ok) throw projected.error;
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projected.value).normalized_result,
|
|
);
|
|
const graph = parseJavaScriptApplicationGraph(result.graph);
|
|
expect(result.summary).toMatchObject({ assemblies: 0, modules: 0 });
|
|
expect(
|
|
graph.nodes.filter(
|
|
({ kind }) => kind === "managed-assembly" || kind === "managed-module",
|
|
),
|
|
).toEqual([]);
|
|
});
|
|
|
|
it("projects a module whose metadata name is empty without a label", () => {
|
|
const bytes = buildManagedPeFixture({ ilBody: Buffer.from([0x06, 0x2a]) });
|
|
const control = inspectManagedMembersBytes(
|
|
bytes,
|
|
managedPeFixtureTarget(bytes, "/fixture/EmptyModuleName.dll"),
|
|
);
|
|
if (control.module === null) throw new Error("Expected a Module row");
|
|
// Point the Module row's Name at the string heap's empty entry.
|
|
bytes.writeUInt16LE(0, control.module.row_offset + 2);
|
|
const binary = managedPeFixtureTarget(
|
|
bytes,
|
|
"/fixture/EmptyModuleName.dll",
|
|
);
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
expect(members.module?.name).toBe("");
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: members,
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
locations: [{ kind: "artifact-path", path: binary.path }],
|
|
}),
|
|
});
|
|
if (!projected.ok) throw projected.error;
|
|
const graph = parseJavaScriptApplicationGraph(
|
|
managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projected.value).normalized_result,
|
|
).graph,
|
|
);
|
|
expect(
|
|
graph.nodes.find(({ kind }) => kind === "managed-module")
|
|
?.observations[0],
|
|
).toMatchObject({ label: null, properties: { name: "" } });
|
|
expect(graph.nodes.map(({ kind }) => kind)).toEqual(
|
|
expect.arrayContaining([
|
|
"managed-module",
|
|
"managed-type",
|
|
"managed-method",
|
|
"managed-field",
|
|
]),
|
|
);
|
|
});
|
|
});
|
|
|
|
it("normalizes MVID casing for node identity while preserving observed values", () => {
|
|
const { memberEvidence, boundaryEvidence } = createManagedInteropEvidence();
|
|
const members = managedMemberInspectionSchema.parse(
|
|
parseEvidence(memberEvidence).normalized_result,
|
|
);
|
|
if (members.module === null)
|
|
throw new Error("Expected fixture module identity");
|
|
const uppercaseMvid = members.module.mvid?.toUpperCase();
|
|
if (uppercaseMvid === undefined || uppercaseMvid === null)
|
|
throw new Error("Expected fixture module MVID");
|
|
const uppercaseMembers = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
module: {
|
|
...members.module,
|
|
mvid: uppercaseMvid,
|
|
enc_id: null,
|
|
enc_base_id: null,
|
|
},
|
|
identity_scope: {
|
|
...members.identity_scope,
|
|
requires_mvid: uppercaseMvid,
|
|
},
|
|
},
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
},
|
|
);
|
|
const baseline = projectManagedApplicationGraphEvidence({
|
|
managed_members: memberEvidence,
|
|
managed_native_boundaries: boundaryEvidence,
|
|
});
|
|
const uppercase = projectManagedApplicationGraphEvidence({
|
|
managed_members: uppercaseMembers,
|
|
managed_native_boundaries: boundaryEvidence,
|
|
});
|
|
if (!baseline.ok) throw baseline.error;
|
|
if (!uppercase.ok) throw uppercase.error;
|
|
const baselineResult = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(baseline.value).normalized_result,
|
|
);
|
|
const uppercaseResult = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(uppercase.value).normalized_result,
|
|
);
|
|
const baselineModule = parseJavaScriptApplicationGraph(
|
|
baselineResult.graph,
|
|
).nodes.find(({ kind }) => kind === "managed-module");
|
|
const uppercaseGraph = parseJavaScriptApplicationGraph(uppercaseResult.graph);
|
|
const uppercaseModule = uppercaseGraph.nodes.find(
|
|
({ kind }) => kind === "managed-module",
|
|
);
|
|
expect(uppercaseGraph.edges).toContainEqual(
|
|
expect.objectContaining({
|
|
relation: "imports",
|
|
source_node_id: uppercaseGraph.nodes.find(
|
|
({ kind }) => kind === "managed-method",
|
|
)?.node_id,
|
|
}),
|
|
);
|
|
expect(uppercaseModule?.node_id).toBe(baselineModule?.node_id);
|
|
expect(uppercaseModule?.observations[0]?.properties.mvid).toBe(uppercaseMvid);
|
|
});
|
|
|
|
describe("managed application graph module identity consistency", () => {
|
|
it("rejects contradictory known module MVIDs across same-artifact inputs", () => {
|
|
const { artifactEvidence, memberEvidence, boundaryEvidence } =
|
|
createManagedInteropEvidence();
|
|
const boundaries = managedNativeBoundaryInspectionSchema.parse(
|
|
parseEvidence(boundaryEvidence).normalized_result,
|
|
);
|
|
const conflictingMvid = "ffffffff-ffff-ffff-ffff-ffffffffffff";
|
|
const conflictingBoundaries = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_native_boundaries",
|
|
parameters: {},
|
|
result: {
|
|
...boundaries,
|
|
module:
|
|
boundaries.module === null
|
|
? null
|
|
: { ...boundaries.module, mvid: conflictingMvid },
|
|
identity_scope: {
|
|
...boundaries.identity_scope,
|
|
requires_mvid: conflictingMvid,
|
|
},
|
|
},
|
|
rawResult: null,
|
|
limitations: boundaries.limitations,
|
|
},
|
|
);
|
|
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_artifact: artifactEvidence,
|
|
managed_members: memberEvidence,
|
|
managed_native_boundaries: conflictingBoundaries,
|
|
});
|
|
if (projected.ok) throw new Error("conflicting MVIDs were accepted");
|
|
expect(JSON.stringify(projected.error)).toContain(
|
|
"conflicting known module MVIDs or token scopes",
|
|
);
|
|
});
|
|
|
|
it("rejects contradictory known module declaration fields", () => {
|
|
const { artifactEvidence, memberEvidence } = createManagedInteropEvidence();
|
|
const members = managedMemberInspectionSchema.parse(
|
|
parseEvidence(memberEvidence).normalized_result,
|
|
);
|
|
if (members.module === null)
|
|
throw new Error("Expected fixture module identity");
|
|
const conflictingMembers = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
module: {
|
|
...members.module,
|
|
generation: members.module.generation + 1,
|
|
},
|
|
},
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
},
|
|
);
|
|
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_artifact: artifactEvidence,
|
|
managed_members: conflictingMembers,
|
|
});
|
|
expect(projected.ok).toBe(false);
|
|
if (projected.ok)
|
|
throw new Error("conflicting module declaration was accepted");
|
|
expect(JSON.stringify(projected.error)).toContain(
|
|
"conflicting module generation values",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("managed application graph token scope", () => {
|
|
it("checks declared token artifact and MVID scopes without a subject digest", () => {
|
|
const { memberEvidence } = createManagedInteropEvidence();
|
|
const members = managedMemberInspectionSchema.parse(
|
|
parseEvidence(memberEvidence).normalized_result,
|
|
);
|
|
const wrongMvidValue = "ffffffff-ffff-ffff-ffff-ffffffffffff";
|
|
const wrongMvidEvidence = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
identity_scope: {
|
|
...members.identity_scope,
|
|
requires_mvid: wrongMvidValue,
|
|
},
|
|
},
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
},
|
|
);
|
|
const wrongArtifactEvidence = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
identity_scope: {
|
|
...members.identity_scope,
|
|
requires_artifact_sha256: "b".repeat(64),
|
|
},
|
|
},
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
},
|
|
);
|
|
|
|
const wrongMvidResult = projectManagedApplicationGraphEvidence({
|
|
managed_members: wrongMvidEvidence,
|
|
});
|
|
expect(wrongMvidResult.ok).toBe(false);
|
|
if (wrongMvidResult.ok)
|
|
throw new Error("mismatched token MVID was accepted");
|
|
expect(JSON.stringify(wrongMvidResult.error)).toContain("token scope MVID");
|
|
|
|
const wrongArtifact = projectManagedApplicationGraphEvidence({
|
|
managed_members: wrongArtifactEvidence,
|
|
});
|
|
expect(wrongArtifact.ok).toBe(false);
|
|
if (wrongArtifact.ok)
|
|
throw new Error("mismatched token artifact digest was accepted");
|
|
expect(JSON.stringify(wrongArtifact.error)).toContain(
|
|
"token scope SHA-256",
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
"module identity absent",
|
|
"module MVID unknown",
|
|
"token scope MVID unknown",
|
|
] as const)("rejects inconsistent identity when %s", (variant) => {
|
|
const { memberEvidence, boundaryEvidence } = createManagedInteropEvidence();
|
|
const members = managedMemberInspectionSchema.parse(
|
|
parseEvidence(memberEvidence).normalized_result,
|
|
);
|
|
if (members.module === null || members.module.mvid === null)
|
|
throw new Error("Expected fixture module identity and MVID");
|
|
const module =
|
|
variant === "module identity absent"
|
|
? null
|
|
: variant === "module MVID unknown"
|
|
? { ...members.module, mvid: null }
|
|
: members.module;
|
|
const identity_scope =
|
|
variant === "token scope MVID unknown"
|
|
? { ...members.identity_scope, requires_mvid: null }
|
|
: members.identity_scope;
|
|
const inconsistentMembers = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: { ...members, module, identity_scope },
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
},
|
|
);
|
|
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_members: inconsistentMembers,
|
|
managed_native_boundaries: boundaryEvidence,
|
|
});
|
|
|
|
expect(projected.ok).toBe(false);
|
|
if (projected.ok)
|
|
throw new Error("An inconsistent token scope was accepted");
|
|
expect(JSON.stringify(projected.error)).toContain("token scope MVID");
|
|
});
|
|
});
|
|
|
|
describe("unknown managed application graph token scope", () => {
|
|
it("keeps absent MVID scope unknown and marks cross-source token joins partial", () => {
|
|
const { memberEvidence, boundaryEvidence } = createManagedInteropEvidence();
|
|
const members = managedMemberInspectionSchema.parse(
|
|
parseEvidence(memberEvidence).normalized_result,
|
|
);
|
|
const boundaries = managedNativeBoundaryInspectionSchema.parse(
|
|
parseEvidence(boundaryEvidence).normalized_result,
|
|
);
|
|
const unknownMembers = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
module:
|
|
members.module === null
|
|
? null
|
|
: {
|
|
...members.module,
|
|
mvid: null,
|
|
enc_id: null,
|
|
enc_base_id: null,
|
|
},
|
|
identity_scope: { ...members.identity_scope, requires_mvid: null },
|
|
},
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
},
|
|
);
|
|
|
|
const projected = projectManagedApplicationGraphEvidence({
|
|
managed_members: unknownMembers,
|
|
managed_native_boundaries: boundaryEvidence,
|
|
});
|
|
if (!projected.ok) throw projected.error;
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projected.value).normalized_result,
|
|
);
|
|
const graph = parseJavaScriptApplicationGraph(result.graph);
|
|
expect(result.summary.modules).toBe(1);
|
|
expect(result.coverage.status).toBe("partial");
|
|
expect(result.limitations).toContainEqual(
|
|
expect.stringMatching(
|
|
/boundary tokens.*not linked.*member nodes.*MVID.*not established/iu,
|
|
),
|
|
);
|
|
const moduleObservation = graph.nodes.find(
|
|
({ kind }) => kind === "managed-module",
|
|
)?.observations[0];
|
|
expect(moduleObservation?.properties.mvid).toBe(boundaries.module?.mvid);
|
|
expect(moduleObservation?.evidence.extractor.operation).toBe(
|
|
"inspect_managed_native_boundaries",
|
|
);
|
|
const method = graph.nodes.find(
|
|
({ kind, observations }) =>
|
|
kind === "managed-method" &&
|
|
observations[0]?.label === "Fixture.Program.Main",
|
|
);
|
|
expect(method).toBeDefined();
|
|
const importEdge = graph.edges.find(
|
|
({ relation, target_node_id }) =>
|
|
relation === "imports" &&
|
|
graph.nodes.find(({ node_id }) => node_id === target_node_id)?.kind ===
|
|
"managed-pinvoke-import",
|
|
);
|
|
expect(importEdge).toBeDefined();
|
|
expect(
|
|
graph.nodes.find(({ node_id }) => node_id === importEdge?.source_node_id)
|
|
?.kind,
|
|
).toBe("artifact");
|
|
});
|
|
});
|
|
|
|
describe("managed application graph coverage", () => {
|
|
it("preserves partial parser coverage in graph and per-fact coverage", () => {
|
|
const bytes = buildManagedPeFixture();
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const parserPartialMembers = {
|
|
...members,
|
|
coverage: {
|
|
state: "partial" as const,
|
|
issues: [
|
|
{
|
|
code: "invalid-blob" as const,
|
|
scope: "metadata.#Blob",
|
|
offset: 0x0a00,
|
|
detail: "Blob content leaves #Blob",
|
|
},
|
|
],
|
|
},
|
|
};
|
|
const parserPartialEvidence = createEvidence(
|
|
binary,
|
|
MANAGED_STATIC_PROVIDER,
|
|
{
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: parserPartialMembers,
|
|
rawResult: null,
|
|
limitations: parserPartialMembers.limitations,
|
|
},
|
|
);
|
|
const parserPartialProjection = projectManagedApplicationGraphEvidence({
|
|
managed_members: parserPartialEvidence,
|
|
});
|
|
|
|
if (!parserPartialProjection.ok)
|
|
throw new Error("partial projection failed");
|
|
const parserPartialResult = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(parserPartialProjection.value).normalized_result,
|
|
);
|
|
expect(parserPartialResult.coverage).toMatchObject({
|
|
status: "partial",
|
|
});
|
|
expect(parserPartialResult.graph.coverage).toEqual({
|
|
status: "partial",
|
|
truncated: false,
|
|
omitted_count: null,
|
|
limits: [],
|
|
});
|
|
const complete = memberGraph(
|
|
buildManagedPeFixture({ ilBody: tinyIl([0x2a]) }),
|
|
);
|
|
for (const [graph, status, evidenceId] of [
|
|
[parserPartialResult.graph, "partial", parserPartialEvidence.evidence_id],
|
|
[complete.graph, "complete", complete.memberEvidence.evidence_id],
|
|
] as const) {
|
|
const expected = {
|
|
status,
|
|
truncated: false,
|
|
omitted_count: status === "partial" ? null : 0,
|
|
limits: [],
|
|
};
|
|
for (const kind of [
|
|
"managed-type",
|
|
"managed-method",
|
|
"managed-field",
|
|
] as const) {
|
|
const nodes = graph.nodes.filter((node) => node.kind === kind);
|
|
expect(nodes.length).toBeGreaterThan(0);
|
|
for (const node of nodes) {
|
|
expect(node.observations.length).toBeGreaterThan(0);
|
|
for (const observation of node.observations) {
|
|
expect(observation.evidence.coverage).toEqual(expected);
|
|
expect(observation.evidence.evidence_ids).toContain(evidenceId);
|
|
}
|
|
}
|
|
const ids = new Set(nodes.map(({ node_id }) => node_id));
|
|
const contains = graph.edges.filter(
|
|
(edge) =>
|
|
edge.relation === "contains" && ids.has(edge.target_node_id),
|
|
);
|
|
expect(contains).toHaveLength(nodes.length);
|
|
for (const edge of contains) {
|
|
expect(edge.evidence.coverage).toEqual(expected);
|
|
expect(edge.evidence.evidence_ids).toContain(evidenceId);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
it("reports an unavailable source as unknown, not partial", () => {
|
|
const bytes = buildManagedPeFixture();
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const evidence = (state: "partial" | "unavailable") =>
|
|
createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: { ...members, coverage: { state, issues: [] } },
|
|
rawResult: null,
|
|
limitations: members.limitations,
|
|
});
|
|
const project = (state: "partial" | "unavailable") => {
|
|
const projection = projectManagedApplicationGraphEvidence({
|
|
managed_members: evidence(state),
|
|
});
|
|
if (!projection.ok) throw new Error("projection failed");
|
|
return managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projection.value).normalized_result,
|
|
);
|
|
};
|
|
|
|
const unavailable = project("unavailable");
|
|
expect(unavailable.coverage).toEqual({ status: "unknown" });
|
|
expect(unavailable.graph.coverage).toEqual({
|
|
status: "unknown",
|
|
truncated: false,
|
|
omitted_count: null,
|
|
limits: [],
|
|
});
|
|
expect(unavailable.limitations).toContainEqual(
|
|
expect.stringMatching(/managed Evidence record is unavailable/iu),
|
|
);
|
|
expect(unavailable.limitations).not.toContainEqual(
|
|
expect.stringMatching(/partial coverage/iu),
|
|
);
|
|
// A known gap alone stays partial.
|
|
expect(project("partial").coverage).toEqual({ status: "partial" });
|
|
});
|
|
});
|
|
|
|
describe("managed application graph type selection", () => {
|
|
const project = (
|
|
result: Parameters<typeof createEvidence>[2]["result"],
|
|
bytes = buildManagedPeFixture(),
|
|
) => {
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
|
|
const projection = projectManagedApplicationGraphEvidence({
|
|
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result,
|
|
rawResult: null,
|
|
limitations: [],
|
|
}),
|
|
});
|
|
if (!projection.ok) throw new Error("projection failed");
|
|
return managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projection.value).normalized_result,
|
|
);
|
|
};
|
|
|
|
it("marks a graph from selected member inspection partial", () => {
|
|
const bytes = buildManagedPeFixture();
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
|
|
const selected = inspectManagedMembersBytes(bytes, binary, {
|
|
typeNames: ["Fixture.Program"],
|
|
});
|
|
|
|
expect(selected.coverage.state).toBe("complete");
|
|
const result = project(selected);
|
|
expect(result.coverage).toMatchObject({ status: "partial" });
|
|
expect(result.limitations).toContainEqual(
|
|
expect.stringMatching(
|
|
/Fixture\.Program.*type, method, and field.*omitted, not absent/iu,
|
|
),
|
|
);
|
|
expect(result.limitations).not.toContainEqual(
|
|
expect.stringMatching(/managed Evidence.*partial coverage/iu),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("managed call and field-access relations", () => {
|
|
// Both fixture methods share this body: 0x06000001 and 0x06000002.
|
|
const relationsFixture = () =>
|
|
buildManagedPeFixture({
|
|
methodRowCount: 2,
|
|
ilBody: tinyIl([
|
|
...[0x28, 0x02, 0, 0, 0x06], // call 0x06000002
|
|
...[0x6f, 0x02, 0, 0, 0x06], // callvirt 0x06000002
|
|
...[0x28, 0x01, 0, 0, 0x06], // call 0x06000001
|
|
...[0xfe, 0x06, 0x02, 0, 0, 0x06], // ldftn 0x06000002
|
|
...[0x28, 0x01, 0, 0, 0x0a], // call MemberRef 0x0a000001
|
|
0x02,
|
|
...[0x7b, 0x01, 0, 0, 0x04], // ldfld 0x04000001
|
|
0x02,
|
|
0x02,
|
|
...[0x7d, 0x01, 0, 0, 0x04], // stfld 0x04000001
|
|
0x02,
|
|
...[0x7c, 0x01, 0, 0, 0x04], // ldflda 0x04000001
|
|
0x26,
|
|
0x2a,
|
|
]),
|
|
});
|
|
|
|
it("links in-module calls and field accesses once per source and target", () => {
|
|
const { members, memberEvidence, result, graph } =
|
|
memberGraph(relationsFixture());
|
|
expect(members.call_edges).toHaveLength(10);
|
|
expect(members.field_accesses).toHaveLength(6);
|
|
const memberTokens = new Map(
|
|
graph.nodes.map((node) => [
|
|
node.node_id,
|
|
node.observations[0]?.properties.token,
|
|
]),
|
|
);
|
|
const relation = (name: string) =>
|
|
graph.edges
|
|
.filter((edge) => edge.relation === name)
|
|
.map((edge) => ({
|
|
source: memberTokens.get(edge.source_node_id),
|
|
target: memberTokens.get(edge.target_node_id),
|
|
properties: edge.properties,
|
|
evidence_ids: edge.evidence.evidence_ids,
|
|
}));
|
|
// These distinct metadata methods intentionally have the same display name.
|
|
expect(
|
|
members.methods.map(
|
|
({ declaring_type, name }) => `${declaring_type}.${name}`,
|
|
),
|
|
).toEqual(["Fixture.Program.Main", "Fixture.Program.Main"]);
|
|
const [first, second] = members.methods.map(({ token }) => token);
|
|
expect(first).not.toBe(second);
|
|
const field = members.fields[0]?.token;
|
|
const evidence_ids = [memberEvidence.evidence_id];
|
|
|
|
const calls = relation("calls");
|
|
expect(calls).toHaveLength(2);
|
|
expect(calls).toEqual(
|
|
expect.arrayContaining([
|
|
{
|
|
source: first,
|
|
target: second,
|
|
properties: {
|
|
kind: "managed-call",
|
|
site_count: 2,
|
|
opcodes: { call: 1, callvirt: 1 },
|
|
},
|
|
evidence_ids,
|
|
},
|
|
{
|
|
source: second,
|
|
target: first,
|
|
properties: {
|
|
kind: "managed-call",
|
|
site_count: 1,
|
|
opcodes: { call: 1 },
|
|
},
|
|
evidence_ids,
|
|
},
|
|
]),
|
|
);
|
|
const accesses = relation("accesses");
|
|
expect(accesses).toHaveLength(2);
|
|
for (const source of [first, second])
|
|
expect(accesses).toContainEqual({
|
|
source,
|
|
target: field,
|
|
properties: {
|
|
kind: "managed-field-access",
|
|
access: ["address", "read", "write"],
|
|
site_count: 3,
|
|
opcodes: { ldfld: 1, ldflda: 1, stfld: 1 },
|
|
},
|
|
evidence_ids,
|
|
});
|
|
expect(result.summary).toMatchObject({ calls: 2, field_accesses: 2 });
|
|
// Two MemberRef calls, two ldftn loads and three self-calls stay counted.
|
|
expect(result.coverage).toEqual({ status: "partial" });
|
|
expect(result.graph.coverage).toEqual({
|
|
status: "partial",
|
|
truncated: false,
|
|
omitted_count: 7,
|
|
limits: [],
|
|
});
|
|
expect(result.limitations).toContainEqual(
|
|
expect.stringMatching(
|
|
/inspected module.*omitted: 2 member-reference, 2 function-pointer.*3 recursive self-call/iu,
|
|
),
|
|
);
|
|
});
|
|
|
|
it("refuses graph Evidence without relation counters", () => {
|
|
const { result } = memberGraph(relationsFixture());
|
|
const { calls: _calls, ...summary } = result.summary;
|
|
|
|
expect(
|
|
managedApplicationGraphResultSchema.safeParse({ ...result, summary })
|
|
.error?.issues[0]?.path,
|
|
).toEqual(["summary", "calls"]);
|
|
});
|
|
|
|
it("counts relations whose endpoints a type selection omitted", () => {
|
|
const bytes = relationsFixture();
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedCalls.exe");
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const projection = projectManagedApplicationGraphEvidence({
|
|
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
// Drop the second method, as a narrower selection would.
|
|
result: { ...members, methods: members.methods.slice(0, 1) },
|
|
rawResult: null,
|
|
limitations: [],
|
|
}),
|
|
});
|
|
if (!projection.ok) throw new Error(JSON.stringify(projection.error));
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projection.value).normalized_result,
|
|
);
|
|
|
|
expect(result.summary).toMatchObject({ calls: 0, field_accesses: 1 });
|
|
expect(
|
|
result.limitations.find((text) => text.startsWith("Calls edges")),
|
|
).toContain("missing-node");
|
|
expect(
|
|
result.limitations.find((text) => text.startsWith("Accesses edges")),
|
|
).toContain("3 missing-node");
|
|
});
|
|
});
|
|
|
|
it("keeps unfamiliar field opcodes unknown in managed graph Evidence", () => {
|
|
const bytes = buildManagedPeFixture({
|
|
ilBody: tinyIl([0x02, 0x7b, 0x01, 0, 0, 0x04, 0x26, 0x2a]),
|
|
});
|
|
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedCalls.exe");
|
|
const members = inspectManagedMembersBytes(bytes, binary);
|
|
const projection = projectManagedApplicationGraphEvidence({
|
|
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result: {
|
|
...members,
|
|
field_accesses: [
|
|
{ ...members.field_accesses[0]!, opcode: "constructor" },
|
|
],
|
|
},
|
|
limitations: [],
|
|
}),
|
|
});
|
|
if (!projection.ok) throw projection.error;
|
|
const result = managedApplicationGraphResultSchema.parse(
|
|
parseEvidence(projection.value).normalized_result,
|
|
);
|
|
expect(
|
|
result.graph.edges.find((edge) => edge.relation === "accesses")?.properties,
|
|
).toMatchObject({ access: ["unknown"], opcodes: { constructor: 1 } });
|
|
});
|