|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { ANDROID_EMULATOR } from '../../../__tests__/test-utils/index.ts'; |
| 4 | +import { AppError } from '../../../utils/errors.ts'; |
| 5 | +import { fillAndroid } from '../index.ts'; |
| 6 | +import { withAndroidAdbProvider, type AndroidAdbExecutor } from '../adb-executor.ts'; |
| 7 | +import { |
| 8 | + androidFillFailureDetails, |
| 9 | + androidFillFailureMessage, |
| 10 | + readAndroidTextAtPointInHierarchy, |
| 11 | + verifyAndroidFilledTextInHierarchy, |
| 12 | +} from '../fill-verification.ts'; |
| 13 | + |
| 14 | +test('fillAndroid reports when the IME captures input instead of the app field', async () => { |
| 15 | + const calls: string[][] = []; |
| 16 | + let imeText = ''; |
| 17 | + await withFillAdb( |
| 18 | + async (args) => { |
| 19 | + calls.push(args); |
| 20 | + if (isTextInput(args)) imeText = args[3] ?? ''; |
| 21 | + return adbResult(args[0] === 'exec-out' ? imeCaptureHierarchy(imeText) : ''); |
| 22 | + }, |
| 23 | + async () => { |
| 24 | + await assert.rejects( |
| 25 | + () => fillAndroid(ANDROID_EMULATOR, 10, 10, 'chips'), |
| 26 | + (error: unknown) => { |
| 27 | + assert.ok(error instanceof AppError); |
| 28 | + assert.equal(error.code, 'COMMAND_FAILED'); |
| 29 | + assert.match(error.message, /captured by the active keyboard/i); |
| 30 | + assert.equal(error.details?.failureReason, 'ime_capture'); |
| 31 | + assert.equal(inputDetails(error, 'actualInput')?.resourceId, IME_RESOURCE_ID); |
| 32 | + assert.equal( |
| 33 | + inputDetails(error, 'targetInput')?.resourceId, |
| 34 | + 'com.example.shop:id/search', |
| 35 | + ); |
| 36 | + return true; |
| 37 | + }, |
| 38 | + ); |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + assert.equal( |
| 43 | + calls.some((args) => args.join('\n') === 'shell\ncmd\nclipboard\nset\ntext'), |
| 44 | + false, |
| 45 | + ); |
| 46 | + assert.equal( |
| 47 | + calls.some((args) => args.join('\n') === 'shell\ninput\nkeyevent\nKEYCODE_PASTE'), |
| 48 | + false, |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +test('verifyAndroidFilledTextInHierarchy accepts matching-length masked password verification', () => { |
| 53 | + const verification = verifyAndroidFilledTextInHierarchy( |
| 54 | + passwordHierarchy(maskBullets('Test@123')), |
| 55 | + 10, |
| 56 | + 10, |
| 57 | + 'Test@123', |
| 58 | + ); |
| 59 | + |
| 60 | + assert.equal(verification.ok, true); |
| 61 | + assert.equal(verification.masked, true); |
| 62 | +}); |
| 63 | + |
| 64 | +test('fillAndroid accepts matching-length masked password verification', async () => { |
| 65 | + let typed = ''; |
| 66 | + await withFillAdb( |
| 67 | + async (args) => { |
| 68 | + if (isDeleteKey(args)) typed = ''; |
| 69 | + if (isTextInput(args)) typed = args[3] ?? ''; |
| 70 | + return adbResult(args[0] === 'exec-out' ? passwordHierarchy(maskBullets(typed)) : ''); |
| 71 | + }, |
| 72 | + async () => { |
| 73 | + await fillAndroid(ANDROID_EMULATOR, 10, 10, 'Test@123'); |
| 74 | + }, |
| 75 | + ); |
| 76 | +}); |
| 77 | + |
| 78 | +test('verifyAndroidFilledTextInHierarchy redacts masked password values on wrong-length failure', () => { |
| 79 | + const exposedPasswordValue = 'secret-value'; |
| 80 | + const verification = verifyAndroidFilledTextInHierarchy( |
| 81 | + passwordHierarchy(exposedPasswordValue), |
| 82 | + 10, |
| 83 | + 10, |
| 84 | + 'Test@123', |
| 85 | + ); |
| 86 | + |
| 87 | + assert.equal(verification.ok, false); |
| 88 | + assertMaskedPasswordFailure( |
| 89 | + androidFillFailureMessage(verification), |
| 90 | + androidFillFailureDetails('Test@123', verification), |
| 91 | + exposedPasswordValue, |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +test('readAndroidTextAtPointInHierarchy prefers focused edit text over point fallback', () => { |
| 96 | + assert.equal(readAndroidTextAtPointInHierarchy(focusedEditHierarchy(), 10, 10), 'focused value'); |
| 97 | +}); |
| 98 | + |
| 99 | +const IME_RESOURCE_ID = 'com.google.android.inputmethod.latin:id/0_resource_name_obfuscated'; |
| 100 | + |
| 101 | +async function withFillAdb(exec: AndroidAdbExecutor, fn: () => Promise<void>): Promise<void> { |
| 102 | + await withAndroidAdbProvider({ exec }, { serial: ANDROID_EMULATOR.id }, fn); |
| 103 | +} |
| 104 | + |
| 105 | +function adbResult(stdout: string) { |
| 106 | + return { stdout, stderr: '', exitCode: 0 }; |
| 107 | +} |
| 108 | + |
| 109 | +function isTextInput(args: string[]): boolean { |
| 110 | + return args[0] === 'shell' && args[1] === 'input' && args[2] === 'text'; |
| 111 | +} |
| 112 | + |
| 113 | +function isDeleteKey(args: string[]): boolean { |
| 114 | + return args.join('\n') === 'shell\ninput\nkeyevent\nKEYCODE_DEL'; |
| 115 | +} |
| 116 | + |
| 117 | +function inputDetails(error: AppError, key: 'actualInput' | 'targetInput') { |
| 118 | + return error.details?.[key] as Record<string, unknown> | null | undefined; |
| 119 | +} |
| 120 | + |
| 121 | +function assertMaskedPasswordFailure( |
| 122 | + message: string, |
| 123 | + details: Record<string, unknown>, |
| 124 | + exposedPasswordValue: string, |
| 125 | +): void { |
| 126 | + assert.match(message, /could not confirm masked text value/i); |
| 127 | + assert.equal(details.failureReason, 'masked_unverified'); |
| 128 | + assert.equal(details.masked, true); |
| 129 | + assert.equal(details.expected, undefined); |
| 130 | + assert.equal(details.expectedLength, 8); |
| 131 | + assert.equal(details.actual, null); |
| 132 | + assert.equal(details.actualLength, exposedPasswordValue.length); |
| 133 | + assert.equal(detailsInput(details, 'actualInput')?.text, null); |
| 134 | + assert.equal(detailsInput(details, 'actualInput')?.textRedacted, true); |
| 135 | + assert.equal(detailsInput(details, 'targetInput')?.text, null); |
| 136 | + assert.doesNotMatch(JSON.stringify(details), /Test@123|secret-value/); |
| 137 | +} |
| 138 | + |
| 139 | +function detailsInput(details: Record<string, unknown>, key: 'actualInput' | 'targetInput') { |
| 140 | + return details[key] as Record<string, unknown> | null | undefined; |
| 141 | +} |
| 142 | + |
| 143 | +function imeCaptureHierarchy(imeText: string): string { |
| 144 | + return `<?xml version="1.0" encoding="UTF-8"?><hierarchy> |
| 145 | +<node package="com.example.shop" class="android.widget.EditText" text="Search Products" resource-id="com.example.shop:id/search" focused="false" bounds="[0,0][300,100]"/> |
| 146 | +<node package="com.google.android.inputmethod.latin" class="android.widget.EditText" text="${imeText}" resource-id="${IME_RESOURCE_ID}" focused="true" bounds="[0,700][300,800]"/> |
| 147 | +</hierarchy>`; |
| 148 | +} |
| 149 | + |
| 150 | +function passwordHierarchy(mask: string): string { |
| 151 | + return `<?xml version="1.0" encoding="UTF-8"?><hierarchy><node package="com.example" class="android.widget.EditText" text="${mask}" password="true" focused="true" bounds="[0,0][200,100]"/></hierarchy>`; |
| 152 | +} |
| 153 | + |
| 154 | +function focusedEditHierarchy(): string { |
| 155 | + return `<?xml version="1.0" encoding="UTF-8"?><hierarchy> |
| 156 | +<node package="com.example" class="android.widget.TextView" text="point fallback" focused="false" bounds="[0,0][200,100]"/> |
| 157 | +<node package="com.example" class="android.widget.EditText" text="focused value" focused="true" bounds="[300,300][500,400]"/> |
| 158 | +</hierarchy>`; |
| 159 | +} |
| 160 | + |
| 161 | +function maskBullets(value: string): string { |
| 162 | + return Array.from(value) |
| 163 | + .map(() => '•') |
| 164 | + .join(''); |
| 165 | +} |
0 commit comments