blob: 2c22081262cca73ab796c789d36045b4d67cc11d [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
Elliott Hughes95646e62023-09-21 14:11:19 -070044#include "utils.h"
45
Tom Cherry4362f892017-11-14 08:50:43 -080046using android::base::Join;
Tom Cherrye88b4082018-05-24 14:44:10 -070047using android::base::ReadFileToString;
48using android::base::Split;
49using android::base::StartsWith;
Tom Cherry4362f892017-11-14 08:50:43 -080050
Tom Cherry6b116d12019-04-25 10:34:07 -070051using namespace std::literals;
52
Yabin Cuia04c79b2014-11-18 16:14:54 -080053enum uid_type_t {
Tom Cherryfa5f61c2018-09-27 13:19:02 -070054 TYPE_APP,
Kenny Root2a54e5e2012-09-13 10:52:52 -070055 TYPE_SYSTEM,
Tom Cherryfa5f61c2018-09-27 13:19:02 -070056 TYPE_VENDOR,
Yabin Cuia04c79b2014-11-18 16:14:54 -080057};
Kenny Root2a54e5e2012-09-13 10:52:52 -070058
Yabin Cuia04c79b2014-11-18 16:14:54 -080059#if defined(__BIONIC__)
60
Tom Cherryb4c25c82018-04-04 15:02:55 -070061static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type,
62 bool check_username) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070063 ASSERT_TRUE(pwd != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -070064 if (check_username) {
65 EXPECT_STREQ(username, pwd->pw_name);
66 }
Tom Cherry2c05c0f2017-11-10 10:57:21 -080067 EXPECT_EQ(uid, pwd->pw_uid);
68 EXPECT_EQ(uid, pwd->pw_gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -070069 EXPECT_EQ(nullptr, pwd->pw_passwd);
Calin Juravlec7688742014-05-09 21:50:53 +010070#ifdef __LP64__
Yi Kong32bc0fc2018-08-02 17:31:13 -070071 EXPECT_EQ(nullptr, pwd->pw_gecos);
Calin Juravlec7688742014-05-09 21:50:53 +010072#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -070073
Tom Cherryfa5f61c2018-09-27 13:19:02 -070074 if (uid_type == TYPE_APP) {
Tom Cherry2c05c0f2017-11-10 10:57:21 -080075 EXPECT_STREQ("/data", pwd->pw_dir);
Tom Cherryfa5f61c2018-09-27 13:19:02 -070076 } else {
77 EXPECT_STREQ("/", pwd->pw_dir);
Kenny Root2a54e5e2012-09-13 10:52:52 -070078 }
Tom Cherryfa5f61c2018-09-27 13:19:02 -070079
Tom Cherryb9fa04d2020-07-10 10:40:21 -070080 // This has changed over time and that causes new GSI + old vendor images testing to fail.
81 // This parameter doesn't matter on Android, so simply ignore its value for older vendor images.
82 if (android::base::GetIntProperty("ro.product.first_api_level", 0) >= 30) {
83 EXPECT_STREQ("/bin/sh", pwd->pw_shell);
84 }
Kenny Root2a54e5e2012-09-13 10:52:52 -070085}
Yabin Cuia04c79b2014-11-18 16:14:54 -080086
Tom Cherryb4c25c82018-04-04 15:02:55 -070087static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type,
88 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080089 errno = 0;
90 passwd* pwd = getpwuid(uid);
Elliott Hughes95646e62023-09-21 14:11:19 -070091 ASSERT_ERRNO(0);
Yabin Cuia04c79b2014-11-18 16:14:54 -080092 SCOPED_TRACE("getpwuid");
Tom Cherryb4c25c82018-04-04 15:02:55 -070093 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -080094}
95
Tom Cherryb4c25c82018-04-04 15:02:55 -070096static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type,
97 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080098 errno = 0;
99 passwd* pwd = getpwnam(username);
Elliott Hughes95646e62023-09-21 14:11:19 -0700100 ASSERT_ERRNO(0);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800101 SCOPED_TRACE("getpwnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700102 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800103}
104
Tom Cherryb4c25c82018-04-04 15:02:55 -0700105static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type,
106 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800107 passwd pwd_storage;
108 char buf[512];
109 int result;
110
111 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700112 passwd* pwd = nullptr;
Yabin Cuia04c79b2014-11-18 16:14:54 -0800113 result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
114 ASSERT_EQ(0, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700115 ASSERT_ERRNO(0);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800116 SCOPED_TRACE("getpwuid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700117 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800118}
119
Tom Cherryb4c25c82018-04-04 15:02:55 -0700120static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type,
121 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800122 passwd pwd_storage;
123 char buf[512];
124 int result;
125
126 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700127 passwd* pwd = nullptr;
Yabin Cuia04c79b2014-11-18 16:14:54 -0800128 result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
129 ASSERT_EQ(0, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700130 ASSERT_ERRNO(0);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800131 SCOPED_TRACE("getpwnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700132 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800133}
134
Tom Cherryb4c25c82018-04-04 15:02:55 -0700135static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type,
136 bool check_username = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700137 SCOPED_TRACE("username '"s + username + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700138 check_getpwuid(username, uid, uid_type, check_username);
139 check_getpwnam(username, uid, uid_type, check_username);
140 check_getpwuid_r(username, uid, uid_type, check_username);
141 check_getpwnam_r(username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800142}
143
Tom Cherry6b116d12019-04-25 10:34:07 -0700144static void expect_no_passwd_id(uid_t uid) {
145 SCOPED_TRACE("uid '" + std::to_string(uid) + "'");
146 errno = 0;
147 passwd* passwd = nullptr;
148 passwd = getpwuid(uid);
149 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
Elliott Hughes95646e62023-09-21 14:11:19 -0700150 EXPECT_ERRNO(ENOENT);
Tom Cherry6b116d12019-04-25 10:34:07 -0700151
152 struct passwd passwd_storage;
153 char buf[512];
154 EXPECT_EQ(ENOENT, getpwuid_r(uid, &passwd_storage, buf, sizeof(buf), &passwd));
155 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
156}
157
158static void expect_no_passwd_name(const char* username) {
159 SCOPED_TRACE("username '"s + username + "'");
160 errno = 0;
161 passwd* passwd = nullptr;
162 passwd = getpwnam(username);
163 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
Elliott Hughes95646e62023-09-21 14:11:19 -0700164 EXPECT_ERRNO(ENOENT);
Tom Cherry6b116d12019-04-25 10:34:07 -0700165
166 struct passwd passwd_storage;
167 char buf[512];
168 EXPECT_EQ(ENOENT, getpwnam_r(username, &passwd_storage, buf, sizeof(buf), &passwd));
169 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
170}
171
Yabin Cuia04c79b2014-11-18 16:14:54 -0800172#else // !defined(__BIONIC__)
173
Tom Cherryb4c25c82018-04-04 15:02:55 -0700174static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */,
175 bool /* check_username */) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800176 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700177}
178
Josh Gao2fe10342018-02-27 14:05:53 -0800179static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800180 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800181}
182
Tom Cherry6b116d12019-04-25 10:34:07 -0700183static void expect_no_passwd_id(uid_t /* uid */) {
184 GTEST_SKIP() << "bionic-only test";
185}
186
187static void expect_no_passwd_name(const char* /* username */) {
188 GTEST_SKIP() << "bionic-only test";
189}
190
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800191#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -0700192
Tom Cherry6b116d12019-04-25 10:34:07 -0700193TEST(pwd, getpwnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800194 check_get_passwd("root", 0, TYPE_SYSTEM);
Tom Cherry6b116d12019-04-25 10:34:07 -0700195 check_get_passwd("daemon", 1, TYPE_SYSTEM);
196 check_get_passwd("bin", 2, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700197
Yabin Cuia04c79b2014-11-18 16:14:54 -0800198 check_get_passwd("system", 1000, TYPE_SYSTEM);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800199 check_get_passwd("radio", 1001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700200
Tom Cherry6b116d12019-04-25 10:34:07 -0700201 check_get_passwd("shell", 2000, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700202
Yabin Cuia04c79b2014-11-18 16:14:54 -0800203 check_get_passwd("nobody", 9999, TYPE_SYSTEM);
Kenny Root8a05a012012-09-13 14:31:50 -0700204}
205
Tom Cherry6b116d12019-04-25 10:34:07 -0700206TEST(pwd, getpwnam_oem_ids) {
207 check_get_passwd("oem_2900", 2900, TYPE_VENDOR, false);
208 check_get_passwd("oem_2945", 2945, TYPE_VENDOR, false);
209 check_get_passwd("oem_2999", 2999, TYPE_VENDOR, false);
210 check_get_passwd("oem_5000", 5000, TYPE_VENDOR, false);
211 check_get_passwd("oem_5454", 5454, TYPE_VENDOR, false);
212 check_get_passwd("oem_5999", 5999, TYPE_VENDOR, false);
213}
214
215TEST(pwd, getpwnam_non_exist) {
216 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
217 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
218 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
219
220 // These ranges are for GIDs only.
221 expect_no_passwd_id(20000);
222 expect_no_passwd_id(30000);
223 expect_no_passwd_id(40000);
224 expect_no_passwd_id(50000);
225
226 // These should not be parsed as users, only as groups.
227 expect_no_passwd_name("u0_a9999_cache");
228 expect_no_passwd_name("u0_a9999_ext");
229 expect_no_passwd_name("u0_a9999_ext_cache");
230 expect_no_passwd_name("all_a9999");
231}
232
233TEST(pwd, getpwnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800234 check_get_passwd("u0_a0", 10000, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800235 check_get_passwd("u0_a1234", 11234, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700236 check_get_passwd("u0_a9999", 19999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700237
Martijn Coenenf9d22992019-01-16 16:25:40 +0100238 check_get_passwd("u0_i1", 90001, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700239 check_get_passwd("u0_i4545", 94545, TYPE_APP);
240 check_get_passwd("u0_i9999", 99999, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800241}
242
Tom Cherry6b116d12019-04-25 10:34:07 -0700243TEST(pwd, getpwnam_app_id_u1_ids) {
244 check_get_passwd("u1_system", 101000, TYPE_SYSTEM);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800245 check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700246
Yabin Cuia04c79b2014-11-18 16:14:54 -0800247 check_get_passwd("u1_a0", 110000, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700248 check_get_passwd("u1_a1234", 111234, TYPE_APP);
249 check_get_passwd("u1_a9999", 119999, TYPE_APP);
250
251 check_get_passwd("u1_i1", 190001, TYPE_APP);
252 check_get_passwd("u1_i4545", 194545, TYPE_APP);
253 check_get_passwd("u1_i9999", 199999, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800254}
255
Tom Cherry6b116d12019-04-25 10:34:07 -0700256TEST(pwd, getpwnam_app_id_u31_ids) {
257 check_get_passwd("u31_system", 3101000, TYPE_SYSTEM);
258 check_get_passwd("u31_radio", 3101001, TYPE_SYSTEM);
259
260 check_get_passwd("u31_a0", 3110000, TYPE_APP);
261 check_get_passwd("u31_a1234", 3111234, TYPE_APP);
262 check_get_passwd("u31_a9999", 3119999, TYPE_APP);
263
264 check_get_passwd("u31_i1", 3190001, TYPE_APP);
265 check_get_passwd("u31_i4545", 3194545, TYPE_APP);
266 check_get_passwd("u31_i9999", 3199999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700267}
268
Tom Cherry6b116d12019-04-25 10:34:07 -0700269TEST(pwd, getpwnam_app_id_not_allowed_platform) {
270 expect_no_passwd_name("u1_root");
271 expect_no_passwd_name("u1_debuggerd");
272
273 expect_no_passwd_name("u31_root");
274 expect_no_passwd_name("u31_debuggerd");
275}
276
277TEST(pwd, getpwuid_app_id_u1_non_exist) {
278 expect_no_passwd_id(100000); // There is no 'root' for secondary users.
279 expect_no_passwd_id(101999); // End of the system reserved range, unallocated.
280 expect_no_passwd_id(102900); // The OEM ranges were never allocated to secondary users.
281 expect_no_passwd_id(105000); // The OEM ranges were never allocated to secondary users.
282
283 // These ranges are for GIDs only.
284 expect_no_passwd_id(120000);
285 expect_no_passwd_id(130000);
286 expect_no_passwd_id(140000);
287 expect_no_passwd_id(150000);
288}
289
290TEST(pwd, getpwuid_app_id_u31_non_exist) {
291 expect_no_passwd_id(3100000); // There is no 'root' for secondary users.
292 expect_no_passwd_id(3101999); // End of the system reserved range, unallocated.
293 expect_no_passwd_id(3102900); // The OEM ranges were never allocated to secondary users.
294 expect_no_passwd_id(3105000); // The OEM ranges were never allocated to secondary users.
295
296 // These ranges are for GIDs only.
297 expect_no_passwd_id(3120000);
298 expect_no_passwd_id(3130000);
299 expect_no_passwd_id(3140000);
300 expect_no_passwd_id(3150000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800301}
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700302
303TEST(pwd, getpwnam_r_alignment) {
304#if defined(__BIONIC__)
305 passwd pwd_storage;
306 alignas(16) char buf[512];
307 passwd* pwd;
308 int result = getpwnam_r("root", &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
309 ASSERT_EQ(0, result);
310 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
311#else
312 GTEST_SKIP() << "bionic-only test";
313#endif
314}
315
316TEST(pwd, getpwuid_r_alignment) {
317#if defined(__BIONIC__)
318 passwd pwd_storage;
319 alignas(16) char buf[512];
320 passwd* pwd;
321 int result = getpwuid_r(0, &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
322 ASSERT_EQ(0, result);
323 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
324#else
325 GTEST_SKIP() << "bionic-only test";
326#endif
327}
328
329TEST(pwd, getpwnam_r_reentrancy) {
330#if defined(__BIONIC__)
331 passwd pwd_storage[2];
332 char buf[2][512];
333 passwd* pwd[3];
334 int result = getpwnam_r("root", &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
335 ASSERT_EQ(0, result);
336 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
337 pwd[1] = getpwnam("system");
338 ASSERT_NE(nullptr, pwd[1]);
339 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
340 result = getpwnam_r("radio", &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
341 ASSERT_EQ(0, result);
342 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
343 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
344 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
345#else
346 GTEST_SKIP() << "bionic-only test";
347#endif
348}
349
350TEST(pwd, getpwuid_r_reentrancy) {
351#if defined(__BIONIC__)
352 passwd pwd_storage[2];
353 char buf[2][512];
354 passwd* pwd[3];
355 int result = getpwuid_r(0, &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
356 ASSERT_EQ(0, result);
357 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
358 pwd[1] = getpwuid(1000);
359 ASSERT_NE(nullptr, pwd[1]);
360 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
361 result = getpwuid_r(1001, &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
362 ASSERT_EQ(0, result);
363 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
364 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
365 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
366#else
367 GTEST_SKIP() << "bionic-only test";
368#endif
369}
370
371TEST(pwd, getpwnam_r_large_enough_suggested_buffer_size) {
372#if defined(__BIONIC__)
373 long size = sysconf(_SC_GETPW_R_SIZE_MAX);
374 ASSERT_GT(size, 0);
375 char buf[size];
376 passwd pwd_storage;
377 passwd* pwd;
378 ASSERT_EQ(0, getpwnam_r("root", &pwd_storage, buf, size, &pwd));
379 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
380#else
381 GTEST_SKIP() << "bionic-only test";
382#endif
383}
384
Tom Cherry5c941432018-10-09 11:01:28 -0700385#if defined(__BIONIC__)
Tom Cherry4362f892017-11-14 08:50:43 -0800386template <typename T>
Tom Cherry777b34d2019-02-17 09:38:23 -0800387static void expect_ids(T ids, bool is_group) {
Tom Cherry4362f892017-11-14 08:50:43 -0800388 std::set<typename T::key_type> expected_ids;
389 // Ensure that all android_ids are iterated through.
390 for (size_t n = 0; n < android_id_count; ++n) {
391 EXPECT_EQ(1U, ids.count(android_ids[n].aid)) << "android_ids[n].aid: " << android_ids[n].aid;
392 expected_ids.emplace(android_ids[n].aid);
393 }
394
395 auto expect_range = [&ids, &expected_ids](uid_t start, uid_t end) {
396 for (size_t n = start; n <= end; ++n) {
397 EXPECT_EQ(1U, ids.count(n)) << "n: " << n;
398 expected_ids.emplace(n);
399 }
400 };
401
402 // Ensure that all reserved ranges are iterated through.
403 expect_range(AID_OEM_RESERVED_START, AID_OEM_RESERVED_END);
404 expect_range(AID_OEM_RESERVED_2_START, AID_OEM_RESERVED_2_END);
405 expect_range(AID_APP_START, AID_APP_END);
Tom Cherry6b116d12019-04-25 10:34:07 -0700406 if (is_group) {
407 expect_range(AID_CACHE_GID_START, AID_CACHE_GID_END);
408 expect_range(AID_EXT_GID_START, AID_EXT_GID_END);
409 expect_range(AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END);
410 expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
411 }
Tom Cherry4362f892017-11-14 08:50:43 -0800412 expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
413
Tom Cherry6f2e8102020-04-10 10:50:09 -0700414 // Prior to R, we didn't have a mechanism to create vendor AIDs in the system or other non-vendor
415 // partitions, therefore we disabled the rest of these checks for older API levels.
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800416 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 29) {
Tom Cherry5c941432018-10-09 11:01:28 -0700417 return;
418 }
419
Tom Cherry777b34d2019-02-17 09:38:23 -0800420 auto allow_range = [&ids](uid_t start, uid_t end) {
421 for (size_t n = start; n <= end; ++n) {
422 ids.erase(n);
423 }
424 };
425
426 allow_range(AID_SYSTEM_RESERVED_START, AID_SYSTEM_EXT_RESERVED_END);
427
Tom Cherry4362f892017-11-14 08:50:43 -0800428 // Ensure that no other ids were returned.
429 auto return_differences = [&ids, &expected_ids] {
430 std::vector<typename T::key_type> missing_from_ids;
431 std::set_difference(expected_ids.begin(), expected_ids.end(), ids.begin(), ids.end(),
432 std::inserter(missing_from_ids, missing_from_ids.begin()));
433 std::vector<typename T::key_type> extra_in_ids;
434 std::set_difference(ids.begin(), ids.end(), expected_ids.begin(), expected_ids.end(),
435 std::inserter(extra_in_ids, extra_in_ids.begin()));
436 std::string result;
437 if (!missing_from_ids.empty()) {
438 result += "Missing ids from results: " + Join(missing_from_ids, " ");
439 }
440 if (!extra_in_ids.empty()) {
441 if (!result.empty()) result += ", ";
442 result += "Extra ids in results: " + Join(extra_in_ids, " ");
443 }
444 return result;
445 };
Orion Hodsonf5fd5ad2022-10-27 10:28:24 +0100446
Elliott Hughes618ce1b2024-09-10 16:01:50 +0000447 // AID_UPROBESTATS (1093) was added in API level 35, but "trunk stable" means
448 // that the 2024Q* builds are tested with the _previous_ release's CTS.
449 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 34) {
Elliott Hughes140e4d32024-02-10 00:39:07 +0000450#if !defined(AID_UPROBESTATS)
451#define AID_UPROBESTATS 1093
Orion Hodsonf5fd5ad2022-10-27 10:28:24 +0100452#endif
Elliott Hughes140e4d32024-02-10 00:39:07 +0000453 ids.erase(AID_UPROBESTATS);
454 expected_ids.erase(AID_UPROBESTATS);
455 if (getpwuid(AID_UPROBESTATS)) {
456 EXPECT_STREQ(getpwuid(AID_UPROBESTATS)->pw_name, "uprobestats");
457 }
Orion Hodsonf5fd5ad2022-10-27 10:28:24 +0100458 }
Elliott Hughes618ce1b2024-09-10 16:01:50 +0000459 // AID_VIRTUALMACHINE (3013) was added in API level 35, but "trunk stable" means
460 // that the 2024Q* builds are tested with the _previous_ release's CTS.
461 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 34) {
Elliott Hughes140e4d32024-02-10 00:39:07 +0000462#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 }
Elliott Hughes618ce1b2024-09-10 16:01:50 +0000471 // AID_CROS_EC (1094) was added in API level 36, but "trunk stable" means
472 // that the 2024Q* builds are tested with the _previous_ release's CTS.
473 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 35) {
474#if !defined(AID_CROS_EC)
475#define AID_CROS_EC 1094
476#endif
477 ids.erase(AID_CROS_EC);
478 expected_ids.erase(AID_CROS_EC);
479 if (getpwuid(AID_CROS_EC)) {
480 EXPECT_STREQ(getpwuid(AID_CROS_EC)->pw_name, "cros_ec");
481 }
482 }
Hung Nguyen08fc8bd2024-11-11 11:27:23 -0800483 // AID_MMD (1095) was added in API level 36, but "trunk stable" means
484 // that the 2024Q* builds are tested with the _previous_ release's CTS.
485 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 35) {
486#if !defined(AID_MMD)
487#define AID_MMD 1095
488#endif
489 ids.erase(AID_MMD);
490 expected_ids.erase(AID_MMD);
491 if (getpwuid(AID_MMD)) {
492 EXPECT_STREQ(getpwuid(AID_MMD)->pw_name, "mmd");
493 }
494 }
HÃ¥kan Kvist88da6022025-01-22 10:25:03 +0100495 // AID_UPDATE_ENGINE_LOG (1096) was added in API level 36, but "trunk stable" means
496 // that the 2024Q* builds are tested with the _previous_ release's CTS.
497 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 35) {
498#if !defined(AID_UPDATE_ENGINE_LOG)
499#define AID_UPDATE_ENGINE_LOG 1096
500#endif
501 ids.erase(AID_UPDATE_ENGINE_LOG);
502 expected_ids.erase(AID_UPDATE_ENGINE_LOG);
503 if (getpwuid(AID_UPDATE_ENGINE_LOG)) {
504 EXPECT_STREQ(getpwuid(AID_UPDATE_ENGINE_LOG)->pw_name, "update_engine_log");
505 }
506 }
Elliott Hughes140e4d32024-02-10 00:39:07 +0000507
Tom Cherry4362f892017-11-14 08:50:43 -0800508 EXPECT_EQ(expected_ids, ids) << return_differences();
509}
Tom Cherry5c941432018-10-09 11:01:28 -0700510#endif
Tom Cherry4362f892017-11-14 08:50:43 -0800511
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800512TEST(pwd, getpwent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800513#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700514 passwd* pwd;
Tom Cherry4362f892017-11-14 08:50:43 -0800515 std::set<uid_t> uids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700516
517 setpwent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700518 while ((pwd = getpwent()) != nullptr) {
519 ASSERT_TRUE(nullptr != pwd->pw_name);
Tom Cherry4362f892017-11-14 08:50:43 -0800520
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800521 EXPECT_EQ(pwd->pw_gid, pwd->pw_uid) << "pwd->pw_uid: " << pwd->pw_uid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700522 EXPECT_EQ(nullptr, pwd->pw_passwd) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700523#ifdef __LP64__
Yi Kong32bc0fc2018-08-02 17:31:13 -0700524 EXPECT_TRUE(nullptr == pwd->pw_gecos) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700525#endif
Yi Kong32bc0fc2018-08-02 17:31:13 -0700526 EXPECT_TRUE(nullptr != pwd->pw_shell);
Tom Cherry4362f892017-11-14 08:50:43 -0800527 if (pwd->pw_uid < AID_APP_START || pwd->pw_uid == AID_OVERFLOWUID) {
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800528 EXPECT_STREQ("/", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
Tom Cherry4362f892017-11-14 08:50:43 -0800529 } else {
530 EXPECT_STREQ("/data", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700531 }
Tom Cherry4362f892017-11-14 08:50:43 -0800532
Tom Cherry0816c902020-04-10 13:00:42 -0700533 EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid;
Tom Cherry4362f892017-11-14 08:50:43 -0800534 uids.emplace(pwd->pw_uid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700535 }
536 endpwent();
537
Tom Cherry6b116d12019-04-25 10:34:07 -0700538 expect_ids(uids, false);
Josh Gao2fe10342018-02-27 14:05:53 -0800539#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800540 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800541#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700542}
543
Tom Cherryb4c25c82018-04-04 15:02:55 -0700544static void check_group(const group* grp, const char* group_name, gid_t gid,
545 bool check_groupname = true) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700546 ASSERT_TRUE(grp != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700547 if (check_groupname) {
548 EXPECT_STREQ(group_name, grp->gr_name);
549 }
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800550 EXPECT_EQ(gid, grp->gr_gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700551 ASSERT_TRUE(grp->gr_mem != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700552 if (check_groupname) {
553 EXPECT_STREQ(group_name, grp->gr_mem[0]);
554 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700555 EXPECT_TRUE(grp->gr_mem[1] == nullptr);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800556}
557
Yabin Cuic4786d32015-07-20 19:46:26 -0700558#if defined(__BIONIC__)
559
Tom Cherryb4c25c82018-04-04 15:02:55 -0700560static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800561 errno = 0;
562 group* grp = getgrgid(gid);
Elliott Hughes95646e62023-09-21 14:11:19 -0700563 ASSERT_ERRNO(0);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800564 SCOPED_TRACE("getgrgid");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700565 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800566}
567
Tom Cherryb4c25c82018-04-04 15:02:55 -0700568static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800569 errno = 0;
570 group* grp = getgrnam(group_name);
Elliott Hughes95646e62023-09-21 14:11:19 -0700571 ASSERT_ERRNO(0);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800572 SCOPED_TRACE("getgrnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700573 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800574}
575
Tom Cherryb4c25c82018-04-04 15:02:55 -0700576static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700577 group grp_storage;
578 char buf[512];
579 group* grp;
580
581 errno = 0;
582 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
583 ASSERT_EQ(0, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700584 ASSERT_ERRNO(0);
Yabin Cuic4786d32015-07-20 19:46:26 -0700585 SCOPED_TRACE("getgrgid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700586 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700587}
588
Tom Cherryb4c25c82018-04-04 15:02:55 -0700589static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700590 group grp_storage;
591 char buf[512];
592 group* grp;
593
594 errno = 0;
595 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
596 ASSERT_EQ(0, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700597 ASSERT_ERRNO(0);
Yabin Cuic4786d32015-07-20 19:46:26 -0700598 SCOPED_TRACE("getgrnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700599 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700600}
601
Tom Cherryb4c25c82018-04-04 15:02:55 -0700602static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700603 SCOPED_TRACE("groupname '"s + group_name + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700604 check_getgrgid(group_name, gid, check_groupname);
605 check_getgrnam(group_name, gid, check_groupname);
606 check_getgrgid_r(group_name, gid, check_groupname);
607 check_getgrnam_r(group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800608}
609
Tom Cherry6b116d12019-04-25 10:34:07 -0700610static void expect_no_group_id(gid_t gid) {
611 SCOPED_TRACE("gid '" + std::to_string(gid) + "'");
612 errno = 0;
613 group* group = nullptr;
614 group = getgrgid(gid);
615 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
Elliott Hughes95646e62023-09-21 14:11:19 -0700616 EXPECT_ERRNO(ENOENT);
Tom Cherry6b116d12019-04-25 10:34:07 -0700617
618 struct group group_storage;
619 char buf[512];
620 EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group));
621 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
622}
623
624static void expect_no_group_name(const char* groupname) {
625 SCOPED_TRACE("groupname '"s + groupname + "'");
626 errno = 0;
627 group* group = nullptr;
628 group = getgrnam(groupname);
629 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
Elliott Hughes95646e62023-09-21 14:11:19 -0700630 EXPECT_ERRNO(ENOENT);
Tom Cherry6b116d12019-04-25 10:34:07 -0700631
632 struct group group_storage;
633 char buf[512];
634 EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group));
635 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
636}
637
Yabin Cuia04c79b2014-11-18 16:14:54 -0800638#else // !defined(__BIONIC__)
639
Tom Cherryb4c25c82018-04-04 15:02:55 -0700640static void check_get_group(const char*, gid_t, bool) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800641 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700642}
643
Yabin Cuic4786d32015-07-20 19:46:26 -0700644static void check_get_group(const char*, gid_t) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800645 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700646}
647
Tom Cherry6b116d12019-04-25 10:34:07 -0700648static void expect_no_group_id(gid_t /* gid */) {
649 GTEST_SKIP() << "bionic-only test";
650}
651
652static void expect_no_group_name(const char* /* groupname */) {
653 GTEST_SKIP() << "bionic-only test";
654}
655
Yabin Cuia04c79b2014-11-18 16:14:54 -0800656#endif
657
Tom Cherry6b116d12019-04-25 10:34:07 -0700658TEST(grp, getgrnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800659 check_get_group("root", 0);
Tom Cherry6b116d12019-04-25 10:34:07 -0700660 check_get_group("daemon", 1);
661 check_get_group("bin", 2);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800662
Yabin Cuia04c79b2014-11-18 16:14:54 -0800663 check_get_group("system", 1000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800664 check_get_group("radio", 1001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800665
Tom Cherry6b116d12019-04-25 10:34:07 -0700666 check_get_group("shell", 2000);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700667
Yabin Cuia04c79b2014-11-18 16:14:54 -0800668 check_get_group("nobody", 9999);
669}
670
Tom Cherry6b116d12019-04-25 10:34:07 -0700671TEST(grp, getgrnam_oem_ids) {
672 check_get_group("oem_2900", 2900, false);
673 check_get_group("oem_2945", 2945, false);
674 check_get_group("oem_2999", 2999, false);
675 check_get_group("oem_5000", 5000, false);
676 check_get_group("oem_5454", 5454, false);
677 check_get_group("oem_5999", 5999, false);
678}
679
680TEST(grp, getgrnam_non_exist) {
681 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
682 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
683 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
684}
685
686TEST(grp, getgrnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800687 check_get_group("u0_a0", 10000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800688 check_get_group("u0_a1234", 11234);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800689 check_get_group("u0_a9999", 19999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800690
Jeff Sharkey934bc862016-12-13 14:03:19 -0700691 check_get_group("u0_a0_cache", 20000);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700692 check_get_group("u0_a1234_cache", 21234);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700693 check_get_group("u0_a9999_cache", 29999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700694
Tom Cherry6b116d12019-04-25 10:34:07 -0700695 check_get_group("u0_a0_ext", 30000);
696 check_get_group("u0_a4545_ext", 34545);
697 check_get_group("u0_a9999_ext", 39999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700698
Tom Cherry6b116d12019-04-25 10:34:07 -0700699 check_get_group("u0_a0_ext_cache", 40000);
700 check_get_group("u0_a4545_ext_cache", 44545);
701 check_get_group("u0_a9999_ext_cache", 49999);
702
703 check_get_group("all_a0", 50000);
704 check_get_group("all_a4545", 54545);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800705 check_get_group("all_a9999", 59999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800706
Martijn Coenenf9d22992019-01-16 16:25:40 +0100707 check_get_group("u0_i1", 90001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800708}
709
Tom Cherry6b116d12019-04-25 10:34:07 -0700710TEST(grp, getgrnam_u1_app_ids) {
711 check_get_group("u1_system", 101000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800712 check_get_group("u1_radio", 101001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800713
Yabin Cuia04c79b2014-11-18 16:14:54 -0800714 check_get_group("u1_a0", 110000);
Tom Cherry6b116d12019-04-25 10:34:07 -0700715 check_get_group("u1_a1234", 111234);
716 check_get_group("u1_a9999", 119999);
717
718 check_get_group("u1_a0_cache", 120000);
719 check_get_group("u1_a1234_cache", 121234);
720 check_get_group("u1_a9999_cache", 129999);
721
722 check_get_group("u1_a0_ext", 130000);
723 check_get_group("u1_a4545_ext", 134545);
724 check_get_group("u1_a9999_ext", 139999);
725
726 check_get_group("u1_a0_ext_cache", 140000);
727 check_get_group("u1_a4545_ext_cache", 144545);
728 check_get_group("u1_a9999_ext_cache", 149999);
729
730 check_get_group("u1_i1", 190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800731}
732
Tom Cherry6b116d12019-04-25 10:34:07 -0700733TEST(grp, getgrnam_u31_app_ids) {
734 check_get_group("u31_system", 3101000);
735 check_get_group("u31_radio", 3101001);
736
737 check_get_group("u31_a0", 3110000);
738 check_get_group("u31_a1234", 3111234);
739 check_get_group("u31_a9999", 3119999);
740
741 check_get_group("u31_a0_cache", 3120000);
742 check_get_group("u31_a1234_cache", 3121234);
743 check_get_group("u31_a9999_cache", 3129999);
744
745 check_get_group("u31_a0_cache", 3120000);
746 check_get_group("u31_a1234_cache", 3121234);
747 check_get_group("u31_a9999_cache", 3129999);
748
749 check_get_group("u31_a0_ext", 3130000);
750 check_get_group("u31_a4545_ext", 3134545);
751 check_get_group("u31_a9999_ext", 3139999);
752
753 check_get_group("u31_a0_ext_cache", 3140000);
754 check_get_group("u31_a4545_ext_cache", 3144545);
755 check_get_group("u31_a9999_ext_cache", 3149999);
756
757 check_get_group("u31_i1", 3190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800758}
759
Tom Cherry6b116d12019-04-25 10:34:07 -0700760TEST(grp, getpgram_app_id_not_allowed_platform) {
761 expect_no_group_name("u1_root");
762 expect_no_group_name("u1_debuggerd");
763
764 expect_no_group_name("u31_root");
765 expect_no_group_name("u31_debuggerd");
766}
767
768TEST(grp, getgrgid_app_id_u1_non_exist) {
769 expect_no_group_id(100000); // There is no 'root' for secondary users.
770 expect_no_group_id(101999); // End of the system reserved range, unallocated.
771 expect_no_group_id(102900); // The OEM ranges were never allocated to secondary users.
772 expect_no_group_id(105000); // The OEM ranges were never allocated to secondary users.
773
774 // The shared range is shared among users, and therefore doesn't exist for secondary users.
775 expect_no_group_id(150000);
776}
777
778TEST(grp, getgrgid_app_id_u31_non_exist) {
779 expect_no_group_id(3100000); // There is no 'root' for secondary users.
780 expect_no_group_id(3101999); // End of the system reserved range, unallocated.
781 expect_no_group_id(3102900); // The OEM ranges were never allocated to secondary users.
782 expect_no_group_id(3105000); // The OEM ranges were never allocated to secondary users.
783
784 // The shared range is shared among users, and therefore doesn't exist for secondary users.
785 expect_no_group_id(3150000);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700786}
Yabin Cuic4786d32015-07-20 19:46:26 -0700787
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700788TEST(grp, getgrnam_r_alignment) {
789#if defined(__BIONIC__)
790 group grp_storage;
791 alignas(16) char buf[512];
792 group* grp;
793 int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
794 ASSERT_EQ(0, result);
795 check_group(grp, "root", 0);
796#else
797 GTEST_SKIP() << "bionic-only test";
798#endif
799}
800
801TEST(grp, getgrgid_r_alignment) {
802#if defined(__BIONIC__)
803 group grp_storage;
804 alignas(16) char buf[512];
805 group* grp;
806 int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
807 ASSERT_EQ(0, result);
808 check_group(grp, "root", 0);
809#else
810 GTEST_SKIP() << "bionic-only test";
811#endif
812}
813
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800814TEST(grp, getgrnam_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700815#if defined(__BIONIC__)
816 group grp_storage[2];
817 char buf[2][512];
818 group* grp[3];
819 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
820 ASSERT_EQ(0, result);
821 check_group(grp[0], "root", 0);
822 grp[1] = getgrnam("system");
823 check_group(grp[1], "system", 1000);
824 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
825 ASSERT_EQ(0, result);
826 check_group(grp[2], "radio", 1001);
827 check_group(grp[0], "root", 0);
828 check_group(grp[1], "system", 1000);
829#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800830 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700831#endif
832}
833
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800834TEST(grp, getgrgid_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700835#if defined(__BIONIC__)
836 group grp_storage[2];
837 char buf[2][512];
838 group* grp[3];
839 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
840 ASSERT_EQ(0, result);
841 check_group(grp[0], "root", 0);
842 grp[1] = getgrgid(1000);
843 check_group(grp[1], "system", 1000);
844 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
845 ASSERT_EQ(0, result);
846 check_group(grp[2], "radio", 1001);
847 check_group(grp[0], "root", 0);
848 check_group(grp[1], "system", 1000);
849#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800850 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700851#endif
852}
853
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800854TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700855 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
856 ASSERT_GT(size, 0);
857 char buf[size];
858 group grp_storage;
859 group* grp;
860 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
861 check_group(grp, "root", 0);
862}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700863
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800864TEST(grp, getgrent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800865#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700866 group* grp;
Tom Cherry4362f892017-11-14 08:50:43 -0800867 std::set<gid_t> gids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700868
869 setgrent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700870 while ((grp = getgrent()) != nullptr) {
871 ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid;
872 ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800873 EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700874 EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800875
Tom Cherry0816c902020-04-10 13:00:42 -0700876 EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800877 gids.emplace(grp->gr_gid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700878 }
879 endgrent();
880
Tom Cherry6b116d12019-04-25 10:34:07 -0700881 expect_ids(gids, true);
Josh Gao2fe10342018-02-27 14:05:53 -0800882#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800883 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800884#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700885}
Tom Cherrye88b4082018-05-24 14:44:10 -0700886
Elliott Hughes7cebf832020-08-12 14:25:41 -0700887TEST(grp, getgrouplist) {
888#if defined(__BIONIC__)
889 // Query the number of groups.
890 int ngroups = 0;
891 ASSERT_EQ(-1, getgrouplist("root", 123, nullptr, &ngroups));
892 ASSERT_EQ(1, ngroups);
893
894 // Query the specific groups (just the one you pass in on Android).
895 ngroups = 8;
896 gid_t groups[ngroups];
897 ASSERT_EQ(1, getgrouplist("root", 123, groups, &ngroups));
898 ASSERT_EQ(1, ngroups);
899 ASSERT_EQ(123u, groups[0]);
900#else
901 GTEST_SKIP() << "bionic-only test (groups too unpredictable)";
902#endif
903}
904
Elliott Hughes6a458842024-02-14 17:10:54 -0800905TEST(grp, initgroups) {
906 if (getuid() != 0) GTEST_SKIP() << "test requires root";
907 ASSERT_EQ(0, initgroups("root", 0));
908}
909
Tom Cherrye88b4082018-05-24 14:44:10 -0700910#if defined(__BIONIC__)
911static void TestAidNamePrefix(const std::string& file_path) {
912 std::string file_contents;
913 if (!ReadFileToString(file_path, &file_contents)) {
914 // If we cannot read this file, then there are no vendor defind AID names, in which case this
915 // test passes by default.
916 return;
917 }
918 auto lines = Split(file_contents, "\n");
919 for (const auto& line : lines) {
920 if (line.empty()) continue;
921 auto name = Split(line, ":")[0];
922 EXPECT_TRUE(StartsWith(name, "vendor_"));
923 }
924}
925#endif
926
927TEST(pwd, vendor_prefix_users) {
928#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800929 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
930 return;
931 }
932
Tom Cherrye88b4082018-05-24 14:44:10 -0700933 TestAidNamePrefix("/vendor/etc/passwd");
934#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800935 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700936#endif
937}
938
939TEST(pwd, vendor_prefix_groups) {
940#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800941 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
942 return;
943 }
944
Tom Cherrye88b4082018-05-24 14:44:10 -0700945 TestAidNamePrefix("/vendor/etc/group");
946#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800947 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700948#endif
949}