xref: /aosp_15_r20/bionic/tests/grp_pwd_test.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  *
4*8d67ca89SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker  *
8*8d67ca89SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker  *
10*8d67ca89SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker  * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker  */
16*8d67ca89SAndroid Build Coastguard Worker 
17*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*8d67ca89SAndroid Build Coastguard Worker 
19*8d67ca89SAndroid Build Coastguard Worker // Below are the header files we want to test.
20*8d67ca89SAndroid Build Coastguard Worker #include <grp.h>
21*8d67ca89SAndroid Build Coastguard Worker #include <pwd.h>
22*8d67ca89SAndroid Build Coastguard Worker 
23*8d67ca89SAndroid Build Coastguard Worker #include <errno.h>
24*8d67ca89SAndroid Build Coastguard Worker #include <limits.h>
25*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h>
26*8d67ca89SAndroid Build Coastguard Worker #include <sys/types.h>
27*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #include <set>
30*8d67ca89SAndroid Build Coastguard Worker #include <vector>
31*8d67ca89SAndroid Build Coastguard Worker 
32*8d67ca89SAndroid Build Coastguard Worker #include <android-base/file.h>
33*8d67ca89SAndroid Build Coastguard Worker #include <android-base/strings.h>
34*8d67ca89SAndroid Build Coastguard Worker #include <private/android_filesystem_config.h>
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
37*8d67ca89SAndroid Build Coastguard Worker #include <android/api-level.h>
38*8d67ca89SAndroid Build Coastguard Worker #include <android-base/properties.h>
39*8d67ca89SAndroid Build Coastguard Worker #endif
40*8d67ca89SAndroid Build Coastguard Worker 
41*8d67ca89SAndroid Build Coastguard Worker // Generated android_ids array
42*8d67ca89SAndroid Build Coastguard Worker #include "generated_android_ids.h"
43*8d67ca89SAndroid Build Coastguard Worker 
44*8d67ca89SAndroid Build Coastguard Worker #include "utils.h"
45*8d67ca89SAndroid Build Coastguard Worker 
46*8d67ca89SAndroid Build Coastguard Worker using android::base::Join;
47*8d67ca89SAndroid Build Coastguard Worker using android::base::ReadFileToString;
48*8d67ca89SAndroid Build Coastguard Worker using android::base::Split;
49*8d67ca89SAndroid Build Coastguard Worker using android::base::StartsWith;
50*8d67ca89SAndroid Build Coastguard Worker 
51*8d67ca89SAndroid Build Coastguard Worker using namespace std::literals;
52*8d67ca89SAndroid Build Coastguard Worker 
53*8d67ca89SAndroid Build Coastguard Worker enum uid_type_t {
54*8d67ca89SAndroid Build Coastguard Worker   TYPE_APP,
55*8d67ca89SAndroid Build Coastguard Worker   TYPE_SYSTEM,
56*8d67ca89SAndroid Build Coastguard Worker   TYPE_VENDOR,
57*8d67ca89SAndroid Build Coastguard Worker };
58*8d67ca89SAndroid Build Coastguard Worker 
59*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
60*8d67ca89SAndroid Build Coastguard Worker 
check_passwd(const passwd * pwd,const char * username,uid_t uid,uid_type_t uid_type,bool check_username)61*8d67ca89SAndroid Build Coastguard Worker static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type,
62*8d67ca89SAndroid Build Coastguard Worker                          bool check_username) {
63*8d67ca89SAndroid Build Coastguard Worker   ASSERT_TRUE(pwd != nullptr);
64*8d67ca89SAndroid Build Coastguard Worker   if (check_username) {
65*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ(username, pwd->pw_name);
66*8d67ca89SAndroid Build Coastguard Worker   }
67*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(uid, pwd->pw_uid);
68*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(uid, pwd->pw_gid);
69*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, pwd->pw_passwd);
70*8d67ca89SAndroid Build Coastguard Worker #ifdef __LP64__
71*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, pwd->pw_gecos);
72*8d67ca89SAndroid Build Coastguard Worker #endif
73*8d67ca89SAndroid Build Coastguard Worker 
74*8d67ca89SAndroid Build Coastguard Worker   if (uid_type == TYPE_APP) {
75*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ("/data", pwd->pw_dir);
76*8d67ca89SAndroid Build Coastguard Worker   } else {
77*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ("/", pwd->pw_dir);
78*8d67ca89SAndroid Build Coastguard Worker   }
79*8d67ca89SAndroid Build Coastguard Worker 
80*8d67ca89SAndroid Build Coastguard Worker   // This has changed over time and that causes new GSI + old vendor images testing to fail.
81*8d67ca89SAndroid Build Coastguard Worker   // This parameter doesn't matter on Android, so simply ignore its value for older vendor images.
82*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.product.first_api_level", 0) >= 30) {
83*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ("/bin/sh", pwd->pw_shell);
84*8d67ca89SAndroid Build Coastguard Worker   }
85*8d67ca89SAndroid Build Coastguard Worker }
86*8d67ca89SAndroid Build Coastguard Worker 
check_getpwuid(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)87*8d67ca89SAndroid Build Coastguard Worker static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type,
88*8d67ca89SAndroid Build Coastguard Worker                            bool check_username) {
89*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
90*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd = getpwuid(uid);
91*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
92*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getpwuid");
93*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, username, uid, uid_type, check_username);
94*8d67ca89SAndroid Build Coastguard Worker }
95*8d67ca89SAndroid Build Coastguard Worker 
check_getpwnam(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)96*8d67ca89SAndroid Build Coastguard Worker static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type,
97*8d67ca89SAndroid Build Coastguard Worker                            bool check_username) {
98*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
99*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd = getpwnam(username);
100*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
101*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getpwnam");
102*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, username, uid, uid_type, check_username);
103*8d67ca89SAndroid Build Coastguard Worker }
104*8d67ca89SAndroid Build Coastguard Worker 
check_getpwuid_r(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)105*8d67ca89SAndroid Build Coastguard Worker static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type,
106*8d67ca89SAndroid Build Coastguard Worker                              bool check_username) {
107*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage;
108*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
109*8d67ca89SAndroid Build Coastguard Worker   int result;
110*8d67ca89SAndroid Build Coastguard Worker 
111*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
112*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd = nullptr;
113*8d67ca89SAndroid Build Coastguard Worker   result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
114*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
115*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
116*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getpwuid_r");
117*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, username, uid, uid_type, check_username);
118*8d67ca89SAndroid Build Coastguard Worker }
119*8d67ca89SAndroid Build Coastguard Worker 
check_getpwnam_r(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)120*8d67ca89SAndroid Build Coastguard Worker static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type,
121*8d67ca89SAndroid Build Coastguard Worker                              bool check_username) {
122*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage;
123*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
124*8d67ca89SAndroid Build Coastguard Worker   int result;
125*8d67ca89SAndroid Build Coastguard Worker 
126*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
127*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd = nullptr;
128*8d67ca89SAndroid Build Coastguard Worker   result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
129*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
130*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
131*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getpwnam_r");
132*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, username, uid, uid_type, check_username);
133*8d67ca89SAndroid Build Coastguard Worker }
134*8d67ca89SAndroid Build Coastguard Worker 
check_get_passwd(const char * username,uid_t uid,uid_type_t uid_type,bool check_username=true)135*8d67ca89SAndroid Build Coastguard Worker static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type,
136*8d67ca89SAndroid Build Coastguard Worker                              bool check_username = true) {
137*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("username '"s + username + "'");
138*8d67ca89SAndroid Build Coastguard Worker   check_getpwuid(username, uid, uid_type, check_username);
139*8d67ca89SAndroid Build Coastguard Worker   check_getpwnam(username, uid, uid_type, check_username);
140*8d67ca89SAndroid Build Coastguard Worker   check_getpwuid_r(username, uid, uid_type, check_username);
141*8d67ca89SAndroid Build Coastguard Worker   check_getpwnam_r(username, uid, uid_type, check_username);
142*8d67ca89SAndroid Build Coastguard Worker }
143*8d67ca89SAndroid Build Coastguard Worker 
expect_no_passwd_id(uid_t uid)144*8d67ca89SAndroid Build Coastguard Worker static void expect_no_passwd_id(uid_t uid) {
145*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("uid '" + std::to_string(uid) + "'");
146*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
147*8d67ca89SAndroid Build Coastguard Worker   passwd* passwd = nullptr;
148*8d67ca89SAndroid Build Coastguard Worker   passwd = getpwuid(uid);
149*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
150*8d67ca89SAndroid Build Coastguard Worker   EXPECT_ERRNO(ENOENT);
151*8d67ca89SAndroid Build Coastguard Worker 
152*8d67ca89SAndroid Build Coastguard Worker   struct passwd passwd_storage;
153*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
154*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(ENOENT, getpwuid_r(uid, &passwd_storage, buf, sizeof(buf), &passwd));
155*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
156*8d67ca89SAndroid Build Coastguard Worker }
157*8d67ca89SAndroid Build Coastguard Worker 
expect_no_passwd_name(const char * username)158*8d67ca89SAndroid Build Coastguard Worker static void expect_no_passwd_name(const char* username) {
159*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("username '"s + username + "'");
160*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
161*8d67ca89SAndroid Build Coastguard Worker   passwd* passwd = nullptr;
162*8d67ca89SAndroid Build Coastguard Worker   passwd = getpwnam(username);
163*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
164*8d67ca89SAndroid Build Coastguard Worker   EXPECT_ERRNO(ENOENT);
165*8d67ca89SAndroid Build Coastguard Worker 
166*8d67ca89SAndroid Build Coastguard Worker   struct passwd passwd_storage;
167*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
168*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(ENOENT, getpwnam_r(username, &passwd_storage, buf, sizeof(buf), &passwd));
169*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
170*8d67ca89SAndroid Build Coastguard Worker }
171*8d67ca89SAndroid Build Coastguard Worker 
172*8d67ca89SAndroid Build Coastguard Worker #else // !defined(__BIONIC__)
173*8d67ca89SAndroid Build Coastguard Worker 
check_get_passwd(const char *,uid_t,uid_type_t,bool)174*8d67ca89SAndroid Build Coastguard Worker static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */,
175*8d67ca89SAndroid Build Coastguard Worker                              bool /* check_username */) {
176*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
177*8d67ca89SAndroid Build Coastguard Worker }
178*8d67ca89SAndroid Build Coastguard Worker 
check_get_passwd(const char *,uid_t,uid_type_t)179*8d67ca89SAndroid Build Coastguard Worker static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
180*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
181*8d67ca89SAndroid Build Coastguard Worker }
182*8d67ca89SAndroid Build Coastguard Worker 
expect_no_passwd_id(uid_t)183*8d67ca89SAndroid Build Coastguard Worker static void expect_no_passwd_id(uid_t /* uid */) {
184*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
185*8d67ca89SAndroid Build Coastguard Worker }
186*8d67ca89SAndroid Build Coastguard Worker 
expect_no_passwd_name(const char *)187*8d67ca89SAndroid Build Coastguard Worker static void expect_no_passwd_name(const char* /* username */) {
188*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
189*8d67ca89SAndroid Build Coastguard Worker }
190*8d67ca89SAndroid Build Coastguard Worker 
191*8d67ca89SAndroid Build Coastguard Worker #endif
192*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_platform_ids)193*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_platform_ids) {
194*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("root", 0, TYPE_SYSTEM);
195*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("daemon", 1, TYPE_SYSTEM);
196*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("bin", 2, TYPE_SYSTEM);
197*8d67ca89SAndroid Build Coastguard Worker 
198*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("system", 1000, TYPE_SYSTEM);
199*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("radio", 1001, TYPE_SYSTEM);
200*8d67ca89SAndroid Build Coastguard Worker 
201*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("shell", 2000, TYPE_SYSTEM);
202*8d67ca89SAndroid Build Coastguard Worker 
203*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("nobody", 9999, TYPE_SYSTEM);
204*8d67ca89SAndroid Build Coastguard Worker }
205*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_oem_ids)206*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_oem_ids) {
207*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("oem_2900", 2900, TYPE_VENDOR, false);
208*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("oem_2945", 2945, TYPE_VENDOR, false);
209*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("oem_2999", 2999, TYPE_VENDOR, false);
210*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("oem_5000", 5000, TYPE_VENDOR, false);
211*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("oem_5454", 5454, TYPE_VENDOR, false);
212*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("oem_5999", 5999, TYPE_VENDOR, false);
213*8d67ca89SAndroid Build Coastguard Worker }
214*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_non_exist)215*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_non_exist) {
216*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(999);   // End of the system reserved range, unallocated.
217*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(1999);  // End of the system reserved range, unallocated.
218*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(2899);  // End of the system reserved range, unallocated.
219*8d67ca89SAndroid Build Coastguard Worker 
220*8d67ca89SAndroid Build Coastguard Worker   // These ranges are for GIDs only.
221*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(20000);
222*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(30000);
223*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(40000);
224*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(50000);
225*8d67ca89SAndroid Build Coastguard Worker 
226*8d67ca89SAndroid Build Coastguard Worker   // These should not be parsed as users, only as groups.
227*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u0_a9999_cache");
228*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u0_a9999_ext");
229*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u0_a9999_ext_cache");
230*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("all_a9999");
231*8d67ca89SAndroid Build Coastguard Worker }
232*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_u0_app_ids)233*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_u0_app_ids) {
234*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u0_a0", 10000, TYPE_APP);
235*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u0_a1234", 11234, TYPE_APP);
236*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u0_a9999", 19999, TYPE_APP);
237*8d67ca89SAndroid Build Coastguard Worker 
238*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u0_i1", 90001, TYPE_APP);
239*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u0_i4545", 94545, TYPE_APP);
240*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u0_i9999", 99999, TYPE_APP);
241*8d67ca89SAndroid Build Coastguard Worker }
242*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_app_id_u1_ids)243*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_app_id_u1_ids) {
244*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_system", 101000, TYPE_SYSTEM);
245*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
246*8d67ca89SAndroid Build Coastguard Worker 
247*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_a0", 110000, TYPE_APP);
248*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_a1234", 111234, TYPE_APP);
249*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_a9999", 119999, TYPE_APP);
250*8d67ca89SAndroid Build Coastguard Worker 
251*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_i1", 190001, TYPE_APP);
252*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_i4545", 194545, TYPE_APP);
253*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u1_i9999", 199999, TYPE_APP);
254*8d67ca89SAndroid Build Coastguard Worker }
255*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_app_id_u31_ids)256*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_app_id_u31_ids) {
257*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_system", 3101000, TYPE_SYSTEM);
258*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_radio", 3101001, TYPE_SYSTEM);
259*8d67ca89SAndroid Build Coastguard Worker 
260*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_a0", 3110000, TYPE_APP);
261*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_a1234", 3111234, TYPE_APP);
262*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_a9999", 3119999, TYPE_APP);
263*8d67ca89SAndroid Build Coastguard Worker 
264*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_i1", 3190001, TYPE_APP);
265*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_i4545", 3194545, TYPE_APP);
266*8d67ca89SAndroid Build Coastguard Worker   check_get_passwd("u31_i9999", 3199999, TYPE_APP);
267*8d67ca89SAndroid Build Coastguard Worker }
268*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_app_id_not_allowed_platform)269*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_app_id_not_allowed_platform) {
270*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u1_root");
271*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u1_debuggerd");
272*8d67ca89SAndroid Build Coastguard Worker 
273*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u31_root");
274*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_name("u31_debuggerd");
275*8d67ca89SAndroid Build Coastguard Worker }
276*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwuid_app_id_u1_non_exist)277*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwuid_app_id_u1_non_exist) {
278*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(100000);  // There is no 'root' for secondary users.
279*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(101999);  // End of the system reserved range, unallocated.
280*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(102900);  // The OEM ranges were never allocated to secondary users.
281*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(105000);  // The OEM ranges were never allocated to secondary users.
282*8d67ca89SAndroid Build Coastguard Worker 
283*8d67ca89SAndroid Build Coastguard Worker   // These ranges are for GIDs only.
284*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(120000);
285*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(130000);
286*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(140000);
287*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(150000);
288*8d67ca89SAndroid Build Coastguard Worker }
289*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwuid_app_id_u31_non_exist)290*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwuid_app_id_u31_non_exist) {
291*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3100000);  // There is no 'root' for secondary users.
292*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3101999);  // End of the system reserved range, unallocated.
293*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3102900);  // The OEM ranges were never allocated to secondary users.
294*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3105000);  // The OEM ranges were never allocated to secondary users.
295*8d67ca89SAndroid Build Coastguard Worker 
296*8d67ca89SAndroid Build Coastguard Worker   // These ranges are for GIDs only.
297*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3120000);
298*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3130000);
299*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3140000);
300*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(3150000);
301*8d67ca89SAndroid Build Coastguard Worker }
302*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_r_alignment)303*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_r_alignment) {
304*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
305*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage;
306*8d67ca89SAndroid Build Coastguard Worker   alignas(16) char buf[512];
307*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd;
308*8d67ca89SAndroid Build Coastguard Worker   int result = getpwnam_r("root", &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
309*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
310*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
311*8d67ca89SAndroid Build Coastguard Worker #else
312*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
313*8d67ca89SAndroid Build Coastguard Worker #endif
314*8d67ca89SAndroid Build Coastguard Worker }
315*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwuid_r_alignment)316*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwuid_r_alignment) {
317*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
318*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage;
319*8d67ca89SAndroid Build Coastguard Worker   alignas(16) char buf[512];
320*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd;
321*8d67ca89SAndroid Build Coastguard Worker   int result = getpwuid_r(0, &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
322*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
323*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
324*8d67ca89SAndroid Build Coastguard Worker #else
325*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
326*8d67ca89SAndroid Build Coastguard Worker #endif
327*8d67ca89SAndroid Build Coastguard Worker }
328*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_r_reentrancy)329*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_r_reentrancy) {
330*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
331*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage[2];
332*8d67ca89SAndroid Build Coastguard Worker   char buf[2][512];
333*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd[3];
334*8d67ca89SAndroid Build Coastguard Worker   int result = getpwnam_r("root", &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
335*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
336*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
337*8d67ca89SAndroid Build Coastguard Worker   pwd[1] = getpwnam("system");
338*8d67ca89SAndroid Build Coastguard Worker   ASSERT_NE(nullptr, pwd[1]);
339*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
340*8d67ca89SAndroid Build Coastguard Worker   result = getpwnam_r("radio", &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
341*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
342*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
343*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
344*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
345*8d67ca89SAndroid Build Coastguard Worker #else
346*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
347*8d67ca89SAndroid Build Coastguard Worker #endif
348*8d67ca89SAndroid Build Coastguard Worker }
349*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwuid_r_reentrancy)350*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwuid_r_reentrancy) {
351*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
352*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage[2];
353*8d67ca89SAndroid Build Coastguard Worker   char buf[2][512];
354*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd[3];
355*8d67ca89SAndroid Build Coastguard Worker   int result = getpwuid_r(0, &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
356*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
357*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
358*8d67ca89SAndroid Build Coastguard Worker   pwd[1] = getpwuid(1000);
359*8d67ca89SAndroid Build Coastguard Worker   ASSERT_NE(nullptr, pwd[1]);
360*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
361*8d67ca89SAndroid Build Coastguard Worker   result = getpwuid_r(1001, &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
362*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
363*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
364*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
365*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
366*8d67ca89SAndroid Build Coastguard Worker #else
367*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
368*8d67ca89SAndroid Build Coastguard Worker #endif
369*8d67ca89SAndroid Build Coastguard Worker }
370*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwnam_r_large_enough_suggested_buffer_size)371*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwnam_r_large_enough_suggested_buffer_size) {
372*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
373*8d67ca89SAndroid Build Coastguard Worker   long size = sysconf(_SC_GETPW_R_SIZE_MAX);
374*8d67ca89SAndroid Build Coastguard Worker   ASSERT_GT(size, 0);
375*8d67ca89SAndroid Build Coastguard Worker   char buf[size];
376*8d67ca89SAndroid Build Coastguard Worker   passwd pwd_storage;
377*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd;
378*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, getpwnam_r("root", &pwd_storage, buf, size, &pwd));
379*8d67ca89SAndroid Build Coastguard Worker   check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
380*8d67ca89SAndroid Build Coastguard Worker #else
381*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
382*8d67ca89SAndroid Build Coastguard Worker #endif
383*8d67ca89SAndroid Build Coastguard Worker }
384*8d67ca89SAndroid Build Coastguard Worker 
385*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
386*8d67ca89SAndroid Build Coastguard Worker template <typename T>
expect_ids(T ids,bool is_group)387*8d67ca89SAndroid Build Coastguard Worker static void expect_ids(T ids, bool is_group) {
388*8d67ca89SAndroid Build Coastguard Worker   std::set<typename T::key_type> expected_ids;
389*8d67ca89SAndroid Build Coastguard Worker   // Ensure that all android_ids are iterated through.
390*8d67ca89SAndroid Build Coastguard Worker   for (size_t n = 0; n < android_id_count; ++n) {
391*8d67ca89SAndroid Build Coastguard Worker     EXPECT_EQ(1U, ids.count(android_ids[n].aid)) << "android_ids[n].aid: " << android_ids[n].aid;
392*8d67ca89SAndroid Build Coastguard Worker     expected_ids.emplace(android_ids[n].aid);
393*8d67ca89SAndroid Build Coastguard Worker   }
394*8d67ca89SAndroid Build Coastguard Worker 
395*8d67ca89SAndroid Build Coastguard Worker   auto expect_range = [&ids, &expected_ids](uid_t start, uid_t end) {
396*8d67ca89SAndroid Build Coastguard Worker     for (size_t n = start; n <= end; ++n) {
397*8d67ca89SAndroid Build Coastguard Worker       EXPECT_EQ(1U, ids.count(n)) << "n: " << n;
398*8d67ca89SAndroid Build Coastguard Worker       expected_ids.emplace(n);
399*8d67ca89SAndroid Build Coastguard Worker     }
400*8d67ca89SAndroid Build Coastguard Worker   };
401*8d67ca89SAndroid Build Coastguard Worker 
402*8d67ca89SAndroid Build Coastguard Worker   // Ensure that all reserved ranges are iterated through.
403*8d67ca89SAndroid Build Coastguard Worker   expect_range(AID_OEM_RESERVED_START, AID_OEM_RESERVED_END);
404*8d67ca89SAndroid Build Coastguard Worker   expect_range(AID_OEM_RESERVED_2_START, AID_OEM_RESERVED_2_END);
405*8d67ca89SAndroid Build Coastguard Worker   expect_range(AID_APP_START, AID_APP_END);
406*8d67ca89SAndroid Build Coastguard Worker   if (is_group) {
407*8d67ca89SAndroid Build Coastguard Worker     expect_range(AID_CACHE_GID_START, AID_CACHE_GID_END);
408*8d67ca89SAndroid Build Coastguard Worker     expect_range(AID_EXT_GID_START, AID_EXT_GID_END);
409*8d67ca89SAndroid Build Coastguard Worker     expect_range(AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END);
410*8d67ca89SAndroid Build Coastguard Worker     expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
411*8d67ca89SAndroid Build Coastguard Worker   }
412*8d67ca89SAndroid Build Coastguard Worker   expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
413*8d67ca89SAndroid Build Coastguard Worker 
414*8d67ca89SAndroid Build Coastguard Worker   // Prior to R, we didn't have a mechanism to create vendor AIDs in the system or other non-vendor
415*8d67ca89SAndroid Build Coastguard Worker   // partitions, therefore we disabled the rest of these checks for older API levels.
416*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 29) {
417*8d67ca89SAndroid Build Coastguard Worker     return;
418*8d67ca89SAndroid Build Coastguard Worker   }
419*8d67ca89SAndroid Build Coastguard Worker 
420*8d67ca89SAndroid Build Coastguard Worker   auto allow_range = [&ids](uid_t start, uid_t end) {
421*8d67ca89SAndroid Build Coastguard Worker     for (size_t n = start; n <= end; ++n) {
422*8d67ca89SAndroid Build Coastguard Worker       ids.erase(n);
423*8d67ca89SAndroid Build Coastguard Worker     }
424*8d67ca89SAndroid Build Coastguard Worker   };
425*8d67ca89SAndroid Build Coastguard Worker 
426*8d67ca89SAndroid Build Coastguard Worker   allow_range(AID_SYSTEM_RESERVED_START, AID_SYSTEM_EXT_RESERVED_END);
427*8d67ca89SAndroid Build Coastguard Worker 
428*8d67ca89SAndroid Build Coastguard Worker   // Ensure that no other ids were returned.
429*8d67ca89SAndroid Build Coastguard Worker   auto return_differences = [&ids, &expected_ids] {
430*8d67ca89SAndroid Build Coastguard Worker     std::vector<typename T::key_type> missing_from_ids;
431*8d67ca89SAndroid Build Coastguard Worker     std::set_difference(expected_ids.begin(), expected_ids.end(), ids.begin(), ids.end(),
432*8d67ca89SAndroid Build Coastguard Worker                         std::inserter(missing_from_ids, missing_from_ids.begin()));
433*8d67ca89SAndroid Build Coastguard Worker     std::vector<typename T::key_type> extra_in_ids;
434*8d67ca89SAndroid Build Coastguard Worker     std::set_difference(ids.begin(), ids.end(), expected_ids.begin(), expected_ids.end(),
435*8d67ca89SAndroid Build Coastguard Worker                         std::inserter(extra_in_ids, extra_in_ids.begin()));
436*8d67ca89SAndroid Build Coastguard Worker     std::string result;
437*8d67ca89SAndroid Build Coastguard Worker     if (!missing_from_ids.empty()) {
438*8d67ca89SAndroid Build Coastguard Worker       result += "Missing ids from results: " + Join(missing_from_ids, " ");
439*8d67ca89SAndroid Build Coastguard Worker     }
440*8d67ca89SAndroid Build Coastguard Worker     if (!extra_in_ids.empty()) {
441*8d67ca89SAndroid Build Coastguard Worker       if (!result.empty()) result += ", ";
442*8d67ca89SAndroid Build Coastguard Worker       result += "Extra ids in results: " + Join(extra_in_ids, " ");
443*8d67ca89SAndroid Build Coastguard Worker     }
444*8d67ca89SAndroid Build Coastguard Worker     return result;
445*8d67ca89SAndroid Build Coastguard Worker   };
446*8d67ca89SAndroid Build Coastguard Worker 
447*8d67ca89SAndroid Build Coastguard Worker   // AID_UPROBESTATS (1093) was added in API level 35, but "trunk stable" means
448*8d67ca89SAndroid Build Coastguard Worker   // that the 2024Q* builds are tested with the _previous_ release's CTS.
449*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 34) {
450*8d67ca89SAndroid Build Coastguard Worker #if !defined(AID_UPROBESTATS)
451*8d67ca89SAndroid Build Coastguard Worker #define AID_UPROBESTATS 1093
452*8d67ca89SAndroid Build Coastguard Worker #endif
453*8d67ca89SAndroid Build Coastguard Worker     ids.erase(AID_UPROBESTATS);
454*8d67ca89SAndroid Build Coastguard Worker     expected_ids.erase(AID_UPROBESTATS);
455*8d67ca89SAndroid Build Coastguard Worker     if (getpwuid(AID_UPROBESTATS)) {
456*8d67ca89SAndroid Build Coastguard Worker       EXPECT_STREQ(getpwuid(AID_UPROBESTATS)->pw_name, "uprobestats");
457*8d67ca89SAndroid Build Coastguard Worker     }
458*8d67ca89SAndroid Build Coastguard Worker   }
459*8d67ca89SAndroid Build Coastguard Worker   // AID_VIRTUALMACHINE (3013) was added in API level 35, but "trunk stable" means
460*8d67ca89SAndroid Build Coastguard Worker   // that the 2024Q* builds are tested with the _previous_ release's CTS.
461*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 34) {
462*8d67ca89SAndroid Build Coastguard Worker #if !defined(AID_VIRTUALMACHINE)
463*8d67ca89SAndroid Build Coastguard Worker #define AID_VIRTUALMACHINE 3013
464*8d67ca89SAndroid Build Coastguard Worker #endif
465*8d67ca89SAndroid Build Coastguard Worker     ids.erase(AID_VIRTUALMACHINE);
466*8d67ca89SAndroid Build Coastguard Worker     expected_ids.erase(AID_VIRTUALMACHINE);
467*8d67ca89SAndroid Build Coastguard Worker     if (getpwuid(AID_VIRTUALMACHINE)) {
468*8d67ca89SAndroid Build Coastguard Worker       EXPECT_STREQ(getpwuid(AID_VIRTUALMACHINE)->pw_name, "virtualmachine");
469*8d67ca89SAndroid Build Coastguard Worker     }
470*8d67ca89SAndroid Build Coastguard Worker   }
471*8d67ca89SAndroid Build Coastguard Worker   // AID_CROS_EC (1094) was added in API level 36, but "trunk stable" means
472*8d67ca89SAndroid Build Coastguard Worker   // that the 2024Q* builds are tested with the _previous_ release's CTS.
473*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 35) {
474*8d67ca89SAndroid Build Coastguard Worker #if !defined(AID_CROS_EC)
475*8d67ca89SAndroid Build Coastguard Worker #define AID_CROS_EC 1094
476*8d67ca89SAndroid Build Coastguard Worker #endif
477*8d67ca89SAndroid Build Coastguard Worker     ids.erase(AID_CROS_EC);
478*8d67ca89SAndroid Build Coastguard Worker     expected_ids.erase(AID_CROS_EC);
479*8d67ca89SAndroid Build Coastguard Worker     if (getpwuid(AID_CROS_EC)) {
480*8d67ca89SAndroid Build Coastguard Worker       EXPECT_STREQ(getpwuid(AID_CROS_EC)->pw_name, "cros_ec");
481*8d67ca89SAndroid Build Coastguard Worker     }
482*8d67ca89SAndroid Build Coastguard Worker   }
483*8d67ca89SAndroid Build Coastguard Worker   // AID_MMD (1095) was added in API level 36, but "trunk stable" means
484*8d67ca89SAndroid Build Coastguard Worker   // that the 2024Q* builds are tested with the _previous_ release's CTS.
485*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.build.version.sdk", 0) == 35) {
486*8d67ca89SAndroid Build Coastguard Worker #if !defined(AID_MMD)
487*8d67ca89SAndroid Build Coastguard Worker #define AID_MMD 1095
488*8d67ca89SAndroid Build Coastguard Worker #endif
489*8d67ca89SAndroid Build Coastguard Worker     ids.erase(AID_MMD);
490*8d67ca89SAndroid Build Coastguard Worker     expected_ids.erase(AID_MMD);
491*8d67ca89SAndroid Build Coastguard Worker     if (getpwuid(AID_MMD)) {
492*8d67ca89SAndroid Build Coastguard Worker       EXPECT_STREQ(getpwuid(AID_MMD)->pw_name, "mmd");
493*8d67ca89SAndroid Build Coastguard Worker     }
494*8d67ca89SAndroid Build Coastguard Worker   }
495*8d67ca89SAndroid Build Coastguard Worker 
496*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(expected_ids, ids) << return_differences();
497*8d67ca89SAndroid Build Coastguard Worker }
498*8d67ca89SAndroid Build Coastguard Worker #endif
499*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,getpwent_iterate)500*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, getpwent_iterate) {
501*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
502*8d67ca89SAndroid Build Coastguard Worker   passwd* pwd;
503*8d67ca89SAndroid Build Coastguard Worker   std::set<uid_t> uids;
504*8d67ca89SAndroid Build Coastguard Worker 
505*8d67ca89SAndroid Build Coastguard Worker   setpwent();
506*8d67ca89SAndroid Build Coastguard Worker   while ((pwd = getpwent()) != nullptr) {
507*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(nullptr != pwd->pw_name);
508*8d67ca89SAndroid Build Coastguard Worker 
509*8d67ca89SAndroid Build Coastguard Worker     EXPECT_EQ(pwd->pw_gid, pwd->pw_uid) << "pwd->pw_uid: " << pwd->pw_uid;
510*8d67ca89SAndroid Build Coastguard Worker     EXPECT_EQ(nullptr, pwd->pw_passwd) << "pwd->pw_uid: " << pwd->pw_uid;
511*8d67ca89SAndroid Build Coastguard Worker #ifdef __LP64__
512*8d67ca89SAndroid Build Coastguard Worker     EXPECT_TRUE(nullptr == pwd->pw_gecos) << "pwd->pw_uid: " << pwd->pw_uid;
513*8d67ca89SAndroid Build Coastguard Worker #endif
514*8d67ca89SAndroid Build Coastguard Worker     EXPECT_TRUE(nullptr != pwd->pw_shell);
515*8d67ca89SAndroid Build Coastguard Worker     if (pwd->pw_uid < AID_APP_START || pwd->pw_uid == AID_OVERFLOWUID) {
516*8d67ca89SAndroid Build Coastguard Worker       EXPECT_STREQ("/", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
517*8d67ca89SAndroid Build Coastguard Worker     } else {
518*8d67ca89SAndroid Build Coastguard Worker       EXPECT_STREQ("/data", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
519*8d67ca89SAndroid Build Coastguard Worker     }
520*8d67ca89SAndroid Build Coastguard Worker 
521*8d67ca89SAndroid Build Coastguard Worker     EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid;
522*8d67ca89SAndroid Build Coastguard Worker     uids.emplace(pwd->pw_uid);
523*8d67ca89SAndroid Build Coastguard Worker   }
524*8d67ca89SAndroid Build Coastguard Worker   endpwent();
525*8d67ca89SAndroid Build Coastguard Worker 
526*8d67ca89SAndroid Build Coastguard Worker   expect_ids(uids, false);
527*8d67ca89SAndroid Build Coastguard Worker #else
528*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
529*8d67ca89SAndroid Build Coastguard Worker #endif
530*8d67ca89SAndroid Build Coastguard Worker }
531*8d67ca89SAndroid Build Coastguard Worker 
check_group(const group * grp,const char * group_name,gid_t gid,bool check_groupname=true)532*8d67ca89SAndroid Build Coastguard Worker static void check_group(const group* grp, const char* group_name, gid_t gid,
533*8d67ca89SAndroid Build Coastguard Worker                         bool check_groupname = true) {
534*8d67ca89SAndroid Build Coastguard Worker   ASSERT_TRUE(grp != nullptr);
535*8d67ca89SAndroid Build Coastguard Worker   if (check_groupname) {
536*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ(group_name, grp->gr_name);
537*8d67ca89SAndroid Build Coastguard Worker   }
538*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(gid, grp->gr_gid);
539*8d67ca89SAndroid Build Coastguard Worker   ASSERT_TRUE(grp->gr_mem != nullptr);
540*8d67ca89SAndroid Build Coastguard Worker   if (check_groupname) {
541*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ(group_name, grp->gr_mem[0]);
542*8d67ca89SAndroid Build Coastguard Worker   }
543*8d67ca89SAndroid Build Coastguard Worker   EXPECT_TRUE(grp->gr_mem[1] == nullptr);
544*8d67ca89SAndroid Build Coastguard Worker }
545*8d67ca89SAndroid Build Coastguard Worker 
546*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
547*8d67ca89SAndroid Build Coastguard Worker 
check_getgrgid(const char * group_name,gid_t gid,bool check_groupname)548*8d67ca89SAndroid Build Coastguard Worker static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
549*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
550*8d67ca89SAndroid Build Coastguard Worker   group* grp = getgrgid(gid);
551*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
552*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getgrgid");
553*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, group_name, gid, check_groupname);
554*8d67ca89SAndroid Build Coastguard Worker }
555*8d67ca89SAndroid Build Coastguard Worker 
check_getgrnam(const char * group_name,gid_t gid,bool check_groupname)556*8d67ca89SAndroid Build Coastguard Worker static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
557*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
558*8d67ca89SAndroid Build Coastguard Worker   group* grp = getgrnam(group_name);
559*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
560*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getgrnam");
561*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, group_name, gid, check_groupname);
562*8d67ca89SAndroid Build Coastguard Worker }
563*8d67ca89SAndroid Build Coastguard Worker 
check_getgrgid_r(const char * group_name,gid_t gid,bool check_groupname)564*8d67ca89SAndroid Build Coastguard Worker static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) {
565*8d67ca89SAndroid Build Coastguard Worker   group grp_storage;
566*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
567*8d67ca89SAndroid Build Coastguard Worker   group* grp;
568*8d67ca89SAndroid Build Coastguard Worker 
569*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
570*8d67ca89SAndroid Build Coastguard Worker   int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
571*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
572*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
573*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getgrgid_r");
574*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, group_name, gid, check_groupname);
575*8d67ca89SAndroid Build Coastguard Worker }
576*8d67ca89SAndroid Build Coastguard Worker 
check_getgrnam_r(const char * group_name,gid_t gid,bool check_groupname)577*8d67ca89SAndroid Build Coastguard Worker static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) {
578*8d67ca89SAndroid Build Coastguard Worker   group grp_storage;
579*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
580*8d67ca89SAndroid Build Coastguard Worker   group* grp;
581*8d67ca89SAndroid Build Coastguard Worker 
582*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
583*8d67ca89SAndroid Build Coastguard Worker   int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
584*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
585*8d67ca89SAndroid Build Coastguard Worker   ASSERT_ERRNO(0);
586*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("getgrnam_r");
587*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, group_name, gid, check_groupname);
588*8d67ca89SAndroid Build Coastguard Worker }
589*8d67ca89SAndroid Build Coastguard Worker 
check_get_group(const char * group_name,gid_t gid,bool check_groupname=true)590*8d67ca89SAndroid Build Coastguard Worker static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) {
591*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("groupname '"s + group_name + "'");
592*8d67ca89SAndroid Build Coastguard Worker   check_getgrgid(group_name, gid, check_groupname);
593*8d67ca89SAndroid Build Coastguard Worker   check_getgrnam(group_name, gid, check_groupname);
594*8d67ca89SAndroid Build Coastguard Worker   check_getgrgid_r(group_name, gid, check_groupname);
595*8d67ca89SAndroid Build Coastguard Worker   check_getgrnam_r(group_name, gid, check_groupname);
596*8d67ca89SAndroid Build Coastguard Worker }
597*8d67ca89SAndroid Build Coastguard Worker 
expect_no_group_id(gid_t gid)598*8d67ca89SAndroid Build Coastguard Worker static void expect_no_group_id(gid_t gid) {
599*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("gid '" + std::to_string(gid) + "'");
600*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
601*8d67ca89SAndroid Build Coastguard Worker   group* group = nullptr;
602*8d67ca89SAndroid Build Coastguard Worker   group = getgrgid(gid);
603*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
604*8d67ca89SAndroid Build Coastguard Worker   EXPECT_ERRNO(ENOENT);
605*8d67ca89SAndroid Build Coastguard Worker 
606*8d67ca89SAndroid Build Coastguard Worker   struct group group_storage;
607*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
608*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group));
609*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
610*8d67ca89SAndroid Build Coastguard Worker }
611*8d67ca89SAndroid Build Coastguard Worker 
expect_no_group_name(const char * groupname)612*8d67ca89SAndroid Build Coastguard Worker static void expect_no_group_name(const char* groupname) {
613*8d67ca89SAndroid Build Coastguard Worker   SCOPED_TRACE("groupname '"s + groupname + "'");
614*8d67ca89SAndroid Build Coastguard Worker   errno = 0;
615*8d67ca89SAndroid Build Coastguard Worker   group* group = nullptr;
616*8d67ca89SAndroid Build Coastguard Worker   group = getgrnam(groupname);
617*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
618*8d67ca89SAndroid Build Coastguard Worker   EXPECT_ERRNO(ENOENT);
619*8d67ca89SAndroid Build Coastguard Worker 
620*8d67ca89SAndroid Build Coastguard Worker   struct group group_storage;
621*8d67ca89SAndroid Build Coastguard Worker   char buf[512];
622*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group));
623*8d67ca89SAndroid Build Coastguard Worker   EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
624*8d67ca89SAndroid Build Coastguard Worker }
625*8d67ca89SAndroid Build Coastguard Worker 
626*8d67ca89SAndroid Build Coastguard Worker #else // !defined(__BIONIC__)
627*8d67ca89SAndroid Build Coastguard Worker 
check_get_group(const char *,gid_t,bool)628*8d67ca89SAndroid Build Coastguard Worker static void check_get_group(const char*, gid_t, bool) {
629*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
630*8d67ca89SAndroid Build Coastguard Worker }
631*8d67ca89SAndroid Build Coastguard Worker 
check_get_group(const char *,gid_t)632*8d67ca89SAndroid Build Coastguard Worker static void check_get_group(const char*, gid_t) {
633*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
634*8d67ca89SAndroid Build Coastguard Worker }
635*8d67ca89SAndroid Build Coastguard Worker 
expect_no_group_id(gid_t)636*8d67ca89SAndroid Build Coastguard Worker static void expect_no_group_id(gid_t /* gid */) {
637*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
638*8d67ca89SAndroid Build Coastguard Worker }
639*8d67ca89SAndroid Build Coastguard Worker 
expect_no_group_name(const char *)640*8d67ca89SAndroid Build Coastguard Worker static void expect_no_group_name(const char* /* groupname */) {
641*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
642*8d67ca89SAndroid Build Coastguard Worker }
643*8d67ca89SAndroid Build Coastguard Worker 
644*8d67ca89SAndroid Build Coastguard Worker #endif
645*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_platform_ids)646*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_platform_ids) {
647*8d67ca89SAndroid Build Coastguard Worker   check_get_group("root", 0);
648*8d67ca89SAndroid Build Coastguard Worker   check_get_group("daemon", 1);
649*8d67ca89SAndroid Build Coastguard Worker   check_get_group("bin", 2);
650*8d67ca89SAndroid Build Coastguard Worker 
651*8d67ca89SAndroid Build Coastguard Worker   check_get_group("system", 1000);
652*8d67ca89SAndroid Build Coastguard Worker   check_get_group("radio", 1001);
653*8d67ca89SAndroid Build Coastguard Worker 
654*8d67ca89SAndroid Build Coastguard Worker   check_get_group("shell", 2000);
655*8d67ca89SAndroid Build Coastguard Worker 
656*8d67ca89SAndroid Build Coastguard Worker   check_get_group("nobody", 9999);
657*8d67ca89SAndroid Build Coastguard Worker }
658*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_oem_ids)659*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_oem_ids) {
660*8d67ca89SAndroid Build Coastguard Worker   check_get_group("oem_2900", 2900, false);
661*8d67ca89SAndroid Build Coastguard Worker   check_get_group("oem_2945", 2945, false);
662*8d67ca89SAndroid Build Coastguard Worker   check_get_group("oem_2999", 2999, false);
663*8d67ca89SAndroid Build Coastguard Worker   check_get_group("oem_5000", 5000, false);
664*8d67ca89SAndroid Build Coastguard Worker   check_get_group("oem_5454", 5454, false);
665*8d67ca89SAndroid Build Coastguard Worker   check_get_group("oem_5999", 5999, false);
666*8d67ca89SAndroid Build Coastguard Worker }
667*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_non_exist)668*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_non_exist) {
669*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(999);   // End of the system reserved range, unallocated.
670*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(1999);  // End of the system reserved range, unallocated.
671*8d67ca89SAndroid Build Coastguard Worker   expect_no_passwd_id(2899);  // End of the system reserved range, unallocated.
672*8d67ca89SAndroid Build Coastguard Worker }
673*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_u0_app_ids)674*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_u0_app_ids) {
675*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a0", 10000);
676*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a1234", 11234);
677*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a9999", 19999);
678*8d67ca89SAndroid Build Coastguard Worker 
679*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a0_cache", 20000);
680*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a1234_cache", 21234);
681*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a9999_cache", 29999);
682*8d67ca89SAndroid Build Coastguard Worker 
683*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a0_ext", 30000);
684*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a4545_ext", 34545);
685*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a9999_ext", 39999);
686*8d67ca89SAndroid Build Coastguard Worker 
687*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a0_ext_cache", 40000);
688*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a4545_ext_cache", 44545);
689*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_a9999_ext_cache", 49999);
690*8d67ca89SAndroid Build Coastguard Worker 
691*8d67ca89SAndroid Build Coastguard Worker   check_get_group("all_a0", 50000);
692*8d67ca89SAndroid Build Coastguard Worker   check_get_group("all_a4545", 54545);
693*8d67ca89SAndroid Build Coastguard Worker   check_get_group("all_a9999", 59999);
694*8d67ca89SAndroid Build Coastguard Worker 
695*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u0_i1", 90001);
696*8d67ca89SAndroid Build Coastguard Worker }
697*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_u1_app_ids)698*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_u1_app_ids) {
699*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_system", 101000);
700*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_radio", 101001);
701*8d67ca89SAndroid Build Coastguard Worker 
702*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a0", 110000);
703*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a1234", 111234);
704*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a9999", 119999);
705*8d67ca89SAndroid Build Coastguard Worker 
706*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a0_cache", 120000);
707*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a1234_cache", 121234);
708*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a9999_cache", 129999);
709*8d67ca89SAndroid Build Coastguard Worker 
710*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a0_ext", 130000);
711*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a4545_ext", 134545);
712*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a9999_ext", 139999);
713*8d67ca89SAndroid Build Coastguard Worker 
714*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a0_ext_cache", 140000);
715*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a4545_ext_cache", 144545);
716*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_a9999_ext_cache", 149999);
717*8d67ca89SAndroid Build Coastguard Worker 
718*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u1_i1", 190001);
719*8d67ca89SAndroid Build Coastguard Worker }
720*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_u31_app_ids)721*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_u31_app_ids) {
722*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_system", 3101000);
723*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_radio", 3101001);
724*8d67ca89SAndroid Build Coastguard Worker 
725*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a0", 3110000);
726*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a1234", 3111234);
727*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a9999", 3119999);
728*8d67ca89SAndroid Build Coastguard Worker 
729*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a0_cache", 3120000);
730*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a1234_cache", 3121234);
731*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a9999_cache", 3129999);
732*8d67ca89SAndroid Build Coastguard Worker 
733*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a0_cache", 3120000);
734*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a1234_cache", 3121234);
735*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a9999_cache", 3129999);
736*8d67ca89SAndroid Build Coastguard Worker 
737*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a0_ext", 3130000);
738*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a4545_ext", 3134545);
739*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a9999_ext", 3139999);
740*8d67ca89SAndroid Build Coastguard Worker 
741*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a0_ext_cache", 3140000);
742*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a4545_ext_cache", 3144545);
743*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_a9999_ext_cache", 3149999);
744*8d67ca89SAndroid Build Coastguard Worker 
745*8d67ca89SAndroid Build Coastguard Worker   check_get_group("u31_i1", 3190001);
746*8d67ca89SAndroid Build Coastguard Worker }
747*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getpgram_app_id_not_allowed_platform)748*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getpgram_app_id_not_allowed_platform) {
749*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_name("u1_root");
750*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_name("u1_debuggerd");
751*8d67ca89SAndroid Build Coastguard Worker 
752*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_name("u31_root");
753*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_name("u31_debuggerd");
754*8d67ca89SAndroid Build Coastguard Worker }
755*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrgid_app_id_u1_non_exist)756*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrgid_app_id_u1_non_exist) {
757*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(100000);  // There is no 'root' for secondary users.
758*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(101999);  // End of the system reserved range, unallocated.
759*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(102900);  // The OEM ranges were never allocated to secondary users.
760*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(105000);  // The OEM ranges were never allocated to secondary users.
761*8d67ca89SAndroid Build Coastguard Worker 
762*8d67ca89SAndroid Build Coastguard Worker   // The shared range is shared among users, and therefore doesn't exist for secondary users.
763*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(150000);
764*8d67ca89SAndroid Build Coastguard Worker }
765*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrgid_app_id_u31_non_exist)766*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrgid_app_id_u31_non_exist) {
767*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(3100000);  // There is no 'root' for secondary users.
768*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(3101999);  // End of the system reserved range, unallocated.
769*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(3102900);  // The OEM ranges were never allocated to secondary users.
770*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(3105000);  // The OEM ranges were never allocated to secondary users.
771*8d67ca89SAndroid Build Coastguard Worker 
772*8d67ca89SAndroid Build Coastguard Worker   // The shared range is shared among users, and therefore doesn't exist for secondary users.
773*8d67ca89SAndroid Build Coastguard Worker   expect_no_group_id(3150000);
774*8d67ca89SAndroid Build Coastguard Worker }
775*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_r_alignment)776*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_r_alignment) {
777*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
778*8d67ca89SAndroid Build Coastguard Worker   group grp_storage;
779*8d67ca89SAndroid Build Coastguard Worker   alignas(16) char buf[512];
780*8d67ca89SAndroid Build Coastguard Worker   group* grp;
781*8d67ca89SAndroid Build Coastguard Worker   int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
782*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
783*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, "root", 0);
784*8d67ca89SAndroid Build Coastguard Worker #else
785*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
786*8d67ca89SAndroid Build Coastguard Worker #endif
787*8d67ca89SAndroid Build Coastguard Worker }
788*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrgid_r_alignment)789*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrgid_r_alignment) {
790*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
791*8d67ca89SAndroid Build Coastguard Worker   group grp_storage;
792*8d67ca89SAndroid Build Coastguard Worker   alignas(16) char buf[512];
793*8d67ca89SAndroid Build Coastguard Worker   group* grp;
794*8d67ca89SAndroid Build Coastguard Worker   int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
795*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
796*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, "root", 0);
797*8d67ca89SAndroid Build Coastguard Worker #else
798*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
799*8d67ca89SAndroid Build Coastguard Worker #endif
800*8d67ca89SAndroid Build Coastguard Worker }
801*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_r_reentrancy)802*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_r_reentrancy) {
803*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
804*8d67ca89SAndroid Build Coastguard Worker   group grp_storage[2];
805*8d67ca89SAndroid Build Coastguard Worker   char buf[2][512];
806*8d67ca89SAndroid Build Coastguard Worker   group* grp[3];
807*8d67ca89SAndroid Build Coastguard Worker   int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
808*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
809*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[0], "root", 0);
810*8d67ca89SAndroid Build Coastguard Worker   grp[1] = getgrnam("system");
811*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[1], "system", 1000);
812*8d67ca89SAndroid Build Coastguard Worker   result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
813*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
814*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[2], "radio", 1001);
815*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[0], "root", 0);
816*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[1], "system", 1000);
817*8d67ca89SAndroid Build Coastguard Worker #else
818*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
819*8d67ca89SAndroid Build Coastguard Worker #endif
820*8d67ca89SAndroid Build Coastguard Worker }
821*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrgid_r_reentrancy)822*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrgid_r_reentrancy) {
823*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
824*8d67ca89SAndroid Build Coastguard Worker   group grp_storage[2];
825*8d67ca89SAndroid Build Coastguard Worker   char buf[2][512];
826*8d67ca89SAndroid Build Coastguard Worker   group* grp[3];
827*8d67ca89SAndroid Build Coastguard Worker   int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
828*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
829*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[0], "root", 0);
830*8d67ca89SAndroid Build Coastguard Worker   grp[1] = getgrgid(1000);
831*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[1], "system", 1000);
832*8d67ca89SAndroid Build Coastguard Worker   result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
833*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, result);
834*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[2], "radio", 1001);
835*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[0], "root", 0);
836*8d67ca89SAndroid Build Coastguard Worker   check_group(grp[1], "system", 1000);
837*8d67ca89SAndroid Build Coastguard Worker #else
838*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
839*8d67ca89SAndroid Build Coastguard Worker #endif
840*8d67ca89SAndroid Build Coastguard Worker }
841*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrnam_r_large_enough_suggested_buffer_size)842*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) {
843*8d67ca89SAndroid Build Coastguard Worker   long size = sysconf(_SC_GETGR_R_SIZE_MAX);
844*8d67ca89SAndroid Build Coastguard Worker   ASSERT_GT(size, 0);
845*8d67ca89SAndroid Build Coastguard Worker   char buf[size];
846*8d67ca89SAndroid Build Coastguard Worker   group grp_storage;
847*8d67ca89SAndroid Build Coastguard Worker   group* grp;
848*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
849*8d67ca89SAndroid Build Coastguard Worker   check_group(grp, "root", 0);
850*8d67ca89SAndroid Build Coastguard Worker }
851*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrent_iterate)852*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrent_iterate) {
853*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
854*8d67ca89SAndroid Build Coastguard Worker   group* grp;
855*8d67ca89SAndroid Build Coastguard Worker   std::set<gid_t> gids;
856*8d67ca89SAndroid Build Coastguard Worker 
857*8d67ca89SAndroid Build Coastguard Worker   setgrent();
858*8d67ca89SAndroid Build Coastguard Worker   while ((grp = getgrent()) != nullptr) {
859*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid;
860*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid;
861*8d67ca89SAndroid Build Coastguard Worker     EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid;
862*8d67ca89SAndroid Build Coastguard Worker     EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid;
863*8d67ca89SAndroid Build Coastguard Worker 
864*8d67ca89SAndroid Build Coastguard Worker     EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid;
865*8d67ca89SAndroid Build Coastguard Worker     gids.emplace(grp->gr_gid);
866*8d67ca89SAndroid Build Coastguard Worker   }
867*8d67ca89SAndroid Build Coastguard Worker   endgrent();
868*8d67ca89SAndroid Build Coastguard Worker 
869*8d67ca89SAndroid Build Coastguard Worker   expect_ids(gids, true);
870*8d67ca89SAndroid Build Coastguard Worker #else
871*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
872*8d67ca89SAndroid Build Coastguard Worker #endif
873*8d67ca89SAndroid Build Coastguard Worker }
874*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,getgrouplist)875*8d67ca89SAndroid Build Coastguard Worker TEST(grp, getgrouplist) {
876*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
877*8d67ca89SAndroid Build Coastguard Worker   // Query the number of groups.
878*8d67ca89SAndroid Build Coastguard Worker   int ngroups = 0;
879*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(-1, getgrouplist("root", 123, nullptr, &ngroups));
880*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(1, ngroups);
881*8d67ca89SAndroid Build Coastguard Worker 
882*8d67ca89SAndroid Build Coastguard Worker   // Query the specific groups (just the one you pass in on Android).
883*8d67ca89SAndroid Build Coastguard Worker   ngroups = 8;
884*8d67ca89SAndroid Build Coastguard Worker   gid_t groups[ngroups];
885*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(1, getgrouplist("root", 123, groups, &ngroups));
886*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(1, ngroups);
887*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(123u, groups[0]);
888*8d67ca89SAndroid Build Coastguard Worker #else
889*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test (groups too unpredictable)";
890*8d67ca89SAndroid Build Coastguard Worker #endif
891*8d67ca89SAndroid Build Coastguard Worker }
892*8d67ca89SAndroid Build Coastguard Worker 
TEST(grp,initgroups)893*8d67ca89SAndroid Build Coastguard Worker TEST(grp, initgroups) {
894*8d67ca89SAndroid Build Coastguard Worker   if (getuid() != 0) GTEST_SKIP() << "test requires root";
895*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0, initgroups("root", 0));
896*8d67ca89SAndroid Build Coastguard Worker }
897*8d67ca89SAndroid Build Coastguard Worker 
898*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
TestAidNamePrefix(const std::string & file_path)899*8d67ca89SAndroid Build Coastguard Worker static void TestAidNamePrefix(const std::string& file_path) {
900*8d67ca89SAndroid Build Coastguard Worker   std::string file_contents;
901*8d67ca89SAndroid Build Coastguard Worker   if (!ReadFileToString(file_path, &file_contents)) {
902*8d67ca89SAndroid Build Coastguard Worker     // If we cannot read this file, then there are no vendor defind AID names, in which case this
903*8d67ca89SAndroid Build Coastguard Worker     // test passes by default.
904*8d67ca89SAndroid Build Coastguard Worker     return;
905*8d67ca89SAndroid Build Coastguard Worker   }
906*8d67ca89SAndroid Build Coastguard Worker   auto lines = Split(file_contents, "\n");
907*8d67ca89SAndroid Build Coastguard Worker   for (const auto& line : lines) {
908*8d67ca89SAndroid Build Coastguard Worker     if (line.empty()) continue;
909*8d67ca89SAndroid Build Coastguard Worker     auto name = Split(line, ":")[0];
910*8d67ca89SAndroid Build Coastguard Worker     EXPECT_TRUE(StartsWith(name, "vendor_"));
911*8d67ca89SAndroid Build Coastguard Worker   }
912*8d67ca89SAndroid Build Coastguard Worker }
913*8d67ca89SAndroid Build Coastguard Worker #endif
914*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,vendor_prefix_users)915*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, vendor_prefix_users) {
916*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
917*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
918*8d67ca89SAndroid Build Coastguard Worker     return;
919*8d67ca89SAndroid Build Coastguard Worker   }
920*8d67ca89SAndroid Build Coastguard Worker 
921*8d67ca89SAndroid Build Coastguard Worker   TestAidNamePrefix("/vendor/etc/passwd");
922*8d67ca89SAndroid Build Coastguard Worker #else
923*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
924*8d67ca89SAndroid Build Coastguard Worker #endif
925*8d67ca89SAndroid Build Coastguard Worker }
926*8d67ca89SAndroid Build Coastguard Worker 
TEST(pwd,vendor_prefix_groups)927*8d67ca89SAndroid Build Coastguard Worker TEST(pwd, vendor_prefix_groups) {
928*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
929*8d67ca89SAndroid Build Coastguard Worker   if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
930*8d67ca89SAndroid Build Coastguard Worker     return;
931*8d67ca89SAndroid Build Coastguard Worker   }
932*8d67ca89SAndroid Build Coastguard Worker 
933*8d67ca89SAndroid Build Coastguard Worker   TestAidNamePrefix("/vendor/etc/group");
934*8d67ca89SAndroid Build Coastguard Worker #else
935*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
936*8d67ca89SAndroid Build Coastguard Worker #endif
937*8d67ca89SAndroid Build Coastguard Worker }
938