Rhed Jao | 21a1dbc | 2019-03-25 14:14:37 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.accessibility; |
| 18 | |
| 19 | import static junit.framework.Assert.assertFalse; |
| 20 | import static junit.framework.TestCase.assertNull; |
| 21 | import static junit.framework.TestCase.assertTrue; |
| 22 | |
| 23 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 24 | import static org.hamcrest.Matchers.emptyArray; |
| 25 | import static org.hamcrest.Matchers.hasSize; |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.mockito.ArgumentMatchers.any; |
| 29 | import static org.mockito.Mockito.doReturn; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
| 33 | import android.accessibilityservice.AccessibilityServiceInfo; |
| 34 | import android.app.AppOpsManager; |
| 35 | import android.appwidget.AppWidgetManagerInternal; |
| 36 | import android.content.ComponentName; |
| 37 | import android.content.Context; |
| 38 | import android.content.pm.ApplicationInfo; |
| 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.ResolveInfo; |
| 41 | import android.content.pm.ServiceInfo; |
| 42 | import android.os.Process; |
| 43 | import android.os.UserHandle; |
| 44 | import android.os.UserManager; |
| 45 | import android.testing.DexmakerShareClassLoaderRule; |
| 46 | import android.util.ArraySet; |
| 47 | import android.view.accessibility.AccessibilityEvent; |
| 48 | import android.view.accessibility.AccessibilityWindowInfo; |
| 49 | |
| 50 | import org.junit.Before; |
| 51 | import org.junit.Rule; |
| 52 | import org.junit.Test; |
| 53 | import org.mockito.Mock; |
| 54 | import org.mockito.Mockito; |
| 55 | import org.mockito.MockitoAnnotations; |
| 56 | |
| 57 | import java.util.Arrays; |
| 58 | import java.util.List; |
| 59 | |
| 60 | /** |
| 61 | * Tests for the AccessibilitySecurityPolicy |
| 62 | */ |
| 63 | public class AccessibilitySecurityPolicyTest { |
| 64 | private static final String PACKAGE_NAME = "com.android.server.accessibility"; |
| 65 | private static final String PACKAGE_NAME2 = "com.android.server.accessibility2"; |
| 66 | private static final int WINDOWID = 0x000a; |
| 67 | private static final int WINDOWID2 = 0x000b; |
| 68 | private static final int APP_UID = 10400; |
| 69 | |
| 70 | private static final int[] ALWAYS_DISPATCH_EVENTS = { |
| 71 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, |
| 72 | AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED, |
| 73 | AccessibilityEvent.TYPE_ANNOUNCEMENT, |
| 74 | AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START, |
| 75 | AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END, |
| 76 | AccessibilityEvent.TYPE_GESTURE_DETECTION_START, |
| 77 | AccessibilityEvent.TYPE_GESTURE_DETECTION_END, |
| 78 | AccessibilityEvent.TYPE_TOUCH_INTERACTION_START, |
| 79 | AccessibilityEvent.TYPE_TOUCH_INTERACTION_END, |
| 80 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER, |
| 81 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT, |
| 82 | AccessibilityEvent.TYPE_ASSIST_READING_CONTEXT, |
| 83 | AccessibilityEvent.TYPE_WINDOWS_CHANGED, |
| 84 | }; |
| 85 | private static final int[] OTHER_EVENTS = { |
| 86 | AccessibilityEvent.TYPE_VIEW_CLICKED, |
| 87 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED, |
| 88 | AccessibilityEvent.TYPE_VIEW_SELECTED, |
| 89 | AccessibilityEvent.TYPE_VIEW_FOCUSED, |
| 90 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED, |
| 91 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED, |
| 92 | AccessibilityEvent.TYPE_VIEW_SCROLLED, |
| 93 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED, |
| 94 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED, |
| 95 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED, |
| 96 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY, |
| 97 | AccessibilityEvent.TYPE_VIEW_CONTEXT_CLICKED, |
| 98 | }; |
| 99 | |
| 100 | private AccessibilitySecurityPolicy mA11ySecurityPolicy; |
| 101 | |
| 102 | // To mock package-private class |
| 103 | @Rule public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = |
| 104 | new DexmakerShareClassLoaderRule(); |
| 105 | |
| 106 | @Mock private Context mMockContext; |
| 107 | @Mock private PackageManager mMockPackageManager; |
| 108 | @Mock private UserManager mMockUserManager; |
| 109 | @Mock private AppOpsManager mMockAppOpsManager; |
| 110 | @Mock private AccessibilityServiceConnection mMockA11yServiceConnection; |
| 111 | @Mock private AccessibilityWindowManager mMockA11yWindowManager; |
| 112 | @Mock private AppWidgetManagerInternal mMockAppWidgetManager; |
| 113 | @Mock private AccessibilitySecurityPolicy.AccessibilityUserManager mMockA11yUserManager; |
| 114 | |
| 115 | @Before |
| 116 | public void setUp() { |
| 117 | MockitoAnnotations.initMocks(this); |
| 118 | when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); |
| 119 | when(mMockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager); |
| 120 | when(mMockContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mMockAppOpsManager); |
| 121 | |
| 122 | mA11ySecurityPolicy = new AccessibilitySecurityPolicy(mMockContext, mMockA11yUserManager); |
| 123 | mA11ySecurityPolicy.setAccessibilityWindowManager(mMockA11yWindowManager); |
| 124 | mA11ySecurityPolicy.setAppWidgetManager(mMockAppWidgetManager); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void canDispatchAccessibilityEvent_alwaysDispatchEvents_returnTrue() { |
| 129 | for (int i = 0; i < ALWAYS_DISPATCH_EVENTS.length; i++) { |
| 130 | final AccessibilityEvent event = AccessibilityEvent.obtain(ALWAYS_DISPATCH_EVENTS[i]); |
| 131 | assertTrue("Should dispatch [" + event + "]", |
| 132 | mA11ySecurityPolicy.canDispatchAccessibilityEventLocked( |
| 133 | UserHandle.USER_SYSTEM, |
| 134 | event)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void canDispatchAccessibilityEvent_otherEvents_invalidWindowId_returnFalse() { |
| 140 | final int invalidWindowId = WINDOWID; |
| 141 | for (int i = 0; i < OTHER_EVENTS.length; i++) { |
| 142 | final AccessibilityEvent event = AccessibilityEvent.obtain(OTHER_EVENTS[i]); |
| 143 | event.setWindowId(invalidWindowId); |
| 144 | assertFalse("Shouldn't dispatch [" + event + "]", |
| 145 | mA11ySecurityPolicy.canDispatchAccessibilityEventLocked( |
| 146 | UserHandle.USER_SYSTEM, |
| 147 | event)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void canDispatchAccessibilityEvent_otherEvents_windowIdIsActive_returnTrue() { |
| 153 | when(mMockA11yWindowManager.getActiveWindowId(UserHandle.USER_SYSTEM)) |
| 154 | .thenReturn(WINDOWID); |
| 155 | for (int i = 0; i < OTHER_EVENTS.length; i++) { |
| 156 | final AccessibilityEvent event = AccessibilityEvent.obtain(OTHER_EVENTS[i]); |
| 157 | event.setWindowId(WINDOWID); |
| 158 | assertTrue("Should dispatch [" + event + "]", |
| 159 | mA11ySecurityPolicy.canDispatchAccessibilityEventLocked( |
| 160 | UserHandle.USER_SYSTEM, |
| 161 | event)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void canDispatchAccessibilityEvent_otherEvents_windowIdExist_returnTrue() { |
| 167 | when(mMockA11yWindowManager.getActiveWindowId(UserHandle.USER_SYSTEM)) |
| 168 | .thenReturn(WINDOWID2); |
Jacky Kao | 3f730eb | 2019-07-23 11:46:48 +0800 | [diff] [blame] | 169 | when(mMockA11yWindowManager.findA11yWindowInfoByIdLocked(WINDOWID)) |
Rhed Jao | 21a1dbc | 2019-03-25 14:14:37 +0800 | [diff] [blame] | 170 | .thenReturn(AccessibilityWindowInfo.obtain()); |
| 171 | for (int i = 0; i < OTHER_EVENTS.length; i++) { |
| 172 | final AccessibilityEvent event = AccessibilityEvent.obtain(OTHER_EVENTS[i]); |
| 173 | event.setWindowId(WINDOWID); |
| 174 | assertTrue("Should dispatch [" + event + "]", |
| 175 | mA11ySecurityPolicy.canDispatchAccessibilityEventLocked( |
| 176 | UserHandle.USER_SYSTEM, |
| 177 | event)); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void resolveValidReportedPackage_nullPkgName_returnNull() { |
| 183 | assertNull(mA11ySecurityPolicy.resolveValidReportedPackageLocked( |
| 184 | null, Process.SYSTEM_UID, UserHandle.USER_SYSTEM)); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void resolveValidReportedPackage_uidIsSystem_returnPkgName() { |
| 189 | assertEquals(mA11ySecurityPolicy.resolveValidReportedPackageLocked( |
| 190 | PACKAGE_NAME, Process.SYSTEM_UID, UserHandle.USER_SYSTEM), |
| 191 | PACKAGE_NAME); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void resolveValidReportedPackage_uidAndPkgNameMatched_returnPkgName() |
| 196 | throws PackageManager.NameNotFoundException { |
| 197 | when(mMockPackageManager.getPackageUidAsUser(PACKAGE_NAME, UserHandle.USER_SYSTEM)) |
| 198 | .thenReturn(APP_UID); |
| 199 | |
| 200 | assertEquals(mA11ySecurityPolicy.resolveValidReportedPackageLocked( |
| 201 | PACKAGE_NAME, APP_UID, UserHandle.USER_SYSTEM), |
| 202 | PACKAGE_NAME); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void resolveValidReportedPackage_uidIsWidgetHost_pkgNameIsAppWidget_returnPkgName() |
| 207 | throws PackageManager.NameNotFoundException { |
| 208 | final int widgetHostUid = APP_UID; |
| 209 | final String hostPackageName = PACKAGE_NAME; |
| 210 | final String widgetPackageName = PACKAGE_NAME2; |
| 211 | final ArraySet<String> widgetPackages = new ArraySet<>(); |
| 212 | widgetPackages.add(widgetPackageName); |
| 213 | |
| 214 | when(mMockAppWidgetManager.getHostedWidgetPackages(widgetHostUid)) |
| 215 | .thenReturn(widgetPackages); |
| 216 | when(mMockPackageManager.getPackageUidAsUser(hostPackageName, UserHandle.USER_SYSTEM)) |
| 217 | .thenReturn(widgetHostUid); |
| 218 | |
| 219 | assertEquals(mA11ySecurityPolicy.resolveValidReportedPackageLocked( |
| 220 | widgetPackageName, widgetHostUid, UserHandle.USER_SYSTEM), |
| 221 | widgetPackageName); |
| 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void resolveValidReportedPackage_pkgNameIsInvalid_returnFirstCorrectPkgName() |
| 226 | throws PackageManager.NameNotFoundException { |
| 227 | final String invalidPackageName = "x"; |
| 228 | final String[] uidPackages = {PACKAGE_NAME, PACKAGE_NAME2}; |
| 229 | when(mMockPackageManager.getPackagesForUid(APP_UID)) |
| 230 | .thenReturn(uidPackages); |
| 231 | when(mMockPackageManager.getPackageUidAsUser(invalidPackageName, UserHandle.USER_SYSTEM)) |
| 232 | .thenThrow(PackageManager.NameNotFoundException.class); |
| 233 | when(mMockAppWidgetManager.getHostedWidgetPackages(APP_UID)) |
| 234 | .thenReturn(new ArraySet<>()); |
| 235 | |
| 236 | assertEquals(mA11ySecurityPolicy.resolveValidReportedPackageLocked( |
| 237 | invalidPackageName, APP_UID, UserHandle.USER_SYSTEM), |
| 238 | PACKAGE_NAME); |
| 239 | } |
| 240 | |
| 241 | @Test |
| 242 | public void computeValidReportedPackages_uidIsSystem_returnEmptyArray() { |
| 243 | assertThat(mA11ySecurityPolicy.computeValidReportedPackages( |
| 244 | PACKAGE_NAME, Process.SYSTEM_UID), emptyArray()); |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | public void computeValidReportedPackages_uidIsAppWidgetHost_returnTargetAndWidgetName() { |
| 249 | final int widgetHostUid = APP_UID; |
| 250 | final String targetPackageName = PACKAGE_NAME; |
| 251 | final String widgetPackageName = PACKAGE_NAME2; |
| 252 | final ArraySet<String> widgetPackages = new ArraySet<>(); |
| 253 | widgetPackages.add(widgetPackageName); |
| 254 | when(mMockAppWidgetManager.getHostedWidgetPackages(widgetHostUid)) |
| 255 | .thenReturn(widgetPackages); |
| 256 | |
| 257 | List<String> packages = Arrays.asList(mA11ySecurityPolicy.computeValidReportedPackages( |
| 258 | targetPackageName, widgetHostUid)); |
| 259 | assertThat(packages, hasSize(2)); |
| 260 | assertThat(packages, containsInAnyOrder(targetPackageName, widgetPackageName)); |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | public void canGetAccessibilityNodeInfo_windowIdNotExist_returnFalse() { |
| 265 | final int invalidWindowId = WINDOWID; |
| 266 | when(mMockA11yServiceConnection.getCapabilities()) |
| 267 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT); |
| 268 | |
| 269 | assertFalse(mA11ySecurityPolicy.canGetAccessibilityNodeInfoLocked(UserHandle.USER_SYSTEM, |
| 270 | mMockA11yServiceConnection, invalidWindowId)); |
| 271 | } |
| 272 | |
| 273 | @Test |
| 274 | public void canGetAccessibilityNodeInfo_hasCapAndWindowIsActive_returnTrue() { |
| 275 | when(mMockA11yServiceConnection.getCapabilities()) |
| 276 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT); |
| 277 | when(mMockA11yWindowManager.getActiveWindowId(UserHandle.USER_SYSTEM)) |
| 278 | .thenReturn(WINDOWID); |
| 279 | |
| 280 | assertTrue(mA11ySecurityPolicy.canGetAccessibilityNodeInfoLocked(UserHandle.USER_SYSTEM, |
| 281 | mMockA11yServiceConnection, WINDOWID)); |
| 282 | } |
| 283 | |
| 284 | @Test |
| 285 | public void canGetAccessibilityNodeInfo_hasCapAndWindowExist_returnTrue() { |
| 286 | when(mMockA11yServiceConnection.getCapabilities()) |
| 287 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT); |
| 288 | when(mMockA11yWindowManager.getActiveWindowId(UserHandle.USER_SYSTEM)) |
| 289 | .thenReturn(WINDOWID2); |
Jacky Kao | 3f730eb | 2019-07-23 11:46:48 +0800 | [diff] [blame] | 290 | when(mMockA11yWindowManager.findA11yWindowInfoByIdLocked(WINDOWID)) |
Rhed Jao | 21a1dbc | 2019-03-25 14:14:37 +0800 | [diff] [blame] | 291 | .thenReturn(AccessibilityWindowInfo.obtain()); |
| 292 | |
| 293 | assertTrue(mA11ySecurityPolicy.canGetAccessibilityNodeInfoLocked(UserHandle.USER_SYSTEM, |
| 294 | mMockA11yServiceConnection, WINDOWID)); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void canRetrieveWindows_retrieveWindowsFlagIsFalse_returnFalse() { |
| 299 | when(mMockA11yServiceConnection.getCapabilities()) |
| 300 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT); |
| 301 | |
| 302 | assertFalse(mA11ySecurityPolicy.canRetrieveWindowsLocked(mMockA11yServiceConnection)); |
| 303 | } |
| 304 | |
| 305 | @Test |
| 306 | public void canRetrieveWindows_hasCapabilityAndRetrieveWindowsFlag_returnTrue() { |
| 307 | when(mMockA11yServiceConnection.getCapabilities()) |
| 308 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT); |
| 309 | mMockA11yServiceConnection.mRetrieveInteractiveWindows = true; |
| 310 | |
| 311 | assertTrue(mA11ySecurityPolicy.canRetrieveWindowsLocked(mMockA11yServiceConnection)); |
| 312 | } |
| 313 | |
| 314 | @Test |
| 315 | public void canRetrieveWindowContent_hasCapability_returnTrue() { |
| 316 | when(mMockA11yServiceConnection.getCapabilities()) |
| 317 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT); |
| 318 | |
| 319 | assertTrue(mA11ySecurityPolicy.canRetrieveWindowContentLocked(mMockA11yServiceConnection)); |
| 320 | } |
| 321 | |
| 322 | @Test |
| 323 | public void canControlMagnification_hasCapability_returnTrue() { |
| 324 | when(mMockA11yServiceConnection.getCapabilities()) |
| 325 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_CONTROL_MAGNIFICATION); |
| 326 | |
| 327 | assertTrue(mA11ySecurityPolicy.canControlMagnification(mMockA11yServiceConnection)); |
| 328 | } |
| 329 | |
| 330 | @Test |
| 331 | public void canPerformGestures_hasCapability_returnTrue() { |
| 332 | assertFalse(mA11ySecurityPolicy.canPerformGestures(mMockA11yServiceConnection)); |
| 333 | when(mMockA11yServiceConnection.getCapabilities()) |
| 334 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_PERFORM_GESTURES); |
| 335 | |
| 336 | assertTrue(mA11ySecurityPolicy.canPerformGestures(mMockA11yServiceConnection)); |
| 337 | } |
| 338 | |
| 339 | @Test |
| 340 | public void canCaptureFingerprintGestures_hasCapability_returnTrue() { |
| 341 | assertFalse(mA11ySecurityPolicy.canCaptureFingerprintGestures(mMockA11yServiceConnection)); |
| 342 | when(mMockA11yServiceConnection.getCapabilities()) |
| 343 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_FINGERPRINT_GESTURES); |
| 344 | |
| 345 | assertTrue(mA11ySecurityPolicy.canCaptureFingerprintGestures(mMockA11yServiceConnection)); |
| 346 | } |
| 347 | |
| 348 | @Test |
Jacky Kao | 09789e4 | 2020-01-15 16:23:52 +0800 | [diff] [blame] | 349 | public void canTakeScreenshot_hasCapability_returnTrue() { |
| 350 | when(mMockA11yServiceConnection.getCapabilities()) |
| 351 | .thenReturn(AccessibilityServiceInfo.CAPABILITY_CAN_TAKE_SCREENSHOT); |
| 352 | |
| 353 | assertTrue(mA11ySecurityPolicy.canTakeScreenshotLocked(mMockA11yServiceConnection)); |
| 354 | } |
| 355 | |
| 356 | @Test |
Rhed Jao | 21a1dbc | 2019-03-25 14:14:37 +0800 | [diff] [blame] | 357 | public void resolveProfileParent_userIdIsCurrentUser_returnCurrentUser() { |
| 358 | final int currentUserId = 10; |
| 359 | final int userId = currentUserId; |
| 360 | when(mMockA11yUserManager.getCurrentUserIdLocked()) |
| 361 | .thenReturn(currentUserId); |
| 362 | |
| 363 | assertEquals(mA11ySecurityPolicy.resolveProfileParentLocked(userId), |
| 364 | currentUserId); |
| 365 | } |
| 366 | |
| 367 | @Test |
| 368 | public void resolveProfileParent_userIdNotCurrentUser_shouldGetProfileParent() { |
| 369 | final int userId = 15; |
| 370 | final int currentUserId = 20; |
| 371 | when(mMockA11yUserManager.getCurrentUserIdLocked()).thenReturn(currentUserId); |
| 372 | |
| 373 | mA11ySecurityPolicy.resolveProfileParentLocked(userId); |
| 374 | verify(mMockUserManager).getProfileParent(userId); |
| 375 | } |
| 376 | |
| 377 | @Test |
| 378 | public void resolveCallingUserId_userIdIsCallingUser_shouldResolveProfileParent() { |
| 379 | final AccessibilitySecurityPolicy spySecurityPolicy = Mockito.spy(mA11ySecurityPolicy); |
| 380 | final int callingUserId = UserHandle.getUserId(Process.myUid()); |
| 381 | final int userId = callingUserId; |
| 382 | |
| 383 | spySecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 384 | verify(spySecurityPolicy).resolveProfileParentLocked(userId); |
| 385 | } |
| 386 | |
| 387 | @Test |
| 388 | public void resolveCallingUserId_callingParentIsCurrentUser_returnCurrentUser() { |
| 389 | final AccessibilitySecurityPolicy spySecurityPolicy = Mockito.spy(mA11ySecurityPolicy); |
| 390 | final int callingUserId = UserHandle.getUserId(Process.myUid()); |
| 391 | final int callingParentId = 20; |
| 392 | final int currentUserId = callingParentId; |
| 393 | when(mMockA11yUserManager.getCurrentUserIdLocked()) |
| 394 | .thenReturn(currentUserId); |
| 395 | doReturn(callingParentId).when(spySecurityPolicy).resolveProfileParentLocked( |
| 396 | callingUserId); |
| 397 | |
| 398 | assertEquals(spySecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked( |
| 399 | UserHandle.USER_CURRENT_OR_SELF), currentUserId); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | @Test(expected = SecurityException.class) |
| 404 | public void resolveCallingUserId_callingParentNotCurrentUserAndNoPerm_shouldException() { |
| 405 | final AccessibilitySecurityPolicy spySecurityPolicy = Mockito.spy(mA11ySecurityPolicy); |
| 406 | final int callingUserId = UserHandle.getUserId(Process.myUid()); |
| 407 | final int callingParentId = 20; |
| 408 | final int currentUserId = 30; |
| 409 | when(mMockA11yUserManager.getCurrentUserIdLocked()) |
| 410 | .thenReturn(currentUserId); |
| 411 | doReturn(callingParentId).when(spySecurityPolicy).resolveProfileParentLocked( |
| 412 | callingUserId); |
| 413 | when(mMockContext.checkCallingPermission(any())) |
| 414 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 415 | |
| 416 | spySecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked( |
| 417 | UserHandle.USER_CURRENT_OR_SELF); |
| 418 | } |
| 419 | |
| 420 | @Test(expected = IllegalArgumentException.class) |
| 421 | public void resolveCallingUserId_callingParentNotCurrentUser_userIdIsInvalid_shouldException() { |
| 422 | final AccessibilitySecurityPolicy spySecurityPolicy = Mockito.spy(mA11ySecurityPolicy); |
| 423 | final int callingUserId = UserHandle.getUserId(Process.myUid()); |
| 424 | final int callingParentId = 20; |
| 425 | final int currentUserId = 30; |
| 426 | final int invalidUserId = 40; |
| 427 | when(mMockA11yUserManager.getCurrentUserIdLocked()) |
| 428 | .thenReturn(currentUserId); |
| 429 | doReturn(callingParentId).when(spySecurityPolicy).resolveProfileParentLocked( |
| 430 | callingUserId); |
| 431 | when(mMockContext.checkCallingPermission(any())) |
| 432 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 433 | |
| 434 | spySecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(invalidUserId); |
| 435 | } |
| 436 | |
| 437 | @Test |
| 438 | public void canRegisterService_shouldCheckAppOps() { |
| 439 | final ServiceInfo serviceInfo = new ServiceInfo(); |
| 440 | serviceInfo.applicationInfo = new ApplicationInfo(); |
| 441 | serviceInfo.packageName = PACKAGE_NAME; |
| 442 | serviceInfo.name = AccessibilitySecurityPolicyTest.class.getSimpleName(); |
| 443 | |
| 444 | assertFalse(mA11ySecurityPolicy.canRegisterService(serviceInfo)); |
| 445 | serviceInfo.permission = android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE; |
| 446 | mA11ySecurityPolicy.canRegisterService(serviceInfo); |
| 447 | verify(mMockAppOpsManager).noteOpNoThrow(AppOpsManager.OPSTR_BIND_ACCESSIBILITY_SERVICE, |
| 448 | serviceInfo.applicationInfo.uid, serviceInfo.packageName); |
| 449 | } |
| 450 | |
| 451 | @Test |
| 452 | public void checkAccessibilityAccess_shouldCheckAppOps() { |
| 453 | final AccessibilityServiceInfo mockServiceInfo = Mockito.mock( |
| 454 | AccessibilityServiceInfo.class); |
| 455 | final ResolveInfo mockResolveInfo = Mockito.mock( |
| 456 | ResolveInfo.class); |
| 457 | final ServiceInfo serviceInfo = new ServiceInfo(); |
| 458 | serviceInfo.applicationInfo = new ApplicationInfo(); |
| 459 | serviceInfo.applicationInfo.uid = APP_UID; |
| 460 | mockResolveInfo.serviceInfo = serviceInfo; |
| 461 | |
| 462 | when(mMockA11yServiceConnection.getServiceInfo()).thenReturn(mockServiceInfo); |
| 463 | when(mockServiceInfo.getResolveInfo()).thenReturn(mockResolveInfo); |
| 464 | when(mMockA11yServiceConnection.getComponentName()) |
| 465 | .thenReturn(new ComponentName( |
| 466 | PACKAGE_NAME, AccessibilitySecurityPolicyTest.class.getSimpleName())); |
| 467 | |
| 468 | mA11ySecurityPolicy.checkAccessibilityAccess(mMockA11yServiceConnection); |
| 469 | verify(mMockAppOpsManager).noteOpNoThrow(AppOpsManager.OPSTR_ACCESS_ACCESSIBILITY, |
| 470 | APP_UID, PACKAGE_NAME); |
| 471 | } |
| 472 | } |