1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2018 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 <unistd.h>
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*8d67ca89SAndroid Build Coastguard Worker
21*8d67ca89SAndroid Build Coastguard Worker #include <android-base/file.h>
22*8d67ca89SAndroid Build Coastguard Worker
23*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
24*8d67ca89SAndroid Build Coastguard Worker #include "../libc/bionic/grp_pwd_file.cpp"
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker template <typename T>
27*8d67ca89SAndroid Build Coastguard Worker class FileUnmapper {
28*8d67ca89SAndroid Build Coastguard Worker public:
FileUnmapper(T & file)29*8d67ca89SAndroid Build Coastguard Worker explicit FileUnmapper(T& file) : file_(file) {
30*8d67ca89SAndroid Build Coastguard Worker }
~FileUnmapper()31*8d67ca89SAndroid Build Coastguard Worker ~FileUnmapper() {
32*8d67ca89SAndroid Build Coastguard Worker file_.Unmap();
33*8d67ca89SAndroid Build Coastguard Worker }
34*8d67ca89SAndroid Build Coastguard Worker
35*8d67ca89SAndroid Build Coastguard Worker private:
36*8d67ca89SAndroid Build Coastguard Worker T& file_;
37*8d67ca89SAndroid Build Coastguard Worker };
38*8d67ca89SAndroid Build Coastguard Worker
FindAndCheckPasswdEntry(PasswdFile * file,const char * name,uid_t uid,gid_t gid,const char * dir,const char * shell)39*8d67ca89SAndroid Build Coastguard Worker void FindAndCheckPasswdEntry(PasswdFile* file, const char* name, uid_t uid, gid_t gid,
40*8d67ca89SAndroid Build Coastguard Worker const char* dir, const char* shell) {
41*8d67ca89SAndroid Build Coastguard Worker passwd_state_t name_passwd_state;
42*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(file->FindByName(name, &name_passwd_state)) << name;
43*8d67ca89SAndroid Build Coastguard Worker
44*8d67ca89SAndroid Build Coastguard Worker passwd& name_passwd = name_passwd_state.passwd_;
45*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(name, name_passwd.pw_name);
46*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, name_passwd.pw_passwd);
47*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(uid, name_passwd.pw_uid);
48*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(gid, name_passwd.pw_gid);
49*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, name_passwd.pw_gecos);
50*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(dir, name_passwd.pw_dir);
51*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(shell, name_passwd.pw_shell);
52*8d67ca89SAndroid Build Coastguard Worker
53*8d67ca89SAndroid Build Coastguard Worker passwd_state_t id_passwd_state;
54*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(file->FindById(uid, &id_passwd_state)) << uid;
55*8d67ca89SAndroid Build Coastguard Worker
56*8d67ca89SAndroid Build Coastguard Worker passwd& id_passwd = id_passwd_state.passwd_;
57*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(name, id_passwd.pw_name);
58*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, id_passwd.pw_passwd);
59*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(uid, id_passwd.pw_uid);
60*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(gid, id_passwd.pw_gid);
61*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, id_passwd.pw_gecos);
62*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(dir, id_passwd.pw_dir);
63*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(shell, id_passwd.pw_shell);
64*8d67ca89SAndroid Build Coastguard Worker }
65*8d67ca89SAndroid Build Coastguard Worker
FindAndCheckGroupEntry(GroupFile * file,const char * name,gid_t gid)66*8d67ca89SAndroid Build Coastguard Worker void FindAndCheckGroupEntry(GroupFile* file, const char* name, gid_t gid) {
67*8d67ca89SAndroid Build Coastguard Worker group_state_t name_group_state;
68*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(file->FindByName(name, &name_group_state)) << name;
69*8d67ca89SAndroid Build Coastguard Worker
70*8d67ca89SAndroid Build Coastguard Worker group& name_group = name_group_state.group_;
71*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(name, name_group.gr_name);
72*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, name_group.gr_passwd);
73*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(gid, name_group.gr_gid);
74*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(name_group.gr_name, name_group.gr_mem[0]);
75*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, name_group.gr_mem[1]);
76*8d67ca89SAndroid Build Coastguard Worker
77*8d67ca89SAndroid Build Coastguard Worker group_state_t id_group_state;
78*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(file->FindById(gid, &id_group_state)) << gid;
79*8d67ca89SAndroid Build Coastguard Worker
80*8d67ca89SAndroid Build Coastguard Worker group& id_group = id_group_state.group_;
81*8d67ca89SAndroid Build Coastguard Worker EXPECT_STREQ(name, id_group.gr_name);
82*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, id_group.gr_passwd);
83*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(gid, id_group.gr_gid);
84*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(id_group.gr_name, id_group.gr_mem[0]);
85*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, id_group.gr_mem[1]);
86*8d67ca89SAndroid Build Coastguard Worker }
87*8d67ca89SAndroid Build Coastguard Worker
88*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
89*8d67ca89SAndroid Build Coastguard Worker
TEST(grp_pwd_file,passwd_file_one_entry)90*8d67ca89SAndroid Build Coastguard Worker TEST(grp_pwd_file, passwd_file_one_entry) {
91*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
92*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
93*8d67ca89SAndroid Build Coastguard Worker ASSERT_NE(-1, file.fd);
94*8d67ca89SAndroid Build Coastguard Worker static const char test_string[] = "name:password:1:2:user_info:dir:shell\n";
95*8d67ca89SAndroid Build Coastguard Worker write(file.fd, test_string, sizeof(test_string) - 1);
96*8d67ca89SAndroid Build Coastguard Worker
97*8d67ca89SAndroid Build Coastguard Worker PasswdFile passwd_file(file.path, nullptr);
98*8d67ca89SAndroid Build Coastguard Worker FileUnmapper unmapper(passwd_file);
99*8d67ca89SAndroid Build Coastguard Worker
100*8d67ca89SAndroid Build Coastguard Worker FindAndCheckPasswdEntry(&passwd_file, "name", 1, 2, "dir", "shell");
101*8d67ca89SAndroid Build Coastguard Worker
102*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(passwd_file.FindByName("not_name", nullptr));
103*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(passwd_file.FindById(3, nullptr));
104*8d67ca89SAndroid Build Coastguard Worker
105*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
106*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "bionic-only test";
107*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
108*8d67ca89SAndroid Build Coastguard Worker }
109*8d67ca89SAndroid Build Coastguard Worker
TEST(grp_pwd_file,group_file_one_entry)110*8d67ca89SAndroid Build Coastguard Worker TEST(grp_pwd_file, group_file_one_entry) {
111*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
112*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
113*8d67ca89SAndroid Build Coastguard Worker ASSERT_NE(-1, file.fd);
114*8d67ca89SAndroid Build Coastguard Worker static const char test_string[] = "name:password:1:one,two,three\n";
115*8d67ca89SAndroid Build Coastguard Worker write(file.fd, test_string, sizeof(test_string) - 1);
116*8d67ca89SAndroid Build Coastguard Worker
117*8d67ca89SAndroid Build Coastguard Worker GroupFile group_file(file.path, nullptr);
118*8d67ca89SAndroid Build Coastguard Worker FileUnmapper unmapper(group_file);
119*8d67ca89SAndroid Build Coastguard Worker
120*8d67ca89SAndroid Build Coastguard Worker FindAndCheckGroupEntry(&group_file, "name", 1);
121*8d67ca89SAndroid Build Coastguard Worker
122*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(group_file.FindByName("not_name", nullptr));
123*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(group_file.FindById(3, nullptr));
124*8d67ca89SAndroid Build Coastguard Worker
125*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
126*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "bionic-only test";
127*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
128*8d67ca89SAndroid Build Coastguard Worker }
129*8d67ca89SAndroid Build Coastguard Worker
TEST(grp_pwd_file,passwd_file_many_entries)130*8d67ca89SAndroid Build Coastguard Worker TEST(grp_pwd_file, passwd_file_many_entries) {
131*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
132*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
133*8d67ca89SAndroid Build Coastguard Worker ASSERT_NE(-1, file.fd);
134*8d67ca89SAndroid Build Coastguard Worker static const char test_string[] =
135*8d67ca89SAndroid Build Coastguard Worker "first:x:1:2::dir:shell\n"
136*8d67ca89SAndroid Build Coastguard Worker "abc1::3:4::def:abc\n"
137*8d67ca89SAndroid Build Coastguard Worker "abc2::5:4:abc::abc\n"
138*8d67ca89SAndroid Build Coastguard Worker "abc3::7:4:abc:def:\n"
139*8d67ca89SAndroid Build Coastguard Worker "abc4::9:4:::abc\n"
140*8d67ca89SAndroid Build Coastguard Worker "abc5::11:4:abc:def:abc\n"
141*8d67ca89SAndroid Build Coastguard Worker "middle-ish::13:4::/:/system/bin/sh\n"
142*8d67ca89SAndroid Build Coastguard Worker "abc7::15:4:abc::\n"
143*8d67ca89SAndroid Build Coastguard Worker "abc8::17:4:::\n"
144*8d67ca89SAndroid Build Coastguard Worker "abc9::19:4:abc:def:abc\n"
145*8d67ca89SAndroid Build Coastguard Worker "abc10::21:4:abc:def:abc\n"
146*8d67ca89SAndroid Build Coastguard Worker "abc11::23:4:abc:def:abc\n"
147*8d67ca89SAndroid Build Coastguard Worker "abc12::25:4:abc:def:abc\n"
148*8d67ca89SAndroid Build Coastguard Worker "abc13::27:4:abc:def:abc\n"
149*8d67ca89SAndroid Build Coastguard Worker "last::29:4::last_user_dir:last_user_shell\n";
150*8d67ca89SAndroid Build Coastguard Worker
151*8d67ca89SAndroid Build Coastguard Worker write(file.fd, test_string, sizeof(test_string) - 1);
152*8d67ca89SAndroid Build Coastguard Worker
153*8d67ca89SAndroid Build Coastguard Worker PasswdFile passwd_file(file.path, nullptr);
154*8d67ca89SAndroid Build Coastguard Worker FileUnmapper unmapper(passwd_file);
155*8d67ca89SAndroid Build Coastguard Worker
156*8d67ca89SAndroid Build Coastguard Worker FindAndCheckPasswdEntry(&passwd_file, "first", 1, 2, "dir", "shell");
157*8d67ca89SAndroid Build Coastguard Worker FindAndCheckPasswdEntry(&passwd_file, "middle-ish", 13, 4, "/", "/system/bin/sh");
158*8d67ca89SAndroid Build Coastguard Worker FindAndCheckPasswdEntry(&passwd_file, "last", 29, 4, "last_user_dir", "last_user_shell");
159*8d67ca89SAndroid Build Coastguard Worker
160*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(passwd_file.FindByName("not_name", nullptr));
161*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(passwd_file.FindById(50, nullptr));
162*8d67ca89SAndroid Build Coastguard Worker
163*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
164*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "bionic-only test";
165*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
166*8d67ca89SAndroid Build Coastguard Worker }
167*8d67ca89SAndroid Build Coastguard Worker
TEST(grp_pwd_file,group_file_many_entries)168*8d67ca89SAndroid Build Coastguard Worker TEST(grp_pwd_file, group_file_many_entries) {
169*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
170*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
171*8d67ca89SAndroid Build Coastguard Worker ASSERT_NE(-1, file.fd);
172*8d67ca89SAndroid Build Coastguard Worker static const char test_string[] =
173*8d67ca89SAndroid Build Coastguard Worker "first:password:1:one,two,three\n"
174*8d67ca89SAndroid Build Coastguard Worker "abc:def:2:group1,group2,group3\n"
175*8d67ca89SAndroid Build Coastguard Worker "abc:def:3:\n"
176*8d67ca89SAndroid Build Coastguard Worker "abc:def:4:\n"
177*8d67ca89SAndroid Build Coastguard Worker "abc:def:5:\n"
178*8d67ca89SAndroid Build Coastguard Worker "middle-ish:def_a_password_that_is_over_32_characters_long:6:\n"
179*8d67ca89SAndroid Build Coastguard Worker "abc:def:7:\n"
180*8d67ca89SAndroid Build Coastguard Worker "abc:def:8:\n"
181*8d67ca89SAndroid Build Coastguard Worker "abc:def:20:\n"
182*8d67ca89SAndroid Build Coastguard Worker "abc:def:25:\n"
183*8d67ca89SAndroid Build Coastguard Worker "abc:def:27:\n"
184*8d67ca89SAndroid Build Coastguard Worker "abc:def:52:\n"
185*8d67ca89SAndroid Build Coastguard Worker "last::800:\n";
186*8d67ca89SAndroid Build Coastguard Worker
187*8d67ca89SAndroid Build Coastguard Worker write(file.fd, test_string, sizeof(test_string) - 1);
188*8d67ca89SAndroid Build Coastguard Worker
189*8d67ca89SAndroid Build Coastguard Worker GroupFile group_file(file.path, nullptr);
190*8d67ca89SAndroid Build Coastguard Worker FileUnmapper unmapper(group_file);
191*8d67ca89SAndroid Build Coastguard Worker
192*8d67ca89SAndroid Build Coastguard Worker FindAndCheckGroupEntry(&group_file, "first", 1);
193*8d67ca89SAndroid Build Coastguard Worker FindAndCheckGroupEntry(&group_file, "middle-ish", 6);
194*8d67ca89SAndroid Build Coastguard Worker FindAndCheckGroupEntry(&group_file, "last", 800);
195*8d67ca89SAndroid Build Coastguard Worker
196*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(group_file.FindByName("not_name", nullptr));
197*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(group_file.FindById(799, nullptr));
198*8d67ca89SAndroid Build Coastguard Worker
199*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
200*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "bionic-only test";
201*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
202*8d67ca89SAndroid Build Coastguard Worker }
203*8d67ca89SAndroid Build Coastguard Worker
TEST(grp_pwd_file,passwd_file_required_prefix)204*8d67ca89SAndroid Build Coastguard Worker TEST(grp_pwd_file, passwd_file_required_prefix) {
205*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
206*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
207*8d67ca89SAndroid Build Coastguard Worker ASSERT_NE(-1, file.fd);
208*8d67ca89SAndroid Build Coastguard Worker static const char test_string[] =
209*8d67ca89SAndroid Build Coastguard Worker "name:password:1:2:user_info:dir:shell\n"
210*8d67ca89SAndroid Build Coastguard Worker "vendor_name:password:3:4:user_info:dir:shell\n";
211*8d67ca89SAndroid Build Coastguard Worker write(file.fd, test_string, sizeof(test_string) - 1);
212*8d67ca89SAndroid Build Coastguard Worker
213*8d67ca89SAndroid Build Coastguard Worker PasswdFile passwd_file(file.path, "vendor_");
214*8d67ca89SAndroid Build Coastguard Worker FileUnmapper unmapper(passwd_file);
215*8d67ca89SAndroid Build Coastguard Worker
216*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(passwd_file.FindByName("name", nullptr));
217*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(passwd_file.FindById(1, nullptr));
218*8d67ca89SAndroid Build Coastguard Worker
219*8d67ca89SAndroid Build Coastguard Worker FindAndCheckPasswdEntry(&passwd_file, "vendor_name", 3, 4, "dir", "shell");
220*8d67ca89SAndroid Build Coastguard Worker
221*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
222*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "bionic-only test";
223*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
224*8d67ca89SAndroid Build Coastguard Worker }
225*8d67ca89SAndroid Build Coastguard Worker
TEST(grp_pwd_file,group_file_required_prefix)226*8d67ca89SAndroid Build Coastguard Worker TEST(grp_pwd_file, group_file_required_prefix) {
227*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
228*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
229*8d67ca89SAndroid Build Coastguard Worker ASSERT_NE(-1, file.fd);
230*8d67ca89SAndroid Build Coastguard Worker static const char test_string[] =
231*8d67ca89SAndroid Build Coastguard Worker "name:password:1:one,two,three\n"
232*8d67ca89SAndroid Build Coastguard Worker "vendor_name:password:2:one,two,three\n";
233*8d67ca89SAndroid Build Coastguard Worker write(file.fd, test_string, sizeof(test_string) - 1);
234*8d67ca89SAndroid Build Coastguard Worker
235*8d67ca89SAndroid Build Coastguard Worker GroupFile group_file(file.path, "vendor_");
236*8d67ca89SAndroid Build Coastguard Worker FileUnmapper unmapper(group_file);
237*8d67ca89SAndroid Build Coastguard Worker
238*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(group_file.FindByName("name", nullptr));
239*8d67ca89SAndroid Build Coastguard Worker EXPECT_FALSE(group_file.FindById(1, nullptr));
240*8d67ca89SAndroid Build Coastguard Worker
241*8d67ca89SAndroid Build Coastguard Worker FindAndCheckGroupEntry(&group_file, "vendor_name", 2);
242*8d67ca89SAndroid Build Coastguard Worker
243*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
244*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "bionic-only test";
245*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
246*8d67ca89SAndroid Build Coastguard Worker }
247