Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 1 | /* |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 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 | * |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 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 |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 14 | * limitations under the License |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 18 | |
| 19 | import static android.content.pm.ApplicationInfo.FLAG_SUSPENDED; |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 20 | |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 22 | import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME; |
| 23 | |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertFalse; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | import static org.mockito.ArgumentMatchers.eq; |
| 28 | import static org.mockito.ArgumentMatchers.nullable; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 29 | |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 30 | import android.app.ActivityManagerInternal; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 31 | import android.app.KeyguardManager; |
| 32 | import android.app.admin.DevicePolicyManagerInternal; |
| 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.pm.ActivityInfo; |
| 36 | import android.content.pm.ApplicationInfo; |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManagerInternal; |
Suprabh Shukla | 389cb6f | 2018-10-01 18:20:39 -0700 | [diff] [blame] | 38 | import android.content.pm.SuspendDialogInfo; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 39 | import android.content.pm.UserInfo; |
| 40 | import android.os.UserHandle; |
| 41 | import android.os.UserManager; |
Ben Gruver | eb8fd1c | 2018-03-07 13:24:18 -0800 | [diff] [blame] | 42 | import android.platform.test.annotations.Presubmit; |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 43 | import android.testing.DexmakerShareClassLoaderRule; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 44 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 45 | import androidx.test.filters.SmallTest; |
| 46 | |
| 47 | import com.android.internal.app.HarmfulAppWarningActivity; |
Suprabh Shukla | 125c771 | 2018-05-07 17:00:07 -0700 | [diff] [blame] | 48 | import com.android.internal.app.SuspendedAppActivity; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 49 | import com.android.internal.app.UnlaunchableAppActivity; |
| 50 | import com.android.server.LocalServices; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 51 | import com.android.server.am.ActivityManagerService; |
Ben Gruver | eb8fd1c | 2018-03-07 13:24:18 -0800 | [diff] [blame] | 52 | import com.android.server.pm.PackageManagerService; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 53 | |
| 54 | import org.junit.Before; |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 55 | import org.junit.Rule; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 56 | import org.junit.Test; |
| 57 | import org.mockito.Mock; |
| 58 | import org.mockito.MockitoAnnotations; |
| 59 | |
| 60 | /** |
| 61 | * Unit tests for {@link ActivityStartInterceptorTest}. |
| 62 | * |
| 63 | * Build/Install/Run: |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 64 | * atest WmTests:ActivityStartInterceptorTest |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 65 | */ |
| 66 | @SmallTest |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 67 | @Presubmit |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 68 | public class ActivityStartInterceptorTest { |
| 69 | private static final int TEST_USER_ID = 1; |
| 70 | private static final int TEST_REAL_CALLING_UID = 2; |
| 71 | private static final int TEST_REAL_CALLING_PID = 3; |
| 72 | private static final String TEST_CALLING_PACKAGE = "com.test.caller"; |
| 73 | private static final int TEST_START_FLAGS = 4; |
| 74 | private static final Intent ADMIN_SUPPORT_INTENT = |
| 75 | new Intent("com.test.ADMIN_SUPPORT"); |
| 76 | private static final Intent CONFIRM_CREDENTIALS_INTENT = |
| 77 | new Intent("com.test.CONFIRM_CREDENTIALS"); |
| 78 | private static final UserInfo PARENT_USER_INFO = new UserInfo(0 /* userId */, "parent", |
| 79 | 0 /* flags */); |
| 80 | private static final String TEST_PACKAGE_NAME = "com.test.package"; |
| 81 | |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 82 | @Rule |
| 83 | public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = |
| 84 | new DexmakerShareClassLoaderRule(); |
| 85 | |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 86 | @Mock |
| 87 | private Context mContext; |
| 88 | @Mock |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 89 | private ActivityManagerService mAm; |
| 90 | @Mock |
| 91 | private ActivityTaskManagerService mService; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 92 | @Mock |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 93 | private RootActivityContainer mRootActivityContainer; |
| 94 | @Mock |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 95 | private ActivityStackSupervisor mSupervisor; |
| 96 | @Mock |
| 97 | private DevicePolicyManagerInternal mDevicePolicyManager; |
| 98 | @Mock |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 99 | private PackageManagerInternal mPackageManagerInternal; |
| 100 | @Mock |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 101 | private UserManager mUserManager; |
| 102 | @Mock |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 103 | private KeyguardManager mKeyguardManager; |
Ben Gruver | eb8fd1c | 2018-03-07 13:24:18 -0800 | [diff] [blame] | 104 | @Mock |
| 105 | private PackageManagerService mPackageManager; |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 106 | @Mock |
| 107 | private ActivityManagerInternal mAmInternal; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 108 | |
| 109 | private ActivityStartInterceptor mInterceptor; |
| 110 | private ActivityInfo mAInfo = new ActivityInfo(); |
| 111 | |
| 112 | @Before |
| 113 | public void setUp() { |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 114 | MockitoAnnotations.initMocks(this); |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 115 | mService.mAmInternal = mAmInternal; |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 116 | mInterceptor = new ActivityStartInterceptor( |
| 117 | mService, mSupervisor, mRootActivityContainer, mContext); |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 118 | mInterceptor.setStates(TEST_USER_ID, TEST_REAL_CALLING_PID, TEST_REAL_CALLING_UID, |
| 119 | TEST_START_FLAGS, TEST_CALLING_PACKAGE); |
| 120 | |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 121 | // Mock ActivityManagerInternal |
| 122 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 123 | LocalServices.addService(ActivityManagerInternal.class, mAmInternal); |
| 124 | |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 125 | // Mock DevicePolicyManagerInternal |
| 126 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
| 127 | LocalServices.addService(DevicePolicyManagerInternal.class, |
| 128 | mDevicePolicyManager); |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 129 | when(mDevicePolicyManager.createShowAdminSupportIntent(TEST_USER_ID, true)) |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 130 | .thenReturn(ADMIN_SUPPORT_INTENT); |
Wale Ogunwale | 906f9c6 | 2018-07-23 11:23:44 -0700 | [diff] [blame] | 131 | when(mService.getPackageManagerInternalLocked()).thenReturn(mPackageManagerInternal); |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 132 | |
| 133 | // Mock UserManager |
| 134 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 135 | when(mUserManager.getProfileParent(TEST_USER_ID)).thenReturn(PARENT_USER_INFO); |
| 136 | |
| 137 | // Mock KeyguardManager |
| 138 | when(mContext.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(mKeyguardManager); |
| 139 | when(mKeyguardManager.createConfirmDeviceCredentialIntent( |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 140 | nullable(CharSequence.class), nullable(CharSequence.class), eq(TEST_USER_ID))) |
| 141 | .thenReturn(CONFIRM_CREDENTIALS_INTENT); |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 142 | |
Ben Gruver | eb8fd1c | 2018-03-07 13:24:18 -0800 | [diff] [blame] | 143 | // Mock PackageManager |
Wale Ogunwale | 906f9c6 | 2018-07-23 11:23:44 -0700 | [diff] [blame] | 144 | when(mService.getPackageManager()).thenReturn(mPackageManager); |
Ben Gruver | eb8fd1c | 2018-03-07 13:24:18 -0800 | [diff] [blame] | 145 | when(mPackageManager.getHarmfulAppWarning(TEST_PACKAGE_NAME, TEST_USER_ID)) |
| 146 | .thenReturn(null); |
| 147 | |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 148 | // Initialise activity info |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 149 | mAInfo.applicationInfo = new ApplicationInfo(); |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 150 | mAInfo.packageName = mAInfo.applicationInfo.packageName = TEST_PACKAGE_NAME; |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 154 | public void testSuspendedByAdminPackage() { |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 155 | // GIVEN the package we're about to launch is currently suspended |
| 156 | mAInfo.applicationInfo.flags = FLAG_SUSPENDED; |
| 157 | |
Suprabh Shukla | 3c3af14 | 2018-03-30 00:28:37 -0700 | [diff] [blame] | 158 | when(mPackageManagerInternal.getSuspendingPackage(TEST_PACKAGE_NAME, TEST_USER_ID)) |
| 159 | .thenReturn(PLATFORM_PACKAGE_NAME); |
| 160 | |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 161 | // THEN calling intercept returns true |
| 162 | assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null)); |
| 163 | |
| 164 | // THEN the returned intent is the admin support intent |
| 165 | assertEquals(ADMIN_SUPPORT_INTENT, mInterceptor.mIntent); |
| 166 | } |
| 167 | |
| 168 | @Test |
Suprabh Shukla | 125c771 | 2018-05-07 17:00:07 -0700 | [diff] [blame] | 169 | public void testSuspendedPackage() { |
| 170 | mAInfo.applicationInfo.flags = FLAG_SUSPENDED; |
| 171 | final String suspendingPackage = "com.test.suspending.package"; |
Suprabh Shukla | 389cb6f | 2018-10-01 18:20:39 -0700 | [diff] [blame] | 172 | final SuspendDialogInfo dialogInfo = new SuspendDialogInfo.Builder() |
| 173 | .setMessage("Test Message") |
| 174 | .setIcon(0x11110001) |
| 175 | .build(); |
Suprabh Shukla | 125c771 | 2018-05-07 17:00:07 -0700 | [diff] [blame] | 176 | when(mPackageManagerInternal.getSuspendingPackage(TEST_PACKAGE_NAME, TEST_USER_ID)) |
| 177 | .thenReturn(suspendingPackage); |
Suprabh Shukla | 389cb6f | 2018-10-01 18:20:39 -0700 | [diff] [blame] | 178 | when(mPackageManagerInternal.getSuspendedDialogInfo(TEST_PACKAGE_NAME, TEST_USER_ID)) |
| 179 | .thenReturn(dialogInfo); |
Suprabh Shukla | 125c771 | 2018-05-07 17:00:07 -0700 | [diff] [blame] | 180 | // THEN calling intercept returns true |
| 181 | assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null)); |
| 182 | |
| 183 | // Check intent parameters |
Suprabh Shukla | 389cb6f | 2018-10-01 18:20:39 -0700 | [diff] [blame] | 184 | assertEquals(dialogInfo, |
| 185 | mInterceptor.mIntent.getParcelableExtra(SuspendedAppActivity.EXTRA_DIALOG_INFO)); |
Suprabh Shukla | 125c771 | 2018-05-07 17:00:07 -0700 | [diff] [blame] | 186 | assertEquals(suspendingPackage, |
| 187 | mInterceptor.mIntent.getStringExtra(SuspendedAppActivity.EXTRA_SUSPENDING_PACKAGE)); |
| 188 | assertEquals(TEST_PACKAGE_NAME, |
| 189 | mInterceptor.mIntent.getStringExtra(SuspendedAppActivity.EXTRA_SUSPENDED_PACKAGE)); |
| 190 | assertEquals(TEST_USER_ID, mInterceptor.mIntent.getIntExtra(Intent.EXTRA_USER_ID, -1000)); |
| 191 | } |
| 192 | |
| 193 | @Test |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 194 | public void testInterceptQuietProfile() { |
| 195 | // GIVEN that the user the activity is starting as is currently in quiet mode |
| 196 | when(mUserManager.isQuietModeEnabled(eq(UserHandle.of(TEST_USER_ID)))).thenReturn(true); |
| 197 | |
| 198 | // THEN calling intercept returns true |
| 199 | assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null)); |
| 200 | |
| 201 | // THEN the returned intent is the quiet mode intent |
| 202 | assertTrue(UnlaunchableAppActivity.createInQuietModeDialogIntent(TEST_USER_ID) |
| 203 | .filterEquals(mInterceptor.mIntent)); |
| 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testWorkChallenge() { |
| 208 | // GIVEN that the user the activity is starting as is currently locked |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 209 | when(mAmInternal.shouldConfirmCredentials(TEST_USER_ID)).thenReturn(true); |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 210 | |
| 211 | // THEN calling intercept returns true |
| 212 | mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null); |
| 213 | |
| 214 | // THEN the returned intent is the quiet mode intent |
| 215 | assertTrue(CONFIRM_CREDENTIALS_INTENT.filterEquals(mInterceptor.mIntent)); |
| 216 | } |
| 217 | |
| 218 | @Test |
Ben Gruver | 4982873 | 2018-03-07 14:20:37 -0800 | [diff] [blame] | 219 | public void testHarmfulAppWarning() { |
| 220 | // GIVEN the package we're about to launch has a harmful app warning set |
| 221 | when(mPackageManager.getHarmfulAppWarning(TEST_PACKAGE_NAME, TEST_USER_ID)) |
| 222 | .thenReturn("This app is bad"); |
| 223 | |
| 224 | // THEN calling intercept returns true |
| 225 | assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null)); |
| 226 | |
| 227 | // THEN the returned intent is the harmful app warning intent |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 228 | assertEquals(HarmfulAppWarningActivity.class.getName(), |
| 229 | mInterceptor.mIntent.getComponent().getClassName()); |
Ben Gruver | 4982873 | 2018-03-07 14:20:37 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Test |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 233 | public void testNoInterception() { |
| 234 | // GIVEN that none of the interception conditions are met |
| 235 | |
| 236 | // THEN calling intercept returns false |
| 237 | assertFalse(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null)); |
| 238 | } |
| 239 | } |