blob: a684780715d4b9ba17bc9b08ec9208cd9417a747 [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
Mark Salyzyn722ab052016-04-06 10:35:48 -070029#include <bitset>
30
31#include <private/android_filesystem_config.h>
32
Yabin Cuia04c79b2014-11-18 16:14:54 -080033enum uid_type_t {
Kenny Root2a54e5e2012-09-13 10:52:52 -070034 TYPE_SYSTEM,
35 TYPE_APP
Yabin Cuia04c79b2014-11-18 16:14:54 -080036};
Kenny Root2a54e5e2012-09-13 10:52:52 -070037
Yabin Cuia04c79b2014-11-18 16:14:54 -080038#if defined(__BIONIC__)
39
40static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type) {
Kenny Root2a54e5e2012-09-13 10:52:52 -070041 ASSERT_TRUE(pwd != NULL);
Yabin Cuia04c79b2014-11-18 16:14:54 -080042 ASSERT_STREQ(username, pwd->pw_name);
43 ASSERT_EQ(uid, pwd->pw_uid);
44 ASSERT_EQ(uid, pwd->pw_gid);
Calin Juravlec7688742014-05-09 21:50:53 +010045 ASSERT_EQ(NULL, pwd->pw_passwd);
46#ifdef __LP64__
47 ASSERT_EQ(NULL, pwd->pw_gecos);
48#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -070049
50 if (uid_type == TYPE_SYSTEM) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080051 ASSERT_STREQ("/", pwd->pw_dir);
52 } else {
53 ASSERT_STREQ("/data", pwd->pw_dir);
Kenny Root2a54e5e2012-09-13 10:52:52 -070054 }
Yabin Cuia04c79b2014-11-18 16:14:54 -080055 ASSERT_STREQ("/system/bin/sh", pwd->pw_shell);
Kenny Root2a54e5e2012-09-13 10:52:52 -070056}
Yabin Cuia04c79b2014-11-18 16:14:54 -080057
58static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type) {
59 errno = 0;
60 passwd* pwd = getpwuid(uid);
61 ASSERT_EQ(0, errno);
62 SCOPED_TRACE("getpwuid");
63 check_passwd(pwd, username, uid, uid_type);
64}
65
66static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
67 errno = 0;
68 passwd* pwd = getpwnam(username);
69 ASSERT_EQ(0, errno);
70 SCOPED_TRACE("getpwnam");
71 check_passwd(pwd, username, uid, uid_type);
72}
73
74static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type) {
75 passwd pwd_storage;
76 char buf[512];
77 int result;
78
79 errno = 0;
80 passwd* pwd = NULL;
81 result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
82 ASSERT_EQ(0, result);
83 ASSERT_EQ(0, errno);
84 SCOPED_TRACE("getpwuid_r");
85 check_passwd(pwd, username, uid, uid_type);
86}
87
88static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type) {
89 passwd pwd_storage;
90 char buf[512];
91 int result;
92
93 errno = 0;
94 passwd* pwd = NULL;
95 result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
96 ASSERT_EQ(0, result);
97 ASSERT_EQ(0, errno);
98 SCOPED_TRACE("getpwnam_r");
99 check_passwd(pwd, username, uid, uid_type);
100}
101
102static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type) {
103 check_getpwuid(username, uid, uid_type);
104 check_getpwnam(username, uid, uid_type);
105 check_getpwuid_r(username, uid, uid_type);
106 check_getpwnam_r(username, uid, uid_type);
107}
108
109#else // !defined(__BIONIC__)
110
111static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
112 GTEST_LOG_(INFO) << "This test is about uid/username translation for Android, which does nothing on libc other than bionic.\n";
113}
114
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800115#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -0700116
117TEST(getpwnam, system_id_root) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800118 check_get_passwd("root", 0, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700119}
120
121TEST(getpwnam, system_id_system) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800122 check_get_passwd("system", 1000, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700123}
124
125TEST(getpwnam, app_id_radio) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800126 check_get_passwd("radio", 1001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700127}
128
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700129TEST(getpwnam, oem_id_5000) {
130 check_get_passwd("oem_5000", 5000, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700131}
132
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700133TEST(getpwnam, oem_id_5999) {
134 check_get_passwd("oem_5999", 5999, TYPE_SYSTEM);
135}
136
137TEST(getpwnam, oem_id_2900) {
138 check_get_passwd("oem_2900", 2900, TYPE_SYSTEM);
139}
140
141TEST(getpwnam, oem_id_2999) {
142 check_get_passwd("oem_2999", 2999, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700143}
144
Kenny Root2a54e5e2012-09-13 10:52:52 -0700145TEST(getpwnam, app_id_nobody) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800146 check_get_passwd("nobody", 9999, TYPE_SYSTEM);
Kenny Root8a05a012012-09-13 14:31:50 -0700147}
148
Kenny Root2a54e5e2012-09-13 10:52:52 -0700149TEST(getpwnam, app_id_u0_a0) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800150 check_get_passwd("u0_a0", 10000, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700151}
152
153TEST(getpwnam, app_id_u0_a1234) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800154 check_get_passwd("u0_a1234", 11234, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700155}
156
Yabin Cuia04c79b2014-11-18 16:14:54 -0800157// Test the difference between uid and shared gid.
158TEST(getpwnam, app_id_u0_a49999) {
159 check_get_passwd("u0_a49999", 59999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700160}
161
Yabin Cuia04c79b2014-11-18 16:14:54 -0800162TEST(getpwnam, app_id_u0_i1) {
163 check_get_passwd("u0_i1", 99001, TYPE_APP);
164}
165
Kenny Root2a54e5e2012-09-13 10:52:52 -0700166TEST(getpwnam, app_id_u1_root) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800167 check_get_passwd("u1_root", 100000, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700168}
169
170TEST(getpwnam, app_id_u1_radio) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800171 check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700172}
173
174TEST(getpwnam, app_id_u1_a0) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800175 check_get_passwd("u1_a0", 110000, TYPE_APP);
176}
177
178TEST(getpwnam, app_id_u1_a40000) {
179 check_get_passwd("u1_a40000", 150000, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700180}
181
182TEST(getpwnam, app_id_u1_i0) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800183 check_get_passwd("u1_i0", 199000, TYPE_APP);
184}
185
Mark Salyzyn722ab052016-04-06 10:35:48 -0700186TEST(getpwent, iterate) {
187 passwd* pwd;
188 std::bitset<10000> exist;
189 bool application = false;
190
191 exist.reset();
192
193 setpwent();
194 while ((pwd = getpwent()) != NULL) {
195 ASSERT_TRUE(NULL != pwd->pw_name);
196 ASSERT_EQ(pwd->pw_gid, pwd->pw_uid);
197 ASSERT_EQ(NULL, pwd->pw_passwd);
198#ifdef __LP64__
199 ASSERT_TRUE(NULL == pwd->pw_gecos);
200#endif
201 ASSERT_TRUE(NULL != pwd->pw_shell);
202 if (pwd->pw_uid >= exist.size()) {
203 ASSERT_STREQ("/data", pwd->pw_dir);
204 application = true;
205 } else {
206 ASSERT_STREQ("/", pwd->pw_dir);
207 ASSERT_FALSE(exist[pwd->pw_uid]);
208 exist[pwd->pw_uid] = true;
209 }
210 }
211 endpwent();
212
213 // Required content
214 for (size_t n = 0; n < android_id_count; ++n) {
215 ASSERT_TRUE(exist[android_ids[n].aid]);
216 }
217 for (size_t n = 2900; n < 2999; ++n) {
218 ASSERT_TRUE(exist[n]);
219 }
220 for (size_t n = 5000; n < 5999; ++n) {
221 ASSERT_TRUE(exist[n]);
222 }
223 ASSERT_TRUE(application);
224}
225
Yabin Cuia04c79b2014-11-18 16:14:54 -0800226static void check_group(const group* grp, const char* group_name, gid_t gid) {
227 ASSERT_TRUE(grp != NULL);
228 ASSERT_STREQ(group_name, grp->gr_name);
229 ASSERT_EQ(gid, grp->gr_gid);
230 ASSERT_TRUE(grp->gr_mem != NULL);
231 ASSERT_STREQ(group_name, grp->gr_mem[0]);
232 ASSERT_TRUE(grp->gr_mem[1] == NULL);
233}
234
Yabin Cuic4786d32015-07-20 19:46:26 -0700235#if defined(__BIONIC__)
236
Yabin Cuia04c79b2014-11-18 16:14:54 -0800237static void check_getgrgid(const char* group_name, gid_t gid) {
238 errno = 0;
239 group* grp = getgrgid(gid);
240 ASSERT_EQ(0, errno);
241 SCOPED_TRACE("getgrgid");
242 check_group(grp, group_name, gid);
243}
244
245static void check_getgrnam(const char* group_name, gid_t gid) {
246 errno = 0;
247 group* grp = getgrnam(group_name);
248 ASSERT_EQ(0, errno);
249 SCOPED_TRACE("getgrnam");
250 check_group(grp, group_name, gid);
251}
252
Yabin Cuic4786d32015-07-20 19:46:26 -0700253static void check_getgrgid_r(const char* group_name, gid_t gid) {
254 group grp_storage;
255 char buf[512];
256 group* grp;
257
258 errno = 0;
259 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
260 ASSERT_EQ(0, result);
261 ASSERT_EQ(0, errno);
262 SCOPED_TRACE("getgrgid_r");
263 check_group(grp, group_name, gid);
264}
265
266static void check_getgrnam_r(const char* group_name, gid_t gid) {
267 group grp_storage;
268 char buf[512];
269 group* grp;
270
271 errno = 0;
272 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
273 ASSERT_EQ(0, result);
274 ASSERT_EQ(0, errno);
275 SCOPED_TRACE("getgrnam_r");
276 check_group(grp, group_name, gid);
277}
278
Yabin Cuia04c79b2014-11-18 16:14:54 -0800279static void check_get_group(const char* group_name, gid_t gid) {
280 check_getgrgid(group_name, gid);
281 check_getgrnam(group_name, gid);
Yabin Cuic4786d32015-07-20 19:46:26 -0700282 check_getgrgid_r(group_name, gid);
283 check_getgrnam_r(group_name, gid);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800284}
285
286#else // !defined(__BIONIC__)
287
Yabin Cuic4786d32015-07-20 19:46:26 -0700288static void print_no_getgrnam_test_info() {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800289 GTEST_LOG_(INFO) << "This test is about gid/group_name translation for Android, which does nothing on libc other than bionic.\n";
290}
291
Yabin Cuic4786d32015-07-20 19:46:26 -0700292static void check_get_group(const char*, gid_t) {
293 print_no_getgrnam_test_info();
294}
295
Yabin Cuia04c79b2014-11-18 16:14:54 -0800296#endif
297
298TEST(getgrnam, system_id_root) {
299 check_get_group("root", 0);
300}
301
302TEST(getgrnam, system_id_system) {
303 check_get_group("system", 1000);
304}
305
306TEST(getgrnam, app_id_radio) {
307 check_get_group("radio", 1001);
308}
309
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700310TEST(getgrnam, oem_id_5000) {
311 check_get_group("oem_5000", 5000);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700312}
313
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700314TEST(getgrnam, oem_id_5999) {
315 check_get_group("oem_5999", 5999);
316}
317
318TEST(getgrnam, oem_id_2900) {
319 check_get_group("oem_2900", 2900);
320}
321
322TEST(getgrnam, oem_id_2999) {
323 check_get_group("oem_2999", 2999);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700324}
325
Yabin Cuia04c79b2014-11-18 16:14:54 -0800326TEST(getgrnam, app_id_nobody) {
327 check_get_group("nobody", 9999);
328}
329
330TEST(getgrnam, app_id_u0_a0) {
331 check_get_group("u0_a0", 10000);
332}
333
334TEST(getgrnam, app_id_u0_a1234) {
335 check_get_group("u0_a1234", 11234);
336}
337
338TEST(getgrnam, app_id_u0_a9999) {
339 check_get_group("u0_a9999", 19999);
340}
341
342// Test the difference between uid and shared gid.
343TEST(getgrnam, app_id_all_a9999) {
344 check_get_group("all_a9999", 59999);
345}
346
347TEST(getgrnam, app_id_u0_i1) {
348 check_get_group("u0_i1", 99001);
349}
350
351TEST(getgrnam, app_id_u1_root) {
352 check_get_group("u1_root", 100000);
353}
354
355TEST(getgrnam, app_id_u1_radio) {
356 check_get_group("u1_radio", 101001);
357}
358
359TEST(getgrnam, app_id_u1_a0) {
360 check_get_group("u1_a0", 110000);
361}
362
363TEST(getgrnam, app_id_u1_a40000) {
364 check_get_group("u1_a40000", 150000);
365}
366
367TEST(getgrnam, app_id_u1_i0) {
368 check_get_group("u1_i0", 199000);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700369}
Yabin Cuic4786d32015-07-20 19:46:26 -0700370
371TEST(getgrnam_r, reentrancy) {
372#if defined(__BIONIC__)
373 group grp_storage[2];
374 char buf[2][512];
375 group* grp[3];
376 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
377 ASSERT_EQ(0, result);
378 check_group(grp[0], "root", 0);
379 grp[1] = getgrnam("system");
380 check_group(grp[1], "system", 1000);
381 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
382 ASSERT_EQ(0, result);
383 check_group(grp[2], "radio", 1001);
384 check_group(grp[0], "root", 0);
385 check_group(grp[1], "system", 1000);
386#else
387 print_no_getgrnam_test_info();
388#endif
389}
390
391TEST(getgrgid_r, reentrancy) {
392#if defined(__BIONIC__)
393 group grp_storage[2];
394 char buf[2][512];
395 group* grp[3];
396 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
397 ASSERT_EQ(0, result);
398 check_group(grp[0], "root", 0);
399 grp[1] = getgrgid(1000);
400 check_group(grp[1], "system", 1000);
401 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
402 ASSERT_EQ(0, result);
403 check_group(grp[2], "radio", 1001);
404 check_group(grp[0], "root", 0);
405 check_group(grp[1], "system", 1000);
406#else
407 print_no_getgrnam_test_info();
408#endif
409}
410
411TEST(getgrnam_r, large_enough_suggested_buffer_size) {
412 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
413 ASSERT_GT(size, 0);
414 char buf[size];
415 group grp_storage;
416 group* grp;
417 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
418 check_group(grp, "root", 0);
419}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700420
421TEST(getgrent, iterate) {
422 group* grp;
423 std::bitset<10000> exist;
424 bool application = false;
425
426 exist.reset();
427
428 setgrent();
429 while ((grp = getgrent()) != NULL) {
430 ASSERT_TRUE(grp->gr_name != NULL);
431 ASSERT_TRUE(grp->gr_mem != NULL);
432 ASSERT_STREQ(grp->gr_name, grp->gr_mem[0]);
433 ASSERT_TRUE(grp->gr_mem[1] == NULL);
434 if (grp->gr_gid >= exist.size()) {
435 application = true;
436 } else {
437 ASSERT_FALSE(exist[grp->gr_gid]);
438 exist[grp->gr_gid] = true;
439 }
440 }
441 endgrent();
442
443 // Required content
444 for (size_t n = 0; n < android_id_count; ++n) {
445 ASSERT_TRUE(exist[android_ids[n].aid]);
446 }
447 for (size_t n = 2900; n < 2999; ++n) {
448 ASSERT_TRUE(exist[n]);
449 }
450 for (size_t n = 5000; n < 5999; ++n) {
451 ASSERT_TRUE(exist[n]);
452 }
453 ASSERT_TRUE(application);
454}