• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(AndroidJUnit4.class)
34 public class UserHandleTest {
35     // NOTE: keep logic in sync with system/core/libcutils/tests/multiuser_test.cpp
36 
37     @Test
testMerge()38     public void testMerge() throws Exception {
39         EXPECT_EQ(0, multiuser_get_uid(0, 0));
40         EXPECT_EQ(1000, multiuser_get_uid(0, 1000));
41         EXPECT_EQ(10000, multiuser_get_uid(0, 10000));
42         EXPECT_EQ(50000, multiuser_get_uid(0, 50000));
43         EXPECT_EQ(1000000, multiuser_get_uid(10, 0));
44         EXPECT_EQ(1001000, multiuser_get_uid(10, 1000));
45         EXPECT_EQ(1010000, multiuser_get_uid(10, 10000));
46         EXPECT_EQ(1050000, multiuser_get_uid(10, 50000));
47     }
48 
49     @Test
testSplitUser()50     public void testSplitUser() throws Exception {
51         EXPECT_EQ(0, multiuser_get_user_id(0));
52         EXPECT_EQ(0, multiuser_get_user_id(1000));
53         EXPECT_EQ(0, multiuser_get_user_id(10000));
54         EXPECT_EQ(0, multiuser_get_user_id(50000));
55         EXPECT_EQ(10, multiuser_get_user_id(1000000));
56         EXPECT_EQ(10, multiuser_get_user_id(1001000));
57         EXPECT_EQ(10, multiuser_get_user_id(1010000));
58         EXPECT_EQ(10, multiuser_get_user_id(1050000));
59     }
60 
61     @Test
testSplitApp()62     public void testSplitApp() throws Exception {
63         EXPECT_EQ(0, multiuser_get_app_id(0));
64         EXPECT_EQ(1000, multiuser_get_app_id(1000));
65         EXPECT_EQ(10000, multiuser_get_app_id(10000));
66         EXPECT_EQ(50000, multiuser_get_app_id(50000));
67         EXPECT_EQ(0, multiuser_get_app_id(1000000));
68         EXPECT_EQ(1000, multiuser_get_app_id(1001000));
69         EXPECT_EQ(10000, multiuser_get_app_id(1010000));
70         EXPECT_EQ(50000, multiuser_get_app_id(1050000));
71     }
72 
73     @Test
testCache()74     public void testCache() throws Exception {
75         EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 0));
76         EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 1000));
77         EXPECT_EQ(20000, multiuser_get_cache_gid(0, 10000));
78         EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 50000));
79         EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 0));
80         EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 1000));
81         EXPECT_EQ(1020000, multiuser_get_cache_gid(10, 10000));
82         EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 50000));
83     }
84 
85     @Test
testShared()86     public void testShared() throws Exception {
87         EXPECT_EQ(0, multiuser_get_shared_gid(0, 0));
88         EXPECT_EQ(1000, multiuser_get_shared_gid(0, 1000));
89         EXPECT_EQ(50000, multiuser_get_shared_gid(0, 10000));
90         EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(0, 50000));
91         EXPECT_EQ(0, multiuser_get_shared_gid(10, 0));
92         EXPECT_EQ(1000, multiuser_get_shared_gid(10, 1000));
93         EXPECT_EQ(50000, multiuser_get_shared_gid(10, 10000));
94         EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(10, 50000));
95     }
96 
EXPECT_EQ(int expected, int actual)97     private static void EXPECT_EQ(int expected, int actual) {
98         assertEquals(expected, actual);
99     }
100 
multiuser_get_uid(int userId, int appId)101     private static int multiuser_get_uid(int userId, int appId) {
102         return getUid(userId, appId);
103     }
104 
multiuser_get_cache_gid(int userId, int appId)105     private static int multiuser_get_cache_gid(int userId, int appId) {
106         return getCacheAppGid(userId, appId);
107     }
108 
multiuser_get_shared_gid(int userId, int appId)109     private static int multiuser_get_shared_gid(int userId, int appId) {
110         return getSharedAppGid(userId, appId);
111     }
112 
multiuser_get_user_id(int uid)113     private static int multiuser_get_user_id(int uid) {
114         return getUserId(uid);
115     }
116 
multiuser_get_app_id(int uid)117     private static int multiuser_get_app_id(int uid) {
118         return getAppId(uid);
119     }
120 }
121