Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.pm; |
| 18 | |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 19 | import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED; |
| 20 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 24 | import android.content.pm.UserInfo; |
| 25 | import android.os.Looper; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 26 | import android.os.UserHandle; |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 27 | import android.os.UserManagerInternal; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 28 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 29 | import androidx.test.InstrumentationRegistry; |
| 30 | import androidx.test.filters.MediumTest; |
| 31 | import androidx.test.runner.AndroidJUnit4; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 32 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 33 | import com.android.server.LocalServices; |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 34 | |
| 35 | import org.junit.After; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 42 | /** |
| 43 | * <p>Run with:<pre> |
| 44 | * runtest -c com.android.server.pm.UserManagerServiceCreateProfileTest frameworks-services |
| 45 | * </pre> |
| 46 | */ |
| 47 | @RunWith(AndroidJUnit4.class) |
| 48 | @MediumTest |
| 49 | public class UserManagerServiceCreateProfileTest { |
| 50 | private UserManagerService mUserManagerService; |
| 51 | |
| 52 | @Before |
| 53 | public void setup() { |
| 54 | // Currently UserManagerService cannot be instantiated twice inside a VM without a cleanup |
| 55 | // TODO: Remove once UMS supports proper dependency injection |
| 56 | if (Looper.myLooper() == null) { |
| 57 | Looper.prepare(); |
| 58 | } |
| 59 | LocalServices.removeServiceForTest(UserManagerInternal.class); |
| 60 | mUserManagerService = new UserManagerService(InstrumentationRegistry.getContext()); |
| 61 | |
| 62 | // The tests assume that the device has one user and its the system user. |
| 63 | List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false); |
| 64 | assertEquals("Multiple users so this test can't run.", 1, users.size()); |
| 65 | assertEquals("Only user present isn't the system user.", |
| 66 | UserHandle.USER_SYSTEM, users.get(0).id); |
| 67 | } |
| 68 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 69 | @After |
| 70 | public void tearDown() { |
| 71 | removeUsers(); |
| 72 | } |
| 73 | |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 74 | /** Tests UMS.getProfileIds() when no specific userType is specified. */ |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 75 | @Test |
| 76 | public void testGetProfiles() { |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 77 | // Pretend we have a secondary user with a profile. |
| 78 | UserInfo secondaryUser = addUser(); |
| 79 | UserInfo profile = addProfile(secondaryUser); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 80 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 81 | // System user should still have no profile so getProfiles should just return 1 user. |
| 82 | List<UserInfo> users = |
| 83 | mUserManagerService.getProfiles(UserHandle.USER_SYSTEM, /*excludeDying*/ false); |
| 84 | assertEquals("Profiles returned where none should exist", 1, users.size()); |
| 85 | assertEquals("Missing system user from profile list of system user", |
| 86 | UserHandle.USER_SYSTEM, users.get(0).id); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 87 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 88 | // Secondary user should have 1 profile, so return that and itself. |
| 89 | users = mUserManagerService.getProfiles(secondaryUser.id, /*excludeDying*/ false); |
| 90 | assertEquals("Profiles returned where none should exist", 2, users.size()); |
| 91 | assertTrue("Missing secondary user id", users.get(0).id == secondaryUser.id |
| 92 | || users.get(1).id == secondaryUser.id); |
| 93 | assertTrue("Missing profile user id", users.get(0).id == profile.id |
| 94 | || users.get(1).id == profile.id); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 97 | /** Tests UMS.getProfileIds() when a specific userType is specified. */ |
| 98 | @Test |
| 99 | public void testGetProfileIds_specifyType() { |
| 100 | // Pretend we have a secondary user with a profile. |
| 101 | UserInfo secondaryUser = addUser(); |
| 102 | UserInfo profile = addProfile(secondaryUser); |
| 103 | |
| 104 | // TODO: When there are multiple profiles types, ensure correct output for mixed types. |
| 105 | final String userType1 = USER_TYPE_PROFILE_MANAGED; |
| 106 | |
| 107 | // System user should still have no userType1 profile so getProfileIds should be empty. |
| 108 | int[] users = mUserManagerService.getProfileIds(UserHandle.USER_SYSTEM, userType1, false); |
| 109 | assertEquals("System user should have no managed profiles", 0, users.length); |
| 110 | |
| 111 | // Secondary user should have one userType1 profile, so return just that. |
| 112 | users = mUserManagerService.getProfileIds(secondaryUser.id, userType1, false); |
| 113 | assertEquals("Wrong number of profiles", 1, users.length); |
| 114 | assertEquals("Wrong profile id", profile.id, users[0]); |
| 115 | |
| 116 | // The profile itself is a userType1 profile, so it should return just itself. |
| 117 | users = mUserManagerService.getProfileIds(profile.id, userType1, false); |
| 118 | assertEquals("Wrong number of profiles", 1, users.length); |
| 119 | assertEquals("Wrong profile id", profile.id, users[0]); |
| 120 | } |
| 121 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 122 | @Test |
| 123 | public void testProfileBadge() { |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 124 | // First profile for system user should get badge 0 |
| 125 | assertEquals("First profile isn't given badge index 0", 0, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 126 | mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM, |
| 127 | USER_TYPE_PROFILE_MANAGED)); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 128 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 129 | // Pretend we have a secondary user. |
| 130 | UserInfo secondaryUser = addUser(); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 131 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 132 | // Check first profile badge for secondary user is also 0. |
| 133 | assertEquals("First profile for secondary user isn't given badge index 0", 0, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 134 | mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id, |
| 135 | USER_TYPE_PROFILE_MANAGED)); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 136 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 137 | // Shouldn't impact the badge for profile in system user |
| 138 | assertEquals("First profile isn't given badge index 0 with secondary user", 0, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 139 | mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM, |
| 140 | USER_TYPE_PROFILE_MANAGED)); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 141 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 142 | // Pretend a secondary user has a profile. |
| 143 | addProfile(secondaryUser); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 144 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 145 | // Shouldn't have impacted the badge for the system user |
| 146 | assertEquals("First profile isn't given badge index 0 in secondary user", 0, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 147 | mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM, |
| 148 | USER_TYPE_PROFILE_MANAGED)); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testProfileBadgeUnique() { |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 153 | List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false); |
| 154 | UserInfo system = users.get(0); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 155 | int max = mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED); |
| 156 | if (max < 0) { |
| 157 | // Indicates no max. Instead of infinite, we'll just do 10. |
| 158 | max = 10; |
| 159 | } |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 160 | // Badges should get allocated 0 -> max |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 161 | for (int i = 0; i < max; ++i) { |
| 162 | int nextBadge = mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM, |
| 163 | USER_TYPE_PROFILE_MANAGED); |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 164 | assertEquals("Wrong badge allocated", i, nextBadge); |
| 165 | UserInfo profile = addProfile(system); |
| 166 | profile.profileBadge = nextBadge; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testProfileBadgeReuse() { |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 172 | // Pretend we have a secondary user with a profile. |
| 173 | UserInfo secondaryUser = addUser(); |
| 174 | UserInfo profile = addProfile(secondaryUser); |
| 175 | // Add the profile it to the users being removed. |
| 176 | mUserManagerService.addRemovingUserIdLocked(profile.id); |
| 177 | // We should reuse the badge from the profile being removed. |
| 178 | assertEquals("Badge index not reused while removing a user", 0, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 179 | mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id, |
| 180 | USER_TYPE_PROFILE_MANAGED)); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 181 | |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 182 | // Edge case of reuse that only applies if we ever support 3 managed profiles |
| 183 | // We should prioritise using lower badge indexes |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 184 | int max = mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED); |
| 185 | if (max < 0 || max > 2) { |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 186 | UserInfo profileBadgeOne = addProfile(secondaryUser); |
| 187 | profileBadgeOne.profileBadge = 1; |
| 188 | // 0 and 2 are free, we should reuse 0 rather than 2. |
| 189 | assertEquals("Lower index not used", 0, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 190 | mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id, |
| 191 | USER_TYPE_PROFILE_MANAGED)); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | @Test |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 196 | public void testCanAddMoreManagedProfiles_removeProfile() { |
| 197 | // if device is low-ram or doesn't support managed profiles for some other reason, just |
| 198 | // skip the test |
| 199 | if (!mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 200 | false /* disallow remove */)) { |
| 201 | return; |
| 202 | } |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 203 | if (mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED) < 0) { |
| 204 | // Indicates no limit, so we cannot run this test; |
| 205 | return; |
| 206 | } |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 207 | |
| 208 | // GIVEN we've reached the limit of managed profiles possible on the system user |
| 209 | while (mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 210 | false /* disallow remove */)) { |
| 211 | addProfile(mUserManagerService.getPrimaryUser()); |
| 212 | } |
| 213 | |
| 214 | // THEN you should be able to add a new profile if you remove an existing one |
| 215 | assertTrue("Cannot add a managed profile by removing another one", |
| 216 | mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 217 | true /* allow remove */)); |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testCanAddMoreManagedProfiles_removeDisabledProfile() { |
| 222 | // if device is low-ram or doesn't support managed profiles for some other reason, just |
| 223 | // skip the test |
| 224 | if (!mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 225 | false /* disallow remove */)) { |
| 226 | return; |
| 227 | } |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 228 | if (mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED) < 0) { |
| 229 | // Indicates no limit, so we cannot run this test; |
| 230 | return; |
| 231 | } |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 232 | |
| 233 | // GIVEN we've reached the limit of managed profiles possible on the system user |
| 234 | // GIVEN that the profiles are not enabled yet |
| 235 | while (mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 236 | false /* disallow remove */)) { |
| 237 | addProfile(mUserManagerService.getPrimaryUser(), true /* disabled */); |
| 238 | } |
| 239 | |
| 240 | // THEN you should be able to add a new profile if you remove an existing one |
| 241 | assertTrue("Cannot add a managed profile by removing another one", |
| 242 | mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 243 | true /* allow remove */)); |
| 244 | } |
| 245 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 246 | private void removeUsers() { |
| 247 | List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false); |
| 248 | for (UserInfo user: users) { |
| 249 | if (user.id != UserHandle.USER_SYSTEM) { |
| 250 | mUserManagerService.removeUserInfo(user.id); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private UserInfo addProfile(UserInfo user) { |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 256 | return addProfile(user, false); |
| 257 | } |
| 258 | |
| 259 | private UserInfo addProfile(UserInfo user, boolean disabled) { |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 260 | user.profileGroupId = user.id; |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 261 | int flags = UserInfo.FLAG_MANAGED_PROFILE; |
| 262 | if (disabled) { |
| 263 | flags |= UserInfo.FLAG_DISABLED; |
| 264 | } |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 265 | UserInfo profile = new UserInfo( |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 266 | mUserManagerService.getNextAvailableId(), "profile", flags); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 267 | profile.profileGroupId = user.id; |
| 268 | mUserManagerService.putUserInfo(profile); |
| 269 | return profile; |
| 270 | } |
| 271 | |
| 272 | private UserInfo addUser() { |
| 273 | UserInfo secondaryUser = new UserInfo( |
| 274 | mUserManagerService.getNextAvailableId(), "secondary", /* flags */ 0); |
| 275 | mUserManagerService.putUserInfo(secondaryUser); |
| 276 | return secondaryUser; |
| 277 | } |
| 278 | } |