|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { parseAndroidRuntimePermissionGrants } from '../permission-grant-state.ts'; |
| 4 | + |
| 5 | +// Captured from `adb shell dumpsys package com.callstack.agentdevicelab` on a Pixel 7 / API 36 |
| 6 | +// emulator, trimmed to the sections that decide the answer. The indentation is load-bearing: |
| 7 | +// `install permissions:` and each `User <id>:` sit at the same depth, and the runtime grants |
| 8 | +// hang under the user block — which is the only reason the two can be told apart. |
| 9 | +const DUMPSYS = [ |
| 10 | + 'Packages:', |
| 11 | + ' Package [com.example.app] (5f3a1c2):', |
| 12 | + ' install permissions:', |
| 13 | + ' android.permission.MODIFY_AUDIO_SETTINGS: granted=true', |
| 14 | + ' android.permission.RECORD_AUDIO: granted=true', |
| 15 | + ' User 0: ceDataInode=1032405 installed=true hidden=false stopped=false', |
| 16 | + ' gids=[3003]', |
| 17 | + ' runtime permissions:', |
| 18 | + ' android.permission.RECORD_AUDIO: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED]', |
| 19 | + ' android.permission.CAMERA: granted=true, flags=[ USER_SET]', |
| 20 | + ' User 10: ceDataInode=99 installed=true hidden=false stopped=false', |
| 21 | + ' runtime permissions:', |
| 22 | + ' android.permission.RECORD_AUDIO: granted=true, flags=[ USER_SET]', |
| 23 | + '', |
| 24 | + 'Queries:', |
| 25 | + ' queryable via interaction:', |
| 26 | + ' User 0:', |
| 27 | + '', |
| 28 | + 'Dexopt state:', |
| 29 | + ' [com.example.app]', |
| 30 | + ' path: /data/app/~~abc==/com.example.app-def==', |
| 31 | +].join('\n'); |
| 32 | + |
| 33 | +// `undefined` is the answer for anything the dump does not place inside the requested user's |
| 34 | +// runtime block: an install-permission grant (`pm revoke` cannot touch it), another profile's |
| 35 | +// grant, or a permission that user never declared. The caller reports that as `unknown`. |
| 36 | +test.each([ |
| 37 | + [0, 'android.permission.RECORD_AUDIO', 'not_granted', 'acting user overrides the install grant'], |
| 38 | + [0, 'android.permission.CAMERA', 'granted', 'acting user'], |
| 39 | + [10, 'android.permission.RECORD_AUDIO', 'granted', 'other profile, only when asked for'], |
| 40 | + [10, 'android.permission.CAMERA', undefined, 'absent for that user'], |
| 41 | + [0, 'android.permission.MODIFY_AUDIO_SETTINGS', undefined, 'install-only'], |
| 42 | + [0, 'android.permission.INTERNET', undefined, 'never mentioned'], |
| 43 | +] as const)('user %s reads %s as %s (%s)', (userId, permission, expected, _why) => { |
| 44 | + assert.equal(parseAndroidRuntimePermissionGrants(DUMPSYS, userId)?.get(permission), expected); |
| 45 | +}); |
| 46 | + |
| 47 | +// A missing block is not an empty one. Every input here means "the device did not tell us", |
| 48 | +// which must stay distinguishable from "the app holds nothing". |
| 49 | +test.each([ |
| 50 | + ['no runtime block for that user', DUMPSYS, 11], |
| 51 | + ['empty output', '', 0], |
| 52 | + ['no Packages section', 'Activity Resolver Table:\n User 0:\n runtime permissions:', 0], |
| 53 | + ['a package section without the user', 'Packages:\n Package [com.example.app] (abc):', 0], |
| 54 | +] as const)('%s reads as unknown', (_label, output, userId) => { |
| 55 | + assert.equal(parseAndroidRuntimePermissionGrants(output, userId), undefined); |
| 56 | +}); |
| 57 | + |
| 58 | +test('an empty runtime block answers not_granted for everything it could have listed', () => { |
| 59 | + // Distinct from a missing block: the device reported the list and it was empty, so the app |
| 60 | + // holds nothing — a claim the parser is allowed to make. |
| 61 | + const grants = parseAndroidRuntimePermissionGrants( |
| 62 | + [ |
| 63 | + 'Packages:', |
| 64 | + ' Package [com.example.app] (5f3a1c2):', |
| 65 | + ' User 0: installed=true', |
| 66 | + ' runtime permissions:', |
| 67 | + ' User 10: installed=true', |
| 68 | + ].join('\n'), |
| 69 | + 0, |
| 70 | + ); |
| 71 | + assert.notEqual(grants, undefined); |
| 72 | + assert.equal(grants?.size, 0); |
| 73 | +}); |
| 74 | + |
| 75 | +test('sections after Packages: cannot reopen the scan', () => { |
| 76 | + // `Queries:` repeats `User 0:` with no grants, and `Shared users:` repeats runtime grant lines |
| 77 | + // for the shared uid. Merging either would flip RECORD_AUDIO to granted for a user whose own |
| 78 | + // block says otherwise — the same wrong-answer class as reading the install section. |
| 79 | + const grants = parseAndroidRuntimePermissionGrants( |
| 80 | + [ |
| 81 | + DUMPSYS, |
| 82 | + 'Shared users:', |
| 83 | + ' SharedUser [android.uid.shared] (a1b2):', |
| 84 | + ' User 0: ceDataInode=0 installed=true', |
| 85 | + ' runtime permissions:', |
| 86 | + ' android.permission.RECORD_AUDIO: granted=true, flags=[ USER_SET]', |
| 87 | + ].join('\n'), |
| 88 | + 0, |
| 89 | + ); |
| 90 | + |
| 91 | + assert.equal(grants?.get('android.permission.RECORD_AUDIO'), 'not_granted'); |
| 92 | + assert.equal(grants?.get('android.permission.CAMERA'), 'granted'); |
| 93 | +}); |
0 commit comments