Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 19 | // Below are the header files we want to test. |
| 20 | #include <grp.h> |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 22 | |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
| 26 | #include <sys/types.h> |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 29 | enum uid_type_t { |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 30 | TYPE_SYSTEM, |
| 31 | TYPE_APP |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 32 | }; |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 33 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 34 | #if defined(__BIONIC__) |
| 35 | |
| 36 | static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type) { |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 37 | ASSERT_TRUE(pwd != NULL); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 38 | ASSERT_STREQ(username, pwd->pw_name); |
| 39 | ASSERT_EQ(uid, pwd->pw_uid); |
| 40 | ASSERT_EQ(uid, pwd->pw_gid); |
Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 41 | ASSERT_EQ(NULL, pwd->pw_passwd); |
| 42 | #ifdef __LP64__ |
| 43 | ASSERT_EQ(NULL, pwd->pw_gecos); |
| 44 | #endif |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 45 | |
| 46 | if (uid_type == TYPE_SYSTEM) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 47 | ASSERT_STREQ("/", pwd->pw_dir); |
| 48 | } else { |
| 49 | ASSERT_STREQ("/data", pwd->pw_dir); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 50 | } |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 51 | ASSERT_STREQ("/system/bin/sh", pwd->pw_shell); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 52 | } |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 53 | |
| 54 | static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type) { |
| 55 | errno = 0; |
| 56 | passwd* pwd = getpwuid(uid); |
| 57 | ASSERT_EQ(0, errno); |
| 58 | SCOPED_TRACE("getpwuid"); |
| 59 | check_passwd(pwd, username, uid, uid_type); |
| 60 | } |
| 61 | |
| 62 | static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) { |
| 63 | errno = 0; |
| 64 | passwd* pwd = getpwnam(username); |
| 65 | ASSERT_EQ(0, errno); |
| 66 | SCOPED_TRACE("getpwnam"); |
| 67 | check_passwd(pwd, username, uid, uid_type); |
| 68 | } |
| 69 | |
| 70 | static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type) { |
| 71 | passwd pwd_storage; |
| 72 | char buf[512]; |
| 73 | int result; |
| 74 | |
| 75 | errno = 0; |
| 76 | passwd* pwd = NULL; |
| 77 | result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd); |
| 78 | ASSERT_EQ(0, result); |
| 79 | ASSERT_EQ(0, errno); |
| 80 | SCOPED_TRACE("getpwuid_r"); |
| 81 | check_passwd(pwd, username, uid, uid_type); |
| 82 | } |
| 83 | |
| 84 | static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type) { |
| 85 | passwd pwd_storage; |
| 86 | char buf[512]; |
| 87 | int result; |
| 88 | |
| 89 | errno = 0; |
| 90 | passwd* pwd = NULL; |
| 91 | result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd); |
| 92 | ASSERT_EQ(0, result); |
| 93 | ASSERT_EQ(0, errno); |
| 94 | SCOPED_TRACE("getpwnam_r"); |
| 95 | check_passwd(pwd, username, uid, uid_type); |
| 96 | } |
| 97 | |
| 98 | static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type) { |
| 99 | check_getpwuid(username, uid, uid_type); |
| 100 | check_getpwnam(username, uid, uid_type); |
| 101 | check_getpwuid_r(username, uid, uid_type); |
| 102 | check_getpwnam_r(username, uid, uid_type); |
| 103 | } |
| 104 | |
| 105 | #else // !defined(__BIONIC__) |
| 106 | |
| 107 | static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) { |
| 108 | GTEST_LOG_(INFO) << "This test is about uid/username translation for Android, which does nothing on libc other than bionic.\n"; |
| 109 | } |
| 110 | |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 111 | #endif |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 112 | |
| 113 | TEST(getpwnam, system_id_root) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 114 | check_get_passwd("root", 0, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(getpwnam, system_id_system) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 118 | check_get_passwd("system", 1000, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST(getpwnam, app_id_radio) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 122 | check_get_passwd("radio", 1001, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST(getpwnam, app_id_nobody) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 126 | check_get_passwd("nobody", 9999, TYPE_SYSTEM); |
Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 129 | TEST(getpwnam, app_id_u0_a0) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 130 | check_get_passwd("u0_a0", 10000, TYPE_APP); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST(getpwnam, app_id_u0_a1234) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 134 | check_get_passwd("u0_a1234", 11234, TYPE_APP); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 137 | // Test the difference between uid and shared gid. |
| 138 | TEST(getpwnam, app_id_u0_a49999) { |
| 139 | check_get_passwd("u0_a49999", 59999, TYPE_APP); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 142 | TEST(getpwnam, app_id_u0_i1) { |
| 143 | check_get_passwd("u0_i1", 99001, TYPE_APP); |
| 144 | } |
| 145 | |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 146 | TEST(getpwnam, app_id_u1_root) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 147 | check_get_passwd("u1_root", 100000, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST(getpwnam, app_id_u1_radio) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 151 | check_get_passwd("u1_radio", 101001, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST(getpwnam, app_id_u1_a0) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 155 | check_get_passwd("u1_a0", 110000, TYPE_APP); |
| 156 | } |
| 157 | |
| 158 | TEST(getpwnam, app_id_u1_a40000) { |
| 159 | check_get_passwd("u1_a40000", 150000, TYPE_APP); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST(getpwnam, app_id_u1_i0) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 163 | check_get_passwd("u1_i0", 199000, TYPE_APP); |
| 164 | } |
| 165 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 166 | static void check_group(const group* grp, const char* group_name, gid_t gid) { |
| 167 | ASSERT_TRUE(grp != NULL); |
| 168 | ASSERT_STREQ(group_name, grp->gr_name); |
| 169 | ASSERT_EQ(gid, grp->gr_gid); |
| 170 | ASSERT_TRUE(grp->gr_mem != NULL); |
| 171 | ASSERT_STREQ(group_name, grp->gr_mem[0]); |
| 172 | ASSERT_TRUE(grp->gr_mem[1] == NULL); |
| 173 | } |
| 174 | |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame^] | 175 | #if defined(__BIONIC__) |
| 176 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 177 | static void check_getgrgid(const char* group_name, gid_t gid) { |
| 178 | errno = 0; |
| 179 | group* grp = getgrgid(gid); |
| 180 | ASSERT_EQ(0, errno); |
| 181 | SCOPED_TRACE("getgrgid"); |
| 182 | check_group(grp, group_name, gid); |
| 183 | } |
| 184 | |
| 185 | static void check_getgrnam(const char* group_name, gid_t gid) { |
| 186 | errno = 0; |
| 187 | group* grp = getgrnam(group_name); |
| 188 | ASSERT_EQ(0, errno); |
| 189 | SCOPED_TRACE("getgrnam"); |
| 190 | check_group(grp, group_name, gid); |
| 191 | } |
| 192 | |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame^] | 193 | static void check_getgrgid_r(const char* group_name, gid_t gid) { |
| 194 | group grp_storage; |
| 195 | char buf[512]; |
| 196 | group* grp; |
| 197 | |
| 198 | errno = 0; |
| 199 | int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp); |
| 200 | ASSERT_EQ(0, result); |
| 201 | ASSERT_EQ(0, errno); |
| 202 | SCOPED_TRACE("getgrgid_r"); |
| 203 | check_group(grp, group_name, gid); |
| 204 | } |
| 205 | |
| 206 | static void check_getgrnam_r(const char* group_name, gid_t gid) { |
| 207 | group grp_storage; |
| 208 | char buf[512]; |
| 209 | group* grp; |
| 210 | |
| 211 | errno = 0; |
| 212 | int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp); |
| 213 | ASSERT_EQ(0, result); |
| 214 | ASSERT_EQ(0, errno); |
| 215 | SCOPED_TRACE("getgrnam_r"); |
| 216 | check_group(grp, group_name, gid); |
| 217 | } |
| 218 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 219 | static void check_get_group(const char* group_name, gid_t gid) { |
| 220 | check_getgrgid(group_name, gid); |
| 221 | check_getgrnam(group_name, gid); |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame^] | 222 | check_getgrgid_r(group_name, gid); |
| 223 | check_getgrnam_r(group_name, gid); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | #else // !defined(__BIONIC__) |
| 227 | |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame^] | 228 | static void print_no_getgrnam_test_info() { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 229 | GTEST_LOG_(INFO) << "This test is about gid/group_name translation for Android, which does nothing on libc other than bionic.\n"; |
| 230 | } |
| 231 | |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame^] | 232 | static void check_get_group(const char*, gid_t) { |
| 233 | print_no_getgrnam_test_info(); |
| 234 | } |
| 235 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 236 | #endif |
| 237 | |
| 238 | TEST(getgrnam, system_id_root) { |
| 239 | check_get_group("root", 0); |
| 240 | } |
| 241 | |
| 242 | TEST(getgrnam, system_id_system) { |
| 243 | check_get_group("system", 1000); |
| 244 | } |
| 245 | |
| 246 | TEST(getgrnam, app_id_radio) { |
| 247 | check_get_group("radio", 1001); |
| 248 | } |
| 249 | |
| 250 | TEST(getgrnam, app_id_nobody) { |
| 251 | check_get_group("nobody", 9999); |
| 252 | } |
| 253 | |
| 254 | TEST(getgrnam, app_id_u0_a0) { |
| 255 | check_get_group("u0_a0", 10000); |
| 256 | } |
| 257 | |
| 258 | TEST(getgrnam, app_id_u0_a1234) { |
| 259 | check_get_group("u0_a1234", 11234); |
| 260 | } |
| 261 | |
| 262 | TEST(getgrnam, app_id_u0_a9999) { |
| 263 | check_get_group("u0_a9999", 19999); |
| 264 | } |
| 265 | |
| 266 | // Test the difference between uid and shared gid. |
| 267 | TEST(getgrnam, app_id_all_a9999) { |
| 268 | check_get_group("all_a9999", 59999); |
| 269 | } |
| 270 | |
| 271 | TEST(getgrnam, app_id_u0_i1) { |
| 272 | check_get_group("u0_i1", 99001); |
| 273 | } |
| 274 | |
| 275 | TEST(getgrnam, app_id_u1_root) { |
| 276 | check_get_group("u1_root", 100000); |
| 277 | } |
| 278 | |
| 279 | TEST(getgrnam, app_id_u1_radio) { |
| 280 | check_get_group("u1_radio", 101001); |
| 281 | } |
| 282 | |
| 283 | TEST(getgrnam, app_id_u1_a0) { |
| 284 | check_get_group("u1_a0", 110000); |
| 285 | } |
| 286 | |
| 287 | TEST(getgrnam, app_id_u1_a40000) { |
| 288 | check_get_group("u1_a40000", 150000); |
| 289 | } |
| 290 | |
| 291 | TEST(getgrnam, app_id_u1_i0) { |
| 292 | check_get_group("u1_i0", 199000); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 293 | } |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame^] | 294 | |
| 295 | TEST(getgrnam_r, reentrancy) { |
| 296 | #if defined(__BIONIC__) |
| 297 | group grp_storage[2]; |
| 298 | char buf[2][512]; |
| 299 | group* grp[3]; |
| 300 | int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]); |
| 301 | ASSERT_EQ(0, result); |
| 302 | check_group(grp[0], "root", 0); |
| 303 | grp[1] = getgrnam("system"); |
| 304 | check_group(grp[1], "system", 1000); |
| 305 | result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]); |
| 306 | ASSERT_EQ(0, result); |
| 307 | check_group(grp[2], "radio", 1001); |
| 308 | check_group(grp[0], "root", 0); |
| 309 | check_group(grp[1], "system", 1000); |
| 310 | #else |
| 311 | print_no_getgrnam_test_info(); |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | TEST(getgrgid_r, reentrancy) { |
| 316 | #if defined(__BIONIC__) |
| 317 | group grp_storage[2]; |
| 318 | char buf[2][512]; |
| 319 | group* grp[3]; |
| 320 | int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]); |
| 321 | ASSERT_EQ(0, result); |
| 322 | check_group(grp[0], "root", 0); |
| 323 | grp[1] = getgrgid(1000); |
| 324 | check_group(grp[1], "system", 1000); |
| 325 | result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]); |
| 326 | ASSERT_EQ(0, result); |
| 327 | check_group(grp[2], "radio", 1001); |
| 328 | check_group(grp[0], "root", 0); |
| 329 | check_group(grp[1], "system", 1000); |
| 330 | #else |
| 331 | print_no_getgrnam_test_info(); |
| 332 | #endif |
| 333 | } |
| 334 | |
| 335 | TEST(getgrnam_r, large_enough_suggested_buffer_size) { |
| 336 | long size = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 337 | ASSERT_GT(size, 0); |
| 338 | char buf[size]; |
| 339 | group grp_storage; |
| 340 | group* grp; |
| 341 | ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp)); |
| 342 | check_group(grp, "root", 0); |
| 343 | } |