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 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assert.fail; 22 23 import android.content.pm.UserInfo; 24 import android.os.Looper; 25 import android.os.UserManagerInternal; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.filters.MediumTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import com.android.server.LocalServices; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.LinkedHashSet; 38 39 /** 40 * <p>Run with:<pre> 41 * m FrameworksServicesTests && 42 * adb install \ 43 * -r out/target/product/hammerhead/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && 44 * adb shell am instrument -e class com.android.server.pm.UserManagerServiceIdRecyclingTest \ 45 * -w com.android.frameworks.servicestests/androidx.test.runner.AndroidJUnitRunner 46 * </pre> 47 */ 48 @RunWith(AndroidJUnit4.class) 49 @MediumTest 50 public class UserManagerServiceIdRecyclingTest { 51 private UserManagerService mUserManagerService; 52 53 @Before setup()54 public void setup() { 55 // Currently UserManagerService cannot be instantiated twice inside a VM without a cleanup 56 // TODO: Remove once UMS supports proper dependency injection 57 if (Looper.myLooper() == null) { 58 Looper.prepare(); 59 } 60 LocalServices.removeServiceForTest(UserManagerInternal.class); 61 mUserManagerService = new UserManagerService(InstrumentationRegistry.getContext()); 62 } 63 64 @Test testUserCreateRecycleIdsAddAllThenRemove()65 public void testUserCreateRecycleIdsAddAllThenRemove() { 66 // Add max possible users 67 for (int i = UserManagerService.MIN_USER_ID; i < UserManagerService.MAX_USER_ID; i++) { 68 int userId = mUserManagerService.getNextAvailableId(); 69 assertEquals(i, userId); 70 mUserManagerService.putUserInfo(newUserInfo(userId)); 71 } 72 73 assertNoNextIdAvailable("All ids should be assigned"); 74 // Now remove RECENTLY_REMOVED_IDS_MAX_SIZE users in the middle 75 int startFrom = UserManagerService.MIN_USER_ID + 10000 /* arbitrary number */; 76 int lastId = startFrom + UserManagerService.MAX_RECENTLY_REMOVED_IDS_SIZE; 77 for (int i = startFrom; i < lastId; i++) { 78 removeUser(i); 79 assertNoNextIdAvailable("There is not enough recently removed users. " 80 + "Next id should not be available. Failed at u" + i); 81 } 82 83 // Now remove first user 84 removeUser(UserManagerService.MIN_USER_ID); 85 86 // Released UserIDs should be returned in the FIFO order 87 int nextId = mUserManagerService.getNextAvailableId(); 88 assertEquals(startFrom, nextId); 89 } 90 91 @Test testUserCreateRecycleIdsOverflow()92 public void testUserCreateRecycleIdsOverflow() { 93 LinkedHashSet<Integer> queue = new LinkedHashSet<>(); 94 // Make sure we can generate more than 2x ids without issues 95 for (int i = 0; i < UserManagerService.MAX_USER_ID * 2; i++) { 96 int userId = mUserManagerService.getNextAvailableId(); 97 assertTrue("Returned id should not be recent. Id=" + userId + ". Recents=" + queue, 98 queue.add(userId)); 99 if (queue.size() > UserManagerService.MAX_RECENTLY_REMOVED_IDS_SIZE) { 100 queue.remove(queue.iterator().next()); 101 } 102 mUserManagerService.putUserInfo(newUserInfo(userId)); 103 removeUser(userId); 104 } 105 } 106 removeUser(int userId)107 private void removeUser(int userId) { 108 mUserManagerService.removeUserInfo(userId); 109 mUserManagerService.addRemovingUserIdLocked(userId); 110 } 111 assertNoNextIdAvailable(String message)112 private void assertNoNextIdAvailable(String message) { 113 try { 114 mUserManagerService.getNextAvailableId(); 115 fail(message); 116 } catch (IllegalStateException e) { 117 //OK 118 } 119 } 120 newUserInfo(int userId)121 private static UserInfo newUserInfo(int userId) { 122 return new UserInfo(userId, "User " + userId, 0); 123 } 124 } 125 126