blob: 6b49e298e737456b862865392446148fcd3db599 [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 Cherry9da8ff12019-02-19 13:23:49 -0800408 // TODO(73062966): We still don't have a good way to create vendor AIDs in the system or other
409 // non-vendor partitions, therefore we keep this check disabled.
410 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= __ANDROID_API_Q__) {
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 Cherry4cddb002018-06-29 10:39:43 -0700465 // TODO(b/27999086): fix this check with the OEM range
466 // If OEMs add their own AIDs to private/android_filesystem_config.h, this check will fail.
467 // Long term we want to create a better solution for OEMs adding AIDs, but we're not there
468 // yet, so therefore we do not check for uid's in the OEM range.
469 if (!(pwd->pw_uid >= 2900 && pwd->pw_uid <= 2999) &&
470 !(pwd->pw_uid >= 5000 && pwd->pw_uid <= 5999)) {
471 EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid;
472 }
Tom Cherry4362f892017-11-14 08:50:43 -0800473 uids.emplace(pwd->pw_uid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700474 }
475 endpwent();
476
Tom Cherry6b116d12019-04-25 10:34:07 -0700477 expect_ids(uids, false);
Josh Gao2fe10342018-02-27 14:05:53 -0800478#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800479 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800480#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700481}
482
Tom Cherryb4c25c82018-04-04 15:02:55 -0700483static void check_group(const group* grp, const char* group_name, gid_t gid,
484 bool check_groupname = true) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700485 ASSERT_TRUE(grp != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700486 if (check_groupname) {
487 EXPECT_STREQ(group_name, grp->gr_name);
488 }
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800489 EXPECT_EQ(gid, grp->gr_gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700490 ASSERT_TRUE(grp->gr_mem != nullptr);
Tom Cherryb4c25c82018-04-04 15:02:55 -0700491 if (check_groupname) {
492 EXPECT_STREQ(group_name, grp->gr_mem[0]);
493 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700494 EXPECT_TRUE(grp->gr_mem[1] == nullptr);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800495}
496
Yabin Cuic4786d32015-07-20 19:46:26 -0700497#if defined(__BIONIC__)
498
Tom Cherryb4c25c82018-04-04 15:02:55 -0700499static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800500 errno = 0;
501 group* grp = getgrgid(gid);
502 ASSERT_EQ(0, errno);
503 SCOPED_TRACE("getgrgid");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700504 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800505}
506
Tom Cherryb4c25c82018-04-04 15:02:55 -0700507static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800508 errno = 0;
509 group* grp = getgrnam(group_name);
510 ASSERT_EQ(0, errno);
511 SCOPED_TRACE("getgrnam");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700512 check_group(grp, group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800513}
514
Tom Cherryb4c25c82018-04-04 15:02:55 -0700515static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700516 group grp_storage;
517 char buf[512];
518 group* grp;
519
520 errno = 0;
521 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
522 ASSERT_EQ(0, result);
523 ASSERT_EQ(0, errno);
524 SCOPED_TRACE("getgrgid_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700525 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700526}
527
Tom Cherryb4c25c82018-04-04 15:02:55 -0700528static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700529 group grp_storage;
530 char buf[512];
531 group* grp;
532
533 errno = 0;
534 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
535 ASSERT_EQ(0, result);
536 ASSERT_EQ(0, errno);
537 SCOPED_TRACE("getgrnam_r");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700538 check_group(grp, group_name, gid, check_groupname);
Yabin Cuic4786d32015-07-20 19:46:26 -0700539}
540
Tom Cherryb4c25c82018-04-04 15:02:55 -0700541static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700542 SCOPED_TRACE("groupname '"s + group_name + "'");
Tom Cherryb4c25c82018-04-04 15:02:55 -0700543 check_getgrgid(group_name, gid, check_groupname);
544 check_getgrnam(group_name, gid, check_groupname);
545 check_getgrgid_r(group_name, gid, check_groupname);
546 check_getgrnam_r(group_name, gid, check_groupname);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800547}
548
Tom Cherry6b116d12019-04-25 10:34:07 -0700549static void expect_no_group_id(gid_t gid) {
550 SCOPED_TRACE("gid '" + std::to_string(gid) + "'");
551 errno = 0;
552 group* group = nullptr;
553 group = getgrgid(gid);
554 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
555 EXPECT_EQ(ENOENT, errno);
556
557 struct group group_storage;
558 char buf[512];
559 EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group));
560 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
561}
562
563static void expect_no_group_name(const char* groupname) {
564 SCOPED_TRACE("groupname '"s + groupname + "'");
565 errno = 0;
566 group* group = nullptr;
567 group = getgrnam(groupname);
568 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
569 EXPECT_EQ(ENOENT, errno);
570
571 struct group group_storage;
572 char buf[512];
573 EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group));
574 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
575}
576
Yabin Cuia04c79b2014-11-18 16:14:54 -0800577#else // !defined(__BIONIC__)
578
Tom Cherryb4c25c82018-04-04 15:02:55 -0700579static void check_get_group(const char*, gid_t, bool) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800580 GTEST_SKIP() << "bionic-only test";
Tom Cherryb4c25c82018-04-04 15:02:55 -0700581}
582
Yabin Cuic4786d32015-07-20 19:46:26 -0700583static void check_get_group(const char*, gid_t) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800584 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700585}
586
Tom Cherry6b116d12019-04-25 10:34:07 -0700587static void expect_no_group_id(gid_t /* gid */) {
588 GTEST_SKIP() << "bionic-only test";
589}
590
591static void expect_no_group_name(const char* /* groupname */) {
592 GTEST_SKIP() << "bionic-only test";
593}
594
Yabin Cuia04c79b2014-11-18 16:14:54 -0800595#endif
596
Tom Cherry6b116d12019-04-25 10:34:07 -0700597TEST(grp, getgrnam_platform_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800598 check_get_group("root", 0);
Tom Cherry6b116d12019-04-25 10:34:07 -0700599 check_get_group("daemon", 1);
600 check_get_group("bin", 2);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800601
Yabin Cuia04c79b2014-11-18 16:14:54 -0800602 check_get_group("system", 1000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800603 check_get_group("radio", 1001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800604
Tom Cherry6b116d12019-04-25 10:34:07 -0700605 check_get_group("shell", 2000);
Jorge Lucangeli Obesa39e3012015-09-22 11:46:43 -0700606
Yabin Cuia04c79b2014-11-18 16:14:54 -0800607 check_get_group("nobody", 9999);
608}
609
Tom Cherry6b116d12019-04-25 10:34:07 -0700610TEST(grp, getgrnam_oem_ids) {
611 check_get_group("oem_2900", 2900, false);
612 check_get_group("oem_2945", 2945, false);
613 check_get_group("oem_2999", 2999, false);
614 check_get_group("oem_5000", 5000, false);
615 check_get_group("oem_5454", 5454, false);
616 check_get_group("oem_5999", 5999, false);
617}
618
619TEST(grp, getgrnam_non_exist) {
620 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
621 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
622 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
623}
624
625TEST(grp, getgrnam_u0_app_ids) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800626 check_get_group("u0_a0", 10000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800627 check_get_group("u0_a1234", 11234);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800628 check_get_group("u0_a9999", 19999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800629
Jeff Sharkey934bc862016-12-13 14:03:19 -0700630 check_get_group("u0_a0_cache", 20000);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700631 check_get_group("u0_a1234_cache", 21234);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700632 check_get_group("u0_a9999_cache", 29999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700633
Tom Cherry6b116d12019-04-25 10:34:07 -0700634 check_get_group("u0_a0_ext", 30000);
635 check_get_group("u0_a4545_ext", 34545);
636 check_get_group("u0_a9999_ext", 39999);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700637
Tom Cherry6b116d12019-04-25 10:34:07 -0700638 check_get_group("u0_a0_ext_cache", 40000);
639 check_get_group("u0_a4545_ext_cache", 44545);
640 check_get_group("u0_a9999_ext_cache", 49999);
641
642 check_get_group("all_a0", 50000);
643 check_get_group("all_a4545", 54545);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800644 check_get_group("all_a9999", 59999);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800645
Martijn Coenenf9d22992019-01-16 16:25:40 +0100646 check_get_group("u0_i1", 90001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800647}
648
Tom Cherry6b116d12019-04-25 10:34:07 -0700649TEST(grp, getgrnam_u1_app_ids) {
650 check_get_group("u1_system", 101000);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800651 check_get_group("u1_radio", 101001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800652
Yabin Cuia04c79b2014-11-18 16:14:54 -0800653 check_get_group("u1_a0", 110000);
Tom Cherry6b116d12019-04-25 10:34:07 -0700654 check_get_group("u1_a1234", 111234);
655 check_get_group("u1_a9999", 119999);
656
657 check_get_group("u1_a0_cache", 120000);
658 check_get_group("u1_a1234_cache", 121234);
659 check_get_group("u1_a9999_cache", 129999);
660
661 check_get_group("u1_a0_ext", 130000);
662 check_get_group("u1_a4545_ext", 134545);
663 check_get_group("u1_a9999_ext", 139999);
664
665 check_get_group("u1_a0_ext_cache", 140000);
666 check_get_group("u1_a4545_ext_cache", 144545);
667 check_get_group("u1_a9999_ext_cache", 149999);
668
669 check_get_group("u1_i1", 190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800670}
671
Tom Cherry6b116d12019-04-25 10:34:07 -0700672TEST(grp, getgrnam_u31_app_ids) {
673 check_get_group("u31_system", 3101000);
674 check_get_group("u31_radio", 3101001);
675
676 check_get_group("u31_a0", 3110000);
677 check_get_group("u31_a1234", 3111234);
678 check_get_group("u31_a9999", 3119999);
679
680 check_get_group("u31_a0_cache", 3120000);
681 check_get_group("u31_a1234_cache", 3121234);
682 check_get_group("u31_a9999_cache", 3129999);
683
684 check_get_group("u31_a0_cache", 3120000);
685 check_get_group("u31_a1234_cache", 3121234);
686 check_get_group("u31_a9999_cache", 3129999);
687
688 check_get_group("u31_a0_ext", 3130000);
689 check_get_group("u31_a4545_ext", 3134545);
690 check_get_group("u31_a9999_ext", 3139999);
691
692 check_get_group("u31_a0_ext_cache", 3140000);
693 check_get_group("u31_a4545_ext_cache", 3144545);
694 check_get_group("u31_a9999_ext_cache", 3149999);
695
696 check_get_group("u31_i1", 3190001);
Yabin Cuia04c79b2014-11-18 16:14:54 -0800697}
698
Tom Cherry6b116d12019-04-25 10:34:07 -0700699TEST(grp, getpgram_app_id_not_allowed_platform) {
700 expect_no_group_name("u1_root");
701 expect_no_group_name("u1_debuggerd");
702
703 expect_no_group_name("u31_root");
704 expect_no_group_name("u31_debuggerd");
705}
706
707TEST(grp, getgrgid_app_id_u1_non_exist) {
708 expect_no_group_id(100000); // There is no 'root' for secondary users.
709 expect_no_group_id(101999); // End of the system reserved range, unallocated.
710 expect_no_group_id(102900); // The OEM ranges were never allocated to secondary users.
711 expect_no_group_id(105000); // The OEM ranges were never allocated to secondary users.
712
713 // The shared range is shared among users, and therefore doesn't exist for secondary users.
714 expect_no_group_id(150000);
715}
716
717TEST(grp, getgrgid_app_id_u31_non_exist) {
718 expect_no_group_id(3100000); // There is no 'root' for secondary users.
719 expect_no_group_id(3101999); // End of the system reserved range, unallocated.
720 expect_no_group_id(3102900); // The OEM ranges were never allocated to secondary users.
721 expect_no_group_id(3105000); // The OEM ranges were never allocated to secondary users.
722
723 // The shared range is shared among users, and therefore doesn't exist for secondary users.
724 expect_no_group_id(3150000);
Kenny Root2a54e5e2012-09-13 10:52:52 -0700725}
Yabin Cuic4786d32015-07-20 19:46:26 -0700726
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700727TEST(grp, getgrnam_r_alignment) {
728#if defined(__BIONIC__)
729 group grp_storage;
730 alignas(16) char buf[512];
731 group* grp;
732 int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
733 ASSERT_EQ(0, result);
734 check_group(grp, "root", 0);
735#else
736 GTEST_SKIP() << "bionic-only test";
737#endif
738}
739
740TEST(grp, getgrgid_r_alignment) {
741#if defined(__BIONIC__)
742 group grp_storage;
743 alignas(16) char buf[512];
744 group* grp;
745 int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
746 ASSERT_EQ(0, result);
747 check_group(grp, "root", 0);
748#else
749 GTEST_SKIP() << "bionic-only test";
750#endif
751}
752
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800753TEST(grp, getgrnam_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700754#if defined(__BIONIC__)
755 group grp_storage[2];
756 char buf[2][512];
757 group* grp[3];
758 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
759 ASSERT_EQ(0, result);
760 check_group(grp[0], "root", 0);
761 grp[1] = getgrnam("system");
762 check_group(grp[1], "system", 1000);
763 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
764 ASSERT_EQ(0, result);
765 check_group(grp[2], "radio", 1001);
766 check_group(grp[0], "root", 0);
767 check_group(grp[1], "system", 1000);
768#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800769 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700770#endif
771}
772
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800773TEST(grp, getgrgid_r_reentrancy) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700774#if defined(__BIONIC__)
775 group grp_storage[2];
776 char buf[2][512];
777 group* grp[3];
778 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
779 ASSERT_EQ(0, result);
780 check_group(grp[0], "root", 0);
781 grp[1] = getgrgid(1000);
782 check_group(grp[1], "system", 1000);
783 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
784 ASSERT_EQ(0, result);
785 check_group(grp[2], "radio", 1001);
786 check_group(grp[0], "root", 0);
787 check_group(grp[1], "system", 1000);
788#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800789 GTEST_SKIP() << "bionic-only test";
Yabin Cuic4786d32015-07-20 19:46:26 -0700790#endif
791}
792
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800793TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) {
Yabin Cuic4786d32015-07-20 19:46:26 -0700794 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
795 ASSERT_GT(size, 0);
796 char buf[size];
797 group grp_storage;
798 group* grp;
799 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
800 check_group(grp, "root", 0);
801}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700802
Elliott Hughes5367d1b2016-12-12 17:32:14 -0800803TEST(grp, getgrent_iterate) {
Josh Gao2fe10342018-02-27 14:05:53 -0800804#if defined(__BIONIC__)
Mark Salyzyn722ab052016-04-06 10:35:48 -0700805 group* grp;
Tom Cherry4362f892017-11-14 08:50:43 -0800806 std::set<gid_t> gids;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700807
808 setgrent();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700809 while ((grp = getgrent()) != nullptr) {
810 ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid;
811 ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry2c05c0f2017-11-10 10:57:21 -0800812 EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700813 EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid;
Tom Cherry4362f892017-11-14 08:50:43 -0800814
Tom Cherry4cddb002018-06-29 10:39:43 -0700815 // TODO(b/27999086): fix this check with the OEM range
816 // If OEMs add their own AIDs to private/android_filesystem_config.h, this check will fail.
817 // Long term we want to create a better solution for OEMs adding AIDs, but we're not there
818 // yet, so therefore we do not check for gid's in the OEM range.
819 if (!(grp->gr_gid >= 2900 && grp->gr_gid <= 2999) &&
820 !(grp->gr_gid >= 5000 && grp->gr_gid <= 5999)) {
821 EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid;
822 }
Tom Cherry4362f892017-11-14 08:50:43 -0800823 gids.emplace(grp->gr_gid);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700824 }
825 endgrent();
826
Tom Cherry6b116d12019-04-25 10:34:07 -0700827 expect_ids(gids, true);
Josh Gao2fe10342018-02-27 14:05:53 -0800828#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800829 GTEST_SKIP() << "bionic-only test";
Josh Gao2fe10342018-02-27 14:05:53 -0800830#endif
Mark Salyzyn722ab052016-04-06 10:35:48 -0700831}
Tom Cherrye88b4082018-05-24 14:44:10 -0700832
833#if defined(__BIONIC__)
834static void TestAidNamePrefix(const std::string& file_path) {
835 std::string file_contents;
836 if (!ReadFileToString(file_path, &file_contents)) {
837 // If we cannot read this file, then there are no vendor defind AID names, in which case this
838 // test passes by default.
839 return;
840 }
841 auto lines = Split(file_contents, "\n");
842 for (const auto& line : lines) {
843 if (line.empty()) continue;
844 auto name = Split(line, ":")[0];
845 EXPECT_TRUE(StartsWith(name, "vendor_"));
846 }
847}
848#endif
849
850TEST(pwd, vendor_prefix_users) {
851#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800852 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
853 return;
854 }
855
Tom Cherrye88b4082018-05-24 14:44:10 -0700856 TestAidNamePrefix("/vendor/etc/passwd");
857#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800858 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700859#endif
860}
861
862TEST(pwd, vendor_prefix_groups) {
863#if defined(__BIONIC__)
Chuwei Xu5d9312b2018-10-23 13:50:04 +0800864 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
865 return;
866 }
867
Tom Cherrye88b4082018-05-24 14:44:10 -0700868 TestAidNamePrefix("/vendor/etc/group");
869#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800870 GTEST_SKIP() << "bionic-only test";
Tom Cherrye88b4082018-05-24 14:44:10 -0700871#endif
872}