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 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/cdefs.h> |
| 21 | #include <pwd.h> |
| 22 | #include <errno.h> |
| 23 | #include <limits.h> |
| 24 | #include <unistd.h> |
| 25 | |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 26 | #if defined(__BIONIC__) |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 27 | #define CHECK_GETPWNAM_FOR(username, uid, uid_type) \ |
| 28 | SCOPED_TRACE(username); \ |
| 29 | ASSERT_NO_FATAL_FAILURE(check_getpwnam(username, uid, uid_type)); |
| 30 | |
| 31 | typedef enum { |
| 32 | TYPE_SYSTEM, |
| 33 | TYPE_APP |
| 34 | } uid_type_t; |
| 35 | |
Elliott Hughes | ac184b2 | 2012-09-26 14:20:22 -0700 | [diff] [blame] | 36 | static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) { |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 37 | errno = 0; |
| 38 | passwd* pwd = getpwuid(uid); |
| 39 | ASSERT_TRUE(pwd != NULL); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 40 | ASSERT_EQ(0, errno); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 41 | EXPECT_STREQ(username, pwd->pw_name); |
| 42 | EXPECT_EQ(uid, pwd->pw_uid); |
| 43 | EXPECT_EQ(uid, pwd->pw_gid); |
Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 44 | ASSERT_EQ(NULL, pwd->pw_passwd); |
| 45 | #ifdef __LP64__ |
| 46 | ASSERT_EQ(NULL, pwd->pw_gecos); |
| 47 | #endif |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 48 | |
| 49 | if (uid_type == TYPE_SYSTEM) { |
| 50 | EXPECT_STREQ("/", pwd->pw_dir); |
| 51 | } else if (uid_type == TYPE_APP) { |
| 52 | EXPECT_STREQ("/data", pwd->pw_dir); |
| 53 | } |
| 54 | |
| 55 | EXPECT_STREQ("/system/bin/sh", pwd->pw_shell); |
| 56 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 57 | #else |
| 58 | #define CHECK_GETPWNAM_FOR(username, uid, uid_type) \ |
| 59 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 60 | #endif |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 61 | |
| 62 | TEST(getpwnam, system_id_root) { |
| 63 | CHECK_GETPWNAM_FOR("root", 0, TYPE_SYSTEM); |
| 64 | } |
| 65 | |
| 66 | TEST(getpwnam, system_id_system) { |
| 67 | CHECK_GETPWNAM_FOR("system", 1000, TYPE_SYSTEM); |
| 68 | } |
| 69 | |
| 70 | TEST(getpwnam, app_id_radio) { |
| 71 | CHECK_GETPWNAM_FOR("radio", 1001, TYPE_SYSTEM); |
| 72 | } |
| 73 | |
| 74 | TEST(getpwnam, app_id_nobody) { |
| 75 | CHECK_GETPWNAM_FOR("nobody", 9999, TYPE_SYSTEM); |
| 76 | } |
| 77 | |
Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 78 | TEST(getpwnam, app_id_all_a0) { |
| 79 | CHECK_GETPWNAM_FOR("all_a0", 50000, TYPE_APP); |
| 80 | } |
| 81 | |
| 82 | TEST(getpwnam, app_id_u1_a40000) { |
| 83 | CHECK_GETPWNAM_FOR("u1_a40000", 150000, TYPE_APP); |
| 84 | } |
| 85 | |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 86 | TEST(getpwnam, app_id_u0_a0) { |
| 87 | CHECK_GETPWNAM_FOR("u0_a0", 10000, TYPE_APP); |
| 88 | } |
| 89 | |
| 90 | TEST(getpwnam, app_id_u0_a1234) { |
| 91 | CHECK_GETPWNAM_FOR("u0_a1234", 11234, TYPE_APP); |
| 92 | } |
| 93 | |
| 94 | TEST(getpwnam, app_id_u0_a9999) { |
| 95 | CHECK_GETPWNAM_FOR("u0_a9999", 19999, TYPE_APP); |
| 96 | } |
| 97 | |
| 98 | // nonsensical, but expected |
| 99 | TEST(getpwnam, app_id_u1_root) { |
| 100 | CHECK_GETPWNAM_FOR("u1_root", 100000, TYPE_SYSTEM); |
| 101 | } |
| 102 | |
| 103 | TEST(getpwnam, app_id_u1_radio) { |
| 104 | CHECK_GETPWNAM_FOR("u1_radio", 101001, TYPE_SYSTEM); |
| 105 | } |
| 106 | |
| 107 | TEST(getpwnam, app_id_u1_a0) { |
| 108 | CHECK_GETPWNAM_FOR("u1_a0", 110000, TYPE_APP); |
| 109 | } |
| 110 | |
| 111 | TEST(getpwnam, app_id_u1_i0) { |
| 112 | CHECK_GETPWNAM_FOR("u1_i0", 199000, TYPE_APP); |
| 113 | } |