blob: fea1b825228c627ed5e5523743d759f09c70d509 [file] [log] [blame]
Tony Makb0d22622018-01-18 12:49:49 +00001package com.android.server.pm;
Tony Mak1b708e62017-10-12 10:59:11 +01002
3import static com.google.common.truth.Truth.assertThat;
4
Tony Mak1b708e62017-10-12 10:59:11 +01005import static org.mockito.ArgumentMatchers.any;
6import static org.mockito.ArgumentMatchers.anyInt;
7import static org.mockito.ArgumentMatchers.anyString;
8import static org.mockito.ArgumentMatchers.eq;
9import static org.mockito.ArgumentMatchers.nullable;
10import static org.mockito.Mockito.doAnswer;
11import static org.mockito.Mockito.never;
12import static org.mockito.Mockito.verify;
13import static org.mockito.Mockito.when;
14import static org.testng.Assert.assertThrows;
15
Alex Kershaw6fb8a022020-01-09 11:33:56 +000016import android.app.ActivityManager;
Tony Makde32b832018-04-30 15:11:57 +010017import android.app.ActivityManagerInternal;
Tony Mak1b708e62017-10-12 10:59:11 +010018import android.app.AppOpsManager;
Tony Makde32b832018-04-30 15:11:57 +010019import android.app.IApplicationThread;
Alex Kershaw6fb8a022020-01-09 11:33:56 +000020import android.app.admin.DevicePolicyManagerInternal;
Tony Mak1b708e62017-10-12 10:59:11 +010021import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.ActivityInfo;
25import android.content.pm.ApplicationInfo;
Alex Kershaw6fb8a022020-01-09 11:33:56 +000026import android.content.pm.IPackageManager;
Tony Mak1b708e62017-10-12 10:59:11 +010027import android.content.pm.PackageInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.PackageManagerInternal;
kholoud mohamed79a89f02020-01-15 15:30:07 +000030import android.content.pm.PermissionInfo;
Tony Mak1b708e62017-10-12 10:59:11 +010031import android.content.pm.ResolveInfo;
32import android.os.Bundle;
33import android.os.UserHandle;
34import android.os.UserManager;
35import android.platform.test.annotations.Presubmit;
36import android.util.SparseArray;
37
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070038import com.android.server.wm.ActivityTaskManagerInternal;
39
Tony Mak1b708e62017-10-12 10:59:11 +010040import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
Tony Mak1b708e62017-10-12 10:59:11 +010043import org.mockito.Mock;
44import org.mockito.junit.MockitoJUnitRunner;
45
46import java.util.ArrayList;
47import java.util.Collections;
48import java.util.List;
49
50/**
51 * Build/Install/Run:
Tony Makb0d22622018-01-18 12:49:49 +000052 * atest FrameworksServicesTests:com.android.server.pm.CrossProfileAppsServiceImplTest
Tony Mak1b708e62017-10-12 10:59:11 +010053 */
54@Presubmit
55@RunWith(MockitoJUnitRunner.class)
56public class CrossProfileAppsServiceImplTest {
57 private static final String PACKAGE_ONE = "com.one";
Philip P. Moltmannee295092020-02-10 08:46:26 -080058 private static final String FEATURE_ID = "feature.one";
Tony Mak1b708e62017-10-12 10:59:11 +010059 private static final int PACKAGE_ONE_UID = 1111;
60 private static final ComponentName ACTIVITY_COMPONENT =
61 new ComponentName("com.one", "test");
62
63 private static final String PACKAGE_TWO = "com.two";
64 private static final int PACKAGE_TWO_UID = 2222;
65
66 private static final int PRIMARY_USER = 0;
67 private static final int PROFILE_OF_PRIMARY_USER = 10;
68 private static final int SECONDARY_USER = 11;
69
70 @Mock
71 private Context mContext;
72 @Mock
73 private UserManager mUserManager;
74 @Mock
75 private PackageManager mPackageManager;
76 @Mock
77 private PackageManagerInternal mPackageManagerInternal;
78 @Mock
79 private AppOpsManager mAppOpsManager;
Tony Makde32b832018-04-30 15:11:57 +010080 @Mock
81 private ActivityManagerInternal mActivityManagerInternal;
Wale Ogunwale6767eae2018-05-03 15:52:51 -070082 @Mock
83 private ActivityTaskManagerInternal mActivityTaskManagerInternal;
Alex Kershaw6fb8a022020-01-09 11:33:56 +000084 @Mock
85 private IPackageManager mIPackageManager;
86 @Mock
87 private DevicePolicyManagerInternal mDevicePolicyManagerInternal;
Tony Mak1b708e62017-10-12 10:59:11 +010088
89 private TestInjector mTestInjector;
90 private ActivityInfo mActivityInfo;
91 private CrossProfileAppsServiceImpl mCrossProfileAppsServiceImpl;
Tony Makde32b832018-04-30 15:11:57 +010092 private IApplicationThread mIApplicationThread;
Tony Mak1b708e62017-10-12 10:59:11 +010093
94 private SparseArray<Boolean> mUserEnabled = new SparseArray<>();
95
96 @Before
97 public void initCrossProfileAppsServiceImpl() {
98 mTestInjector = new TestInjector();
99 mCrossProfileAppsServiceImpl = new CrossProfileAppsServiceImpl(mContext, mTestInjector);
kholoud mohamed79a89f02020-01-15 15:30:07 +0000100 when(mContext.getPackageManager()).thenReturn(mPackageManager);
Tony Mak1b708e62017-10-12 10:59:11 +0100101 }
102
103 @Before
104 public void setupEnabledProfiles() {
105 mUserEnabled.put(PRIMARY_USER, true);
106 mUserEnabled.put(PROFILE_OF_PRIMARY_USER, true);
107 mUserEnabled.put(SECONDARY_USER, true);
108
109 when(mUserManager.getEnabledProfileIds(anyInt())).thenAnswer(
110 invocation -> {
111 List<Integer> users = new ArrayList<>();
112 final int targetUser = invocation.getArgument(0);
113 users.add(targetUser);
114
115 int profileUserId = -1;
116 if (targetUser == PRIMARY_USER) {
117 profileUserId = PROFILE_OF_PRIMARY_USER;
118 } else if (targetUser == PROFILE_OF_PRIMARY_USER) {
119 profileUserId = PRIMARY_USER;
120 }
121
122 if (profileUserId != -1 && mUserEnabled.get(profileUserId)) {
123 users.add(profileUserId);
124 }
125 return users.stream().mapToInt(i -> i).toArray();
126 });
127 }
128
129 @Before
130 public void setupCaller() {
131 mTestInjector.setCallingUid(PACKAGE_ONE_UID);
132 mTestInjector.setCallingUserId(PRIMARY_USER);
133 }
134
135 @Before
136 public void setupPackage() throws Exception {
137 // PACKAGE_ONE are installed in all users.
138 mockAppsInstalled(PACKAGE_ONE, PRIMARY_USER, true);
139 mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, true);
140 mockAppsInstalled(PACKAGE_ONE, SECONDARY_USER, true);
141
142 // Packages are resolved to their corresponding UID.
143 doAnswer(invocation -> {
144 final int uid = invocation.getArgument(0);
145 final String packageName = invocation.getArgument(1);
146 if (uid == PACKAGE_ONE_UID && PACKAGE_ONE.equals(packageName)) {
147 return null;
148 } else if (uid ==PACKAGE_TWO_UID && PACKAGE_TWO.equals(packageName)) {
149 return null;
150 }
151 throw new SecurityException("Not matching");
152 }).when(mAppOpsManager).checkPackage(anyInt(), anyString());
153
154 // The intent is resolved to the ACTIVITY_COMPONENT.
155 mockActivityLaunchIntentResolvedTo(ACTIVITY_COMPONENT);
156 }
157
158 @Test
159 public void getTargetUserProfiles_fromPrimaryUser_installed() throws Exception {
160 List<UserHandle> targetProfiles =
161 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
162 assertThat(targetProfiles).containsExactly(UserHandle.of(PROFILE_OF_PRIMARY_USER));
163 }
164
165 @Test
166 public void getTargetUserProfiles_fromPrimaryUser_notInstalled() throws Exception {
167 mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, false);
168
169 List<UserHandle> targetProfiles =
170 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
171 assertThat(targetProfiles).isEmpty();
172 }
173
174 @Test
175 public void getTargetUserProfiles_fromPrimaryUser_userNotEnabled() throws Exception {
176 mUserEnabled.put(PROFILE_OF_PRIMARY_USER, false);
177
178 List<UserHandle> targetProfiles =
179 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
180 assertThat(targetProfiles).isEmpty();
181 }
182
183 @Test
184 public void getTargetUserProfiles_fromSecondaryUser() throws Exception {
185 mTestInjector.setCallingUserId(SECONDARY_USER);
186
187 List<UserHandle> targetProfiles =
188 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
189 assertThat(targetProfiles).isEmpty();
190 }
191
192 @Test
193 public void getTargetUserProfiles_fromProfile_installed() throws Exception {
194 mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
195
196 List<UserHandle> targetProfiles =
197 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
198 assertThat(targetProfiles).containsExactly(UserHandle.of(PRIMARY_USER));
199 }
200
201 @Test
202 public void getTargetUserProfiles_fromProfile_notInstalled() throws Exception {
203 mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
204 mockAppsInstalled(PACKAGE_ONE, PRIMARY_USER, false);
205
206 List<UserHandle> targetProfiles =
207 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
208 assertThat(targetProfiles).isEmpty();
209 }
210
211 @Test(expected = SecurityException.class)
212 public void getTargetUserProfiles_fakeCaller() throws Exception {
213 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_TWO);
214 }
215
216 @Test
217 public void startActivityAsUser_currentUser() throws Exception {
218 assertThrows(
219 SecurityException.class,
220 () ->
221 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100222 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100223 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800224 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100225 ACTIVITY_COMPONENT,
Varun Shahacad1382018-11-28 11:56:08 -0800226 UserHandle.of(PRIMARY_USER).getIdentifier(),
227 true));
228
229 verify(mActivityTaskManagerInternal, never())
230 .startActivityAsUser(
231 nullable(IApplicationThread.class),
232 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800233 nullable(String.class),
Varun Shahacad1382018-11-28 11:56:08 -0800234 any(Intent.class),
235 nullable(Bundle.class),
236 anyInt());
237 }
238
239 @Test
240 public void startAnyActivityAsUser_currentUser() {
241 assertThrows(
242 SecurityException.class,
243 () ->
244 mCrossProfileAppsServiceImpl.startActivityAsUser(
245 mIApplicationThread,
246 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800247 FEATURE_ID,
Varun Shahacad1382018-11-28 11:56:08 -0800248 ACTIVITY_COMPONENT,
249 UserHandle.of(PRIMARY_USER).getIdentifier(),
250 false));
Tony Mak1b708e62017-10-12 10:59:11 +0100251
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700252 verify(mActivityTaskManagerInternal, never())
Tony Mak1b708e62017-10-12 10:59:11 +0100253 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100254 nullable(IApplicationThread.class),
255 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800256 nullable(String.class),
Tony Mak1b708e62017-10-12 10:59:11 +0100257 any(Intent.class),
258 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100259 anyInt());
Tony Mak1b708e62017-10-12 10:59:11 +0100260 }
261
262 @Test
Tony Mak1b708e62017-10-12 10:59:11 +0100263 public void startActivityAsUser_profile_notInstalled() throws Exception {
264 mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, false);
265
266 assertThrows(
267 SecurityException.class,
268 () ->
269 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100270 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100271 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800272 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100273 ACTIVITY_COMPONENT,
Varun Shahacad1382018-11-28 11:56:08 -0800274 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
275 true));
276
277 verify(mActivityTaskManagerInternal, never())
278 .startActivityAsUser(
279 nullable(IApplicationThread.class),
280 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800281 nullable(String.class),
Varun Shahacad1382018-11-28 11:56:08 -0800282 any(Intent.class),
283 nullable(Bundle.class),
284 anyInt());
285 }
286
287 @Test
288 public void startAnyActivityAsUser_profile_notInstalled() {
289 mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, false);
290
291 assertThrows(
292 SecurityException.class,
293 () ->
294 mCrossProfileAppsServiceImpl.startActivityAsUser(
295 mIApplicationThread,
296 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800297 FEATURE_ID,
Varun Shahacad1382018-11-28 11:56:08 -0800298 ACTIVITY_COMPONENT,
299 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
300 false));
Tony Mak1b708e62017-10-12 10:59:11 +0100301
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700302 verify(mActivityTaskManagerInternal, never())
Tony Mak1b708e62017-10-12 10:59:11 +0100303 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100304 nullable(IApplicationThread.class),
305 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800306 nullable(String.class),
Tony Mak1b708e62017-10-12 10:59:11 +0100307 any(Intent.class),
308 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100309 anyInt());
Tony Mak1b708e62017-10-12 10:59:11 +0100310 }
311
312 @Test
313 public void startActivityAsUser_profile_fakeCaller() throws Exception {
314 assertThrows(
315 SecurityException.class,
316 () ->
317 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100318 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100319 PACKAGE_TWO,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800320 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100321 ACTIVITY_COMPONENT,
Varun Shahacad1382018-11-28 11:56:08 -0800322 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
323 true));
324
325 verify(mActivityTaskManagerInternal, never())
326 .startActivityAsUser(
327 nullable(IApplicationThread.class),
328 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800329 nullable(String.class),
Varun Shahacad1382018-11-28 11:56:08 -0800330 any(Intent.class),
331 nullable(Bundle.class),
332 anyInt());
333 }
334
335 @Test
336 public void startAnyActivityAsUser_profile_fakeCaller() {
337 assertThrows(
338 SecurityException.class,
339 () ->
340 mCrossProfileAppsServiceImpl.startActivityAsUser(
341 mIApplicationThread,
342 PACKAGE_TWO,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800343 FEATURE_ID,
Varun Shahacad1382018-11-28 11:56:08 -0800344 ACTIVITY_COMPONENT,
345 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
346 false));
Tony Mak1b708e62017-10-12 10:59:11 +0100347
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700348 verify(mActivityTaskManagerInternal, never())
Tony Mak1b708e62017-10-12 10:59:11 +0100349 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100350 nullable(IApplicationThread.class),
351 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800352 nullable(String.class),
Tony Mak1b708e62017-10-12 10:59:11 +0100353 any(Intent.class),
354 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100355 anyInt());
Tony Mak1b708e62017-10-12 10:59:11 +0100356 }
357
358 @Test
359 public void startActivityAsUser_profile_notExported() throws Exception {
360 mActivityInfo.exported = false;
361
362 assertThrows(
363 SecurityException.class,
364 () ->
365 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100366 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100367 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800368 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100369 ACTIVITY_COMPONENT,
Varun Shahacad1382018-11-28 11:56:08 -0800370 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
371 true));
372
373 verify(mActivityTaskManagerInternal, never())
374 .startActivityAsUser(
375 nullable(IApplicationThread.class),
376 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800377 nullable(String.class),
Varun Shahacad1382018-11-28 11:56:08 -0800378 any(Intent.class),
379 nullable(Bundle.class),
380 anyInt());
381 }
382
383 @Test
384 public void startAnyActivityAsUser_profile_notExported() {
kholoud mohamed79a89f02020-01-15 15:30:07 +0000385 try {
386 when(mPackageManager.getPermissionInfo(anyString(), anyInt()))
387 .thenReturn(new PermissionInfo());
388 } catch (PackageManager.NameNotFoundException ignored) {
389 }
Varun Shahacad1382018-11-28 11:56:08 -0800390 mActivityInfo.exported = false;
391
392 assertThrows(
393 SecurityException.class,
394 () ->
395 mCrossProfileAppsServiceImpl.startActivityAsUser(
396 mIApplicationThread,
397 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800398 FEATURE_ID,
Varun Shahacad1382018-11-28 11:56:08 -0800399 ACTIVITY_COMPONENT,
400 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
401 false));
Tony Mak1b708e62017-10-12 10:59:11 +0100402
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700403 verify(mActivityTaskManagerInternal, never())
Tony Mak1b708e62017-10-12 10:59:11 +0100404 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100405 nullable(IApplicationThread.class),
406 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800407 nullable(String.class),
Tony Mak1b708e62017-10-12 10:59:11 +0100408 any(Intent.class),
409 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100410 anyInt());
Tony Mak1b708e62017-10-12 10:59:11 +0100411 }
412
413 @Test
414 public void startActivityAsUser_profile_anotherPackage() throws Exception {
415 assertThrows(
416 SecurityException.class,
417 () ->
418 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100419 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100420 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800421 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100422 new ComponentName(PACKAGE_TWO, "test"),
Varun Shahacad1382018-11-28 11:56:08 -0800423 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
424 true));
425
426 verify(mActivityTaskManagerInternal, never())
427 .startActivityAsUser(
428 nullable(IApplicationThread.class),
429 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800430 nullable(String.class),
Varun Shahacad1382018-11-28 11:56:08 -0800431 any(Intent.class),
432 nullable(Bundle.class),
433 anyInt());
434 }
435
436 @Test
437 public void startAnyActivityAsUser_profile_anotherPackage() {
438 assertThrows(
439 SecurityException.class,
440 () ->
441 mCrossProfileAppsServiceImpl.startActivityAsUser(
442 mIApplicationThread,
443 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800444 FEATURE_ID,
Varun Shahacad1382018-11-28 11:56:08 -0800445 new ComponentName(PACKAGE_TWO, "test"),
446 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
447 false));
Tony Mak1b708e62017-10-12 10:59:11 +0100448
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700449 verify(mActivityTaskManagerInternal, never())
Tony Mak1b708e62017-10-12 10:59:11 +0100450 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100451 nullable(IApplicationThread.class),
452 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800453 nullable(String.class),
Tony Mak1b708e62017-10-12 10:59:11 +0100454 any(Intent.class),
455 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100456 anyInt());
Tony Mak1b708e62017-10-12 10:59:11 +0100457 }
458
459 @Test
460 public void startActivityAsUser_secondaryUser() throws Exception {
461 assertThrows(
462 SecurityException.class,
463 () ->
464 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100465 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100466 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800467 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100468 ACTIVITY_COMPONENT,
Varun Shahacad1382018-11-28 11:56:08 -0800469 UserHandle.of(SECONDARY_USER).getIdentifier(),
470 true));
471
472 verify(mActivityTaskManagerInternal, never())
473 .startActivityAsUser(
474 nullable(IApplicationThread.class),
475 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800476 nullable(String.class),
Varun Shahacad1382018-11-28 11:56:08 -0800477 any(Intent.class),
478 nullable(Bundle.class),
479 anyInt());
480 }
481
482 @Test
483 public void startAnyActivityAsUser_secondaryUser() {
484 assertThrows(
485 SecurityException.class,
486 () ->
487 mCrossProfileAppsServiceImpl.startActivityAsUser(
488 mIApplicationThread,
489 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800490 FEATURE_ID,
Varun Shahacad1382018-11-28 11:56:08 -0800491 ACTIVITY_COMPONENT,
492 UserHandle.of(SECONDARY_USER).getIdentifier(),
493 false));
Tony Mak1b708e62017-10-12 10:59:11 +0100494
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700495 verify(mActivityTaskManagerInternal, never())
Tony Mak1b708e62017-10-12 10:59:11 +0100496 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100497 nullable(IApplicationThread.class),
498 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800499 nullable(String.class),
Tony Mak1b708e62017-10-12 10:59:11 +0100500 any(Intent.class),
501 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100502 anyInt());
Tony Mak1b708e62017-10-12 10:59:11 +0100503 }
504
505 @Test
506 public void startActivityAsUser_fromProfile_success() throws Exception {
507 mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
508
509 mCrossProfileAppsServiceImpl.startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100510 mIApplicationThread,
Tony Mak1b708e62017-10-12 10:59:11 +0100511 PACKAGE_ONE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800512 FEATURE_ID,
Tony Mak1b708e62017-10-12 10:59:11 +0100513 ACTIVITY_COMPONENT,
Varun Shahacad1382018-11-28 11:56:08 -0800514 UserHandle.of(PRIMARY_USER).getIdentifier(),
515 true);
Tony Mak1b708e62017-10-12 10:59:11 +0100516
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700517 verify(mActivityTaskManagerInternal)
Tony Mak1b708e62017-10-12 10:59:11 +0100518 .startActivityAsUser(
Tony Makde32b832018-04-30 15:11:57 +0100519 nullable(IApplicationThread.class),
520 eq(PACKAGE_ONE),
Philip P. Moltmannee295092020-02-10 08:46:26 -0800521 eq(FEATURE_ID),
Tony Mak1b708e62017-10-12 10:59:11 +0100522 any(Intent.class),
523 nullable(Bundle.class),
Tony Makde32b832018-04-30 15:11:57 +0100524 eq(PRIMARY_USER));
Tony Mak1b708e62017-10-12 10:59:11 +0100525 }
526
527 private void mockAppsInstalled(String packageName, int user, boolean installed) {
528 when(mPackageManagerInternal.getPackageInfo(
529 eq(packageName),
530 anyInt(),
531 anyInt(),
532 eq(user)))
533 .thenReturn(installed ? createInstalledPackageInfo() : null);
534 }
535
536 private PackageInfo createInstalledPackageInfo() {
537 PackageInfo packageInfo = new PackageInfo();
538 packageInfo.applicationInfo = new ApplicationInfo();
539 packageInfo.applicationInfo.enabled = true;
540 return packageInfo;
541 }
542
543 private void mockActivityLaunchIntentResolvedTo(ComponentName componentName) {
544 ResolveInfo resolveInfo = new ResolveInfo();
545 ActivityInfo activityInfo = new ActivityInfo();
546 activityInfo.packageName = componentName.getPackageName();
547 activityInfo.name = componentName.getClassName();
548 activityInfo.exported = true;
549 resolveInfo.activityInfo = activityInfo;
550 mActivityInfo = activityInfo;
551
552 when(mPackageManagerInternal.queryIntentActivities(
Patrick Baumann182ea1c2019-11-18 13:43:11 -0800553 any(Intent.class), nullable(String.class), anyInt(), anyInt(), anyInt()))
Tony Mak1b708e62017-10-12 10:59:11 +0100554 .thenReturn(Collections.singletonList(resolveInfo));
555 }
556
557 private class TestInjector implements CrossProfileAppsServiceImpl.Injector {
558 private int mCallingUid;
559 private int mCallingUserId;
kholoud mohamed79a89f02020-01-15 15:30:07 +0000560 private int mCallingPid;
Tony Mak1b708e62017-10-12 10:59:11 +0100561
562 public void setCallingUid(int uid) {
563 mCallingUid = uid;
564 }
565
kholoud mohamed79a89f02020-01-15 15:30:07 +0000566 public void setCallingPid(int pid) {
567 mCallingPid = pid;
568 }
569
Tony Mak1b708e62017-10-12 10:59:11 +0100570 public void setCallingUserId(int userId) {
571 mCallingUserId = userId;
572 }
573
574 @Override
575 public int getCallingUid() {
576 return mCallingUid;
577 }
578
579 @Override
kholoud mohamed79a89f02020-01-15 15:30:07 +0000580 public int getCallingPid() {
581 return mCallingPid;
582 }
583
584 @Override
Tony Mak1b708e62017-10-12 10:59:11 +0100585 public int getCallingUserId() {
586 return mCallingUserId;
587 }
588
589 @Override
590 public UserHandle getCallingUserHandle() {
591 return UserHandle.of(mCallingUserId);
592 }
593
594 @Override
595 public long clearCallingIdentity() {
596 return 0;
597 }
598
599 @Override
600 public void restoreCallingIdentity(long token) {
601 }
602
603 @Override
604 public UserManager getUserManager() {
605 return mUserManager;
606 }
607
608 @Override
609 public PackageManagerInternal getPackageManagerInternal() {
610 return mPackageManagerInternal;
611 }
612
613 @Override
614 public PackageManager getPackageManager() {
615 return mPackageManager;
616 }
617
618 @Override
619 public AppOpsManager getAppOpsManager() {
620 return mAppOpsManager;
621 }
Tony Makde32b832018-04-30 15:11:57 +0100622
623 @Override
624 public ActivityManagerInternal getActivityManagerInternal() {
625 return mActivityManagerInternal;
626 }
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700627
628 @Override
629 public ActivityTaskManagerInternal getActivityTaskManagerInternal() {
630 return mActivityTaskManagerInternal;
631 }
Alex Kershaw6fb8a022020-01-09 11:33:56 +0000632
633 @Override
634 public IPackageManager getIPackageManager() {
635 return mIPackageManager;
636 }
637
638 @Override
639 public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
640 return mDevicePolicyManagerInternal;
641 }
642
643 @Override
644 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
645 mContext.sendBroadcastAsUser(intent, user);
646 }
647
648 @Override
649 public int checkComponentPermission(
650 String permission, int uid, int owningUid, boolean exported) {
651 return ActivityManager.checkComponentPermission(permission, uid, owningUid, exported);
652 }
Tony Mak1b708e62017-10-12 10:59:11 +0100653 }
654}