blob: 79f9a1065ee28cceadaa9df733605a29870315c6 [file] [log] [blame]
Tom Cherry6034ef82018-02-02 16:10:07 -08001/*
2 * Copyright (C) 2018 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 <unistd.h>
18
19#include <gtest/gtest.h>
20
21#include "TemporaryFile.h"
22
23#if defined(__BIONIC__)
24#include "../libc/bionic/grp_pwd_file.cpp"
25
26void FindAndCheckPasswdEntry(PasswdFile* file, const char* name, uid_t uid, gid_t gid,
27 const char* dir, const char* shell) {
28 passwd_state_t name_passwd_state;
29 ASSERT_TRUE(file->FindByName(name, &name_passwd_state)) << name;
30
31 passwd& name_passwd = name_passwd_state.passwd_;
32 EXPECT_STREQ(name, name_passwd.pw_name);
33 EXPECT_EQ(nullptr, name_passwd.pw_passwd);
34 EXPECT_EQ(uid, name_passwd.pw_uid);
35 EXPECT_EQ(gid, name_passwd.pw_gid);
36 EXPECT_EQ(nullptr, name_passwd.pw_gecos);
37 EXPECT_STREQ(dir, name_passwd.pw_dir);
38 EXPECT_STREQ(shell, name_passwd.pw_shell);
39
40 passwd_state_t id_passwd_state;
41 ASSERT_TRUE(file->FindById(uid, &id_passwd_state)) << uid;
42
43 passwd& id_passwd = id_passwd_state.passwd_;
44 EXPECT_STREQ(name, id_passwd.pw_name);
45 EXPECT_EQ(nullptr, id_passwd.pw_passwd);
46 EXPECT_EQ(uid, id_passwd.pw_uid);
47 EXPECT_EQ(gid, id_passwd.pw_gid);
48 EXPECT_EQ(nullptr, id_passwd.pw_gecos);
49 EXPECT_STREQ(dir, id_passwd.pw_dir);
50 EXPECT_STREQ(shell, id_passwd.pw_shell);
51}
52
53void FindAndCheckGroupEntry(GroupFile* file, const char* name, gid_t gid) {
54 group_state_t name_group_state;
55 ASSERT_TRUE(file->FindByName(name, &name_group_state)) << name;
56
57 group& name_group = name_group_state.group_;
58 EXPECT_STREQ(name, name_group.gr_name);
59 EXPECT_EQ(nullptr, name_group.gr_passwd);
60 EXPECT_EQ(gid, name_group.gr_gid);
61 EXPECT_EQ(name_group.gr_name, name_group.gr_mem[0]);
62 EXPECT_EQ(nullptr, name_group.gr_mem[1]);
63
64 group_state_t id_group_state;
65 ASSERT_TRUE(file->FindById(gid, &id_group_state)) << gid;
66
67 group& id_group = id_group_state.group_;
68 EXPECT_STREQ(name, id_group.gr_name);
69 EXPECT_EQ(nullptr, id_group.gr_passwd);
70 EXPECT_EQ(gid, id_group.gr_gid);
71 EXPECT_EQ(id_group.gr_name, id_group.gr_mem[0]);
72 EXPECT_EQ(nullptr, id_group.gr_mem[1]);
73}
74
75#endif // __BIONIC__
76
77TEST(grp_pwd_file, passwd_file_one_entry) {
78#if defined(__BIONIC__)
79 TemporaryFile file;
80 ASSERT_NE(-1, file.fd);
81 static const char test_string[] = "name:password:1:2:user_info:dir:shell\n";
82 write(file.fd, test_string, sizeof(test_string) - 1);
83
84 PasswdFile passwd_file(file.filename);
85
86 FindAndCheckPasswdEntry(&passwd_file, "name", 1, 2, "dir", "shell");
87
88 EXPECT_FALSE(passwd_file.FindByName("not_name", nullptr));
89 EXPECT_FALSE(passwd_file.FindById(3, nullptr));
90
91#else // __BIONIC__
92 GTEST_LOG_(INFO) << "This test does nothing.\n";
93#endif // __BIONIC__
94}
95
96TEST(grp_pwd_file, group_file_one_entry) {
97#if defined(__BIONIC__)
98 TemporaryFile file;
99 ASSERT_NE(-1, file.fd);
100 static const char test_string[] = "name:password:1:one,two,three\n";
101 write(file.fd, test_string, sizeof(test_string) - 1);
102
103 GroupFile group_file(file.filename);
104
105 FindAndCheckGroupEntry(&group_file, "name", 1);
106
107 EXPECT_FALSE(group_file.FindByName("not_name", nullptr));
108 EXPECT_FALSE(group_file.FindById(3, nullptr));
109
110#else // __BIONIC__
111 GTEST_LOG_(INFO) << "This test does nothing.\n";
112#endif // __BIONIC__
113}
114
115TEST(grp_pwd_file, passwd_file_many_entries) {
116#if defined(__BIONIC__)
117 TemporaryFile file;
118 ASSERT_NE(-1, file.fd);
119 static const char test_string[] =
120 "first:x:1:2::dir:shell\n"
121 "abc1::3:4::def:abc\n"
122 "abc2::5:4:abc::abc\n"
123 "abc3::7:4:abc:def:\n"
124 "abc4::9:4:::abc\n"
125 "abc5::11:4:abc:def:abc\n"
126 "middle-ish::13:4::/:/system/bin/sh\n"
127 "abc7::15:4:abc::\n"
128 "abc8::17:4:::\n"
129 "abc9::19:4:abc:def:abc\n"
130 "abc10::21:4:abc:def:abc\n"
131 "abc11::23:4:abc:def:abc\n"
132 "abc12::25:4:abc:def:abc\n"
133 "abc13::27:4:abc:def:abc\n"
134 "last::29:4::last_user_dir:last_user_shell\n";
135
136 write(file.fd, test_string, sizeof(test_string) - 1);
137
138 PasswdFile passwd_file(file.filename);
139
140 FindAndCheckPasswdEntry(&passwd_file, "first", 1, 2, "dir", "shell");
141 FindAndCheckPasswdEntry(&passwd_file, "middle-ish", 13, 4, "/", "/system/bin/sh");
142 FindAndCheckPasswdEntry(&passwd_file, "last", 29, 4, "last_user_dir", "last_user_shell");
143
144 EXPECT_FALSE(passwd_file.FindByName("not_name", nullptr));
145 EXPECT_FALSE(passwd_file.FindById(50, nullptr));
146
147#else // __BIONIC__
148 GTEST_LOG_(INFO) << "This test does nothing.\n";
149#endif // __BIONIC__
150}
151
152TEST(grp_pwd_file, group_file_many_entries) {
153#if defined(__BIONIC__)
154 TemporaryFile file;
155 ASSERT_NE(-1, file.fd);
156 static const char test_string[] =
157 "first:password:1:one,two,three\n"
158 "abc:def:2:group1,group2,group3\n"
159 "abc:def:3:\n"
160 "abc:def:4:\n"
161 "abc:def:5:\n"
162 "middle-ish:def_a_password_that_is_over_32_characters_long:6:\n"
163 "abc:def:7:\n"
164 "abc:def:8:\n"
165 "abc:def:20:\n"
166 "abc:def:25:\n"
167 "abc:def:27:\n"
168 "abc:def:52:\n"
169 "last::800:\n";
170
171 write(file.fd, test_string, sizeof(test_string) - 1);
172
173 GroupFile group_file(file.filename);
174
175 FindAndCheckGroupEntry(&group_file, "first", 1);
176 FindAndCheckGroupEntry(&group_file, "middle-ish", 6);
177 FindAndCheckGroupEntry(&group_file, "last", 800);
178
179 EXPECT_FALSE(group_file.FindByName("not_name", nullptr));
180 EXPECT_FALSE(group_file.FindById(799, nullptr));
181
182#else // __BIONIC__
183 GTEST_LOG_(INFO) << "This test does nothing.\n";
184#endif // __BIONIC__
185}