blob: af04f76412500c6f8b773df047f973a9bd14d96a [file] [log] [blame]
Benjamin Franz563707b2017-06-29 15:06:13 +01001/*
Wale Ogunwale59507092018-10-29 09:00:30 -07002 * Copyright (C) 2018 The Android Open Source Project
Benjamin Franz563707b2017-06-29 15:06:13 +01003 *
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. Takaoka74ccec22018-10-23 11:07:13 +09008 * http://www.apache.org/licenses/LICENSE-2.0
Benjamin Franz563707b2017-06-29 15:06:13 +01009 *
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 Ogunwale59507092018-10-29 09:00:30 -070014 * limitations under the License
Benjamin Franz563707b2017-06-29 15:06:13 +010015 */
16
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Benjamin Franz563707b2017-06-29 15:06:13 +010018
Yuhan Zhaof3b3d412020-01-24 23:37:13 +000019import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
Benjamin Franz563707b2017-06-29 15:06:13 +010020import static android.content.pm.ApplicationInfo.FLAG_SUSPENDED;
Suprabh Shukla3c3af142018-03-30 00:28:37 -070021
Tadashi G. Takaokaa7a66952018-11-16 15:04:21 +090022import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
Suprabh Shukla3c3af142018-03-30 00:28:37 -070023import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME;
24
Benjamin Franz563707b2017-06-29 15:06:13 +010025import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertTrue;
28import static org.mockito.ArgumentMatchers.eq;
29import static org.mockito.ArgumentMatchers.nullable;
Benjamin Franz563707b2017-06-29 15:06:13 +010030
Wale Ogunwale86b74462018-07-02 08:42:43 -070031import android.app.ActivityManagerInternal;
Benjamin Franz563707b2017-06-29 15:06:13 +010032import android.app.KeyguardManager;
33import android.app.admin.DevicePolicyManagerInternal;
34import android.content.Context;
35import android.content.Intent;
36import android.content.pm.ActivityInfo;
37import android.content.pm.ApplicationInfo;
Suprabh Shukla3c3af142018-03-30 00:28:37 -070038import android.content.pm.PackageManagerInternal;
Suprabh Shukla389cb6f2018-10-01 18:20:39 -070039import android.content.pm.SuspendDialogInfo;
Benjamin Franz563707b2017-06-29 15:06:13 +010040import android.content.pm.UserInfo;
41import android.os.UserHandle;
42import android.os.UserManager;
Ben Gruvereb8fd1c2018-03-07 13:24:18 -080043import android.platform.test.annotations.Presubmit;
Adrian Roos3150dbf2018-03-28 18:06:52 +020044import android.testing.DexmakerShareClassLoaderRule;
Benjamin Franz563707b2017-06-29 15:06:13 +010045
Brett Chabota26eda92018-07-23 13:08:30 -070046import androidx.test.filters.SmallTest;
47
Yuhan Zhaof3b3d412020-01-24 23:37:13 +000048import com.android.internal.app.BlockedAppActivity;
Brett Chabota26eda92018-07-23 13:08:30 -070049import com.android.internal.app.HarmfulAppWarningActivity;
Suprabh Shukla125c7712018-05-07 17:00:07 -070050import com.android.internal.app.SuspendedAppActivity;
Benjamin Franz563707b2017-06-29 15:06:13 +010051import com.android.internal.app.UnlaunchableAppActivity;
52import com.android.server.LocalServices;
Wale Ogunwale59507092018-10-29 09:00:30 -070053import com.android.server.am.ActivityManagerService;
Ben Gruvereb8fd1c2018-03-07 13:24:18 -080054import com.android.server.pm.PackageManagerService;
Benjamin Franz563707b2017-06-29 15:06:13 +010055
56import org.junit.Before;
Adrian Roos3150dbf2018-03-28 18:06:52 +020057import org.junit.Rule;
Benjamin Franz563707b2017-06-29 15:06:13 +010058import org.junit.Test;
59import org.mockito.Mock;
60import org.mockito.MockitoAnnotations;
61
62/**
63 * Unit tests for {@link ActivityStartInterceptorTest}.
64 *
65 * Build/Install/Run:
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090066 * atest WmTests:ActivityStartInterceptorTest
Benjamin Franz563707b2017-06-29 15:06:13 +010067 */
68@SmallTest
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090069@Presubmit
Benjamin Franz563707b2017-06-29 15:06:13 +010070public class ActivityStartInterceptorTest {
71 private static final int TEST_USER_ID = 1;
72 private static final int TEST_REAL_CALLING_UID = 2;
73 private static final int TEST_REAL_CALLING_PID = 3;
74 private static final String TEST_CALLING_PACKAGE = "com.test.caller";
75 private static final int TEST_START_FLAGS = 4;
76 private static final Intent ADMIN_SUPPORT_INTENT =
77 new Intent("com.test.ADMIN_SUPPORT");
78 private static final Intent CONFIRM_CREDENTIALS_INTENT =
79 new Intent("com.test.CONFIRM_CREDENTIALS");
80 private static final UserInfo PARENT_USER_INFO = new UserInfo(0 /* userId */, "parent",
81 0 /* flags */);
82 private static final String TEST_PACKAGE_NAME = "com.test.package";
83
Adrian Roos3150dbf2018-03-28 18:06:52 +020084 @Rule
85 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
86 new DexmakerShareClassLoaderRule();
87
Benjamin Franz563707b2017-06-29 15:06:13 +010088 @Mock
89 private Context mContext;
90 @Mock
Wale Ogunwalec9e57de2018-05-08 14:28:07 -070091 private ActivityManagerService mAm;
92 @Mock
93 private ActivityTaskManagerService mService;
Benjamin Franz563707b2017-06-29 15:06:13 +010094 @Mock
Louis Chang149d5c82019-12-30 09:47:39 +080095 private RootWindowContainer mRootWindowContainer;
Wale Ogunwaled32da472018-11-16 07:19:28 -080096 @Mock
Benjamin Franz563707b2017-06-29 15:06:13 +010097 private ActivityStackSupervisor mSupervisor;
98 @Mock
99 private DevicePolicyManagerInternal mDevicePolicyManager;
100 @Mock
Suprabh Shukla3c3af142018-03-30 00:28:37 -0700101 private PackageManagerInternal mPackageManagerInternal;
102 @Mock
Benjamin Franz563707b2017-06-29 15:06:13 +0100103 private UserManager mUserManager;
104 @Mock
Benjamin Franz563707b2017-06-29 15:06:13 +0100105 private KeyguardManager mKeyguardManager;
Ben Gruvereb8fd1c2018-03-07 13:24:18 -0800106 @Mock
107 private PackageManagerService mPackageManager;
Wale Ogunwale86b74462018-07-02 08:42:43 -0700108 @Mock
109 private ActivityManagerInternal mAmInternal;
Yuhan Zhaof3b3d412020-01-24 23:37:13 +0000110 @Mock
111 private LockTaskController mLockTaskController;
Benjamin Franz563707b2017-06-29 15:06:13 +0100112
113 private ActivityStartInterceptor mInterceptor;
114 private ActivityInfo mAInfo = new ActivityInfo();
115
116 @Before
117 public void setUp() {
Benjamin Franz563707b2017-06-29 15:06:13 +0100118 MockitoAnnotations.initMocks(this);
Wale Ogunwale86b74462018-07-02 08:42:43 -0700119 mService.mAmInternal = mAmInternal;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800120 mInterceptor = new ActivityStartInterceptor(
Louis Chang149d5c82019-12-30 09:47:39 +0800121 mService, mSupervisor, mRootWindowContainer, mContext);
Benjamin Franz563707b2017-06-29 15:06:13 +0100122 mInterceptor.setStates(TEST_USER_ID, TEST_REAL_CALLING_PID, TEST_REAL_CALLING_UID,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800123 TEST_START_FLAGS, TEST_CALLING_PACKAGE, null);
Benjamin Franz563707b2017-06-29 15:06:13 +0100124
Wale Ogunwale86b74462018-07-02 08:42:43 -0700125 // Mock ActivityManagerInternal
126 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
127 LocalServices.addService(ActivityManagerInternal.class, mAmInternal);
128
Benjamin Franz563707b2017-06-29 15:06:13 +0100129 // Mock DevicePolicyManagerInternal
130 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
131 LocalServices.addService(DevicePolicyManagerInternal.class,
132 mDevicePolicyManager);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700133 when(mDevicePolicyManager.createShowAdminSupportIntent(TEST_USER_ID, true))
Benjamin Franz563707b2017-06-29 15:06:13 +0100134 .thenReturn(ADMIN_SUPPORT_INTENT);
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700135 when(mService.getPackageManagerInternalLocked()).thenReturn(mPackageManagerInternal);
Benjamin Franz563707b2017-06-29 15:06:13 +0100136
137 // Mock UserManager
138 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
139 when(mUserManager.getProfileParent(TEST_USER_ID)).thenReturn(PARENT_USER_INFO);
140
141 // Mock KeyguardManager
142 when(mContext.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(mKeyguardManager);
143 when(mKeyguardManager.createConfirmDeviceCredentialIntent(
joshmccloskey15c0a442019-10-18 16:09:06 -0700144 nullable(CharSequence.class), nullable(CharSequence.class), eq(TEST_USER_ID),
145 eq(true))).thenReturn(CONFIRM_CREDENTIALS_INTENT);
Benjamin Franz563707b2017-06-29 15:06:13 +0100146
Ben Gruvereb8fd1c2018-03-07 13:24:18 -0800147 // Mock PackageManager
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700148 when(mService.getPackageManager()).thenReturn(mPackageManager);
Ben Gruvereb8fd1c2018-03-07 13:24:18 -0800149 when(mPackageManager.getHarmfulAppWarning(TEST_PACKAGE_NAME, TEST_USER_ID))
150 .thenReturn(null);
151
Yuhan Zhaof3b3d412020-01-24 23:37:13 +0000152 // Mock LockTaskController
153 mAInfo.lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
154 when(mService.getLockTaskController()).thenReturn(mLockTaskController);
155 when(mLockTaskController.isActivityAllowed(
156 TEST_USER_ID, TEST_PACKAGE_NAME, LOCK_TASK_LAUNCH_MODE_DEFAULT))
157 .thenReturn(true);
158
Benjamin Franz563707b2017-06-29 15:06:13 +0100159 // Initialise activity info
Benjamin Franz563707b2017-06-29 15:06:13 +0100160 mAInfo.applicationInfo = new ApplicationInfo();
Suprabh Shukla3c3af142018-03-30 00:28:37 -0700161 mAInfo.packageName = mAInfo.applicationInfo.packageName = TEST_PACKAGE_NAME;
Benjamin Franz563707b2017-06-29 15:06:13 +0100162 }
163
164 @Test
Suprabh Shukla3c3af142018-03-30 00:28:37 -0700165 public void testSuspendedByAdminPackage() {
Benjamin Franz563707b2017-06-29 15:06:13 +0100166 // GIVEN the package we're about to launch is currently suspended
167 mAInfo.applicationInfo.flags = FLAG_SUSPENDED;
168
Suprabh Shukla3c3af142018-03-30 00:28:37 -0700169 when(mPackageManagerInternal.getSuspendingPackage(TEST_PACKAGE_NAME, TEST_USER_ID))
170 .thenReturn(PLATFORM_PACKAGE_NAME);
171
Benjamin Franz563707b2017-06-29 15:06:13 +0100172 // THEN calling intercept returns true
173 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
174
175 // THEN the returned intent is the admin support intent
176 assertEquals(ADMIN_SUPPORT_INTENT, mInterceptor.mIntent);
177 }
178
179 @Test
Suprabh Shukla125c7712018-05-07 17:00:07 -0700180 public void testSuspendedPackage() {
Suprabh Shukla125c7712018-05-07 17:00:07 -0700181 final String suspendingPackage = "com.test.suspending.package";
Louis Chang97f743f2019-11-26 13:03:10 +0800182 final SuspendDialogInfo dialogInfo = suspendPackage(suspendingPackage);
Suprabh Shukla125c7712018-05-07 17:00:07 -0700183 // THEN calling intercept returns true
184 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
185
186 // Check intent parameters
Suprabh Shukla389cb6f2018-10-01 18:20:39 -0700187 assertEquals(dialogInfo,
188 mInterceptor.mIntent.getParcelableExtra(SuspendedAppActivity.EXTRA_DIALOG_INFO));
Suprabh Shukla125c7712018-05-07 17:00:07 -0700189 assertEquals(suspendingPackage,
190 mInterceptor.mIntent.getStringExtra(SuspendedAppActivity.EXTRA_SUSPENDING_PACKAGE));
191 assertEquals(TEST_PACKAGE_NAME,
192 mInterceptor.mIntent.getStringExtra(SuspendedAppActivity.EXTRA_SUSPENDED_PACKAGE));
193 assertEquals(TEST_USER_ID, mInterceptor.mIntent.getIntExtra(Intent.EXTRA_USER_ID, -1000));
194 }
195
Louis Chang97f743f2019-11-26 13:03:10 +0800196 private SuspendDialogInfo suspendPackage(String suspendingPackage) {
197 mAInfo.applicationInfo.flags = FLAG_SUSPENDED;
198 final SuspendDialogInfo dialogInfo = new SuspendDialogInfo.Builder()
199 .setMessage("Test Message")
200 .setIcon(0x11110001)
201 .build();
202 when(mPackageManagerInternal.getSuspendingPackage(TEST_PACKAGE_NAME, TEST_USER_ID))
203 .thenReturn(suspendingPackage);
204 when(mPackageManagerInternal.getSuspendedDialogInfo(TEST_PACKAGE_NAME, suspendingPackage,
205 TEST_USER_ID)).thenReturn(dialogInfo);
206 return dialogInfo;
207 }
208
Suprabh Shukla125c7712018-05-07 17:00:07 -0700209 @Test
Yuhan Zhaof3b3d412020-01-24 23:37:13 +0000210 public void testInterceptLockTaskModeViolationPackage() {
211 when(mLockTaskController.isActivityAllowed(
212 TEST_USER_ID, TEST_PACKAGE_NAME, LOCK_TASK_LAUNCH_MODE_DEFAULT))
213 .thenReturn(false);
214
215 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
216
217 assertTrue(BlockedAppActivity.createIntent(TEST_USER_ID, TEST_PACKAGE_NAME)
218 .filterEquals(mInterceptor.mIntent));
219 }
220
221 @Test
Benjamin Franz563707b2017-06-29 15:06:13 +0100222 public void testInterceptQuietProfile() {
223 // GIVEN that the user the activity is starting as is currently in quiet mode
224 when(mUserManager.isQuietModeEnabled(eq(UserHandle.of(TEST_USER_ID)))).thenReturn(true);
225
226 // THEN calling intercept returns true
227 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
228
229 // THEN the returned intent is the quiet mode intent
230 assertTrue(UnlaunchableAppActivity.createInQuietModeDialogIntent(TEST_USER_ID)
231 .filterEquals(mInterceptor.mIntent));
232 }
233
234 @Test
Louis Chang97f743f2019-11-26 13:03:10 +0800235 public void testInterceptQuietProfileWhenPackageSuspended() {
236 suspendPackage("com.test.suspending.package");
237 // GIVEN that the user the activity is starting as is currently in quiet mode
238 when(mUserManager.isQuietModeEnabled(eq(UserHandle.of(TEST_USER_ID)))).thenReturn(true);
239
240 // THEN calling intercept returns true
241 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
242
243 // THEN the returned intent is the quiet mode intent
244 assertTrue(UnlaunchableAppActivity.createInQuietModeDialogIntent(TEST_USER_ID)
245 .filterEquals(mInterceptor.mIntent));
246 }
247
248 @Test
Benjamin Franz563707b2017-06-29 15:06:13 +0100249 public void testWorkChallenge() {
250 // GIVEN that the user the activity is starting as is currently locked
Wale Ogunwale86b74462018-07-02 08:42:43 -0700251 when(mAmInternal.shouldConfirmCredentials(TEST_USER_ID)).thenReturn(true);
Benjamin Franz563707b2017-06-29 15:06:13 +0100252
253 // THEN calling intercept returns true
254 mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null);
255
256 // THEN the returned intent is the quiet mode intent
257 assertTrue(CONFIRM_CREDENTIALS_INTENT.filterEquals(mInterceptor.mIntent));
258 }
259
260 @Test
Ben Gruver49828732018-03-07 14:20:37 -0800261 public void testHarmfulAppWarning() {
262 // GIVEN the package we're about to launch has a harmful app warning set
263 when(mPackageManager.getHarmfulAppWarning(TEST_PACKAGE_NAME, TEST_USER_ID))
264 .thenReturn("This app is bad");
265
266 // THEN calling intercept returns true
267 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
268
269 // THEN the returned intent is the harmful app warning intent
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900270 assertEquals(HarmfulAppWarningActivity.class.getName(),
271 mInterceptor.mIntent.getComponent().getClassName());
Ben Gruver49828732018-03-07 14:20:37 -0800272 }
273
274 @Test
Benjamin Franz563707b2017-06-29 15:06:13 +0100275 public void testNoInterception() {
276 // GIVEN that none of the interception conditions are met
277
278 // THEN calling intercept returns false
279 assertFalse(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
280 }
281}