blob: 29cd907d2c67e8cff7c2c2e1bb7ee4c2dd86e502 [file] [log] [blame]
Kenny Root2a54e5e2012-09-13 10:52:52 -07001/*
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
Yabin Cuia04c79b2014-11-18 16:14:54 -080019// Below are the header files we want to test.
20#include <grp.h>
Kenny Root2a54e5e2012-09-13 10:52:52 -070021#include <pwd.h>
Yabin Cuia04c79b2014-11-18 16:14:54 -080022
Kenny Root2a54e5e2012-09-13 10:52:52 -070023#include <errno.h>
24#include <limits.h>
Yabin Cuia04c79b2014-11-18 16:14:54 -080025#include <sys/cdefs.h>
26#include <sys/types.h>
Kenny Root2a54e5e2012-09-13 10:52:52 -070027#include <unistd.h>
28
Yabin Cuia04c79b2014-11-18 16:14:54 -080029enum uid_type_t {
Kenny Root2a54e5e2012-09-13 10:52:52 -070030 TYPE_SYSTEM,
31 TYPE_APP
Yabin Cuia04c79b2014-11-18 16:14:54 -080032};
Kenny Root2a54e5e2012-09-13 10:52:52 -070033
Yabin Cuia04c79b2014-11-18 16:14:54 -080034#if defined(__BIONIC__)
35
36static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type) {
Kenny Root2a54e5e2012-09-13 10:52:52 -070037 ASSERT_TRUE(pwd != NULL);
Yabin Cuia04c79b2014-11-18 16:14:54 -080038 ASSERT_STREQ(username, pwd->pw_name);
39 ASSERT_EQ(uid, pwd->pw_uid);
40 ASSERT_EQ(uid, pwd->pw_gid);
Calin Juravlec7688742014-05-09 21:50:53 +010041 ASSERT_EQ(NULL, pwd->pw_passwd);
42#ifdef __LP64__
43 ASSERT_EQ(NULL, pwd->pw_gecos);
44#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -070045
46 if (uid_type == TYPE_SYSTEM) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080047 ASSERT_STREQ("/", pwd->pw_dir);
48 } else {
49 ASSERT_STREQ("/data", pwd->pw_dir);
Kenny Root2a54e5e2012-09-13 10:52:52 -070050 }
Yabin Cuia04c79b2014-11-18 16:14:54 -080051 ASSERT_STREQ("/system/bin/sh", pwd->pw_shell);
Kenny Root2a54e5e2012-09-13 10:52:52 -070052}
Yabin Cuia04c79b2014-11-18 16:14:54 -080053
54static 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
62static 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
70static 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
84static 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
98static 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
107static 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
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800111#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -0700112
113TEST(getpwnam, system_id_root) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800114 check_get_passwd("root", 0, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700115}
116
117TEST(getpwnam, system_id_system) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800118 check_get_passwd("system", 1000, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700119}
120
121TEST(getpwnam, app_id_radio) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800122 check_get_passwd("radio", 1001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700123}
124
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700125TEST(getpwnam, oem_id_5000) {
126 check_get_passwd("oem_5000", 5000, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700127}
128
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700129TEST(getpwnam, oem_id_5999) {
130 check_get_passwd("oem_5999", 5999, TYPE_SYSTEM);
131}
132
133TEST(getpwnam, oem_id_2900) {
134 check_get_passwd("oem_2900", 2900, TYPE_SYSTEM);
135}
136
137TEST(getpwnam, oem_id_2999) {
138 check_get_passwd("oem_2999", 2999, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700139}
140
Kenny Root2a54e5e2012-09-13 10:52:52 -0700141TEST(getpwnam, app_id_nobody) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800142 check_get_passwd("nobody", 9999, TYPE_SYSTEM);
Kenny Root8a05a012012-09-13 14:31:50 -0700143}
144
Kenny Root2a54e5e2012-09-13 10:52:52 -0700145TEST(getpwnam, app_id_u0_a0) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800146 check_get_passwd("u0_a0", 10000, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700147}
148
149TEST(getpwnam, app_id_u0_a1234) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800150 check_get_passwd("u0_a1234", 11234, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700151}
152
Yabin Cuia04c79b2014-11-18 16:14:54 -0800153// Test the difference between uid and shared gid.
154TEST(getpwnam, app_id_u0_a49999) {
155 check_get_passwd("u0_a49999", 59999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700156}
157
Yabin Cuia04c79b2014-11-18 16:14:54 -0800158TEST(getpwnam, app_id_u0_i1) {
159 check_get_passwd("u0_i1", 99001, TYPE_APP);
160}
161
Kenny Root2a54e5e2012-09-13 10:52:52 -0700162TEST(getpwnam, app_id_u1_root) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800163 check_get_passwd("u1_root", 100000, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700164}
165
166TEST(getpwnam, app_id_u1_radio) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800167 check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700168}
169
170TEST(getpwnam, app_id_u1_a0) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800171 check_get_passwd("u1_a0", 110000, TYPE_APP);
172}
173
174TEST(getpwnam, app_id_u1_a40000) {
175 check_get_passwd("u1_a40000", 150000, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700176}
177
178TEST(getpwnam, app_id_u1_i0) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800179 check_get_passwd("u1_i0", 199000, TYPE_APP);
180}
181
Yabin Cuia04c79b2014-11-18 16:14:54 -0800182static void check_group(const group* grp, const char* group_name, gid_t gid) {
183 ASSERT_TRUE(grp != NULL);
184 ASSERT_STREQ(group_name, grp->gr_name);
185 ASSERT_EQ(gid, grp->gr_gid);
186 ASSERT_TRUE(grp->gr_mem != NULL);
187 ASSERT_STREQ(group_name, grp->gr_mem[0]);
188 ASSERT_TRUE(grp->gr_mem[1] == NULL);
189}
190
Yabin Cuic4786d32015-07-20 19:46:26 -0700191#if defined(__BIONIC__)
192
Yabin Cuia04c79b2014-11-18 16:14:54 -0800193static void check_getgrgid(const char* group_name, gid_t gid) {
194 errno = 0;
195 group* grp = getgrgid(gid);
196 ASSERT_EQ(0, errno);
197 SCOPED_TRACE("getgrgid");
198 check_group(grp, group_name, gid);
199}
200
201static void check_getgrnam(const char* group_name, gid_t gid) {
202 errno = 0;
203 group* grp = getgrnam(group_name);
204 ASSERT_EQ(0, errno);
205 SCOPED_TRACE("getgrnam");
206 check_group(grp, group_name, gid);
207}
208
Yabin Cuic4786d32015-07-20 19:46:26 -0700209static void check_getgrgid_r(const char* group_name, gid_t gid) {
210 group grp_storage;
211 char buf[512];
212 group* grp;
213
214 errno = 0;
215 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
216 ASSERT_EQ(0, result);
217 ASSERT_EQ(0, errno);
218 SCOPED_TRACE("getgrgid_r");
219 check_group(grp, group_name, gid);
220}
221
222static void check_getgrnam_r(const char* group_name, gid_t gid) {
223 group grp_storage;
224 char buf[512];
225 group* grp;
226
227 errno = 0;
228 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
229 ASSERT_EQ(0, result);
230 ASSERT_EQ(0, errno);
231 SCOPED_TRACE("getgrnam_r");
232 check_group(grp, group_name, gid);
233}
234
Yabin Cuia04c79b2014-11-18 16:14:54 -0800235static void check_get_group(const char* group_name, gid_t gid) {
236 check_getgrgid(group_name, gid);
237 check_getgrnam(group_name, gid);
Yabin Cuic4786d32015-07-20 19:46:26 -0700238 check_getgrgid_r(group_name, gid);
239 check_getgrnam_r(group_name, gid);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800240}
241
242#else // !defined(__BIONIC__)
243
Yabin Cuic4786d32015-07-20 19:46:26 -0700244static void print_no_getgrnam_test_info() {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800245 GTEST_LOG_(INFO) << "This test is about gid/group_name translation for Android, which does nothing on libc other than bionic.\n";
246}
247
Yabin Cuic4786d32015-07-20 19:46:26 -0700248static void check_get_group(const char*, gid_t) {
249 print_no_getgrnam_test_info();
250}
251
Yabin Cuia04c79b2014-11-18 16:14:54 -0800252#endif
253
254TEST(getgrnam, system_id_root) {
255 check_get_group("root", 0);
256}
257
258TEST(getgrnam, system_id_system) {
259 check_get_group("system", 1000);
260}
261
262TEST(getgrnam, app_id_radio) {
263 check_get_group("radio", 1001);
264}
265
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700266TEST(getgrnam, oem_id_5000) {
267 check_get_group("oem_5000", 5000);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700268}
269
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700270TEST(getgrnam, oem_id_5999) {
271 check_get_group("oem_5999", 5999);
272}
273
274TEST(getgrnam, oem_id_2900) {
275 check_get_group("oem_2900", 2900);
276}
277
278TEST(getgrnam, oem_id_2999) {
279 check_get_group("oem_2999", 2999);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700280}
281
Yabin Cuia04c79b2014-11-18 16:14:54 -0800282TEST(getgrnam, app_id_nobody) {
283 check_get_group("nobody", 9999);
284}
285
286TEST(getgrnam, app_id_u0_a0) {
287 check_get_group("u0_a0", 10000);
288}
289
290TEST(getgrnam, app_id_u0_a1234) {
291 check_get_group("u0_a1234", 11234);
292}
293
294TEST(getgrnam, app_id_u0_a9999) {
295 check_get_group("u0_a9999", 19999);
296}
297
298// Test the difference between uid and shared gid.
299TEST(getgrnam, app_id_all_a9999) {
300 check_get_group("all_a9999", 59999);
301}
302
303TEST(getgrnam, app_id_u0_i1) {
304 check_get_group("u0_i1", 99001);
305}
306
307TEST(getgrnam, app_id_u1_root) {
308 check_get_group("u1_root", 100000);
309}
310
311TEST(getgrnam, app_id_u1_radio) {
312 check_get_group("u1_radio", 101001);
313}
314
315TEST(getgrnam, app_id_u1_a0) {
316 check_get_group("u1_a0", 110000);
317}
318
319TEST(getgrnam, app_id_u1_a40000) {
320 check_get_group("u1_a40000", 150000);
321}
322
323TEST(getgrnam, app_id_u1_i0) {
324 check_get_group("u1_i0", 199000);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700325}
Yabin Cuic4786d32015-07-20 19:46:26 -0700326
327TEST(getgrnam_r, reentrancy) {
328#if defined(__BIONIC__)
329 group grp_storage[2];
330 char buf[2][512];
331 group* grp[3];
332 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
333 ASSERT_EQ(0, result);
334 check_group(grp[0], "root", 0);
335 grp[1] = getgrnam("system");
336 check_group(grp[1], "system", 1000);
337 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
338 ASSERT_EQ(0, result);
339 check_group(grp[2], "radio", 1001);
340 check_group(grp[0], "root", 0);
341 check_group(grp[1], "system", 1000);
342#else
343 print_no_getgrnam_test_info();
344#endif
345}
346
347TEST(getgrgid_r, reentrancy) {
348#if defined(__BIONIC__)
349 group grp_storage[2];
350 char buf[2][512];
351 group* grp[3];
352 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
353 ASSERT_EQ(0, result);
354 check_group(grp[0], "root", 0);
355 grp[1] = getgrgid(1000);
356 check_group(grp[1], "system", 1000);
357 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
358 ASSERT_EQ(0, result);
359 check_group(grp[2], "radio", 1001);
360 check_group(grp[0], "root", 0);
361 check_group(grp[1], "system", 1000);
362#else
363 print_no_getgrnam_test_info();
364#endif
365}
366
367TEST(getgrnam_r, large_enough_suggested_buffer_size) {
368 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
369 ASSERT_GT(size, 0);
370 char buf[size];
371 group grp_storage;
372 group* grp;
373 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
374 check_group(grp, "root", 0);
375}