Files
reverse-engineering-anything/src/apktool/ApktoolDecodeOutput.test.ts
T
morluto 78fd365bdd refactor(macho): name load-command codes through the shared LC table (#10714)
* refactor(macho): name load-command codes through the shared LC table

* refactor(macho): name version-min and string commands through the shared tables

* refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure (#10760)

* refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure

* refactor(xml): type XML parse failures as malformed or unsupported only

* refactor(parsers): catch per-format document and source failure classes (#10794)

* refactor(parsers): report apktool strings and XML plist source failures through ParseFailure

* refactor(apktool): drop the unused ParseFailureError import

* refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes (#10818)

* refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes

* refactor(plist): report property-list and keyed-archive failures through ParseFailure (#10822)

* refactor(plist): report property-list and keyed-archive failures through ParseFailure

* refactor(kuna): report Kuna document failures through ParseFailure (#10824)

* refactor(kuna): report Kuna document failures through ParseFailure

* test(kuna): pin the limit and malformed facts of Kuna document failures

* refactor(plist): report plist decode budgets through ParseFailure (#10842)

* refactor(plist): report plist decode budgets through ParseFailure

The plist preflight owner throws a ParseFailure limit (plist-decode-bytes) for its byte budget, the shared PlistNestingLimitError for nesting depth, and a malformed failure for a reference cycle, instead of a per-format PropertyListDecodeBudgetFailure whose meaning each caller re-mapped through a fail callback. The fail option, PropertyListDecodeBudgetFailure and interfaceBuilderDecodeBudgetFailure are removed; the parser-format-failure-class baseline reaches zero.

* refactor(plist): report keyed-archive allowances as typed limit failures (#10844)

keyedArchive.ts and decodeKeyedArchiveBytes throw decodeLimit parse failures (keyed-archive-bytes, -object-table-entries, -values-per-object, -reference-edges) and malformed parse failures instead of plain RangeError/TypeError. KeyedArchiveReader no longer classifies every RangeError as a limit: only a limit parse failure or a host stack/capacity limit is one. The unreachable 128-level scan nesting check is removed; projectPlistValue already refuses the archive first.

* test(plist): expect the nesting limit as its ParseFailure carrier

* refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation (#10895)

* refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation

* fix(plist): report a cut-short NIB header or plist length record as truncated

* fix(native): project the selected Mach-O's parse failures through the shared owner

nativeMachoSourcePrimary mapped every non-limit ParseFailureError to
AnalysisOutputError, so a malformed or truncated selected Mach-O was
reported as unreadable tool output and an unsupported one lost its
category. It now uses parseFailureAnalysisError like the ELF, Go and
other byte-format providers: malformed and truncated bytes are an
invalid path, unsupported bytes an unsupported target, and a limit
still falls through to its producer's budget.
2026-11-15 18:24:35 +08:00

473 lines
14 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ok } from "../domain/result.js";
import {
parseApktoolYml,
parseDecodedStrings,
parseManifestPackage,
} from "./ApktoolDecodeOutput.js";
const REAL_YML = [
"!!brut.androlib.meta.MetaInfo",
"apkFileName: probe.apk",
"compressionType: false",
"doNotCompress:",
"- resources.arsc",
"isFrameworkApk: false",
"packageInfo:",
" forcedPackageId: '127'",
" renameManifestPackage: null",
"sdkInfo:",
" minSdkVersion: '24'",
" targetSdkVersion: '34'",
"sharedLibrary: false",
"sparseResources: false",
"unknownFiles: {}",
"usesFramework:",
" ids:",
" - 1",
" tag: null",
"versionInfo:",
" versionCode: '7'",
" versionName: 1.2.3",
"",
].join("\n");
describe("parseApktoolYml", () => {
it("projects version and SDK facts from a real apktool.yml", () => {
expect(parseApktoolYml(REAL_YML)).toEqual({
versionName: "1.2.3",
versionCode: "7",
minSdkVersion: "24",
targetSdkVersion: "34",
});
});
it("accepts quoted version values some builds emit", () => {
expect(
parseApktoolYml(
'!!brut.androlib.meta.MetaInfo\nversionInfo:\n versionName: "2.0.0"\n versionCode: 45\n',
),
).toEqual({
versionName: "2.0.0",
versionCode: "45",
minSdkVersion: null,
targetSdkVersion: null,
});
});
it("reads only the producer's nested fields and preserves scalar spelling", () => {
expect(
parseApktoolYml(
[
"!!brut.androlib.meta.MetaInfo",
"versionName: decoy",
"versionInfo:",
" versionName: 01.2.3",
" versionCode: 045",
" custom:",
" versionName: unrelated",
"sdkInfo:",
" minSdkVersion: 24",
" targetSdkVersion: '35'",
"",
].join("\n"),
),
).toEqual({
versionName: "01.2.3",
versionCode: "045",
minSdkVersion: "24",
targetSdkVersion: "35",
});
});
it("distinguishes YAML nulls from quoted null strings in flow mappings", () => {
expect(
parseApktoolYml(
[
"!!brut.androlib.meta.MetaInfo",
'versionInfo: { versionName: "null", versionCode: 0045, nested: { versionName: decoy } }',
"sdkInfo: { minSdkVersion: null, targetSdkVersion: ~ }",
"",
].join("\n"),
),
).toEqual({
versionName: "null",
versionCode: "0045",
minSdkVersion: null,
targetSdkVersion: null,
});
});
it("reports null facts for unrelated YAML", () => {
expect(parseApktoolYml("version: 2.7.0\n")).toEqual({
versionName: null,
versionCode: null,
minSdkVersion: null,
targetSdkVersion: null,
});
});
// Source-derived fixtures from Apktool 3.0.3 VersionInfo.write and
// YamlStringEscapeUtils at 18b5e99cb56ff9451e8aa55b065dcf5bbd616975.
it.each([
["1.0 #beta", "1.0 #beta"],
[String.raw`1.0\\beta`, String.raw`1.0\beta`],
[String.raw`1.0 \uD83D\uDE80`, "1.0 🚀"],
[String.raw`literal \\uD83D\\uDE80`, String.raw`literal \uD83D\uDE80`],
[String.raw`release \"quoted\"`, 'release "quoted"'],
["'release'", "'release'"],
["1.0: beta", "1.0: beta"],
["[beta]", "[beta]"],
[" release ", " release "],
["release\u2028beta", "release\u2028beta"],
["null", "null"],
["", ""],
['"1.0 #beta"', "1.0 #beta"],
])(
"reads Apktool 3 version string %j without YAML scalar loss",
(source, expected) => {
expect(
parseApktoolYml(
`version: 3.0.3\nversionInfo:\n versionCode: 7\n versionName: ${source}\nsdkInfo:\n minSdkVersion: 24\n targetSdkVersion: 35\n`,
),
).toEqual({
versionName: expected,
versionCode: "7",
minSdkVersion: "24",
targetSdkVersion: "35",
});
},
);
it("keeps Apktool 2 quoted and plain YAML semantics", () => {
expect(
parseApktoolYml(
String.raw`version: 2.7.0
versionInfo:
versionName: 'literal \n #beta'
versionCode: 0045
sdkInfo:
minSdkVersion: null
targetSdkVersion: 35 # comment
`,
),
).toEqual({
versionName: String.raw`literal \n #beta`,
versionCode: "0045",
minSdkVersion: null,
targetSdkVersion: "35",
});
});
it("keeps Apktool 3 field ownership across nested decoys and CRLF", () => {
expect(
parseApktoolYml(
[
"version: 3.0.3",
"versionName: root decoy",
"versionInfo:",
" custom:",
" versionName: nested decoy",
" versionName: 1.0 #beta",
"other:",
" versionName: unrelated decoy",
"",
].join("\r\n"),
),
).toMatchObject({ versionName: "1.0 #beta" });
});
it.each([
"versionInfo:\n versionName: one\n versionName: two\n",
"versionInfo:\n versionName: one\nversionInfo:\n versionName: two\n",
"version: 2.7.0\nversionInfo:\n versionName: ambiguous\n",
String.raw`versionInfo:
versionName: invalid\uZZZZ
`,
])("rejects ambiguous or malformed Apktool 3 fields", (fields) => {
expect(() => parseApktoolYml(`version: 3.0.3\n${fields}`)).toThrow();
});
});
describe("parseApktoolYml Apktool 2.9+ metadata dialect", () => {
// Source-derived from ApkInfo.write, VersionInfo.write and YamlWriter at
// Apktool v2.9.0 and v2.12.1, whose string escaping matches 3.0.3.
const yml = (version: string, versionName: string) =>
[
`version: ${version}`,
"apkFileName: app-release.apk",
"isFrameworkApk: false",
"usesFramework:",
" ids:",
" - 1",
" tag: null",
"sdkInfo:",
" minSdkVersion: 24",
" targetSdkVersion: 34",
"packageInfo:",
" forcedPackageId: 127",
" renameManifestPackage: null",
"versionInfo:",
" versionCode: 7",
` versionName: ${versionName}`,
"sharedLibrary: false",
"sparseResources: false",
"doNotCompress:",
"- arsc",
"",
].join("\n");
it.each([
["2.9.0", "1.0 #beta", "1.0 #beta"],
["2.9.3", String.raw`release \"quoted\"`, 'release "quoted"'],
["2.12.1", "1.0: beta", "1.0: beta"],
["2.12.1", "", ""],
])(
"reads Apktool %s version string %j as its writer escaped it",
(version, source, expected) => {
expect(parseApktoolYml(yml(version, source))).toEqual({
versionName: expected,
versionCode: "7",
minSdkVersion: "24",
targetSdkVersion: "34",
});
},
);
});
describe("parseManifestPackage", () => {
it("reads the package attribute from a decoded manifest", () => {
const manifest =
'<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="34" package="com.rea.apktool.probe" platformBuildVersionCode="34">';
expect(parseManifestPackage(`${manifest}</manifest>`)).toBe(
"com.rea.apktool.probe",
);
});
it("reads package with reordered or single-quoted XML attributes", () => {
expect(
parseManifestPackage("<manifest debug='false' package='org.rea.app' />"),
).toBe("org.rea.app");
});
it("reports null when no package attribute exists", () => {
expect(parseManifestPackage("<manifest>\n</manifest>\n")).toBeNull();
});
it("reads the package when unrelated manifest text contains U+FFFD", () => {
expect(
parseManifestPackage(
'<manifest package="org.rea.app"><application label="\uFFFD"/></manifest>',
),
).toBe("org.rea.app");
});
it("still rejects malformed attributes alongside U+FFFD", () => {
expect(() =>
parseManifestPackage(
'<manifest package=org.rea.app><application label="\uFFFD"/></manifest>',
),
).toThrow(/AndroidManifest.xml is not well-formed XML/u);
});
it("rejects a well-formed document with the wrong root element", () => {
expect(() => parseManifestPackage("<resources/>")).toThrowError(
new TypeError("AndroidManifest.xml root must be <manifest>"),
);
});
});
describe("parseDecodedStrings", () => {
it("projects entries from a real decoded strings.xml", () => {
const stringsXml = [
'<?xml version="1.0" encoding="utf-8"?>',
"<resources>",
' <string name="api_endpoint">https://api.example.com/v1</string>',
' <string name="app_name">ApktoolProbe</string>',
' <string name="debug_flag">true</string>',
"</resources>",
"",
].join("\n");
expect(parseDecodedStrings(stringsXml)).toEqual(
ok({
entries: [
{ name: "api_endpoint", value: "https://api.example.com/v1" },
{ name: "app_name", value: "ApktoolProbe" },
{ name: "debug_flag", value: "true" },
],
unparsedCount: 0,
}),
);
});
it("counts entries with nested markup instead of flattening them", () => {
const stringsXml =
'<resources><string name="styled">Hello <b>world</b></string><string name="plain">ok</string></resources>';
expect(parseDecodedStrings(stringsXml)).toEqual(
ok({
entries: [{ name: "plain", value: "ok" }],
unparsedCount: 1,
}),
);
});
it("preserves escaped entities verbatim", () => {
const stringsXml =
'<resources><string name="escaped">a &amp; b &lt;c&gt;</string></resources>';
expect(parseDecodedStrings(stringsXml)).toEqual(
ok({
entries: [{ name: "escaped", value: "a &amp; b &lt;c&gt;" }],
unparsedCount: 0,
}),
);
});
it("preserves literal U+FFFD and unaffected sibling strings", () => {
expect(
parseDecodedStrings(
'<resources><string name="good">Hello</string><string name="edge">\uFFFD</string></resources>',
),
).toEqual(
ok({
entries: [
{ name: "good", value: "Hello" },
{ name: "edge", value: "\uFFFD" },
],
unparsedCount: 0,
}),
);
});
it.each([
"<resources><string name=unquoted>\uFFFD</string></resources>",
'<resources><string name="broken">\uFFFD</resources>',
])("still rejects malformed XML containing U+FFFD: %s", (xml) => {
expect(parseDecodedStrings(xml)).toMatchObject({
ok: false,
error: {
_tag: "ParseFailureError",
kind: "malformed",
failure: { format: "xml" },
},
});
});
it("keeps empty, self-closing, and reordered string attributes", () => {
expect(
parseDecodedStrings(
'<resources><string translatable="false" name="empty"/><string name="paired"></string></resources>',
),
).toEqual(
ok({
entries: [
{ name: "empty", value: "" },
{ name: "paired", value: "" },
],
unparsedCount: 0,
}),
);
});
it("ignores comments without mistaking them for nested markup", () => {
expect(
parseDecodedStrings(
'<resources><!-- <string name="fake">bad</string> --><string name="real">before<!-- note -->after</string></resources>',
),
).toEqual(
ok({
entries: [{ name: "real", value: "beforeafter" }],
unparsedCount: 0,
}),
);
});
it("does not include a trailing document comment in the last string", () => {
expect(
parseDecodedStrings(
'<resources><string name="a">one</string></resources><!-- </string> -->',
),
).toEqual(
ok({
entries: [{ name: "a", value: "one" }],
unparsedCount: 0,
}),
);
});
it("preserves source whitespace and entity spelling", () => {
expect(
parseDecodedStrings(
'<resources><string name="raw"> a &amp; b </string></resources>',
),
).toEqual(
ok({
entries: [{ name: "raw", value: " a &amp; b " }],
unparsedCount: 0,
}),
);
});
it("keeps source slices aligned across CRLF and CR line endings", () => {
for (const newline of ["\r\n", "\r"]) {
expect(
parseDecodedStrings(
`<resources>${newline} <string name="raw">x &amp; y</string>${newline}</resources>`,
),
).toEqual(
ok({
entries: [{ name: "raw", value: "x &amp; y" }],
unparsedCount: 0,
}),
);
}
});
});
describe("string document validity and omission counts", () => {
it("counts a string with no name as unsupported instead of emitting an empty name", () => {
expect(
parseDecodedStrings("<resources><string>value</string></resources>"),
).toEqual(ok({ entries: [], unparsedCount: 1 }));
});
it("distinguishes malformed XML from counted unsupported string markup", () => {
expect(
parseDecodedStrings("<resources><string name='broken'>no</resources>"),
).toMatchObject({
ok: false,
error: {
_tag: "ParseFailureError",
kind: "malformed",
failure: { format: "xml" },
},
});
expect(
parseDecodedStrings(
'<resources><string name="styled">Hello <b>world</b></string></resources>',
),
).toEqual(ok({ entries: [], unparsedCount: 1 }));
});
});
it.each(["<manifest/>", '<other><string name="a">one</string></other>'])(
"keeps a wrong document root separate from an entry count: %s",
(xml) => {
const parsed = parseDecodedStrings(xml);
expect(parsed).toMatchObject({
ok: false,
error: {
_tag: "ParseFailureError",
kind: "malformed",
failure: {
format: "android-string-resources",
field: "documentElement",
},
},
});
expect(parsed).not.toHaveProperty("value");
if (parsed.ok) throw new Error("Expected document failure");
expect(parsed.error).not.toHaveProperty("unparsedCount");
},
);