test(apktool): share decoded string assertions (#16003)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 14:24:02 +00:00
1 parent 480d68b342
commit 1018ea65f4
1 file changed
+63 -91
+63 -91
View File
@@ -4,6 +4,7 @@ import { ok } from "../domain/result.js";
import {
parseApktoolYml,
parseDecodedStrings,
type DecodedStringEntry,
parseManifestPackage,
} from "./ApktoolDecodeOutput.js";
@@ -298,6 +299,14 @@ describe("parseManifestPackage", () => {
});
});
const expectDecodedStrings = (
xml: string,
entries: readonly DecodedStringEntry[],
unparsedCount: number,
): void => {
expect(parseDecodedStrings(xml)).toEqual(ok({ entries, unparsedCount }));
};
describe("parseDecodedStrings", () => {
it("projects entries from a real decoded strings.xml", () => {
const stringsXml = [
@@ -309,61 +318,47 @@ describe("parseDecodedStrings", () => {
"</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,
}),
expectDecodedStrings(
stringsXml,
[
{ name: "api_endpoint", value: "https://api.example.com/v1" },
{ name: "app_name", value: "ApktoolProbe" },
{ name: "debug_flag", value: "true" },
],
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,
}),
);
expectDecodedStrings(stringsXml, [{ name: "plain", value: "ok" }], 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,
}),
expectDecodedStrings(
stringsXml,
[{ name: "escaped", value: "a &amp; b &lt;c&gt;" }],
0,
);
});
it("ignores foreign namespace string elements", () => {
const xml =
'<resources><string xmlns="urn:vendor" name="foreign">decoy</string><string name="local">one</string></resources>';
expect(parseDecodedStrings(xml)).toEqual(
ok({ entries: [{ name: "local", value: "one" }], unparsedCount: 0 }),
);
expectDecodedStrings(xml, [{ name: "local", value: "one" }], 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,
}),
expectDecodedStrings(
'<resources><string name="good">Hello</string><string name="edge">\uFFFD</string></resources>',
[
{ name: "good", value: "Hello" },
{ name: "edge", value: "\uFFFD" },
],
0,
);
});
@@ -382,71 +377,46 @@ describe("parseDecodedStrings", () => {
});
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,
}),
expectDecodedStrings(
'<resources><string translatable="false" name="empty"/><string name="paired"></string></resources>',
[
{ name: "empty", value: "" },
{ name: "paired", value: "" },
],
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,
}),
expectDecodedStrings(
'<resources><!-- <string name="fake">bad</string> --><string name="real">before<!-- note -->after</string></resources>',
[{ name: "real", value: "beforeafter" }],
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,
}),
expectDecodedStrings(
'<resources><string name="a">one</string></resources><!-- </string> -->',
[{ name: "a", value: "one" }],
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,
}),
expectDecodedStrings(
'<resources><string name="raw"> a &amp; b </string></resources>',
[{ name: "raw", value: " a &amp; b " }],
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,
}),
expectDecodedStrings(
`<resources>${newline} <string name="raw">x &amp; y</string>${newline}</resources>`,
[{ name: "raw", value: "x &amp; y" }],
0,
);
}
});
@@ -454,9 +424,11 @@ describe("parseDecodedStrings", () => {
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 }));
expectDecodedStrings(
"<resources><string>value</string></resources>",
[],
1,
);
});
it("distinguishes malformed XML from counted unsupported string markup", () => {
@@ -470,11 +442,11 @@ describe("string document validity and omission counts", () => {
failure: { format: "xml" },
},
});
expect(
parseDecodedStrings(
'<resources><string name="styled">Hello <b>world</b></string></resources>',
),
).toEqual(ok({ entries: [], unparsedCount: 1 }));
expectDecodedStrings(
'<resources><string name="styled">Hello <b>world</b></string></resources>',
[],
1,
);
});
});