1 /* 2 * Copyright (C) 2017 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 android.os; 18 19 import static android.os.UserHandle.ERR_GID; 20 import static android.os.UserHandle.getAppId; 21 import static android.os.UserHandle.getCacheAppGid; 22 import static android.os.UserHandle.getSharedAppGid; 23 import static android.os.UserHandle.getUid; 24 import static android.os.UserHandle.getUserId; 25 26 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertSame; 28 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.util.Random; 35 36 @RunWith(AndroidJUnit4.class) 37 public class UserHandleTest { 38 // NOTE: keep logic in sync with system/core/libcutils/tests/multiuser_test.cpp 39 40 @Test testMerge()41 public void testMerge() throws Exception { 42 EXPECT_EQ(0, multiuser_get_uid(0, 0)); 43 EXPECT_EQ(1000, multiuser_get_uid(0, 1000)); 44 EXPECT_EQ(10000, multiuser_get_uid(0, 10000)); 45 EXPECT_EQ(50000, multiuser_get_uid(0, 50000)); 46 EXPECT_EQ(1000000, multiuser_get_uid(10, 0)); 47 EXPECT_EQ(1001000, multiuser_get_uid(10, 1000)); 48 EXPECT_EQ(1010000, multiuser_get_uid(10, 10000)); 49 EXPECT_EQ(1050000, multiuser_get_uid(10, 50000)); 50 } 51 52 @Test testSplitUser()53 public void testSplitUser() throws Exception { 54 EXPECT_EQ(0, multiuser_get_user_id(0)); 55 EXPECT_EQ(0, multiuser_get_user_id(1000)); 56 EXPECT_EQ(0, multiuser_get_user_id(10000)); 57 EXPECT_EQ(0, multiuser_get_user_id(50000)); 58 EXPECT_EQ(10, multiuser_get_user_id(1000000)); 59 EXPECT_EQ(10, multiuser_get_user_id(1001000)); 60 EXPECT_EQ(10, multiuser_get_user_id(1010000)); 61 EXPECT_EQ(10, multiuser_get_user_id(1050000)); 62 } 63 64 @Test testSplitApp()65 public void testSplitApp() throws Exception { 66 EXPECT_EQ(0, multiuser_get_app_id(0)); 67 EXPECT_EQ(1000, multiuser_get_app_id(1000)); 68 EXPECT_EQ(10000, multiuser_get_app_id(10000)); 69 EXPECT_EQ(50000, multiuser_get_app_id(50000)); 70 EXPECT_EQ(0, multiuser_get_app_id(1000000)); 71 EXPECT_EQ(1000, multiuser_get_app_id(1001000)); 72 EXPECT_EQ(10000, multiuser_get_app_id(1010000)); 73 EXPECT_EQ(50000, multiuser_get_app_id(1050000)); 74 } 75 76 @Test testCache()77 public void testCache() throws Exception { 78 EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 0)); 79 EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 1000)); 80 EXPECT_EQ(20000, multiuser_get_cache_gid(0, 10000)); 81 EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 50000)); 82 EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 0)); 83 EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 1000)); 84 EXPECT_EQ(1020000, multiuser_get_cache_gid(10, 10000)); 85 EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 50000)); 86 } 87 88 @Test testShared()89 public void testShared() throws Exception { 90 EXPECT_EQ(0, multiuser_get_shared_gid(0, 0)); 91 EXPECT_EQ(1000, multiuser_get_shared_gid(0, 1000)); 92 EXPECT_EQ(50000, multiuser_get_shared_gid(0, 10000)); 93 EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(0, 50000)); 94 EXPECT_EQ(0, multiuser_get_shared_gid(10, 0)); 95 EXPECT_EQ(1000, multiuser_get_shared_gid(10, 1000)); 96 EXPECT_EQ(50000, multiuser_get_shared_gid(10, 10000)); 97 EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(10, 50000)); 98 } 99 EXPECT_EQ(int expected, int actual)100 private static void EXPECT_EQ(int expected, int actual) { 101 assertEquals(expected, actual); 102 } 103 multiuser_get_uid(int userId, int appId)104 private static int multiuser_get_uid(int userId, int appId) { 105 return getUid(userId, appId); 106 } 107 multiuser_get_cache_gid(int userId, int appId)108 private static int multiuser_get_cache_gid(int userId, int appId) { 109 return getCacheAppGid(userId, appId); 110 } 111 multiuser_get_shared_gid(int userId, int appId)112 private static int multiuser_get_shared_gid(int userId, int appId) { 113 return getSharedAppGid(userId, appId); 114 } 115 multiuser_get_user_id(int uid)116 private static int multiuser_get_user_id(int uid) { 117 return getUserId(uid); 118 } 119 multiuser_get_app_id(int uid)120 private static int multiuser_get_app_id(int uid) { 121 return getAppId(uid); 122 } 123 124 @Test testExtraCache()125 public void testExtraCache() { 126 assertEquals(0, UserHandle.sExtraUserHandleCache.size()); 127 128 // This shouldn't hit the extra cache. 129 assertEquals(10, UserHandle.of(10).getIdentifier()); 130 131 assertEquals(0, UserHandle.sExtraUserHandleCache.size()); 132 133 // This should hit the cache 134 assertEquals(20, UserHandle.of(20).getIdentifier()); 135 136 assertEquals(1, UserHandle.sExtraUserHandleCache.size()); 137 138 // Make sure the cache works. 139 final Random rnd = new Random(); 140 for (int i = 0; i < 10000; i++) { 141 final int userId = rnd.nextInt(100); 142 assertEquals(userId, UserHandle.of(userId).getIdentifier()); 143 assertSame(UserHandle.of(userId), UserHandle.of(userId)); 144 } 145 146 // Make sure the cache size doesn't exceed the max size. 147 assertEquals(UserHandle.MAX_EXTRA_USER_HANDLE_CACHE_SIZE, 148 UserHandle.sExtraUserHandleCache.size()); 149 } 150 } 151