blob: e375aef3b7f06dc318d8371d54319ee360b76795 [file] [log] [blame]
Kenny Guy02c89902016-11-15 19:36:38 +00001/*
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
17package com.android.server.pm;
18
Bookatz029832a2019-10-04 16:50:22 -070019import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED;
20
Benjamin Franzc8776152017-01-06 14:16:41 +000021import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertTrue;
23
Kenny Guy02c89902016-11-15 19:36:38 +000024import android.content.pm.UserInfo;
25import android.os.Looper;
Kenny Guy02c89902016-11-15 19:36:38 +000026import android.os.UserHandle;
Benjamin Franzc8776152017-01-06 14:16:41 +000027import android.os.UserManagerInternal;
Kenny Guy02c89902016-11-15 19:36:38 +000028
Brett Chabota26eda92018-07-23 13:08:30 -070029import androidx.test.InstrumentationRegistry;
30import androidx.test.filters.MediumTest;
31import androidx.test.runner.AndroidJUnit4;
Kenny Guy02c89902016-11-15 19:36:38 +000032
Brett Chabota26eda92018-07-23 13:08:30 -070033import com.android.server.LocalServices;
Benjamin Franzc8776152017-01-06 14:16:41 +000034
35import org.junit.After;
Kenny Guy02c89902016-11-15 19:36:38 +000036import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39
Brett Chabota26eda92018-07-23 13:08:30 -070040import java.util.List;
41
Kenny Guy02c89902016-11-15 19:36:38 +000042/**
43 * <p>Run with:<pre>
44 * runtest -c com.android.server.pm.UserManagerServiceCreateProfileTest frameworks-services
45 * </pre>
46 */
47@RunWith(AndroidJUnit4.class)
48@MediumTest
49public 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 Franzc8776152017-01-06 14:16:41 +000069 @After
70 public void tearDown() {
71 removeUsers();
72 }
73
Bookatz029832a2019-10-04 16:50:22 -070074 /** Tests UMS.getProfileIds() when no specific userType is specified. */
Kenny Guy02c89902016-11-15 19:36:38 +000075 @Test
76 public void testGetProfiles() {
Benjamin Franzc8776152017-01-06 14:16:41 +000077 // Pretend we have a secondary user with a profile.
78 UserInfo secondaryUser = addUser();
79 UserInfo profile = addProfile(secondaryUser);
Kenny Guy02c89902016-11-15 19:36:38 +000080
Benjamin Franzc8776152017-01-06 14:16:41 +000081 // 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 Guy02c89902016-11-15 19:36:38 +000087
Benjamin Franzc8776152017-01-06 14:16:41 +000088 // 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 Guy02c89902016-11-15 19:36:38 +000095 }
96
Bookatz029832a2019-10-04 16:50:22 -070097 /** 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 Guy02c89902016-11-15 19:36:38 +0000122 @Test
123 public void testProfileBadge() {
Benjamin Franzc8776152017-01-06 14:16:41 +0000124 // First profile for system user should get badge 0
125 assertEquals("First profile isn't given badge index 0", 0,
Bookatz029832a2019-10-04 16:50:22 -0700126 mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM,
127 USER_TYPE_PROFILE_MANAGED));
Kenny Guy02c89902016-11-15 19:36:38 +0000128
Benjamin Franzc8776152017-01-06 14:16:41 +0000129 // Pretend we have a secondary user.
130 UserInfo secondaryUser = addUser();
Kenny Guy02c89902016-11-15 19:36:38 +0000131
Benjamin Franzc8776152017-01-06 14:16:41 +0000132 // Check first profile badge for secondary user is also 0.
133 assertEquals("First profile for secondary user isn't given badge index 0", 0,
Bookatz029832a2019-10-04 16:50:22 -0700134 mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id,
135 USER_TYPE_PROFILE_MANAGED));
Kenny Guy02c89902016-11-15 19:36:38 +0000136
Benjamin Franzc8776152017-01-06 14:16:41 +0000137 // Shouldn't impact the badge for profile in system user
138 assertEquals("First profile isn't given badge index 0 with secondary user", 0,
Bookatz029832a2019-10-04 16:50:22 -0700139 mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM,
140 USER_TYPE_PROFILE_MANAGED));
Kenny Guy02c89902016-11-15 19:36:38 +0000141
Benjamin Franzc8776152017-01-06 14:16:41 +0000142 // Pretend a secondary user has a profile.
143 addProfile(secondaryUser);
Kenny Guy02c89902016-11-15 19:36:38 +0000144
Benjamin Franzc8776152017-01-06 14:16:41 +0000145 // Shouldn't have impacted the badge for the system user
146 assertEquals("First profile isn't given badge index 0 in secondary user", 0,
Bookatz029832a2019-10-04 16:50:22 -0700147 mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM,
148 USER_TYPE_PROFILE_MANAGED));
Kenny Guy02c89902016-11-15 19:36:38 +0000149 }
150
151 @Test
152 public void testProfileBadgeUnique() {
Benjamin Franzc8776152017-01-06 14:16:41 +0000153 List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
154 UserInfo system = users.get(0);
Bookatz029832a2019-10-04 16:50:22 -0700155 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 Franzc8776152017-01-06 14:16:41 +0000160 // Badges should get allocated 0 -> max
Bookatz029832a2019-10-04 16:50:22 -0700161 for (int i = 0; i < max; ++i) {
162 int nextBadge = mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM,
163 USER_TYPE_PROFILE_MANAGED);
Benjamin Franzc8776152017-01-06 14:16:41 +0000164 assertEquals("Wrong badge allocated", i, nextBadge);
165 UserInfo profile = addProfile(system);
166 profile.profileBadge = nextBadge;
Kenny Guy02c89902016-11-15 19:36:38 +0000167 }
168 }
169
170 @Test
171 public void testProfileBadgeReuse() {
Benjamin Franzc8776152017-01-06 14:16:41 +0000172 // 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,
Bookatz029832a2019-10-04 16:50:22 -0700179 mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id,
180 USER_TYPE_PROFILE_MANAGED));
Kenny Guy02c89902016-11-15 19:36:38 +0000181
Benjamin Franzc8776152017-01-06 14:16:41 +0000182 // Edge case of reuse that only applies if we ever support 3 managed profiles
183 // We should prioritise using lower badge indexes
Bookatz029832a2019-10-04 16:50:22 -0700184 int max = mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED);
185 if (max < 0 || max > 2) {
Benjamin Franzc8776152017-01-06 14:16:41 +0000186 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,
Bookatz029832a2019-10-04 16:50:22 -0700190 mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id,
191 USER_TYPE_PROFILE_MANAGED));
Kenny Guy02c89902016-11-15 19:36:38 +0000192 }
193 }
194
195 @Test
Benjamin Franzc8776152017-01-06 14:16:41 +0000196 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 }
Bookatz029832a2019-10-04 16:50:22 -0700203 if (mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED) < 0) {
204 // Indicates no limit, so we cannot run this test;
205 return;
206 }
Benjamin Franzc8776152017-01-06 14:16:41 +0000207
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 }
Bookatz029832a2019-10-04 16:50:22 -0700228 if (mUserManagerService.getMaxUsersOfTypePerParent(USER_TYPE_PROFILE_MANAGED) < 0) {
229 // Indicates no limit, so we cannot run this test;
230 return;
231 }
Benjamin Franzc8776152017-01-06 14:16:41 +0000232
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 Guy02c89902016-11-15 19:36:38 +0000246 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 Franzc8776152017-01-06 14:16:41 +0000256 return addProfile(user, false);
257 }
258
259 private UserInfo addProfile(UserInfo user, boolean disabled) {
Kenny Guy02c89902016-11-15 19:36:38 +0000260 user.profileGroupId = user.id;
Benjamin Franzc8776152017-01-06 14:16:41 +0000261 int flags = UserInfo.FLAG_MANAGED_PROFILE;
262 if (disabled) {
263 flags |= UserInfo.FLAG_DISABLED;
264 }
Kenny Guy02c89902016-11-15 19:36:38 +0000265 UserInfo profile = new UserInfo(
Benjamin Franzc8776152017-01-06 14:16:41 +0000266 mUserManagerService.getNextAvailableId(), "profile", flags);
Kenny Guy02c89902016-11-15 19:36:38 +0000267 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}