blob: 9b239215af76550ca7b15eec85bcaf0b645833f2 [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 Cherry777b34d2019-02-17 09:38:23 -080078 EXPECT_STREQ("/bin/sh", pwd->pw_shell);
Kenny Root2a54e5e2012-09-13 10:52:52 -070079}
Yabin Cuia04c79b2014-11-18 16:14:54 -080080
Tom Cherryb4c25c82018-04-04 15:02:55 -070081static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type,
82 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080083 errno = 0;
84 passwd* pwd = getpwuid(uid);
85 ASSERT_EQ(0, errno);
86 SCOPED_TRACE("getpwuid");
Tom Cherryb4c25c82018-04-04 15:02:55 -070087 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -080088}
89
Tom Cherryb4c25c82018-04-04 15:02:55 -070090static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type,
91 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -080092 errno = 0;
93 passwd* pwd = getpwnam(username);
94 ASSERT_EQ(0, errno);
95 SCOPED_TRACE("getpwnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -070096 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -080097}
98
Tom Cherryb4c25c82018-04-04 15:02:55 -070099static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type,
100 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800101 passwd pwd_storage;
102 char buf[512];
103 int result;
104
105 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700106 passwd* pwd = nullptr;
Yabin Cuia04c79b2014-11-18 16:14:54 -0800107 result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
108 ASSERT_EQ(0, result);
109 ASSERT_EQ(0, errno);
110 SCOPED_TRACE("getpwuid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700111 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800112}
113
Tom Cherryb4c25c82018-04-04 15:02:55 -0700114static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type,
115 bool check_username) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800116 passwd pwd_storage;
117 char buf[512];
118 int result;
119
120 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 passwd* pwd = nullptr;
Yabin Cuia04c79b2014-11-18 16:14:54 -0800122 result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
123 ASSERT_EQ(0, result);
124 ASSERT_EQ(0, errno);
125 SCOPED_TRACE("getpwnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700126 check_passwd(pwd, username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800127}
128
Tom Cherryb4c25c82018-04-04 15:02:55 -0700129static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type,
130 bool check_username = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700131 SCOPED_TRACE("username '"s + username + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700132 check_getpwuid(username, uid, uid_type, check_username);
133 check_getpwnam(username, uid, uid_type, check_username);
134 check_getpwuid_r(username, uid, uid_type, check_username);
135 check_getpwnam_r(username, uid, uid_type, check_username);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800136}
137
Tom Cherry6b116d12019-04-25 10:34:07 -0700138static void expect_no_passwd_id(uid_t uid) {
139 SCOPED_TRACE("uid '" + std::to_string(uid) + "'");
140 errno = 0;
141 passwd* passwd = nullptr;
142 passwd = getpwuid(uid);
143 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
144 EXPECT_EQ(ENOENT, errno);
145
146 struct passwd passwd_storage;
147 char buf[512];
148 EXPECT_EQ(ENOENT, getpwuid_r(uid, &passwd_storage, buf, sizeof(buf), &passwd));
149 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
150}
151
152static void expect_no_passwd_name(const char* username) {
153 SCOPED_TRACE("username '"s + username + "'");
154 errno = 0;
155 passwd* passwd = nullptr;
156 passwd = getpwnam(username);
157 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
158 EXPECT_EQ(ENOENT, errno);
159
160 struct passwd passwd_storage;
161 char buf[512];
162 EXPECT_EQ(ENOENT, getpwnam_r(username, &passwd_storage, buf, sizeof(buf), &passwd));
163 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
164}
165
Yabin Cuia04c79b2014-11-18 16:14:54 -0800166#else // !defined(__BIONIC__)
167
Tom Cherryb4c25c82018-04-04 15:02:55 -0700168static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */,
169 bool /* check_username */) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800170 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700171}
172
Josh Gao2fe10342018-02-27 14:05:53 -0800173static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800174 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800175}
176
Tom Cherry6b116d12019-04-25 10:34:07 -0700177static void expect_no_passwd_id(uid_t /* uid */) {
178 GTEST_SKIP() << "bionic-only test";
179}
180
181static void expect_no_passwd_name(const char* /* username */) {
182 GTEST_SKIP() << "bionic-only test";
183}
184
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800185#endif
Kenny Root2a54e5e2012-09-13 10:52:52 -0700186
Tom Cherry6b116d12019-04-25 10:34:07 -0700187TEST(pwd, getpwnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800188 check_get_passwd("root", 0, TYPE_SYSTEM);
Tom Cherry6b116d12019-04-25 10:34:07 -0700189 check_get_passwd("daemon", 1, TYPE_SYSTEM);
190 check_get_passwd("bin", 2, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700191
Yabin Cuia04c79b2014-11-18 16:14:54 -0800192 check_get_passwd("system", 1000, TYPE_SYSTEM);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800193 check_get_passwd("radio", 1001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700194
Tom Cherry6b116d12019-04-25 10:34:07 -0700195 check_get_passwd("shell", 2000, TYPE_SYSTEM);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700196
Yabin Cuia04c79b2014-11-18 16:14:54 -0800197 check_get_passwd("nobody", 9999, TYPE_SYSTEM);
Kenny Root8a05a012012-09-13 14:31:50 -0700198}
199
Tom Cherry6b116d12019-04-25 10:34:07 -0700200TEST(pwd, getpwnam_oem_ids) {
201 check_get_passwd("oem_2900", 2900, TYPE_VENDOR, false);
202 check_get_passwd("oem_2945", 2945, TYPE_VENDOR, false);
203 check_get_passwd("oem_2999", 2999, TYPE_VENDOR, false);
204 check_get_passwd("oem_5000", 5000, TYPE_VENDOR, false);
205 check_get_passwd("oem_5454", 5454, TYPE_VENDOR, false);
206 check_get_passwd("oem_5999", 5999, TYPE_VENDOR, false);
207}
208
209TEST(pwd, getpwnam_non_exist) {
210 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
211 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
212 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
213
214 // These ranges are for GIDs only.
215 expect_no_passwd_id(20000);
216 expect_no_passwd_id(30000);
217 expect_no_passwd_id(40000);
218 expect_no_passwd_id(50000);
219
220 // These should not be parsed as users, only as groups.
221 expect_no_passwd_name("u0_a9999_cache");
222 expect_no_passwd_name("u0_a9999_ext");
223 expect_no_passwd_name("u0_a9999_ext_cache");
224 expect_no_passwd_name("all_a9999");
225}
226
227TEST(pwd, getpwnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800228 check_get_passwd("u0_a0", 10000, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800229 check_get_passwd("u0_a1234", 11234, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700230 check_get_passwd("u0_a9999", 19999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700231
Martijn Coenenf9d22992019-01-16 16:25:40 +0100232 check_get_passwd("u0_i1", 90001, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700233 check_get_passwd("u0_i4545", 94545, TYPE_APP);
234 check_get_passwd("u0_i9999", 99999, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800235}
236
Tom Cherry6b116d12019-04-25 10:34:07 -0700237TEST(pwd, getpwnam_app_id_u1_ids) {
238 check_get_passwd("u1_system", 101000, TYPE_SYSTEM);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800239 check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700240
Yabin Cuia04c79b2014-11-18 16:14:54 -0800241 check_get_passwd("u1_a0", 110000, TYPE_APP);
Tom Cherry6b116d12019-04-25 10:34:07 -0700242 check_get_passwd("u1_a1234", 111234, TYPE_APP);
243 check_get_passwd("u1_a9999", 119999, TYPE_APP);
244
245 check_get_passwd("u1_i1", 190001, TYPE_APP);
246 check_get_passwd("u1_i4545", 194545, TYPE_APP);
247 check_get_passwd("u1_i9999", 199999, TYPE_APP);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800248}
249
Tom Cherry6b116d12019-04-25 10:34:07 -0700250TEST(pwd, getpwnam_app_id_u31_ids) {
251 check_get_passwd("u31_system", 3101000, TYPE_SYSTEM);
252 check_get_passwd("u31_radio", 3101001, TYPE_SYSTEM);
253
254 check_get_passwd("u31_a0", 3110000, TYPE_APP);
255 check_get_passwd("u31_a1234", 3111234, TYPE_APP);
256 check_get_passwd("u31_a9999", 3119999, TYPE_APP);
257
258 check_get_passwd("u31_i1", 3190001, TYPE_APP);
259 check_get_passwd("u31_i4545", 3194545, TYPE_APP);
260 check_get_passwd("u31_i9999", 3199999, TYPE_APP);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700261}
262
Tom Cherry6b116d12019-04-25 10:34:07 -0700263TEST(pwd, getpwnam_app_id_not_allowed_platform) {
264 expect_no_passwd_name("u1_root");
265 expect_no_passwd_name("u1_debuggerd");
266
267 expect_no_passwd_name("u31_root");
268 expect_no_passwd_name("u31_debuggerd");
269}
270
271TEST(pwd, getpwuid_app_id_u1_non_exist) {
272 expect_no_passwd_id(100000); // There is no 'root' for secondary users.
273 expect_no_passwd_id(101999); // End of the system reserved range, unallocated.
274 expect_no_passwd_id(102900); // The OEM ranges were never allocated to secondary users.
275 expect_no_passwd_id(105000); // The OEM ranges were never allocated to secondary users.
276
277 // These ranges are for GIDs only.
278 expect_no_passwd_id(120000);
279 expect_no_passwd_id(130000);
280 expect_no_passwd_id(140000);
281 expect_no_passwd_id(150000);
282}
283
284TEST(pwd, getpwuid_app_id_u31_non_exist) {
285 expect_no_passwd_id(3100000); // There is no 'root' for secondary users.
286 expect_no_passwd_id(3101999); // End of the system reserved range, unallocated.
287 expect_no_passwd_id(3102900); // The OEM ranges were never allocated to secondary users.
288 expect_no_passwd_id(3105000); // The OEM ranges were never allocated to secondary users.
289
290 // These ranges are for GIDs only.
291 expect_no_passwd_id(3120000);
292 expect_no_passwd_id(3130000);
293 expect_no_passwd_id(3140000);
294 expect_no_passwd_id(3150000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800295}
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700296
297TEST(pwd, getpwnam_r_alignment) {
298#if defined(__BIONIC__)
299 passwd pwd_storage;
300 alignas(16) char buf[512];
301 passwd* pwd;
302 int result = getpwnam_r("root", &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
303 ASSERT_EQ(0, result);
304 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
305#else
306 GTEST_SKIP() << "bionic-only test";
307#endif
308}
309
310TEST(pwd, getpwuid_r_alignment) {
311#if defined(__BIONIC__)
312 passwd pwd_storage;
313 alignas(16) char buf[512];
314 passwd* pwd;
315 int result = getpwuid_r(0, &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
316 ASSERT_EQ(0, result);
317 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
318#else
319 GTEST_SKIP() << "bionic-only test";
320#endif
321}
322
323TEST(pwd, getpwnam_r_reentrancy) {
324#if defined(__BIONIC__)
325 passwd pwd_storage[2];
326 char buf[2][512];
327 passwd* pwd[3];
328 int result = getpwnam_r("root", &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
329 ASSERT_EQ(0, result);
330 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
331 pwd[1] = getpwnam("system");
332 ASSERT_NE(nullptr, pwd[1]);
333 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
334 result = getpwnam_r("radio", &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
335 ASSERT_EQ(0, result);
336 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
337 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
338 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
339#else
340 GTEST_SKIP() << "bionic-only test";
341#endif
342}
343
344TEST(pwd, getpwuid_r_reentrancy) {
345#if defined(__BIONIC__)
346 passwd pwd_storage[2];
347 char buf[2][512];
348 passwd* pwd[3];
349 int result = getpwuid_r(0, &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
350 ASSERT_EQ(0, result);
351 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
352 pwd[1] = getpwuid(1000);
353 ASSERT_NE(nullptr, pwd[1]);
354 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
355 result = getpwuid_r(1001, &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
356 ASSERT_EQ(0, result);
357 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
358 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
359 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
360#else
361 GTEST_SKIP() << "bionic-only test";
362#endif
363}
364
365TEST(pwd, getpwnam_r_large_enough_suggested_buffer_size) {
366#if defined(__BIONIC__)
367 long size = sysconf(_SC_GETPW_R_SIZE_MAX);
368 ASSERT_GT(size, 0);
369 char buf[size];
370 passwd pwd_storage;
371 passwd* pwd;
372 ASSERT_EQ(0, getpwnam_r("root", &pwd_storage, buf, size, &pwd));
373 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
374#else
375 GTEST_SKIP() << "bionic-only test";
376#endif
377}
378
Tom Cherry5c941432018-10-09 11:01:28 -0700379#if defined(__BIONIC__)
Tom Cherry4362f892017-11-14 08:50:43 -0800380template <typename T>
Tom Cherry777b34d2019-02-17 09:38:23 -0800381static void expect_ids(T ids, bool is_group) {
Tom Cherry4362f892017-11-14 08:50:43 -0800382 std::set<typename T::key_type> expected_ids;
383 // Ensure that all android_ids are iterated through.
384 for (size_t n = 0; n < android_id_count; ++n) {
385 EXPECT_EQ(1U, ids.count(android_ids[n].aid)) << "android_ids[n].aid: " << android_ids[n].aid;
386 expected_ids.emplace(android_ids[n].aid);
387 }
388
389 auto expect_range = [&ids, &expected_ids](uid_t start, uid_t end) {
390 for (size_t n = start; n <= end; ++n) {
391 EXPECT_EQ(1U, ids.count(n)) << "n: " << n;
392 expected_ids.emplace(n);
393 }
394 };
395
396 // Ensure that all reserved ranges are iterated through.
397 expect_range(AID_OEM_RESERVED_START, AID_OEM_RESERVED_END);
398 expect_range(AID_OEM_RESERVED_2_START, AID_OEM_RESERVED_2_END);
399 expect_range(AID_APP_START, AID_APP_END);
Tom Cherry6b116d12019-04-25 10:34:07 -0700400 if (is_group) {
401 expect_range(AID_CACHE_GID_START, AID_CACHE_GID_END);
402 expect_range(AID_EXT_GID_START, AID_EXT_GID_END);
403 expect_range(AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END);
404 expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
405 }
Tom Cherry4362f892017-11-14 08:50:43 -0800406 expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
407
Tom Cherry6f2e8102020-04-10 10:50:09 -0700408 // Prior to R, we didn't have a mechanism to create vendor AIDs in the system or other non-vendor
409 // partitions, therefore we disabled the rest of these checks for older API levels.
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800410 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 29) {
Tom Cherry5c941432018-10-09 11:01:28 -0700411 return;
412 }
413
Tom Cherry777b34d2019-02-17 09:38:23 -0800414 auto allow_range = [&ids](uid_t start, uid_t end) {
415 for (size_t n = start; n <= end; ++n) {
416 ids.erase(n);
417 }
418 };
419
420 allow_range(AID_SYSTEM_RESERVED_START, AID_SYSTEM_EXT_RESERVED_END);
421
Tom Cherry4362f892017-11-14 08:50:43 -0800422 // Ensure that no other ids were returned.
423 auto return_differences = [&ids, &expected_ids] {
424 std::vector<typename T::key_type> missing_from_ids;
425 std::set_difference(expected_ids.begin(), expected_ids.end(), ids.begin(), ids.end(),
426 std::inserter(missing_from_ids, missing_from_ids.begin()));
427 std::vector<typename T::key_type> extra_in_ids;
428 std::set_difference(ids.begin(), ids.end(), expected_ids.begin(), expected_ids.end(),
429 std::inserter(extra_in_ids, extra_in_ids.begin()));
430 std::string result;
431 if (!missing_from_ids.empty()) {
432 result += "Missing ids from results: " + Join(missing_from_ids, " ");
433 }
434 if (!extra_in_ids.empty()) {
435 if (!result.empty()) result += ", ";
436 result += "Extra ids in results: " + Join(extra_in_ids, " ");
437 }
438 return result;
439 };
440 EXPECT_EQ(expected_ids, ids) << return_differences();
441}
Tom Cherry5c941432018-10-09 11:01:28 -0700442#endif
Tom Cherry4362f892017-11-14 08:50:43 -0800443
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800444TEST(pwd, getpwent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800445#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700446 passwd* pwd;
Tom Cherry4362f892017-11-14 08:50:43 -0800447 std::set<uid_t> uids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700448
449 setpwent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700450 while ((pwd = getpwent()) != nullptr) {
451 ASSERT_TRUE(nullptr != pwd->pw_name);
Tom Cherry4362f892017-11-14 08:50:43 -0800452
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800453 EXPECT_EQ(pwd->pw_gid, pwd->pw_uid) << "pwd->pw_uid: " << pwd->pw_uid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700454 EXPECT_EQ(nullptr, pwd->pw_passwd) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700455#ifdef __LP64__
Yi Kong32bc0fc2018-08-02 17:31:13 -0700456 EXPECT_TRUE(nullptr == pwd->pw_gecos) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700457#endif
Yi Kong32bc0fc2018-08-02 17:31:13 -0700458 EXPECT_TRUE(nullptr != pwd->pw_shell);
Tom Cherry4362f892017-11-14 08:50:43 -0800459 if (pwd->pw_uid < AID_APP_START || pwd->pw_uid == AID_OVERFLOWUID) {
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800460 EXPECT_STREQ("/", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
Tom Cherry4362f892017-11-14 08:50:43 -0800461 } else {
462 EXPECT_STREQ("/data", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700463 }
Tom Cherry4362f892017-11-14 08:50:43 -0800464
Tom Cherry0816c902020-04-10 13:00:42 -0700465 EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid;
Tom Cherry4362f892017-11-14 08:50:43 -0800466 uids.emplace(pwd->pw_uid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700467 }
468 endpwent();
469
Tom Cherry6b116d12019-04-25 10:34:07 -0700470 expect_ids(uids, false);
Josh Gao2fe10342018-02-27 14:05:53 -0800471#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800472 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800473#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700474}
475
Tom Cherryb4c25c82018-04-04 15:02:55 -0700476static void check_group(const group* grp, const char* group_name, gid_t gid,
477 bool check_groupname = true) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700478 ASSERT_TRUE(grp != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700479 if (check_groupname) {
480 EXPECT_STREQ(group_name, grp->gr_name);
481 }
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800482 EXPECT_EQ(gid, grp->gr_gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700483 ASSERT_TRUE(grp->gr_mem != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700484 if (check_groupname) {
485 EXPECT_STREQ(group_name, grp->gr_mem[0]);
486 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700487 EXPECT_TRUE(grp->gr_mem[1] == nullptr);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800488}
489
Yabin Cuic4786d32015-07-20 19:46:26 -0700490#if defined(__BIONIC__)
491
Tom Cherryb4c25c82018-04-04 15:02:55 -0700492static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800493 errno = 0;
494 group* grp = getgrgid(gid);
495 ASSERT_EQ(0, errno);
496 SCOPED_TRACE("getgrgid");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700497 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800498}
499
Tom Cherryb4c25c82018-04-04 15:02:55 -0700500static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800501 errno = 0;
502 group* grp = getgrnam(group_name);
503 ASSERT_EQ(0, errno);
504 SCOPED_TRACE("getgrnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700505 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800506}
507
Tom Cherryb4c25c82018-04-04 15:02:55 -0700508static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700509 group grp_storage;
510 char buf[512];
511 group* grp;
512
513 errno = 0;
514 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
515 ASSERT_EQ(0, result);
516 ASSERT_EQ(0, errno);
517 SCOPED_TRACE("getgrgid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700518 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700519}
520
Tom Cherryb4c25c82018-04-04 15:02:55 -0700521static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700522 group grp_storage;
523 char buf[512];
524 group* grp;
525
526 errno = 0;
527 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
528 ASSERT_EQ(0, result);
529 ASSERT_EQ(0, errno);
530 SCOPED_TRACE("getgrnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700531 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700532}
533
Tom Cherryb4c25c82018-04-04 15:02:55 -0700534static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700535 SCOPED_TRACE("groupname '"s + group_name + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700536 check_getgrgid(group_name, gid, check_groupname);
537 check_getgrnam(group_name, gid, check_groupname);
538 check_getgrgid_r(group_name, gid, check_groupname);
539 check_getgrnam_r(group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800540}
541
Tom Cherry6b116d12019-04-25 10:34:07 -0700542static void expect_no_group_id(gid_t gid) {
543 SCOPED_TRACE("gid '" + std::to_string(gid) + "'");
544 errno = 0;
545 group* group = nullptr;
546 group = getgrgid(gid);
547 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
548 EXPECT_EQ(ENOENT, errno);
549
550 struct group group_storage;
551 char buf[512];
552 EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group));
553 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
554}
555
556static void expect_no_group_name(const char* groupname) {
557 SCOPED_TRACE("groupname '"s + groupname + "'");
558 errno = 0;
559 group* group = nullptr;
560 group = getgrnam(groupname);
561 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
562 EXPECT_EQ(ENOENT, errno);
563
564 struct group group_storage;
565 char buf[512];
566 EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group));
567 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
568}
569
Yabin Cuia04c79b2014-11-18 16:14:54 -0800570#else // !defined(__BIONIC__)
571
Tom Cherryb4c25c82018-04-04 15:02:55 -0700572static void check_get_group(const char*, gid_t, bool) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800573 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700574}
575
Yabin Cuic4786d32015-07-20 19:46:26 -0700576static void check_get_group(const char*, gid_t) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800577 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700578}
579
Tom Cherry6b116d12019-04-25 10:34:07 -0700580static void expect_no_group_id(gid_t /* gid */) {
581 GTEST_SKIP() << "bionic-only test";
582}
583
584static void expect_no_group_name(const char* /* groupname */) {
585 GTEST_SKIP() << "bionic-only test";
586}
587
Yabin Cuia04c79b2014-11-18 16:14:54 -0800588#endif
589
Tom Cherry6b116d12019-04-25 10:34:07 -0700590TEST(grp, getgrnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800591 check_get_group("root", 0);
Tom Cherry6b116d12019-04-25 10:34:07 -0700592 check_get_group("daemon", 1);
593 check_get_group("bin", 2);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800594
Yabin Cuia04c79b2014-11-18 16:14:54 -0800595 check_get_group("system", 1000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800596 check_get_group("radio", 1001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800597
Tom Cherry6b116d12019-04-25 10:34:07 -0700598 check_get_group("shell", 2000);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700599
Yabin Cuia04c79b2014-11-18 16:14:54 -0800600 check_get_group("nobody", 9999);
601}
602
Tom Cherry6b116d12019-04-25 10:34:07 -0700603TEST(grp, getgrnam_oem_ids) {
604 check_get_group("oem_2900", 2900, false);
605 check_get_group("oem_2945", 2945, false);
606 check_get_group("oem_2999", 2999, false);
607 check_get_group("oem_5000", 5000, false);
608 check_get_group("oem_5454", 5454, false);
609 check_get_group("oem_5999", 5999, false);
610}
611
612TEST(grp, getgrnam_non_exist) {
613 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
614 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
615 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
616}
617
618TEST(grp, getgrnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800619 check_get_group("u0_a0", 10000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800620 check_get_group("u0_a1234", 11234);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800621 check_get_group("u0_a9999", 19999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800622
Jeff Sharkey934bc862016-12-13 14:03:19 -0700623 check_get_group("u0_a0_cache", 20000);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700624 check_get_group("u0_a1234_cache", 21234);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700625 check_get_group("u0_a9999_cache", 29999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700626
Tom Cherry6b116d12019-04-25 10:34:07 -0700627 check_get_group("u0_a0_ext", 30000);
628 check_get_group("u0_a4545_ext", 34545);
629 check_get_group("u0_a9999_ext", 39999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700630
Tom Cherry6b116d12019-04-25 10:34:07 -0700631 check_get_group("u0_a0_ext_cache", 40000);
632 check_get_group("u0_a4545_ext_cache", 44545);
633 check_get_group("u0_a9999_ext_cache", 49999);
634
635 check_get_group("all_a0", 50000);
636 check_get_group("all_a4545", 54545);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800637 check_get_group("all_a9999", 59999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800638
Martijn Coenenf9d22992019-01-16 16:25:40 +0100639 check_get_group("u0_i1", 90001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800640}
641
Tom Cherry6b116d12019-04-25 10:34:07 -0700642TEST(grp, getgrnam_u1_app_ids) {
643 check_get_group("u1_system", 101000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800644 check_get_group("u1_radio", 101001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800645
Yabin Cuia04c79b2014-11-18 16:14:54 -0800646 check_get_group("u1_a0", 110000);
Tom Cherry6b116d12019-04-25 10:34:07 -0700647 check_get_group("u1_a1234", 111234);
648 check_get_group("u1_a9999", 119999);
649
650 check_get_group("u1_a0_cache", 120000);
651 check_get_group("u1_a1234_cache", 121234);
652 check_get_group("u1_a9999_cache", 129999);
653
654 check_get_group("u1_a0_ext", 130000);
655 check_get_group("u1_a4545_ext", 134545);
656 check_get_group("u1_a9999_ext", 139999);
657
658 check_get_group("u1_a0_ext_cache", 140000);
659 check_get_group("u1_a4545_ext_cache", 144545);
660 check_get_group("u1_a9999_ext_cache", 149999);
661
662 check_get_group("u1_i1", 190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800663}
664
Tom Cherry6b116d12019-04-25 10:34:07 -0700665TEST(grp, getgrnam_u31_app_ids) {
666 check_get_group("u31_system", 3101000);
667 check_get_group("u31_radio", 3101001);
668
669 check_get_group("u31_a0", 3110000);
670 check_get_group("u31_a1234", 3111234);
671 check_get_group("u31_a9999", 3119999);
672
673 check_get_group("u31_a0_cache", 3120000);
674 check_get_group("u31_a1234_cache", 3121234);
675 check_get_group("u31_a9999_cache", 3129999);
676
677 check_get_group("u31_a0_cache", 3120000);
678 check_get_group("u31_a1234_cache", 3121234);
679 check_get_group("u31_a9999_cache", 3129999);
680
681 check_get_group("u31_a0_ext", 3130000);
682 check_get_group("u31_a4545_ext", 3134545);
683 check_get_group("u31_a9999_ext", 3139999);
684
685 check_get_group("u31_a0_ext_cache", 3140000);
686 check_get_group("u31_a4545_ext_cache", 3144545);
687 check_get_group("u31_a9999_ext_cache", 3149999);
688
689 check_get_group("u31_i1", 3190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800690}
691
Tom Cherry6b116d12019-04-25 10:34:07 -0700692TEST(grp, getpgram_app_id_not_allowed_platform) {
693 expect_no_group_name("u1_root");
694 expect_no_group_name("u1_debuggerd");
695
696 expect_no_group_name("u31_root");
697 expect_no_group_name("u31_debuggerd");
698}
699
700TEST(grp, getgrgid_app_id_u1_non_exist) {
701 expect_no_group_id(100000); // There is no 'root' for secondary users.
702 expect_no_group_id(101999); // End of the system reserved range, unallocated.
703 expect_no_group_id(102900); // The OEM ranges were never allocated to secondary users.
704 expect_no_group_id(105000); // The OEM ranges were never allocated to secondary users.
705
706 // The shared range is shared among users, and therefore doesn't exist for secondary users.
707 expect_no_group_id(150000);
708}
709
710TEST(grp, getgrgid_app_id_u31_non_exist) {
711 expect_no_group_id(3100000); // There is no 'root' for secondary users.
712 expect_no_group_id(3101999); // End of the system reserved range, unallocated.
713 expect_no_group_id(3102900); // The OEM ranges were never allocated to secondary users.
714 expect_no_group_id(3105000); // The OEM ranges were never allocated to secondary users.
715
716 // The shared range is shared among users, and therefore doesn't exist for secondary users.
717 expect_no_group_id(3150000);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700718}
Yabin Cuic4786d32015-07-20 19:46:26 -0700719
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700720TEST(grp, getgrnam_r_alignment) {
721#if defined(__BIONIC__)
722 group grp_storage;
723 alignas(16) char buf[512];
724 group* grp;
725 int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
726 ASSERT_EQ(0, result);
727 check_group(grp, "root", 0);
728#else
729 GTEST_SKIP() << "bionic-only test";
730#endif
731}
732
733TEST(grp, getgrgid_r_alignment) {
734#if defined(__BIONIC__)
735 group grp_storage;
736 alignas(16) char buf[512];
737 group* grp;
738 int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
739 ASSERT_EQ(0, result);
740 check_group(grp, "root", 0);
741#else
742 GTEST_SKIP() << "bionic-only test";
743#endif
744}
745
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800746TEST(grp, getgrnam_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700747#if defined(__BIONIC__)
748 group grp_storage[2];
749 char buf[2][512];
750 group* grp[3];
751 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
752 ASSERT_EQ(0, result);
753 check_group(grp[0], "root", 0);
754 grp[1] = getgrnam("system");
755 check_group(grp[1], "system", 1000);
756 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
757 ASSERT_EQ(0, result);
758 check_group(grp[2], "radio", 1001);
759 check_group(grp[0], "root", 0);
760 check_group(grp[1], "system", 1000);
761#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800762 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700763#endif
764}
765
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800766TEST(grp, getgrgid_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700767#if defined(__BIONIC__)
768 group grp_storage[2];
769 char buf[2][512];
770 group* grp[3];
771 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
772 ASSERT_EQ(0, result);
773 check_group(grp[0], "root", 0);
774 grp[1] = getgrgid(1000);
775 check_group(grp[1], "system", 1000);
776 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
777 ASSERT_EQ(0, result);
778 check_group(grp[2], "radio", 1001);
779 check_group(grp[0], "root", 0);
780 check_group(grp[1], "system", 1000);
781#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800782 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700783#endif
784}
785
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800786TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700787 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
788 ASSERT_GT(size, 0);
789 char buf[size];
790 group grp_storage;
791 group* grp;
792 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
793 check_group(grp, "root", 0);
794}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700795
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800796TEST(grp, getgrent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800797#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700798 group* grp;
Tom Cherry4362f892017-11-14 08:50:43 -0800799 std::set<gid_t> gids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700800
801 setgrent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700802 while ((grp = getgrent()) != nullptr) {
803 ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid;
804 ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800805 EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700806 EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800807
Tom Cherry0816c902020-04-10 13:00:42 -0700808 EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800809 gids.emplace(grp->gr_gid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700810 }
811 endgrent();
812
Tom Cherry6b116d12019-04-25 10:34:07 -0700813 expect_ids(gids, true);
Josh Gao2fe10342018-02-27 14:05:53 -0800814#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800815 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800816#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700817}
Tom Cherrye88b4082018-05-24 14:44:10 -0700818
819#if defined(__BIONIC__)
820static void TestAidNamePrefix(const std::string& file_path) {
821 std::string file_contents;
822 if (!ReadFileToString(file_path, &file_contents)) {
823 // If we cannot read this file, then there are no vendor defind AID names, in which case this
824 // test passes by default.
825 return;
826 }
827 auto lines = Split(file_contents, "\n");
828 for (const auto& line : lines) {
829 if (line.empty()) continue;
830 auto name = Split(line, ":")[0];
831 EXPECT_TRUE(StartsWith(name, "vendor_"));
832 }
833}
834#endif
835
836TEST(pwd, vendor_prefix_users) {
837#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800838 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
839 return;
840 }
841
Tom Cherrye88b4082018-05-24 14:44:10 -0700842 TestAidNamePrefix("/vendor/etc/passwd");
843#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800844 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700845#endif
846}
847
848TEST(pwd, vendor_prefix_groups) {
849#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800850 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
851 return;
852 }
853
Tom Cherrye88b4082018-05-24 14:44:10 -0700854 TestAidNamePrefix("/vendor/etc/group");
855#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800856 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700857#endif
858}