Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/backends/adb-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,10 @@ describe('AdbBackend.typeText', () => {

await backend.typeText('abc');

expect(inputCalls().at(-1)).toStrictEqual(['input', 'text', 'abc']);
expect(inputCalls().at(-1)).toStrictEqual(['input', 'text', "'abc'"]);
});

it('clears the field, then types the escaped text', async () => {
it('clears the field, then types the quoted text', async () => {
await backend.typeText('a b&c');

expect(inputCalls()).toStrictEqual([
Expand All @@ -930,10 +930,31 @@ describe('AdbBackend.typeText', () => {
'KEYCODE_MOVE_END',
],
['input', 'keyevent', '67'],
['input', 'text', 'a%sb\\&c'],
['input', 'text', "'a b&c'"],
]);
});

it.each([
['abc', ["'abc'"]],
['a b', ["'a b'"]],
["a'b", ["'a'\\''b'"]],
['a"b', [`'a"b'`]],
['$HOME', [`'$HOME'`]],
['`id`', ["'`id`'"]],
['!', ["'!'"]],
['\\', ["'\\'"]],
['*', ["'*'"]],
['%', ["'%'"]],
['50%s (ok)?', [`'50%'`, `'s (ok)?'`]],
["a'b$c!d e", ["'a'\\''b$c!d e'"]],
])('quotes %j for the device shell', async (text, expected) => {
await backend.typeText(text);

expect(inputCalls().slice(3)).toStrictEqual(
expected.map((argument) => ['input', 'text', argument]),
);
});

it('only clears the field for an empty string', async () => {
await backend.typeText('');

Expand Down
35 changes: 28 additions & 7 deletions src/backends/adb-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ function describeDumpRemediation(failures: string): string {
);
}

/**
* Single-quotes text for the device shell: `adb shell` joins its argv unescaped.
*
* @param text - The text to quote.
* @returns The quoted text.
*/
function quoteForDeviceShell(text: string): string {
return `'${text.replace(/'/gu, `'\\''`)}'`;
}

export class AdbBackend implements DeviceBackend {
readonly kind = 'adb' as const;

Expand Down Expand Up @@ -397,9 +407,15 @@ export class AdbBackend implements DeviceBackend {
if (text.length === 0) {
return;
}
// Android input text requires escaping spaces and special chars
const escaped = text.replace(/ /gu, '%s').replace(/[&|;<>]/gu, '\\$&');
await this.#adb(['shell', 'input', 'text', escaped]);
// `input text` reads `%s` as a space, so a literal `%s` is typed in two
// calls; everything else is quoted for the device shell.
const pieces = text.split('%s');
for (const [index, piece] of pieces.entries()) {
const quoted = quoteForDeviceShell(
`${index > 0 ? 's' : ''}${piece}${index < pieces.length - 1 ? '%' : ''}`,
);
await this.#adb(['shell', 'input', 'text', quoted]);
}
}

async swipe(
Expand Down Expand Up @@ -459,7 +475,7 @@ export class AdbBackend implements DeviceBackend {
'pm',
'list',
'packages',
bundleId,
quoteForDeviceShell(bundleId),
]);
if (pmResult.includes(bundleId)) {
return { bundleId, state: 'Installed (not running)' };
Expand Down Expand Up @@ -841,15 +857,20 @@ export class AdbBackend implements DeviceBackend {
'shell',
'monkey',
'-p',
bundleId,
quoteForDeviceShell(bundleId),
'-c',
'android.intent.category.LAUNCHER',
'1',
]);
}

async closeApp(bundleId: string): Promise<void> {
await this.#adb(['shell', 'am', 'force-stop', bundleId]);
await this.#adb([
'shell',
'am',
'force-stop',
quoteForDeviceShell(bundleId),
]);
}

async pressButton(button: DeviceButton): Promise<void> {
Expand Down Expand Up @@ -961,7 +982,7 @@ export class AdbBackend implements DeviceBackend {
async getLogs(durationSeconds = 30, filter?: string): Promise<LogsResult> {
const args = ['shell', 'logcat', '-d', '-t', String(durationSeconds)];
if (filter) {
args.push('-e', filter);
args.push('-e', quoteForDeviceShell(filter));
}
const raw = await this.#adb(args);
const entries = raw
Expand Down
Loading