blob: 3d5a9335a9a7c625cb97efc4d5354c20c212c36c [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
Tom Cherry4362f892017-11-14 08:50:43 -080029#include <set>
30#include <vector>
Mark Salyzyn722ab052016-04-06 10:35:48 -070031
Tom Cherrye88b4082018-05-24 14:44:10 -070032#include <android-base/file.h>
Tom Cherry4362f892017-11-14 08:50:43 -080033#include <android-base/strings.h>
Mark Salyzyn722ab052016-04-06 10:35:48 -070034#include <private/android_filesystem_config.h>
35
Tom Cherry5c941432018-10-09 11:01:28 -070036#if defined(__BIONIC__)
Tom Cherry9da8ff12019-02-19 13:23:49 -080037#include <android/api-level.h>
Tom Cherry5c941432018-10-09 11:01:28 -070038#include <android-base/properties.h>
39#endif
40
Elliott Hughes3f6eee92016-12-13 23:47:25 +000041// Generated android_ids array
42#include "generated_android_ids.h"
43
Tom Cherry4362f892017-11-14 08:50:43 -080044using android::base::Join;
Tom Cherrye88b4082018-05-24 14:44:10 -070045using android::base::ReadFileToString;
46using android::base::Split;
47using android::base::StartsWith;
Tom Cherry4362f892017-11-14 08:50:43 -080048
Tom Cherry6b116d12019-04-25 10:34:07 -070049using namespace std::literals;
50
Yabin Cuia04c79b2014-11-18 16:14:54 -080051enum uid_type_t {
Tom Cherryfa5f61c2018-09-27 13:19:02 -070052 TYPE_APP,
Kenny Root2a54e5e2012-09-13 10:52:52 -070053 TYPE_SYSTEM,
Tom Cherryfa5f61c2018-09-27 13:19:02 -070054 TYPE_VENDOR,
Yabin Cuia04c79b2014-11-18 16:14:54 -080055};
Kenny Root2a54e5e2012-09-13 10:52:52 -070056
Yabin Cuia04c79b2014-11-18 16:14:54 -080057#if defined(__BIONIC__)
58
Tom Cherryb4c25c82018-04-04 15:02:55 -070059static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type,
60 bool check_username) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070061 ASSERT_TRUE(pwd != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -070062 if (check_username) {
63 EXPECT_STREQ(username, pwd->pw_name);
64 }
Tom Cherry2c05c0f2017-11-10 10:57:21 -080065 EXPECT_EQ(uid, pwd->pw_uid);
66 EXPECT_EQ(uid, pwd->pw_gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -070067 EXPECT_EQ(nullptr, pwd->pw_passwd);
Calin Juravlec7688742014-05-09 21:50:53 +010068#ifdef __LP64__
Yi Kong32bc0fc2018-08-02 17:31:13 -070069 EXPECT_EQ(nullptr, pwd->pw_gecos);
Calin Juravlec7688742014-05-09 21:50:53 +010070#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -070071
Tom Cherryfa5f61c2018-09-27 13:19:02 -070072 if (uid_type == TYPE_APP) {
Tom Cherry2c05c0f2017-11-10 10:57:21 -080073 EXPECT_STREQ("/data", pwd->pw_dir);
Tom Cherryfa5f61c2018-09-27 13:19:02 -070074 } else {
75 EXPECT_STREQ("/", pwd->pw_dir);
Kenny Root2a54e5e2012-09-13 10:52:52 -070076 }
Tom Cherryfa5f61c2018-09-27 13:19:02 -070077
Tom Cherryb9fa04d2020-07-10 10:40:21 -070078 // This has changed over time and that causes new GSI + old vendor images testing to fail.
79 // This parameter doesn't matter on Android, so simply ignore its value for older vendor images.
80 if (android::base::GetIntProperty("ro.product.first_api_level", 0) >= 30) {
81 EXPECT_STREQ("/bin/sh", pwd->pw_shell);
82 }
Kenny Root2a54e5e2012-09-13 10:52:52 -070083}
Yabin Cuia04c79b2014-11-18 16:14:54 -080084
Tom Cherryb4c25c82018-04-04 15:02:55 -070085static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type,
86 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080087 errno = 0;
88 passwd* pwd = getpwuid(uid);
89 ASSERT_EQ(0, errno);
90 SCOPED_TRACE("getpwuid");
Tom Cherryb4c25c82018-04-04 15:02:55 -070091 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -080092}
93
Tom Cherryb4c25c82018-04-04 15:02:55 -070094static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type,
95 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080096 errno = 0;
97 passwd* pwd = getpwnam(username);
98 ASSERT_EQ(0, errno);
99 SCOPED_TRACE("getpwnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700100 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800101}
102
Tom Cherryb4c25c82018-04-04 15:02:55 -0700103static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type,
104 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800105 passwd pwd_storage;
106 char buf[512];
107 int result;
108
109 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700110 passwd* pwd = nullptr;
Yabin Cuia04c79b2014-11-18 16:14:54 -0800111 result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
112 ASSERT_EQ(0, result);
113 ASSERT_EQ(0, errno);
114 SCOPED_TRACE("getpwuid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700115 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800116}
117
Tom Cherryb4c25c82018-04-04 15:02:55 -0700118static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type,
119 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800120 passwd pwd_storage;
121 char buf[512];
122 int result;
123
124 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700125 passwd* pwd = nullptr;
Yabin Cuia04c79b2014-11-18 16:14:54 -0800126 result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
127 ASSERT_EQ(0, result);
128 ASSERT_EQ(0, errno);
129 SCOPED_TRACE("getpwnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700130 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800131}
132
Tom Cherryb4c25c82018-04-04 15:02:55 -0700133static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type,
134 bool check_username = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700135 SCOPED_TRACE("username '"s + username + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700136 check_getpwuid(username, uid, uid_type, check_username);
137 check_getpwnam(username, uid, uid_type, check_username);
138 check_getpwuid_r(username, uid, uid_type, check_username);
139 check_getpwnam_r(username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800140}
141
Tom Cherry6b116d12019-04-25 10:34:07 -0700142static void expect_no_passwd_id(uid_t uid) {
143 SCOPED_TRACE("uid '" + std::to_string(uid) + "'");
144 errno = 0;
145 passwd* passwd = nullptr;
146 passwd = getpwuid(uid);
147 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
148 EXPECT_EQ(ENOENT, errno);
149
150 struct passwd passwd_storage;
151 char buf[512];
152 EXPECT_EQ(ENOENT, getpwuid_r(uid, &passwd_storage, buf, sizeof(buf), &passwd));
153 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
154}
155
156static void expect_no_passwd_name(const char* username) {
157 SCOPED_TRACE("username '"s + username + "'");
158 errno = 0;
159 passwd* passwd = nullptr;
160 passwd = getpwnam(username);
161 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
162 EXPECT_EQ(ENOENT, errno);
163
164 struct passwd passwd_storage;
165 char buf[512];
166 EXPECT_EQ(ENOENT, getpwnam_r(username, &passwd_storage, buf, sizeof(buf), &passwd));
167 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
168}
169
Yabin Cuia04c79b2014-11-18 16:14:54 -0800170#else // !defined(__BIONIC__)
171
Tom Cherryb4c25c82018-04-04 15:02:55 -0700172static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */,
173 bool /* check_username */) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800174 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700175}
176
Josh Gao2fe10342018-02-27 14:05:53 -0800177static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800178 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800179}
180
Tom Cherry6b116d12019-04-25 10:34:07 -0700181static void expect_no_passwd_id(uid_t /* uid */) {
182 GTEST_SKIP() << "bionic-only test";
183}
184
185static void expect_no_passwd_name(const char* /* username */) {
186 GTEST_SKIP() << "bionic-only test";
187}
188
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800189#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -0700190
Tom Cherry6b116d12019-04-25 10:34:07 -0700191TEST(pwd, getpwnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800192 check_get_passwd("root", 0, TYPE_SYSTEM);
Tom Cherry6b116d12019-04-25 10:34:07 -0700193 check_get_passwd("daemon", 1, TYPE_SYSTEM);
194 check_get_passwd("bin", 2, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700195
Yabin Cuia04c79b2014-11-18 16:14:54 -0800196 check_get_passwd("system", 1000, TYPE_SYSTEM);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800197 check_get_passwd("radio", 1001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700198
Tom Cherry6b116d12019-04-25 10:34:07 -0700199 check_get_passwd("shell", 2000, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700200
Yabin Cuia04c79b2014-11-18 16:14:54 -0800201 check_get_passwd("nobody", 9999, TYPE_SYSTEM);
Kenny Root8a05a012012-09-13 14:31:50 -0700202}
203
Tom Cherry6b116d12019-04-25 10:34:07 -0700204TEST(pwd, getpwnam_oem_ids) {
205 check_get_passwd("oem_2900", 2900, TYPE_VENDOR, false);
206 check_get_passwd("oem_2945", 2945, TYPE_VENDOR, false);
207 check_get_passwd("oem_2999", 2999, TYPE_VENDOR, false);
208 check_get_passwd("oem_5000", 5000, TYPE_VENDOR, false);
209 check_get_passwd("oem_5454", 5454, TYPE_VENDOR, false);
210 check_get_passwd("oem_5999", 5999, TYPE_VENDOR, false);
211}
212
213TEST(pwd, getpwnam_non_exist) {
214 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
215 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
216 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
217
218 // These ranges are for GIDs only.
219 expect_no_passwd_id(20000);
220 expect_no_passwd_id(30000);
221 expect_no_passwd_id(40000);
222 expect_no_passwd_id(50000);
223
224 // These should not be parsed as users, only as groups.
225 expect_no_passwd_name("u0_a9999_cache");
226 expect_no_passwd_name("u0_a9999_ext");
227 expect_no_passwd_name("u0_a9999_ext_cache");
228 expect_no_passwd_name("all_a9999");
229}
230
231TEST(pwd, getpwnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800232 check_get_passwd("u0_a0", 10000, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800233 check_get_passwd("u0_a1234", 11234, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700234 check_get_passwd("u0_a9999", 19999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700235
Martijn Coenenf9d22992019-01-16 16:25:40 +0100236 check_get_passwd("u0_i1", 90001, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700237 check_get_passwd("u0_i4545", 94545, TYPE_APP);
238 check_get_passwd("u0_i9999", 99999, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800239}
240
Tom Cherry6b116d12019-04-25 10:34:07 -0700241TEST(pwd, getpwnam_app_id_u1_ids) {
242 check_get_passwd("u1_system", 101000, TYPE_SYSTEM);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800243 check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700244
Yabin Cuia04c79b2014-11-18 16:14:54 -0800245 check_get_passwd("u1_a0", 110000, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700246 check_get_passwd("u1_a1234", 111234, TYPE_APP);
247 check_get_passwd("u1_a9999", 119999, TYPE_APP);
248
249 check_get_passwd("u1_i1", 190001, TYPE_APP);
250 check_get_passwd("u1_i4545", 194545, TYPE_APP);
251 check_get_passwd("u1_i9999", 199999, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800252}
253
Tom Cherry6b116d12019-04-25 10:34:07 -0700254TEST(pwd, getpwnam_app_id_u31_ids) {
255 check_get_passwd("u31_system", 3101000, TYPE_SYSTEM);
256 check_get_passwd("u31_radio", 3101001, TYPE_SYSTEM);
257
258 check_get_passwd("u31_a0", 3110000, TYPE_APP);
259 check_get_passwd("u31_a1234", 3111234, TYPE_APP);
260 check_get_passwd("u31_a9999", 3119999, TYPE_APP);
261
262 check_get_passwd("u31_i1", 3190001, TYPE_APP);
263 check_get_passwd("u31_i4545", 3194545, TYPE_APP);
264 check_get_passwd("u31_i9999", 3199999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700265}
266
Tom Cherry6b116d12019-04-25 10:34:07 -0700267TEST(pwd, getpwnam_app_id_not_allowed_platform) {
268 expect_no_passwd_name("u1_root");
269 expect_no_passwd_name("u1_debuggerd");
270
271 expect_no_passwd_name("u31_root");
272 expect_no_passwd_name("u31_debuggerd");
273}
274
275TEST(pwd, getpwuid_app_id_u1_non_exist) {
276 expect_no_passwd_id(100000); // There is no 'root' for secondary users.
277 expect_no_passwd_id(101999); // End of the system reserved range, unallocated.
278 expect_no_passwd_id(102900); // The OEM ranges were never allocated to secondary users.
279 expect_no_passwd_id(105000); // The OEM ranges were never allocated to secondary users.
280
281 // These ranges are for GIDs only.
282 expect_no_passwd_id(120000);
283 expect_no_passwd_id(130000);
284 expect_no_passwd_id(140000);
285 expect_no_passwd_id(150000);
286}
287
288TEST(pwd, getpwuid_app_id_u31_non_exist) {
289 expect_no_passwd_id(3100000); // There is no 'root' for secondary users.
290 expect_no_passwd_id(3101999); // End of the system reserved range, unallocated.
291 expect_no_passwd_id(3102900); // The OEM ranges were never allocated to secondary users.
292 expect_no_passwd_id(3105000); // The OEM ranges were never allocated to secondary users.
293
294 // These ranges are for GIDs only.
295 expect_no_passwd_id(3120000);
296 expect_no_passwd_id(3130000);
297 expect_no_passwd_id(3140000);
298 expect_no_passwd_id(3150000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800299}
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700300
301TEST(pwd, getpwnam_r_alignment) {
302#if defined(__BIONIC__)
303 passwd pwd_storage;
304 alignas(16) char buf[512];
305 passwd* pwd;
306 int result = getpwnam_r("root", &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
307 ASSERT_EQ(0, result);
308 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
309#else
310 GTEST_SKIP() << "bionic-only test";
311#endif
312}
313
314TEST(pwd, getpwuid_r_alignment) {
315#if defined(__BIONIC__)
316 passwd pwd_storage;
317 alignas(16) char buf[512];
318 passwd* pwd;
319 int result = getpwuid_r(0, &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
320 ASSERT_EQ(0, result);
321 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
322#else
323 GTEST_SKIP() << "bionic-only test";
324#endif
325}
326
327TEST(pwd, getpwnam_r_reentrancy) {
328#if defined(__BIONIC__)
329 passwd pwd_storage[2];
330 char buf[2][512];
331 passwd* pwd[3];
332 int result = getpwnam_r("root", &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
333 ASSERT_EQ(0, result);
334 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
335 pwd[1] = getpwnam("system");
336 ASSERT_NE(nullptr, pwd[1]);
337 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
338 result = getpwnam_r("radio", &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
339 ASSERT_EQ(0, result);
340 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
341 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
342 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
343#else
344 GTEST_SKIP() << "bionic-only test";
345#endif
346}
347
348TEST(pwd, getpwuid_r_reentrancy) {
349#if defined(__BIONIC__)
350 passwd pwd_storage[2];
351 char buf[2][512];
352 passwd* pwd[3];
353 int result = getpwuid_r(0, &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
354 ASSERT_EQ(0, result);
355 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
356 pwd[1] = getpwuid(1000);
357 ASSERT_NE(nullptr, pwd[1]);
358 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
359 result = getpwuid_r(1001, &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
360 ASSERT_EQ(0, result);
361 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
362 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
363 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
364#else
365 GTEST_SKIP() << "bionic-only test";
366#endif
367}
368
369TEST(pwd, getpwnam_r_large_enough_suggested_buffer_size) {
370#if defined(__BIONIC__)
371 long size = sysconf(_SC_GETPW_R_SIZE_MAX);
372 ASSERT_GT(size, 0);
373 char buf[size];
374 passwd pwd_storage;
375 passwd* pwd;
376 ASSERT_EQ(0, getpwnam_r("root", &pwd_storage, buf, size, &pwd));
377 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
378#else
379 GTEST_SKIP() << "bionic-only test";
380#endif
381}
382
Tom Cherry5c941432018-10-09 11:01:28 -0700383#if defined(__BIONIC__)
Tom Cherry4362f892017-11-14 08:50:43 -0800384template <typename T>
Tom Cherry777b34d2019-02-17 09:38:23 -0800385static void expect_ids(T ids, bool is_group) {
Tom Cherry4362f892017-11-14 08:50:43 -0800386 std::set<typename T::key_type> expected_ids;
387 // Ensure that all android_ids are iterated through.
388 for (size_t n = 0; n < android_id_count; ++n) {
389 EXPECT_EQ(1U, ids.count(android_ids[n].aid)) << "android_ids[n].aid: " << android_ids[n].aid;
390 expected_ids.emplace(android_ids[n].aid);
391 }
392
393 auto expect_range = [&ids, &expected_ids](uid_t start, uid_t end) {
394 for (size_t n = start; n <= end; ++n) {
395 EXPECT_EQ(1U, ids.count(n)) << "n: " << n;
396 expected_ids.emplace(n);
397 }
398 };
399
400 // Ensure that all reserved ranges are iterated through.
401 expect_range(AID_OEM_RESERVED_START, AID_OEM_RESERVED_END);
402 expect_range(AID_OEM_RESERVED_2_START, AID_OEM_RESERVED_2_END);
403 expect_range(AID_APP_START, AID_APP_END);
Tom Cherry6b116d12019-04-25 10:34:07 -0700404 if (is_group) {
405 expect_range(AID_CACHE_GID_START, AID_CACHE_GID_END);
406 expect_range(AID_EXT_GID_START, AID_EXT_GID_END);
407 expect_range(AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END);
408 expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
409 }
Tom Cherry4362f892017-11-14 08:50:43 -0800410 expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
411
Tom Cherry6f2e8102020-04-10 10:50:09 -0700412 // Prior to R, we didn't have a mechanism to create vendor AIDs in the system or other non-vendor
413 // partitions, therefore we disabled the rest of these checks for older API levels.
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800414 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 29) {
Tom Cherry5c941432018-10-09 11:01:28 -0700415 return;
416 }
417
Tom Cherry777b34d2019-02-17 09:38:23 -0800418 auto allow_range = [&ids](uid_t start, uid_t end) {
419 for (size_t n = start; n <= end; ++n) {
420 ids.erase(n);
421 }
422 };
423
424 allow_range(AID_SYSTEM_RESERVED_START, AID_SYSTEM_EXT_RESERVED_END);
425
Tom Cherry4362f892017-11-14 08:50:43 -0800426 // Ensure that no other ids were returned.
427 auto return_differences = [&ids, &expected_ids] {
428 std::vector<typename T::key_type> missing_from_ids;
429 std::set_difference(expected_ids.begin(), expected_ids.end(), ids.begin(), ids.end(),
430 std::inserter(missing_from_ids, missing_from_ids.begin()));
431 std::vector<typename T::key_type> extra_in_ids;
432 std::set_difference(ids.begin(), ids.end(), expected_ids.begin(), expected_ids.end(),
433 std::inserter(extra_in_ids, extra_in_ids.begin()));
434 std::string result;
435 if (!missing_from_ids.empty()) {
436 result += "Missing ids from results: " + Join(missing_from_ids, " ");
437 }
438 if (!extra_in_ids.empty()) {
439 if (!result.empty()) result += ", ";
440 result += "Extra ids in results: " + Join(extra_in_ids, " ");
441 }
442 return result;
443 };
Orion Hodsonf5fd5ad2022-10-27 10:28:24 +0100444
Elliott Hughes140e4d32024-02-10 00:39:07 +0000445 // AID_UPROBESTATS (1093) was added in V, but "trunk stable" means
446 // that the 2024Q builds don't have branches like the QPR builds used
447 // to, and are tested with the _previous_ release's CTS.
448 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == __ANDROID_API_U__) {
449#if !defined(AID_UPROBESTATS)
450#define AID_UPROBESTATS 1093
Orion Hodsonf5fd5ad2022-10-27 10:28:24 +0100451#endif
Elliott Hughes140e4d32024-02-10 00:39:07 +0000452 ids.erase(AID_UPROBESTATS);
453 expected_ids.erase(AID_UPROBESTATS);
454 if (getpwuid(AID_UPROBESTATS)) {
455 EXPECT_STREQ(getpwuid(AID_UPROBESTATS)->pw_name, "uprobestats");
456 }
Orion Hodsonf5fd5ad2022-10-27 10:28:24 +0100457 }
Elliott Hughes140e4d32024-02-10 00:39:07 +0000458 // AID_VIRTUALMACHINE (3013) was added in V, but "trunk stable" means
459 // that the 2024Q builds don't have branches like the QPR builds used
460 // to, and are tested with the _previous_ release's CTS.
461 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == __ANDROID_API_U__) {
462#if !defined(AID_VIRTUALMACHINE)
463#define AID_VIRTUALMACHINE 3013
464#endif
465 ids.erase(AID_VIRTUALMACHINE);
466 expected_ids.erase(AID_VIRTUALMACHINE);
467 if (getpwuid(AID_VIRTUALMACHINE)) {
468 EXPECT_STREQ(getpwuid(AID_VIRTUALMACHINE)->pw_name, "virtualmachine");
469 }
470 }
471
Tom Cherry4362f892017-11-14 08:50:43 -0800472 EXPECT_EQ(expected_ids, ids) << return_differences();
473}
Tom Cherry5c941432018-10-09 11:01:28 -0700474#endif
Tom Cherry4362f892017-11-14 08:50:43 -0800475
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800476TEST(pwd, getpwent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800477#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700478 passwd* pwd;
Tom Cherry4362f892017-11-14 08:50:43 -0800479 std::set<uid_t> uids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700480
481 setpwent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700482 while ((pwd = getpwent()) != nullptr) {
483 ASSERT_TRUE(nullptr != pwd->pw_name);
Tom Cherry4362f892017-11-14 08:50:43 -0800484
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800485 EXPECT_EQ(pwd->pw_gid, pwd->pw_uid) << "pwd->pw_uid: " << pwd->pw_uid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700486 EXPECT_EQ(nullptr, pwd->pw_passwd) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700487#ifdef __LP64__
Yi Kong32bc0fc2018-08-02 17:31:13 -0700488 EXPECT_TRUE(nullptr == pwd->pw_gecos) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700489#endif
Yi Kong32bc0fc2018-08-02 17:31:13 -0700490 EXPECT_TRUE(nullptr != pwd->pw_shell);
Tom Cherry4362f892017-11-14 08:50:43 -0800491 if (pwd->pw_uid < AID_APP_START || pwd->pw_uid == AID_OVERFLOWUID) {
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800492 EXPECT_STREQ("/", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
Tom Cherry4362f892017-11-14 08:50:43 -0800493 } else {
494 EXPECT_STREQ("/data", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700495 }
Tom Cherry4362f892017-11-14 08:50:43 -0800496
Tom Cherry0816c902020-04-10 13:00:42 -0700497 EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid;
Tom Cherry4362f892017-11-14 08:50:43 -0800498 uids.emplace(pwd->pw_uid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700499 }
500 endpwent();
501
Tom Cherry6b116d12019-04-25 10:34:07 -0700502 expect_ids(uids, false);
Josh Gao2fe10342018-02-27 14:05:53 -0800503#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800504 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800505#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700506}
507
Tom Cherryb4c25c82018-04-04 15:02:55 -0700508static void check_group(const group* grp, const char* group_name, gid_t gid,
509 bool check_groupname = true) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700510 ASSERT_TRUE(grp != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700511 if (check_groupname) {
512 EXPECT_STREQ(group_name, grp->gr_name);
513 }
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800514 EXPECT_EQ(gid, grp->gr_gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700515 ASSERT_TRUE(grp->gr_mem != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700516 if (check_groupname) {
517 EXPECT_STREQ(group_name, grp->gr_mem[0]);
518 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700519 EXPECT_TRUE(grp->gr_mem[1] == nullptr);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800520}
521
Yabin Cuic4786d32015-07-20 19:46:26 -0700522#if defined(__BIONIC__)
523
Tom Cherryb4c25c82018-04-04 15:02:55 -0700524static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800525 errno = 0;
526 group* grp = getgrgid(gid);
527 ASSERT_EQ(0, errno);
528 SCOPED_TRACE("getgrgid");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700529 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800530}
531
Tom Cherryb4c25c82018-04-04 15:02:55 -0700532static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800533 errno = 0;
534 group* grp = getgrnam(group_name);
535 ASSERT_EQ(0, errno);
536 SCOPED_TRACE("getgrnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700537 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800538}
539
Tom Cherryb4c25c82018-04-04 15:02:55 -0700540static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700541 group grp_storage;
542 char buf[512];
543 group* grp;
544
545 errno = 0;
546 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
547 ASSERT_EQ(0, result);
548 ASSERT_EQ(0, errno);
549 SCOPED_TRACE("getgrgid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700550 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700551}
552
Tom Cherryb4c25c82018-04-04 15:02:55 -0700553static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700554 group grp_storage;
555 char buf[512];
556 group* grp;
557
558 errno = 0;
559 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
560 ASSERT_EQ(0, result);
561 ASSERT_EQ(0, errno);
562 SCOPED_TRACE("getgrnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700563 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700564}
565
Tom Cherryb4c25c82018-04-04 15:02:55 -0700566static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700567 SCOPED_TRACE("groupname '"s + group_name + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700568 check_getgrgid(group_name, gid, check_groupname);
569 check_getgrnam(group_name, gid, check_groupname);
570 check_getgrgid_r(group_name, gid, check_groupname);
571 check_getgrnam_r(group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800572}
573
Tom Cherry6b116d12019-04-25 10:34:07 -0700574static void expect_no_group_id(gid_t gid) {
575 SCOPED_TRACE("gid '" + std::to_string(gid) + "'");
576 errno = 0;
577 group* group = nullptr;
578 group = getgrgid(gid);
579 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
580 EXPECT_EQ(ENOENT, errno);
581
582 struct group group_storage;
583 char buf[512];
584 EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group));
585 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
586}
587
588static void expect_no_group_name(const char* groupname) {
589 SCOPED_TRACE("groupname '"s + groupname + "'");
590 errno = 0;
591 group* group = nullptr;
592 group = getgrnam(groupname);
593 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
594 EXPECT_EQ(ENOENT, errno);
595
596 struct group group_storage;
597 char buf[512];
598 EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group));
599 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
600}
601
Yabin Cuia04c79b2014-11-18 16:14:54 -0800602#else // !defined(__BIONIC__)
603
Tom Cherryb4c25c82018-04-04 15:02:55 -0700604static void check_get_group(const char*, gid_t, bool) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800605 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700606}
607
Yabin Cuic4786d32015-07-20 19:46:26 -0700608static void check_get_group(const char*, gid_t) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800609 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700610}
611
Tom Cherry6b116d12019-04-25 10:34:07 -0700612static void expect_no_group_id(gid_t /* gid */) {
613 GTEST_SKIP() << "bionic-only test";
614}
615
616static void expect_no_group_name(const char* /* groupname */) {
617 GTEST_SKIP() << "bionic-only test";
618}
619
Yabin Cuia04c79b2014-11-18 16:14:54 -0800620#endif
621
Tom Cherry6b116d12019-04-25 10:34:07 -0700622TEST(grp, getgrnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800623 check_get_group("root", 0);
Tom Cherry6b116d12019-04-25 10:34:07 -0700624 check_get_group("daemon", 1);
625 check_get_group("bin", 2);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800626
Yabin Cuia04c79b2014-11-18 16:14:54 -0800627 check_get_group("system", 1000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800628 check_get_group("radio", 1001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800629
Tom Cherry6b116d12019-04-25 10:34:07 -0700630 check_get_group("shell", 2000);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700631
Yabin Cuia04c79b2014-11-18 16:14:54 -0800632 check_get_group("nobody", 9999);
633}
634
Tom Cherry6b116d12019-04-25 10:34:07 -0700635TEST(grp, getgrnam_oem_ids) {
636 check_get_group("oem_2900", 2900, false);
637 check_get_group("oem_2945", 2945, false);
638 check_get_group("oem_2999", 2999, false);
639 check_get_group("oem_5000", 5000, false);
640 check_get_group("oem_5454", 5454, false);
641 check_get_group("oem_5999", 5999, false);
642}
643
644TEST(grp, getgrnam_non_exist) {
645 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
646 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
647 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
648}
649
650TEST(grp, getgrnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800651 check_get_group("u0_a0", 10000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800652 check_get_group("u0_a1234", 11234);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800653 check_get_group("u0_a9999", 19999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800654
Jeff Sharkey934bc862016-12-13 14:03:19 -0700655 check_get_group("u0_a0_cache", 20000);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700656 check_get_group("u0_a1234_cache", 21234);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700657 check_get_group("u0_a9999_cache", 29999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700658
Tom Cherry6b116d12019-04-25 10:34:07 -0700659 check_get_group("u0_a0_ext", 30000);
660 check_get_group("u0_a4545_ext", 34545);
661 check_get_group("u0_a9999_ext", 39999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700662
Tom Cherry6b116d12019-04-25 10:34:07 -0700663 check_get_group("u0_a0_ext_cache", 40000);
664 check_get_group("u0_a4545_ext_cache", 44545);
665 check_get_group("u0_a9999_ext_cache", 49999);
666
667 check_get_group("all_a0", 50000);
668 check_get_group("all_a4545", 54545);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800669 check_get_group("all_a9999", 59999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800670
Martijn Coenenf9d22992019-01-16 16:25:40 +0100671 check_get_group("u0_i1", 90001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800672}
673
Tom Cherry6b116d12019-04-25 10:34:07 -0700674TEST(grp, getgrnam_u1_app_ids) {
675 check_get_group("u1_system", 101000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800676 check_get_group("u1_radio", 101001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800677
Yabin Cuia04c79b2014-11-18 16:14:54 -0800678 check_get_group("u1_a0", 110000);
Tom Cherry6b116d12019-04-25 10:34:07 -0700679 check_get_group("u1_a1234", 111234);
680 check_get_group("u1_a9999", 119999);
681
682 check_get_group("u1_a0_cache", 120000);
683 check_get_group("u1_a1234_cache", 121234);
684 check_get_group("u1_a9999_cache", 129999);
685
686 check_get_group("u1_a0_ext", 130000);
687 check_get_group("u1_a4545_ext", 134545);
688 check_get_group("u1_a9999_ext", 139999);
689
690 check_get_group("u1_a0_ext_cache", 140000);
691 check_get_group("u1_a4545_ext_cache", 144545);
692 check_get_group("u1_a9999_ext_cache", 149999);
693
694 check_get_group("u1_i1", 190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800695}
696
Tom Cherry6b116d12019-04-25 10:34:07 -0700697TEST(grp, getgrnam_u31_app_ids) {
698 check_get_group("u31_system", 3101000);
699 check_get_group("u31_radio", 3101001);
700
701 check_get_group("u31_a0", 3110000);
702 check_get_group("u31_a1234", 3111234);
703 check_get_group("u31_a9999", 3119999);
704
705 check_get_group("u31_a0_cache", 3120000);
706 check_get_group("u31_a1234_cache", 3121234);
707 check_get_group("u31_a9999_cache", 3129999);
708
709 check_get_group("u31_a0_cache", 3120000);
710 check_get_group("u31_a1234_cache", 3121234);
711 check_get_group("u31_a9999_cache", 3129999);
712
713 check_get_group("u31_a0_ext", 3130000);
714 check_get_group("u31_a4545_ext", 3134545);
715 check_get_group("u31_a9999_ext", 3139999);
716
717 check_get_group("u31_a0_ext_cache", 3140000);
718 check_get_group("u31_a4545_ext_cache", 3144545);
719 check_get_group("u31_a9999_ext_cache", 3149999);
720
721 check_get_group("u31_i1", 3190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800722}
723
Tom Cherry6b116d12019-04-25 10:34:07 -0700724TEST(grp, getpgram_app_id_not_allowed_platform) {
725 expect_no_group_name("u1_root");
726 expect_no_group_name("u1_debuggerd");
727
728 expect_no_group_name("u31_root");
729 expect_no_group_name("u31_debuggerd");
730}
731
732TEST(grp, getgrgid_app_id_u1_non_exist) {
733 expect_no_group_id(100000); // There is no 'root' for secondary users.
734 expect_no_group_id(101999); // End of the system reserved range, unallocated.
735 expect_no_group_id(102900); // The OEM ranges were never allocated to secondary users.
736 expect_no_group_id(105000); // The OEM ranges were never allocated to secondary users.
737
738 // The shared range is shared among users, and therefore doesn't exist for secondary users.
739 expect_no_group_id(150000);
740}
741
742TEST(grp, getgrgid_app_id_u31_non_exist) {
743 expect_no_group_id(3100000); // There is no 'root' for secondary users.
744 expect_no_group_id(3101999); // End of the system reserved range, unallocated.
745 expect_no_group_id(3102900); // The OEM ranges were never allocated to secondary users.
746 expect_no_group_id(3105000); // The OEM ranges were never allocated to secondary users.
747
748 // The shared range is shared among users, and therefore doesn't exist for secondary users.
749 expect_no_group_id(3150000);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700750}
Yabin Cuic4786d32015-07-20 19:46:26 -0700751
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700752TEST(grp, getgrnam_r_alignment) {
753#if defined(__BIONIC__)
754 group grp_storage;
755 alignas(16) char buf[512];
756 group* grp;
757 int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
758 ASSERT_EQ(0, result);
759 check_group(grp, "root", 0);
760#else
761 GTEST_SKIP() << "bionic-only test";
762#endif
763}
764
765TEST(grp, getgrgid_r_alignment) {
766#if defined(__BIONIC__)
767 group grp_storage;
768 alignas(16) char buf[512];
769 group* grp;
770 int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
771 ASSERT_EQ(0, result);
772 check_group(grp, "root", 0);
773#else
774 GTEST_SKIP() << "bionic-only test";
775#endif
776}
777
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800778TEST(grp, getgrnam_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700779#if defined(__BIONIC__)
780 group grp_storage[2];
781 char buf[2][512];
782 group* grp[3];
783 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
784 ASSERT_EQ(0, result);
785 check_group(grp[0], "root", 0);
786 grp[1] = getgrnam("system");
787 check_group(grp[1], "system", 1000);
788 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
789 ASSERT_EQ(0, result);
790 check_group(grp[2], "radio", 1001);
791 check_group(grp[0], "root", 0);
792 check_group(grp[1], "system", 1000);
793#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800794 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700795#endif
796}
797
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800798TEST(grp, getgrgid_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700799#if defined(__BIONIC__)
800 group grp_storage[2];
801 char buf[2][512];
802 group* grp[3];
803 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
804 ASSERT_EQ(0, result);
805 check_group(grp[0], "root", 0);
806 grp[1] = getgrgid(1000);
807 check_group(grp[1], "system", 1000);
808 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
809 ASSERT_EQ(0, result);
810 check_group(grp[2], "radio", 1001);
811 check_group(grp[0], "root", 0);
812 check_group(grp[1], "system", 1000);
813#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800814 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700815#endif
816}
817
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800818TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700819 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
820 ASSERT_GT(size, 0);
821 char buf[size];
822 group grp_storage;
823 group* grp;
824 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
825 check_group(grp, "root", 0);
826}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700827
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800828TEST(grp, getgrent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800829#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700830 group* grp;
Tom Cherry4362f892017-11-14 08:50:43 -0800831 std::set<gid_t> gids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700832
833 setgrent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700834 while ((grp = getgrent()) != nullptr) {
835 ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid;
836 ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800837 EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700838 EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800839
Tom Cherry0816c902020-04-10 13:00:42 -0700840 EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800841 gids.emplace(grp->gr_gid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700842 }
843 endgrent();
844
Tom Cherry6b116d12019-04-25 10:34:07 -0700845 expect_ids(gids, true);
Josh Gao2fe10342018-02-27 14:05:53 -0800846#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800847 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800848#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700849}
Tom Cherrye88b4082018-05-24 14:44:10 -0700850
Elliott Hughes7cebf832020-08-12 14:25:41 -0700851TEST(grp, getgrouplist) {
852#if defined(__BIONIC__)
853 // Query the number of groups.
854 int ngroups = 0;
855 ASSERT_EQ(-1, getgrouplist("root", 123, nullptr, &ngroups));
856 ASSERT_EQ(1, ngroups);
857
858 // Query the specific groups (just the one you pass in on Android).
859 ngroups = 8;
860 gid_t groups[ngroups];
861 ASSERT_EQ(1, getgrouplist("root", 123, groups, &ngroups));
862 ASSERT_EQ(1, ngroups);
863 ASSERT_EQ(123u, groups[0]);
864#else
865 GTEST_SKIP() << "bionic-only test (groups too unpredictable)";
866#endif
867}
868
Tom Cherrye88b4082018-05-24 14:44:10 -0700869#if defined(__BIONIC__)
870static void TestAidNamePrefix(const std::string& file_path) {
871 std::string file_contents;
872 if (!ReadFileToString(file_path, &file_contents)) {
873 // If we cannot read this file, then there are no vendor defind AID names, in which case this
874 // test passes by default.
875 return;
876 }
877 auto lines = Split(file_contents, "\n");
878 for (const auto& line : lines) {
879 if (line.empty()) continue;
880 auto name = Split(line, ":")[0];
881 EXPECT_TRUE(StartsWith(name, "vendor_"));
882 }
883}
884#endif
885
886TEST(pwd, vendor_prefix_users) {
887#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800888 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
889 return;
890 }
891
Tom Cherrye88b4082018-05-24 14:44:10 -0700892 TestAidNamePrefix("/vendor/etc/passwd");
893#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800894 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700895#endif
896}
897
898TEST(pwd, vendor_prefix_groups) {
899#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800900 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
901 return;
902 }
903
Tom Cherrye88b4082018-05-24 14:44:10 -0700904 TestAidNamePrefix("/vendor/etc/group");
905#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800906 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700907#endif
908}