1 /*
2  * Copyright (C) 2012 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 <gtest/gtest.h>
18 
19 // Below are the header files we want to test.
20 #include <grp.h>
21 #include <pwd.h>
22 
23 #include <errno.h>
24 #include <limits.h>
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 
29 enum uid_type_t {
30   TYPE_SYSTEM,
31   TYPE_APP
32 };
33 
34 #if defined(__BIONIC__)
35 
check_passwd(const passwd * pwd,const char * username,uid_t uid,uid_type_t uid_type)36 static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type) {
37   ASSERT_TRUE(pwd != NULL);
38   ASSERT_STREQ(username, pwd->pw_name);
39   ASSERT_EQ(uid, pwd->pw_uid);
40   ASSERT_EQ(uid, pwd->pw_gid);
41   ASSERT_EQ(NULL, pwd->pw_passwd);
42 #ifdef __LP64__
43   ASSERT_EQ(NULL, pwd->pw_gecos);
44 #endif
45 
46   if (uid_type == TYPE_SYSTEM) {
47     ASSERT_STREQ("/", pwd->pw_dir);
48   } else {
49     ASSERT_STREQ("/data", pwd->pw_dir);
50   }
51   ASSERT_STREQ("/system/bin/sh", pwd->pw_shell);
52 }
53 
check_getpwuid(const char * username,uid_t uid,uid_type_t uid_type)54 static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type) {
55   errno = 0;
56   passwd* pwd = getpwuid(uid);
57   ASSERT_EQ(0, errno);
58   SCOPED_TRACE("getpwuid");
59   check_passwd(pwd, username, uid, uid_type);
60 }
61 
check_getpwnam(const char * username,uid_t uid,uid_type_t uid_type)62 static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
63   errno = 0;
64   passwd* pwd = getpwnam(username);
65   ASSERT_EQ(0, errno);
66   SCOPED_TRACE("getpwnam");
67   check_passwd(pwd, username, uid, uid_type);
68 }
69 
check_getpwuid_r(const char * username,uid_t uid,uid_type_t uid_type)70 static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type) {
71   passwd pwd_storage;
72   char buf[512];
73   int result;
74 
75   errno = 0;
76   passwd* pwd = NULL;
77   result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
78   ASSERT_EQ(0, result);
79   ASSERT_EQ(0, errno);
80   SCOPED_TRACE("getpwuid_r");
81   check_passwd(pwd, username, uid, uid_type);
82 }
83 
check_getpwnam_r(const char * username,uid_t uid,uid_type_t uid_type)84 static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type) {
85   passwd pwd_storage;
86   char buf[512];
87   int result;
88 
89   errno = 0;
90   passwd* pwd = NULL;
91   result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
92   ASSERT_EQ(0, result);
93   ASSERT_EQ(0, errno);
94   SCOPED_TRACE("getpwnam_r");
95   check_passwd(pwd, username, uid, uid_type);
96 }
97 
check_get_passwd(const char * username,uid_t uid,uid_type_t uid_type)98 static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type) {
99   check_getpwuid(username, uid, uid_type);
100   check_getpwnam(username, uid, uid_type);
101   check_getpwuid_r(username, uid, uid_type);
102   check_getpwnam_r(username, uid, uid_type);
103 }
104 
105 #else // !defined(__BIONIC__)
106 
check_get_passwd(const char *,uid_t,uid_type_t)107 static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
108   GTEST_LOG_(INFO) << "This test is about uid/username translation for Android, which does nothing on libc other than bionic.\n";
109 }
110 
111 #endif
112 
TEST(getpwnam,system_id_root)113 TEST(getpwnam, system_id_root) {
114   check_get_passwd("root", 0, TYPE_SYSTEM);
115 }
116 
TEST(getpwnam,system_id_system)117 TEST(getpwnam, system_id_system) {
118   check_get_passwd("system", 1000, TYPE_SYSTEM);
119 }
120 
TEST(getpwnam,app_id_radio)121 TEST(getpwnam, app_id_radio) {
122   check_get_passwd("radio", 1001, TYPE_SYSTEM);
123 }
124 
TEST(getpwnam,app_id_nobody)125 TEST(getpwnam, app_id_nobody) {
126   check_get_passwd("nobody", 9999, TYPE_SYSTEM);
127 }
128 
TEST(getpwnam,app_id_u0_a0)129 TEST(getpwnam, app_id_u0_a0) {
130   check_get_passwd("u0_a0", 10000, TYPE_APP);
131 }
132 
TEST(getpwnam,app_id_u0_a1234)133 TEST(getpwnam, app_id_u0_a1234) {
134   check_get_passwd("u0_a1234", 11234, TYPE_APP);
135 }
136 
137 // Test the difference between uid and shared gid.
TEST(getpwnam,app_id_u0_a49999)138 TEST(getpwnam, app_id_u0_a49999) {
139   check_get_passwd("u0_a49999", 59999, TYPE_APP);
140 }
141 
TEST(getpwnam,app_id_u0_i1)142 TEST(getpwnam, app_id_u0_i1) {
143   check_get_passwd("u0_i1", 99001, TYPE_APP);
144 }
145 
TEST(getpwnam,app_id_u1_root)146 TEST(getpwnam, app_id_u1_root) {
147   check_get_passwd("u1_root", 100000, TYPE_SYSTEM);
148 }
149 
TEST(getpwnam,app_id_u1_radio)150 TEST(getpwnam, app_id_u1_radio) {
151   check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
152 }
153 
TEST(getpwnam,app_id_u1_a0)154 TEST(getpwnam, app_id_u1_a0) {
155   check_get_passwd("u1_a0", 110000, TYPE_APP);
156 }
157 
TEST(getpwnam,app_id_u1_a40000)158 TEST(getpwnam, app_id_u1_a40000) {
159   check_get_passwd("u1_a40000", 150000, TYPE_APP);
160 }
161 
TEST(getpwnam,app_id_u1_i0)162 TEST(getpwnam, app_id_u1_i0) {
163   check_get_passwd("u1_i0", 199000, TYPE_APP);
164 }
165 
166 #if defined(__BIONIC__)
167 
check_group(const group * grp,const char * group_name,gid_t gid)168 static void check_group(const group* grp, const char* group_name, gid_t gid) {
169   ASSERT_TRUE(grp != NULL);
170   ASSERT_STREQ(group_name, grp->gr_name);
171   ASSERT_EQ(gid, grp->gr_gid);
172   ASSERT_TRUE(grp->gr_mem != NULL);
173   ASSERT_STREQ(group_name, grp->gr_mem[0]);
174   ASSERT_TRUE(grp->gr_mem[1] == NULL);
175 }
176 
check_getgrgid(const char * group_name,gid_t gid)177 static void check_getgrgid(const char* group_name, gid_t gid) {
178   errno = 0;
179   group* grp = getgrgid(gid);
180   ASSERT_EQ(0, errno);
181   SCOPED_TRACE("getgrgid");
182   check_group(grp, group_name, gid);
183 }
184 
check_getgrnam(const char * group_name,gid_t gid)185 static void check_getgrnam(const char* group_name, gid_t gid) {
186   errno = 0;
187   group* grp = getgrnam(group_name);
188   ASSERT_EQ(0, errno);
189   SCOPED_TRACE("getgrnam");
190   check_group(grp, group_name, gid);
191 }
192 
check_get_group(const char * group_name,gid_t gid)193 static void check_get_group(const char* group_name, gid_t gid) {
194   check_getgrgid(group_name, gid);
195   check_getgrnam(group_name, gid);
196 }
197 
198 #else // !defined(__BIONIC__)
199 
check_get_group(const char *,gid_t)200 static void check_get_group(const char* /* group_name */, gid_t /* gid */) {
201   GTEST_LOG_(INFO) << "This test is about gid/group_name translation for Android, which does nothing on libc other than bionic.\n";
202 }
203 
204 #endif
205 
TEST(getgrnam,system_id_root)206 TEST(getgrnam, system_id_root) {
207   check_get_group("root", 0);
208 }
209 
TEST(getgrnam,system_id_system)210 TEST(getgrnam, system_id_system) {
211   check_get_group("system", 1000);
212 }
213 
TEST(getgrnam,app_id_radio)214 TEST(getgrnam, app_id_radio) {
215   check_get_group("radio", 1001);
216 }
217 
TEST(getgrnam,app_id_nobody)218 TEST(getgrnam, app_id_nobody) {
219   check_get_group("nobody", 9999);
220 }
221 
TEST(getgrnam,app_id_u0_a0)222 TEST(getgrnam, app_id_u0_a0) {
223   check_get_group("u0_a0", 10000);
224 }
225 
TEST(getgrnam,app_id_u0_a1234)226 TEST(getgrnam, app_id_u0_a1234) {
227   check_get_group("u0_a1234", 11234);
228 }
229 
TEST(getgrnam,app_id_u0_a9999)230 TEST(getgrnam, app_id_u0_a9999) {
231   check_get_group("u0_a9999", 19999);
232 }
233 
234 // Test the difference between uid and shared gid.
TEST(getgrnam,app_id_all_a9999)235 TEST(getgrnam, app_id_all_a9999) {
236   check_get_group("all_a9999", 59999);
237 }
238 
TEST(getgrnam,app_id_u0_i1)239 TEST(getgrnam, app_id_u0_i1) {
240   check_get_group("u0_i1", 99001);
241 }
242 
TEST(getgrnam,app_id_u1_root)243 TEST(getgrnam, app_id_u1_root) {
244   check_get_group("u1_root", 100000);
245 }
246 
TEST(getgrnam,app_id_u1_radio)247 TEST(getgrnam, app_id_u1_radio) {
248   check_get_group("u1_radio", 101001);
249 }
250 
TEST(getgrnam,app_id_u1_a0)251 TEST(getgrnam, app_id_u1_a0) {
252   check_get_group("u1_a0", 110000);
253 }
254 
TEST(getgrnam,app_id_u1_a40000)255 TEST(getgrnam, app_id_u1_a40000) {
256   check_get_group("u1_a40000", 150000);
257 }
258 
TEST(getgrnam,app_id_u1_i0)259 TEST(getgrnam, app_id_u1_i0) {
260   check_get_group("u1_i0", 199000);
261 }
262