1 /* 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 17 package com.android.server.pm; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.UserInfo; 24 import android.os.Bundle; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.test.AndroidTestCase; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** Test {@link UserManager} functionality. */ 33 public class UserManagerTest extends AndroidTestCase { 34 35 UserManager mUserManager = null; 36 Object mUserLock = new Object(); 37 38 @Override 39 public void setUp() throws Exception { 40 mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE); 41 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED); 42 getContext().registerReceiver(new BroadcastReceiver() { 43 @Override 44 public void onReceive(Context context, Intent intent) { 45 synchronized (mUserLock) { 46 mUserLock.notifyAll(); 47 } 48 } 49 }, filter); 50 51 removeExistingUsers(); 52 } 53 54 private void removeExistingUsers() { 55 List<UserInfo> list = mUserManager.getUsers(); 56 boolean found = false; 57 for (UserInfo user : list) { 58 if (user.id != UserHandle.USER_OWNER) { 59 removeUser(user.id); 60 } 61 } 62 } 63 64 public void testHasPrimary() throws Exception { 65 assertTrue(findUser(0)); 66 } 67 68 public void testAddUser() throws Exception { 69 UserInfo userInfo = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST); 70 assertTrue(userInfo != null); 71 72 List<UserInfo> list = mUserManager.getUsers(); 73 boolean found = false; 74 for (UserInfo user : list) { 75 if (user.id == userInfo.id && user.name.equals("Guest 1") 76 && user.isGuest() 77 && !user.isAdmin() 78 && !user.isPrimary()) { 79 found = true; 80 Bundle restrictions = mUserManager.getUserRestrictions(user.getUserHandle()); 81 assertFalse("New user should have DISALLOW_CONFIG_WIFI =false by default", 82 restrictions.getBoolean(UserManager.DISALLOW_CONFIG_WIFI)); 83 } 84 } 85 assertTrue(found); 86 removeUser(userInfo.id); 87 } 88 89 public void testAdd2Users() throws Exception { 90 UserInfo user1 = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST); 91 UserInfo user2 = mUserManager.createUser("User 2", UserInfo.FLAG_ADMIN); 92 93 assertTrue(user1 != null); 94 assertTrue(user2 != null); 95 96 assertTrue(findUser(0)); 97 assertTrue(findUser(user1.id)); 98 assertTrue(findUser(user2.id)); 99 removeUser(user1.id); 100 removeUser(user2.id); 101 } 102 103 public void testRemoveUser() throws Exception { 104 UserInfo userInfo = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST); 105 removeUser(userInfo.id); 106 107 assertFalse(findUser(userInfo.id)); 108 } 109 110 public void testAddGuest() throws Exception { 111 UserInfo userInfo1 = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST); 112 UserInfo userInfo2 = mUserManager.createUser("Guest 2", UserInfo.FLAG_GUEST); 113 assertNotNull(userInfo1); 114 assertNull(userInfo2); 115 116 // Cleanup 117 removeUser(userInfo1.id); 118 } 119 120 // Make sure only one managed profile can be created 121 public void testAddManagedProfile() throws Exception { 122 UserInfo userInfo1 = mUserManager.createProfileForUser("Managed 1", 123 UserInfo.FLAG_MANAGED_PROFILE, UserHandle.USER_OWNER); 124 UserInfo userInfo2 = mUserManager.createProfileForUser("Managed 2", 125 UserInfo.FLAG_MANAGED_PROFILE, UserHandle.USER_OWNER); 126 assertNotNull(userInfo1); 127 assertNull(userInfo2); 128 // Verify that current user is not a managed profile 129 assertFalse(mUserManager.isManagedProfile()); 130 // Cleanup 131 removeUser(userInfo1.id); 132 } 133 134 private boolean findUser(int id) { 135 List<UserInfo> list = mUserManager.getUsers(); 136 137 for (UserInfo user : list) { 138 if (user.id == id) { 139 return true; 140 } 141 } 142 return false; 143 } 144 145 public void testSerialNumber() { 146 UserInfo user1 = mUserManager.createUser("User 1", UserInfo.FLAG_RESTRICTED); 147 int serialNumber1 = user1.serialNumber; 148 assertEquals(serialNumber1, mUserManager.getUserSerialNumber(user1.id)); 149 assertEquals(user1.id, mUserManager.getUserHandle(serialNumber1)); 150 removeUser(user1.id); 151 UserInfo user2 = mUserManager.createUser("User 2", UserInfo.FLAG_RESTRICTED); 152 int serialNumber2 = user2.serialNumber; 153 assertFalse(serialNumber1 == serialNumber2); 154 assertEquals(serialNumber2, mUserManager.getUserSerialNumber(user2.id)); 155 assertEquals(user2.id, mUserManager.getUserHandle(serialNumber2)); 156 removeUser(user2.id); 157 } 158 159 public void testMaxUsers() { 160 int N = UserManager.getMaxSupportedUsers(); 161 int count = mUserManager.getUsers().size(); 162 List<UserInfo> created = new ArrayList<UserInfo>(); 163 // Create as many users as permitted and make sure creation passes 164 while (count < N) { 165 UserInfo ui = mUserManager.createUser("User " + count, 0); 166 assertNotNull(ui); 167 created.add(ui); 168 count++; 169 } 170 // Try to create one more user and make sure it fails 171 UserInfo extra = null; 172 assertNull(extra = mUserManager.createUser("One more", 0)); 173 if (extra != null) { 174 removeUser(extra.id); 175 } 176 while (!created.isEmpty()) { 177 UserInfo user = created.remove(0); 178 removeUser(user.id); 179 } 180 } 181 182 public void testRestrictions() { 183 List<UserInfo> users = mUserManager.getUsers(); 184 if (users.size() > 1) { 185 Bundle restrictions = new Bundle(); 186 restrictions.putBoolean(UserManager.DISALLOW_INSTALL_APPS, true); 187 restrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, false); 188 mUserManager.setUserRestrictions(restrictions, new UserHandle(users.get(1).id)); 189 Bundle stored = mUserManager.getUserRestrictions(new UserHandle(users.get(1).id)); 190 assertEquals(stored.getBoolean(UserManager.DISALLOW_CONFIG_WIFI), false); 191 assertEquals(stored.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS), false); 192 assertEquals(stored.getBoolean(UserManager.DISALLOW_INSTALL_APPS), true); 193 } 194 } 195 196 private void removeUser(int userId) { 197 synchronized (mUserLock) { 198 mUserManager.removeUser(userId); 199 while (mUserManager.getUserInfo(userId) != null) { 200 try { 201 mUserLock.wait(1000); 202 } catch (InterruptedException ie) { 203 } 204 } 205 } 206 } 207 208 } 209