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 #include <cutils/multiuser.h>
18 #include <gtest/gtest.h>
19 
20 static constexpr auto ERR_GID = static_cast<gid_t>(-1);
21 
TEST(MultiuserTest,TestMerge)22 TEST(MultiuserTest, TestMerge) {
23     EXPECT_EQ(0U, multiuser_get_uid(0, 0));
24     EXPECT_EQ(1000U, multiuser_get_uid(0, 1000));
25     EXPECT_EQ(10000U, multiuser_get_uid(0, 10000));
26     EXPECT_EQ(50000U, multiuser_get_uid(0, 50000));
27     EXPECT_EQ(1000000U, multiuser_get_uid(10, 0));
28     EXPECT_EQ(1001000U, multiuser_get_uid(10, 1000));
29     EXPECT_EQ(1010000U, multiuser_get_uid(10, 10000));
30     EXPECT_EQ(1050000U, multiuser_get_uid(10, 50000));
31 }
32 
TEST(MultiuserTest,TestSplitUser)33 TEST(MultiuserTest, TestSplitUser) {
34     EXPECT_EQ(0U, multiuser_get_user_id(0));
35     EXPECT_EQ(0U, multiuser_get_user_id(1000));
36     EXPECT_EQ(0U, multiuser_get_user_id(10000));
37     EXPECT_EQ(0U, multiuser_get_user_id(50000));
38     EXPECT_EQ(10U, multiuser_get_user_id(1000000));
39     EXPECT_EQ(10U, multiuser_get_user_id(1001000));
40     EXPECT_EQ(10U, multiuser_get_user_id(1010000));
41     EXPECT_EQ(10U, multiuser_get_user_id(1050000));
42 }
43 
TEST(MultiuserTest,TestSplitApp)44 TEST(MultiuserTest, TestSplitApp) {
45     EXPECT_EQ(0U, multiuser_get_app_id(0));
46     EXPECT_EQ(1000U, multiuser_get_app_id(1000));
47     EXPECT_EQ(10000U, multiuser_get_app_id(10000));
48     EXPECT_EQ(50000U, multiuser_get_app_id(50000));
49     EXPECT_EQ(0U, multiuser_get_app_id(1000000));
50     EXPECT_EQ(1000U, multiuser_get_app_id(1001000));
51     EXPECT_EQ(10000U, multiuser_get_app_id(1010000));
52     EXPECT_EQ(50000U, multiuser_get_app_id(1050000));
53 }
54 
TEST(MultiuserTest,TestCache)55 TEST(MultiuserTest, TestCache) {
56     EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 0));
57     EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 1000));
58     EXPECT_EQ(20000U, multiuser_get_cache_gid(0, 10000));
59     EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 50000));
60     EXPECT_EQ(1020000U, multiuser_get_cache_gid(10, 10000));
61 }
62 
TEST(MultiuserTest,TestExt)63 TEST(MultiuserTest, TestExt) {
64     EXPECT_EQ(ERR_GID, multiuser_get_ext_gid(0, 0));
65     EXPECT_EQ(ERR_GID, multiuser_get_ext_gid(0, 1000));
66     EXPECT_EQ(30000U, multiuser_get_ext_gid(0, 10000));
67     EXPECT_EQ(ERR_GID, multiuser_get_ext_gid(0, 50000));
68     EXPECT_EQ(1030000U, multiuser_get_ext_gid(10, 10000));
69 }
70 
TEST(MultiuserTest,TestExtCache)71 TEST(MultiuserTest, TestExtCache) {
72     EXPECT_EQ(ERR_GID, multiuser_get_ext_cache_gid(0, 0));
73     EXPECT_EQ(ERR_GID, multiuser_get_ext_cache_gid(0, 1000));
74     EXPECT_EQ(40000U, multiuser_get_ext_cache_gid(0, 10000));
75     EXPECT_EQ(ERR_GID, multiuser_get_ext_cache_gid(0, 50000));
76     EXPECT_EQ(1040000U, multiuser_get_ext_cache_gid(10, 10000));
77 }
78 
TEST(MultiuserTest,TestShared)79 TEST(MultiuserTest, TestShared) {
80     EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(0, 0));
81     EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(0, 1000));
82     EXPECT_EQ(50000U, multiuser_get_shared_gid(0, 10000));
83     EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(0, 50000));
84     EXPECT_EQ(1050000U, multiuser_get_shared_gid(10, 10000));
85 }
86