fix(adb): count UTF-8 bytes in logcat total_bytes (#2212)
* fix(adb): count UTF-8 bytes in logcat total_bytes read_adb_logcat assigned the decoded stdout string's UTF-16 code-unit length to total_bytes, undercounting non-ASCII dumps (a 37-byte CJK line reported 33). Measure the UTF-8 byte length instead, matching the field's byte-unit contract and the repo's Buffer.byteLength convention. Fixes #2175 * fix(adb): count raw captured bytes in logcat total_bytes Carry the pre-decode stdout byte length through runAdb so malformed UTF-8 (replaced by U+FFFD on decode) still reports the true captured byte count instead of the re-encoded replacement size.
This commit is contained in:
@@ -508,6 +508,44 @@ it("reads a bounded logcat dump with the caller count and buffer", async () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("reports UTF-8 byte counts in total_bytes for non-ASCII logcat output", async () => {
|
||||
const { binary } = await writeAdbStub(
|
||||
shellStub({
|
||||
logcat:
|
||||
"08-10 13:00:00.000 I/Tag( 1): 中文\n08-10 13:00:00.000 I/Tag( 1): 😀\n",
|
||||
}),
|
||||
);
|
||||
const result = await evidenceResult(providerOver(binary), {
|
||||
operation: "read_adb_logcat",
|
||||
input: { serial: "emulator-5554", count: 500, buffer: "main" },
|
||||
});
|
||||
expect(result.lines).toEqual([
|
||||
"08-10 13:00:00.000 I/Tag( 1): 中文",
|
||||
"08-10 13:00:00.000 I/Tag( 1): 😀",
|
||||
]);
|
||||
// 30-byte prefix + 6-byte CJK + LF = 37; 30-byte prefix + 4-byte emoji + LF = 35.
|
||||
expect(result.total_bytes).toBe(72);
|
||||
});
|
||||
|
||||
it("counts raw captured bytes in total_bytes for malformed UTF-8 logcat output", async () => {
|
||||
const { binary } = await writeAdbStub(
|
||||
() => `if [ "$1" = "version" ]; then ${VERSION_BLOCK}; exit 0; fi
|
||||
if [ "$1" = "-s" ] && [ "$3" = "shell" ]; then
|
||||
printf '%b' '08-10 13:00:00.000 I/Tag( 1): A\\377B\\n'
|
||||
exit 0
|
||||
fi
|
||||
printf 'unexpected arguments: %s\\n' "$@"
|
||||
exit 1`,
|
||||
);
|
||||
const result = await evidenceResult(providerOver(binary), {
|
||||
operation: "read_adb_logcat",
|
||||
input: { serial: "emulator-5554", count: 500, buffer: "main" },
|
||||
});
|
||||
// 30-byte prefix + "A" + one raw 0xff byte + "B" + LF = 34 captured bytes.
|
||||
// Re-encoding the decoded U+FFFD replacement would report 36.
|
||||
expect(result.total_bytes).toBe(34);
|
||||
});
|
||||
|
||||
it("projects package details from dumpsys package output", async () => {
|
||||
const { binary } = await writeAdbStub(
|
||||
shellStub({
|
||||
|
||||
@@ -232,12 +232,17 @@ const runAdbCapture = async (
|
||||
/** Decode textual adb protocols after the shared byte capture and error boundary. */
|
||||
const runAdb = async (
|
||||
...arguments_: Parameters<typeof runAdbCapture>
|
||||
): Promise<Result<{ stdout: string; stderr: string }, AnalysisError>> => {
|
||||
): Promise<
|
||||
Result<{ stdout: string; stderr: string; stdoutBytes: number }, AnalysisError>
|
||||
> => {
|
||||
const captured = await runAdbCapture(...arguments_);
|
||||
if (!captured.ok) return captured;
|
||||
return ok({
|
||||
stdout: captured.value.stdout.toString("utf8"),
|
||||
stderr: captured.value.stderr.toString("utf8"),
|
||||
// Byte count of the captured output before UTF-8 decoding, so malformed
|
||||
// sequences replaced by U+FFFD still report their true captured size.
|
||||
stdoutBytes: captured.value.stdout.byteLength,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -815,7 +820,7 @@ export class AdbProvider {
|
||||
buffer: request.input.buffer,
|
||||
pid: request.input.pid ?? null,
|
||||
lines: bounded,
|
||||
total_bytes: ran.value.stdout.length,
|
||||
total_bytes: ran.value.stdoutBytes,
|
||||
coverage:
|
||||
bounded.length !== lines.length ||
|
||||
bounded.length === request.input.count
|
||||
|
||||
Reference in New Issue
Block a user