blob: 2e1acc11a7878cd70bc0129f78b7e4b6c8486c1a [file] [log] [blame]
Kenny Root2a54e5e2012-09-13 10:52:52 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
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
26#if __BIONIC__
27
28#define CHECK_GETPWNAM_FOR(username, uid, uid_type) \
29 SCOPED_TRACE(username); \
30 ASSERT_NO_FATAL_FAILURE(check_getpwnam(username, uid, uid_type));
31
32typedef enum {
33 TYPE_SYSTEM,
34 TYPE_APP
35} uid_type_t;
36
Elliott Hughesac184b22012-09-26 14:20:22 -070037static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
Kenny Root2a54e5e2012-09-13 10:52:52 -070038 errno = 0;
39 passwd* pwd = getpwuid(uid);
40 ASSERT_TRUE(pwd != NULL);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080041 ASSERT_EQ(0, errno);
Kenny Root2a54e5e2012-09-13 10:52:52 -070042 EXPECT_STREQ(username, pwd->pw_name);
43 EXPECT_EQ(uid, pwd->pw_uid);
44 EXPECT_EQ(uid, pwd->pw_gid);
45
46 if (uid_type == TYPE_SYSTEM) {
47 EXPECT_STREQ("/", pwd->pw_dir);
48 } else if (uid_type == TYPE_APP) {
49 EXPECT_STREQ("/data", pwd->pw_dir);
50 }
51
52 EXPECT_STREQ("/system/bin/sh", pwd->pw_shell);
53}
54
55TEST(getpwnam, system_id_root) {
56 CHECK_GETPWNAM_FOR("root", 0, TYPE_SYSTEM);
57}
58
59TEST(getpwnam, system_id_system) {
60 CHECK_GETPWNAM_FOR("system", 1000, TYPE_SYSTEM);
61}
62
63TEST(getpwnam, app_id_radio) {
64 CHECK_GETPWNAM_FOR("radio", 1001, TYPE_SYSTEM);
65}
66
67TEST(getpwnam, app_id_nobody) {
68 CHECK_GETPWNAM_FOR("nobody", 9999, TYPE_SYSTEM);
69}
70
Kenny Root8a05a012012-09-13 14:31:50 -070071TEST(getpwnam, app_id_all_a0) {
72 CHECK_GETPWNAM_FOR("all_a0", 50000, TYPE_APP);
73}
74
75TEST(getpwnam, app_id_u1_a40000) {
76 CHECK_GETPWNAM_FOR("u1_a40000", 150000, TYPE_APP);
77}
78
Kenny Root2a54e5e2012-09-13 10:52:52 -070079TEST(getpwnam, app_id_u0_a0) {
80 CHECK_GETPWNAM_FOR("u0_a0", 10000, TYPE_APP);
81}
82
83TEST(getpwnam, app_id_u0_a1234) {
84 CHECK_GETPWNAM_FOR("u0_a1234", 11234, TYPE_APP);
85}
86
87TEST(getpwnam, app_id_u0_a9999) {
88 CHECK_GETPWNAM_FOR("u0_a9999", 19999, TYPE_APP);
89}
90
91// nonsensical, but expected
92TEST(getpwnam, app_id_u1_root) {
93 CHECK_GETPWNAM_FOR("u1_root", 100000, TYPE_SYSTEM);
94}
95
96TEST(getpwnam, app_id_u1_radio) {
97 CHECK_GETPWNAM_FOR("u1_radio", 101001, TYPE_SYSTEM);
98}
99
100TEST(getpwnam, app_id_u1_a0) {
101 CHECK_GETPWNAM_FOR("u1_a0", 110000, TYPE_APP);
102}
103
104TEST(getpwnam, app_id_u1_i0) {
105 CHECK_GETPWNAM_FOR("u1_i0", 199000, TYPE_APP);
106}
107
108#endif /* __BIONIC__ */