blob: b4bca3eb761cf1280c02bd7c9456bedbd5c6abf7 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 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
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070019import android.content.BroadcastReceiver;
Amith Yamasani2a003292012-08-14 18:25:45 -070020import android.content.Context;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070021import android.content.Intent;
22import android.content.IntentFilter;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070023import android.content.pm.UserInfo;
Amith Yamasanie4cf7342012-12-17 11:12:09 -080024import android.os.Bundle;
Amith Yamasani20949a72013-02-11 15:47:30 -080025import android.os.UserHandle;
Amith Yamasani2a003292012-08-14 18:25:45 -070026import android.os.UserManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070027import android.test.AndroidTestCase;
28
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070029import java.util.ArrayList;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070030import java.util.List;
31
Amith Yamasani2a003292012-08-14 18:25:45 -070032/** Test {@link UserManager} functionality. */
Amith Yamasani0b285492011-04-14 17:35:23 -070033public class UserManagerTest extends AndroidTestCase {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070034 private static final int REMOVE_CHECK_INTERVAL = 500;
35 private static final int REMOVE_TIMEOUT = 60 * 1000;
Fyodor Kupolovff7233e2015-04-08 11:28:52 -070036 private UserManager mUserManager = null;
37 private final Object mUserLock = new Object();
38 private List<Integer> usersToRemove;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070039
40 @Override
41 public void setUp() throws Exception {
Xiaohui Chen72d9c022015-08-21 09:23:23 -070042 super.setUp();
43 mUserManager = UserManager.get(getContext());
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070044 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
45 getContext().registerReceiver(new BroadcastReceiver() {
46 @Override
47 public void onReceive(Context context, Intent intent) {
48 synchronized (mUserLock) {
49 mUserLock.notifyAll();
50 }
51 }
52 }, filter);
Amith Yamasani95ab7842014-08-11 17:09:26 -070053
54 removeExistingUsers();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -070055 usersToRemove = new ArrayList<>();
56 }
57
58 @Override
59 protected void tearDown() throws Exception {
60 for (Integer userId : usersToRemove) {
61 removeUser(userId);
62 }
63 super.tearDown();
Amith Yamasani95ab7842014-08-11 17:09:26 -070064 }
65
66 private void removeExistingUsers() {
67 List<UserInfo> list = mUserManager.getUsers();
Amith Yamasani95ab7842014-08-11 17:09:26 -070068 for (UserInfo user : list) {
Xiaohui Chen72d9c022015-08-21 09:23:23 -070069 // Keep system and primary user.
70 // We do not have to keep primary user, but in split system user mode, we need it
71 // until http://b/22976637 is fixed. Right now in split system user mode, you need to
72 // switch to primary user and run tests under primary user.
73 if (user.id != UserHandle.USER_SYSTEM && !user.isPrimary()) {
Amith Yamasani95ab7842014-08-11 17:09:26 -070074 removeUser(user.id);
75 }
76 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070077 }
78
Xiaohui Chen72d9c022015-08-21 09:23:23 -070079 public void testHasSystemUser() throws Exception {
80 assertTrue(findUser(UserHandle.USER_SYSTEM));
Amith Yamasani4b2e9342011-03-31 12:38:53 -070081 }
82
83 public void testAddUser() throws Exception {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -070084 UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST);
Amith Yamasani4b2e9342011-03-31 12:38:53 -070085 assertTrue(userInfo != null);
86
Amith Yamasani2a003292012-08-14 18:25:45 -070087 List<UserInfo> list = mUserManager.getUsers();
Amith Yamasani4b2e9342011-03-31 12:38:53 -070088 boolean found = false;
89 for (UserInfo user : list) {
90 if (user.id == userInfo.id && user.name.equals("Guest 1")
91 && user.isGuest()
92 && !user.isAdmin()
93 && !user.isPrimary()) {
94 found = true;
Maggie Benthall67944582013-02-22 14:58:27 -050095 Bundle restrictions = mUserManager.getUserRestrictions(user.getUserHandle());
Amith Yamasani05191052013-03-26 15:32:29 -070096 assertFalse("New user should have DISALLOW_CONFIG_WIFI =false by default",
97 restrictions.getBoolean(UserManager.DISALLOW_CONFIG_WIFI));
Amith Yamasani4b2e9342011-03-31 12:38:53 -070098 }
99 }
100 assertTrue(found);
101 }
102
103 public void testAdd2Users() throws Exception {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700104 UserInfo user1 = createUser("Guest 1", UserInfo.FLAG_GUEST);
105 UserInfo user2 = createUser("User 2", UserInfo.FLAG_ADMIN);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700106
107 assertTrue(user1 != null);
108 assertTrue(user2 != null);
109
110 assertTrue(findUser(0));
111 assertTrue(findUser(user1.id));
112 assertTrue(findUser(user2.id));
113 }
114
115 public void testRemoveUser() throws Exception {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700116 UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700117 removeUser(userInfo.id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700118
119 assertFalse(findUser(userInfo.id));
120 }
121
Amith Yamasani95ab7842014-08-11 17:09:26 -0700122 public void testAddGuest() throws Exception {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700123 UserInfo userInfo1 = createUser("Guest 1", UserInfo.FLAG_GUEST);
124 UserInfo userInfo2 = createUser("Guest 2", UserInfo.FLAG_GUEST);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700125 assertNotNull(userInfo1);
126 assertNull(userInfo2);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700127 }
128
129 // Make sure only one managed profile can be created
130 public void testAddManagedProfile() throws Exception {
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700131 final int primaryUserId = mUserManager.getPrimaryUser().id;
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700132 UserInfo userInfo1 = createProfileForUser("Managed 1",
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700133 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700134 UserInfo userInfo2 = createProfileForUser("Managed 2",
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700135 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700136 assertNotNull(userInfo1);
137 assertNull(userInfo2);
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700138 // Verify that current user is not a managed profile
139 assertFalse(mUserManager.isManagedProfile());
Amith Yamasani95ab7842014-08-11 17:09:26 -0700140 }
141
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700142 public void testGetUserCreationTime() throws Exception {
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700143 final int primaryUserId = mUserManager.getPrimaryUser().id;
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700144 UserInfo profile = createProfileForUser("Managed 1",
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700145 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700146 assertNotNull(profile);
147 assertTrue("creationTime must be set when the profile is created",
148 profile.creationTime > 0);
Fyodor Kupolov385de622015-04-10 18:00:19 -0700149 assertEquals(profile.creationTime, mUserManager.getUserCreationTime(
150 new UserHandle(profile.id)));
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700151
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700152 long ownerCreationTime = mUserManager.getUserInfo(primaryUserId).creationTime;
Fyodor Kupolov385de622015-04-10 18:00:19 -0700153 assertEquals(ownerCreationTime, mUserManager.getUserCreationTime(
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700154 new UserHandle(primaryUserId)));
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700155
156 try {
157 int noSuchUserId = 100500;
Fyodor Kupolov385de622015-04-10 18:00:19 -0700158 mUserManager.getUserCreationTime(new UserHandle(noSuchUserId));
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700159 fail("SecurityException should be thrown for nonexistent user");
160 } catch (Exception e) {
161 assertTrue("SecurityException should be thrown for nonexistent user, but was: " + e,
162 e instanceof SecurityException);
163 }
164
165 UserInfo user = createUser("User 1", 0);
166 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -0700167 mUserManager.getUserCreationTime(new UserHandle(user.id));
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700168 fail("SecurityException should be thrown for other user");
169 } catch (Exception e) {
170 assertTrue("SecurityException should be thrown for other user, but was: " + e,
171 e instanceof SecurityException);
172 }
173 }
174
175
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700176 private boolean findUser(int id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700177 List<UserInfo> list = mUserManager.getUsers();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700178
179 for (UserInfo user : list) {
180 if (user.id == id) {
181 return true;
182 }
183 }
184 return false;
185 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700186
187 public void testSerialNumber() {
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700188 UserInfo user1 = createUser("User 1", 0);
Amith Yamasani2a003292012-08-14 18:25:45 -0700189 int serialNumber1 = user1.serialNumber;
190 assertEquals(serialNumber1, mUserManager.getUserSerialNumber(user1.id));
191 assertEquals(user1.id, mUserManager.getUserHandle(serialNumber1));
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700192 UserInfo user2 = createUser("User 2", 0);
Amith Yamasani2a003292012-08-14 18:25:45 -0700193 int serialNumber2 = user2.serialNumber;
194 assertFalse(serialNumber1 == serialNumber2);
195 assertEquals(serialNumber2, mUserManager.getUserSerialNumber(user2.id));
196 assertEquals(user2.id, mUserManager.getUserHandle(serialNumber2));
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700197 }
198
199 public void testMaxUsers() {
200 int N = UserManager.getMaxSupportedUsers();
201 int count = mUserManager.getUsers().size();
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700202 // Create as many users as permitted and make sure creation passes
203 while (count < N) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700204 UserInfo ui = createUser("User " + count, 0);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700205 assertNotNull(ui);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700206 count++;
207 }
208 // Try to create one more user and make sure it fails
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700209 UserInfo extra = createUser("One more", 0);
210 assertNull(extra);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700211 }
212
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800213 public void testRestrictions() {
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700214 UserInfo testUser = createUser("User 1", 0);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700215
216 mUserManager.setUserRestriction(
217 UserManager.DISALLOW_INSTALL_APPS, true, new UserHandle(testUser.id));
218 mUserManager.setUserRestriction(
219 UserManager.DISALLOW_CONFIG_WIFI, false, new UserHandle(testUser.id));
220
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700221 Bundle stored = mUserManager.getUserRestrictions(new UserHandle(testUser.id));
Makoto Onuki068c54a2015-10-13 14:34:03 -0700222 // Note this will fail if DO already sets those restrictions.
Xiaohui Chen72d9c022015-08-21 09:23:23 -0700223 assertEquals(stored.getBoolean(UserManager.DISALLOW_CONFIG_WIFI), false);
224 assertEquals(stored.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS), false);
225 assertEquals(stored.getBoolean(UserManager.DISALLOW_INSTALL_APPS), true);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800226 }
227
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700228 private void removeUser(int userId) {
229 synchronized (mUserLock) {
230 mUserManager.removeUser(userId);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700231 long time = System.currentTimeMillis();
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700232 while (mUserManager.getUserInfo(userId) != null) {
233 try {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700234 mUserLock.wait(REMOVE_CHECK_INTERVAL);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700235 } catch (InterruptedException ie) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700236 Thread.currentThread().interrupt();
237 return;
238 }
239 if (System.currentTimeMillis() - time > REMOVE_TIMEOUT) {
240 fail("Timeout waiting for removeUser. userId = " + userId);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700241 }
242 }
243 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700244 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800245
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700246 private UserInfo createUser(String name, int flags) {
247 UserInfo user = mUserManager.createUser(name, flags);
248 if (user != null) {
249 usersToRemove.add(user.id);
250 }
251 return user;
252 }
253
254 private UserInfo createProfileForUser(String name, int flags, int userHandle) {
255 UserInfo profile = mUserManager.createProfileForUser(name, flags, userHandle);
256 if (profile != null) {
257 usersToRemove.add(profile.id);
258 }
259 return profile;
260 }
261
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700262}