Files
reverse-engineering-anything/src/android/AndroidNativeInventory.test.ts
T
18b72ca461 fix(android): qualify the JNI library inventory by required splits
A base APK whose manifest requires ABI or density splits keeps its native
libraries in those splits, yet inspect_android_jni_bindings reported a
complete library inventory with zero libraries. The bridge's native
declaration inventory now also decodes the root binary manifest without a
session and reports its requiredSplitTypes and isSplitRequired facts, or why
they are unknown (bridge identity 11). When splits are required or unknown,
the library inventory is partial with an unknown that says so, and a binding
without a candidate is unknown instead of unmatched.

The binary manifest fixture from #9769 was used to check the bridge with the
real jadx-headless-mcp 0.7.1 JAR, including stripped attribute names that
resolve through framework resource IDs.

Fixes #9756

Co-Authored-By: 2SinceJuly <202121119+2SinceJuly@users.noreply.github.com>
Co-Authored-By: Phon <twilyt1@proton.me>
Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-14 16:15:32 +01:00

127 lines
3.9 KiB
TypeScript

import { expect, it } from "vitest";
import { readAndroidNativeInventory } from "./AndroidNativeInventory.js";
import type { JadxToolPort } from "./JadxAnalysis.js";
import type { JsonValue } from "../domain/jsonValue.js";
import type { AndroidNativeDeclarations } from "../domain/android/androidNativeDeclarations.js";
const SNAPSHOT = "/admitted/immutable target.apk";
const row = {
source_index: 0,
class_name: "fixture.Native",
method_name: "read_原",
descriptor: "([B)J",
dex_entry: "classes.dex",
dex_entry_index: 0,
class_index: 2,
method_index: 5,
};
const inventory = () => ({
scope: "apk-root-dex-declarations",
methods: [row],
coverage: {
status: "complete",
dex_entries: 1,
examined_dex_entries: 1,
examined_classes: 3,
examined_methods: 9,
},
unknowns: [] as string[],
manifest_splits: {
status: "observed",
required_split_types: [] as string[],
split_required: false,
},
});
const tools = (value: unknown): JadxToolPort => ({
json: async (name, input) => {
expect(name).toBe("rea_native_declarations");
expect(input).toEqual({ path: SNAPSHOT });
return value as JsonValue;
},
text: async () => {
throw new Error("Native inventory must not request source");
},
listClasses: async () => {
throw new Error("Native inventory must not page display classes");
},
});
it("preserves duplicate native identities from distinct original DEX members", async () => {
const source = inventory();
source.methods.push({
...row,
source_index: 1,
dex_entry: "classes2.dex",
dex_entry_index: 2,
});
source.coverage.dex_entries = 2;
source.coverage.examined_dex_entries = 2;
const actual = await readAndroidNativeInventory(tools(source), SNAPSHOT);
expect(actual.methods).toEqual(source.methods);
expect(actual.coverage.status).toBe("complete");
});
it("keeps the manifest's split requirements and unread split facts", async () => {
for (const manifest_splits of [
{
status: "observed",
required_split_types: ["base__abi", "base__density"],
split_required: true,
},
{ status: "unknown", reason: "Root AndroidManifest.xml is absent" },
] satisfies AndroidNativeDeclarations["manifest_splits"][]) {
const source = { ...inventory(), manifest_splits };
expect(
(await readAndroidNativeInventory(tools(source), SNAPSHOT))
.manifest_splits,
).toEqual(manifest_splits);
}
});
it("keeps usable facts and explicit incomplete DEX coverage", async () => {
const source = inventory();
source.coverage.status = "partial";
source.coverage.dex_entries = 2;
source.unknowns.push("classes2.dex cannot be read");
expect(await readAndroidNativeInventory(tools(source), SNAPSHOT)).toEqual(
source,
);
});
it.each([
(source: ReturnType<typeof inventory>) => {
source.methods[0] = { ...row, descriptor: "not a prototype" };
},
(source: ReturnType<typeof inventory>) => {
source.methods[0] = { ...row, source_index: 7 };
},
(source: ReturnType<typeof inventory>) => {
source.coverage.examined_dex_entries = 2;
},
(source: ReturnType<typeof inventory>) => {
source.coverage.dex_entries = 2;
},
(source: ReturnType<typeof inventory>) => {
source.coverage.examined_methods = 0;
},
(source: ReturnType<typeof inventory>) => {
source.unknowns.push("hidden incomplete input");
},
(source: ReturnType<typeof inventory>) => {
source.methods.push({ ...row, source_index: 1, dex_entry: "other.dex" });
},
// A bridge that predates split facts cannot say whether splits hold code.
(source: ReturnType<typeof inventory>) => {
delete (source as Partial<typeof source>).manifest_splits;
},
])("rejects malformed or inconsistent producer facts (%#)", async (mutate) => {
const source = inventory();
mutate(source);
await expect(
readAndroidNativeInventory(tools(source), SNAPSHOT),
).rejects.toMatchObject({
_tag: "AnalysisOutputError",
});
});