Tony Mak | b0d2262 | 2018-01-18 12:49:49 +0000 | [diff] [blame] | 1 | package com.android.server.pm; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 2 | |
| 3 | import static com.google.common.truth.Truth.assertThat; |
| 4 | |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 5 | import static org.mockito.ArgumentMatchers.any; |
| 6 | import static org.mockito.ArgumentMatchers.anyInt; |
| 7 | import static org.mockito.ArgumentMatchers.anyString; |
| 8 | import static org.mockito.ArgumentMatchers.eq; |
| 9 | import static org.mockito.ArgumentMatchers.nullable; |
| 10 | import static org.mockito.Mockito.doAnswer; |
| 11 | import static org.mockito.Mockito.never; |
| 12 | import static org.mockito.Mockito.verify; |
| 13 | import static org.mockito.Mockito.when; |
| 14 | import static org.testng.Assert.assertThrows; |
| 15 | |
Alex Kershaw | 6fb8a02 | 2020-01-09 11:33:56 +0000 | [diff] [blame] | 16 | import android.app.ActivityManager; |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 17 | import android.app.ActivityManagerInternal; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 18 | import android.app.AppOpsManager; |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 19 | import android.app.IApplicationThread; |
Alex Kershaw | 6fb8a02 | 2020-01-09 11:33:56 +0000 | [diff] [blame] | 20 | import android.app.admin.DevicePolicyManagerInternal; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.ActivityInfo; |
| 25 | import android.content.pm.ApplicationInfo; |
Alex Kershaw | 6fb8a02 | 2020-01-09 11:33:56 +0000 | [diff] [blame] | 26 | import android.content.pm.IPackageManager; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 27 | import android.content.pm.PackageInfo; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.PackageManagerInternal; |
kholoud mohamed | 79a89f0 | 2020-01-15 15:30:07 +0000 | [diff] [blame] | 30 | import android.content.pm.PermissionInfo; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.UserHandle; |
| 34 | import android.os.UserManager; |
| 35 | import android.platform.test.annotations.Presubmit; |
| 36 | import android.util.SparseArray; |
| 37 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 38 | import com.android.server.wm.ActivityTaskManagerInternal; |
| 39 | |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 40 | import org.junit.Before; |
| 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 43 | import org.mockito.Mock; |
| 44 | import org.mockito.junit.MockitoJUnitRunner; |
| 45 | |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.Collections; |
| 48 | import java.util.List; |
| 49 | |
| 50 | /** |
| 51 | * Build/Install/Run: |
Tony Mak | b0d2262 | 2018-01-18 12:49:49 +0000 | [diff] [blame] | 52 | * atest FrameworksServicesTests:com.android.server.pm.CrossProfileAppsServiceImplTest |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 53 | */ |
| 54 | @Presubmit |
| 55 | @RunWith(MockitoJUnitRunner.class) |
| 56 | public class CrossProfileAppsServiceImplTest { |
| 57 | private static final String PACKAGE_ONE = "com.one"; |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 58 | private static final String FEATURE_ID = "feature.one"; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 59 | 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 Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 80 | @Mock |
| 81 | private ActivityManagerInternal mActivityManagerInternal; |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 82 | @Mock |
| 83 | private ActivityTaskManagerInternal mActivityTaskManagerInternal; |
Alex Kershaw | 6fb8a02 | 2020-01-09 11:33:56 +0000 | [diff] [blame] | 84 | @Mock |
| 85 | private IPackageManager mIPackageManager; |
| 86 | @Mock |
| 87 | private DevicePolicyManagerInternal mDevicePolicyManagerInternal; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 88 | |
| 89 | private TestInjector mTestInjector; |
| 90 | private ActivityInfo mActivityInfo; |
| 91 | private CrossProfileAppsServiceImpl mCrossProfileAppsServiceImpl; |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 92 | private IApplicationThread mIApplicationThread; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 93 | |
| 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 mohamed | 79a89f0 | 2020-01-15 15:30:07 +0000 | [diff] [blame] | 100 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 101 | } |
| 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 Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 222 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 223 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 224 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 225 | ACTIVITY_COMPONENT, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 226 | UserHandle.of(PRIMARY_USER).getIdentifier(), |
| 227 | true)); |
| 228 | |
| 229 | verify(mActivityTaskManagerInternal, never()) |
| 230 | .startActivityAsUser( |
| 231 | nullable(IApplicationThread.class), |
| 232 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 233 | nullable(String.class), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 234 | 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. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 247 | FEATURE_ID, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 248 | ACTIVITY_COMPONENT, |
| 249 | UserHandle.of(PRIMARY_USER).getIdentifier(), |
| 250 | false)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 251 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 252 | verify(mActivityTaskManagerInternal, never()) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 253 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 254 | nullable(IApplicationThread.class), |
| 255 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 256 | nullable(String.class), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 257 | any(Intent.class), |
| 258 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 259 | anyInt()); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Test |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 263 | 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 Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 270 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 271 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 272 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 273 | ACTIVITY_COMPONENT, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 274 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 275 | true)); |
| 276 | |
| 277 | verify(mActivityTaskManagerInternal, never()) |
| 278 | .startActivityAsUser( |
| 279 | nullable(IApplicationThread.class), |
| 280 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 281 | nullable(String.class), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 282 | 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. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 297 | FEATURE_ID, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 298 | ACTIVITY_COMPONENT, |
| 299 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 300 | false)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 301 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 302 | verify(mActivityTaskManagerInternal, never()) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 303 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 304 | nullable(IApplicationThread.class), |
| 305 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 306 | nullable(String.class), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 307 | any(Intent.class), |
| 308 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 309 | anyInt()); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Test |
| 313 | public void startActivityAsUser_profile_fakeCaller() throws Exception { |
| 314 | assertThrows( |
| 315 | SecurityException.class, |
| 316 | () -> |
| 317 | mCrossProfileAppsServiceImpl.startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 318 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 319 | PACKAGE_TWO, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 320 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 321 | ACTIVITY_COMPONENT, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 322 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 323 | true)); |
| 324 | |
| 325 | verify(mActivityTaskManagerInternal, never()) |
| 326 | .startActivityAsUser( |
| 327 | nullable(IApplicationThread.class), |
| 328 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 329 | nullable(String.class), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 330 | 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. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 343 | FEATURE_ID, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 344 | ACTIVITY_COMPONENT, |
| 345 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 346 | false)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 347 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 348 | verify(mActivityTaskManagerInternal, never()) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 349 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 350 | nullable(IApplicationThread.class), |
| 351 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 352 | nullable(String.class), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 353 | any(Intent.class), |
| 354 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 355 | anyInt()); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 356 | } |
| 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 Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 366 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 367 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 368 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 369 | ACTIVITY_COMPONENT, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 370 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 371 | true)); |
| 372 | |
| 373 | verify(mActivityTaskManagerInternal, never()) |
| 374 | .startActivityAsUser( |
| 375 | nullable(IApplicationThread.class), |
| 376 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 377 | nullable(String.class), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 378 | any(Intent.class), |
| 379 | nullable(Bundle.class), |
| 380 | anyInt()); |
| 381 | } |
| 382 | |
| 383 | @Test |
| 384 | public void startAnyActivityAsUser_profile_notExported() { |
kholoud mohamed | 79a89f0 | 2020-01-15 15:30:07 +0000 | [diff] [blame] | 385 | try { |
| 386 | when(mPackageManager.getPermissionInfo(anyString(), anyInt())) |
| 387 | .thenReturn(new PermissionInfo()); |
| 388 | } catch (PackageManager.NameNotFoundException ignored) { |
| 389 | } |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 390 | mActivityInfo.exported = false; |
| 391 | |
| 392 | assertThrows( |
| 393 | SecurityException.class, |
| 394 | () -> |
| 395 | mCrossProfileAppsServiceImpl.startActivityAsUser( |
| 396 | mIApplicationThread, |
| 397 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 398 | FEATURE_ID, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 399 | ACTIVITY_COMPONENT, |
| 400 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 401 | false)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 402 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 403 | verify(mActivityTaskManagerInternal, never()) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 404 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 405 | nullable(IApplicationThread.class), |
| 406 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 407 | nullable(String.class), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 408 | any(Intent.class), |
| 409 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 410 | anyInt()); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | @Test |
| 414 | public void startActivityAsUser_profile_anotherPackage() throws Exception { |
| 415 | assertThrows( |
| 416 | SecurityException.class, |
| 417 | () -> |
| 418 | mCrossProfileAppsServiceImpl.startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 419 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 420 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 421 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 422 | new ComponentName(PACKAGE_TWO, "test"), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 423 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 424 | true)); |
| 425 | |
| 426 | verify(mActivityTaskManagerInternal, never()) |
| 427 | .startActivityAsUser( |
| 428 | nullable(IApplicationThread.class), |
| 429 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 430 | nullable(String.class), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 431 | 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. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 444 | FEATURE_ID, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 445 | new ComponentName(PACKAGE_TWO, "test"), |
| 446 | UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(), |
| 447 | false)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 448 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 449 | verify(mActivityTaskManagerInternal, never()) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 450 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 451 | nullable(IApplicationThread.class), |
| 452 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 453 | nullable(String.class), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 454 | any(Intent.class), |
| 455 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 456 | anyInt()); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Test |
| 460 | public void startActivityAsUser_secondaryUser() throws Exception { |
| 461 | assertThrows( |
| 462 | SecurityException.class, |
| 463 | () -> |
| 464 | mCrossProfileAppsServiceImpl.startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 465 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 466 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 467 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 468 | ACTIVITY_COMPONENT, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 469 | UserHandle.of(SECONDARY_USER).getIdentifier(), |
| 470 | true)); |
| 471 | |
| 472 | verify(mActivityTaskManagerInternal, never()) |
| 473 | .startActivityAsUser( |
| 474 | nullable(IApplicationThread.class), |
| 475 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 476 | nullable(String.class), |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 477 | 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. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 490 | FEATURE_ID, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 491 | ACTIVITY_COMPONENT, |
| 492 | UserHandle.of(SECONDARY_USER).getIdentifier(), |
| 493 | false)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 494 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 495 | verify(mActivityTaskManagerInternal, never()) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 496 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 497 | nullable(IApplicationThread.class), |
| 498 | anyString(), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 499 | nullable(String.class), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 500 | any(Intent.class), |
| 501 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 502 | anyInt()); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | @Test |
| 506 | public void startActivityAsUser_fromProfile_success() throws Exception { |
| 507 | mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER); |
| 508 | |
| 509 | mCrossProfileAppsServiceImpl.startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 510 | mIApplicationThread, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 511 | PACKAGE_ONE, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 512 | FEATURE_ID, |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 513 | ACTIVITY_COMPONENT, |
Varun Shah | acad138 | 2018-11-28 11:56:08 -0800 | [diff] [blame] | 514 | UserHandle.of(PRIMARY_USER).getIdentifier(), |
| 515 | true); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 516 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 517 | verify(mActivityTaskManagerInternal) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 518 | .startActivityAsUser( |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 519 | nullable(IApplicationThread.class), |
| 520 | eq(PACKAGE_ONE), |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame^] | 521 | eq(FEATURE_ID), |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 522 | any(Intent.class), |
| 523 | nullable(Bundle.class), |
Tony Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 524 | eq(PRIMARY_USER)); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 525 | } |
| 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 Baumann | 182ea1c | 2019-11-18 13:43:11 -0800 | [diff] [blame] | 553 | any(Intent.class), nullable(String.class), anyInt(), anyInt(), anyInt())) |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 554 | .thenReturn(Collections.singletonList(resolveInfo)); |
| 555 | } |
| 556 | |
| 557 | private class TestInjector implements CrossProfileAppsServiceImpl.Injector { |
| 558 | private int mCallingUid; |
| 559 | private int mCallingUserId; |
kholoud mohamed | 79a89f0 | 2020-01-15 15:30:07 +0000 | [diff] [blame] | 560 | private int mCallingPid; |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 561 | |
| 562 | public void setCallingUid(int uid) { |
| 563 | mCallingUid = uid; |
| 564 | } |
| 565 | |
kholoud mohamed | 79a89f0 | 2020-01-15 15:30:07 +0000 | [diff] [blame] | 566 | public void setCallingPid(int pid) { |
| 567 | mCallingPid = pid; |
| 568 | } |
| 569 | |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 570 | public void setCallingUserId(int userId) { |
| 571 | mCallingUserId = userId; |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public int getCallingUid() { |
| 576 | return mCallingUid; |
| 577 | } |
| 578 | |
| 579 | @Override |
kholoud mohamed | 79a89f0 | 2020-01-15 15:30:07 +0000 | [diff] [blame] | 580 | public int getCallingPid() { |
| 581 | return mCallingPid; |
| 582 | } |
| 583 | |
| 584 | @Override |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 585 | 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 Mak | de32b83 | 2018-04-30 15:11:57 +0100 | [diff] [blame] | 622 | |
| 623 | @Override |
| 624 | public ActivityManagerInternal getActivityManagerInternal() { |
| 625 | return mActivityManagerInternal; |
| 626 | } |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 627 | |
| 628 | @Override |
| 629 | public ActivityTaskManagerInternal getActivityTaskManagerInternal() { |
| 630 | return mActivityTaskManagerInternal; |
| 631 | } |
Alex Kershaw | 6fb8a02 | 2020-01-09 11:33:56 +0000 | [diff] [blame] | 632 | |
| 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 Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 653 | } |
| 654 | } |