xref: /freebsd/contrib/kyua/utils/passwd_test.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2010 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "utils/passwd.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis extern "C" {
32*b0d29bc4SBrooks Davis #include <sys/wait.h>
33*b0d29bc4SBrooks Davis 
34*b0d29bc4SBrooks Davis #include <pwd.h>
35*b0d29bc4SBrooks Davis #include <unistd.h>
36*b0d29bc4SBrooks Davis }
37*b0d29bc4SBrooks Davis 
38*b0d29bc4SBrooks Davis #include <cstdlib>
39*b0d29bc4SBrooks Davis #include <stdexcept>
40*b0d29bc4SBrooks Davis 
41*b0d29bc4SBrooks Davis #include <atf-c++.hpp>
42*b0d29bc4SBrooks Davis 
43*b0d29bc4SBrooks Davis namespace passwd_ns = utils::passwd;
44*b0d29bc4SBrooks Davis 
45*b0d29bc4SBrooks Davis 
46*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(user__public_fields);
ATF_TEST_CASE_BODY(user__public_fields)47*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(user__public_fields)
48*b0d29bc4SBrooks Davis {
49*b0d29bc4SBrooks Davis     const passwd_ns::user user("the-name", 1, 2);
50*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ("the-name", user.name);
51*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(1, user.uid);
52*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(2, user.gid);
53*b0d29bc4SBrooks Davis }
54*b0d29bc4SBrooks Davis 
55*b0d29bc4SBrooks Davis 
56*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(user__is_root__true);
ATF_TEST_CASE_BODY(user__is_root__true)57*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(user__is_root__true)
58*b0d29bc4SBrooks Davis {
59*b0d29bc4SBrooks Davis     const passwd_ns::user user("i-am-root", 0, 10);
60*b0d29bc4SBrooks Davis     ATF_REQUIRE(user.is_root());
61*b0d29bc4SBrooks Davis }
62*b0d29bc4SBrooks Davis 
63*b0d29bc4SBrooks Davis 
64*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(user__is_root__false);
ATF_TEST_CASE_BODY(user__is_root__false)65*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(user__is_root__false)
66*b0d29bc4SBrooks Davis {
67*b0d29bc4SBrooks Davis     const passwd_ns::user user("i-am-not-root", 123, 10);
68*b0d29bc4SBrooks Davis     ATF_REQUIRE(!user.is_root());
69*b0d29bc4SBrooks Davis }
70*b0d29bc4SBrooks Davis 
71*b0d29bc4SBrooks Davis 
72*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(current_user);
ATF_TEST_CASE_BODY(current_user)73*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(current_user)
74*b0d29bc4SBrooks Davis {
75*b0d29bc4SBrooks Davis     const passwd_ns::user user = passwd_ns::current_user();
76*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(::getuid(), user.uid);
77*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(::getgid(), user.gid);
78*b0d29bc4SBrooks Davis }
79*b0d29bc4SBrooks Davis 
80*b0d29bc4SBrooks Davis 
81*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(current_user__fake);
ATF_TEST_CASE_BODY(current_user__fake)82*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(current_user__fake)
83*b0d29bc4SBrooks Davis {
84*b0d29bc4SBrooks Davis     const passwd_ns::user new_user("someone-else", ::getuid() + 1, 0);
85*b0d29bc4SBrooks Davis     passwd_ns::set_current_user_for_testing(new_user);
86*b0d29bc4SBrooks Davis 
87*b0d29bc4SBrooks Davis     const passwd_ns::user user = passwd_ns::current_user();
88*b0d29bc4SBrooks Davis     ATF_REQUIRE(::getuid() != user.uid);
89*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(new_user.uid, user.uid);
90*b0d29bc4SBrooks Davis }
91*b0d29bc4SBrooks Davis 
92*b0d29bc4SBrooks Davis 
93*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(find_user_by_name__ok);
ATF_TEST_CASE_BODY(find_user_by_name__ok)94*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(find_user_by_name__ok)
95*b0d29bc4SBrooks Davis {
96*b0d29bc4SBrooks Davis     const struct ::passwd* pw = ::getpwuid(::getuid());
97*b0d29bc4SBrooks Davis     ATF_REQUIRE(pw != NULL);
98*b0d29bc4SBrooks Davis 
99*b0d29bc4SBrooks Davis     const passwd_ns::user user = passwd_ns::find_user_by_name(pw->pw_name);
100*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(::getuid(), user.uid);
101*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(::getgid(), user.gid);
102*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(pw->pw_name, user.name);
103*b0d29bc4SBrooks Davis }
104*b0d29bc4SBrooks Davis 
105*b0d29bc4SBrooks Davis 
106*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(find_user_by_name__fail);
ATF_TEST_CASE_BODY(find_user_by_name__fail)107*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(find_user_by_name__fail)
108*b0d29bc4SBrooks Davis {
109*b0d29bc4SBrooks Davis     ATF_REQUIRE_THROW_RE(std::runtime_error, "Failed.*user 'i-do-not-exist'",
110*b0d29bc4SBrooks Davis                          passwd_ns::find_user_by_name("i-do-not-exist"));
111*b0d29bc4SBrooks Davis }
112*b0d29bc4SBrooks Davis 
113*b0d29bc4SBrooks Davis 
114*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(find_user_by_name__fake);
ATF_TEST_CASE_BODY(find_user_by_name__fake)115*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(find_user_by_name__fake)
116*b0d29bc4SBrooks Davis {
117*b0d29bc4SBrooks Davis     std::vector< passwd_ns::user > users;
118*b0d29bc4SBrooks Davis     users.push_back(passwd_ns::user("myself2", 20, 40));
119*b0d29bc4SBrooks Davis     users.push_back(passwd_ns::user("myself1", 10, 15));
120*b0d29bc4SBrooks Davis     users.push_back(passwd_ns::user("myself3", 30, 60));
121*b0d29bc4SBrooks Davis     passwd_ns::set_mock_users_for_testing(users);
122*b0d29bc4SBrooks Davis 
123*b0d29bc4SBrooks Davis     const passwd_ns::user user = passwd_ns::find_user_by_name("myself1");
124*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(10, user.uid);
125*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(15, user.gid);
126*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ("myself1", user.name);
127*b0d29bc4SBrooks Davis 
128*b0d29bc4SBrooks Davis     ATF_REQUIRE_THROW_RE(std::runtime_error, "Failed.*user 'root'",
129*b0d29bc4SBrooks Davis                          passwd_ns::find_user_by_name("root"));
130*b0d29bc4SBrooks Davis }
131*b0d29bc4SBrooks Davis 
132*b0d29bc4SBrooks Davis 
133*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(find_user_by_uid__ok);
ATF_TEST_CASE_BODY(find_user_by_uid__ok)134*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(find_user_by_uid__ok)
135*b0d29bc4SBrooks Davis {
136*b0d29bc4SBrooks Davis     const passwd_ns::user user = passwd_ns::find_user_by_uid(::getuid());
137*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(::getuid(), user.uid);
138*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(::getgid(), user.gid);
139*b0d29bc4SBrooks Davis 
140*b0d29bc4SBrooks Davis     const struct ::passwd* pw = ::getpwuid(::getuid());
141*b0d29bc4SBrooks Davis     ATF_REQUIRE(pw != NULL);
142*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(pw->pw_name, user.name);
143*b0d29bc4SBrooks Davis }
144*b0d29bc4SBrooks Davis 
145*b0d29bc4SBrooks Davis 
146*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(find_user_by_uid__fake);
ATF_TEST_CASE_BODY(find_user_by_uid__fake)147*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(find_user_by_uid__fake)
148*b0d29bc4SBrooks Davis {
149*b0d29bc4SBrooks Davis     std::vector< passwd_ns::user > users;
150*b0d29bc4SBrooks Davis     users.push_back(passwd_ns::user("myself2", 20, 40));
151*b0d29bc4SBrooks Davis     users.push_back(passwd_ns::user("myself1", 10, 15));
152*b0d29bc4SBrooks Davis     users.push_back(passwd_ns::user("myself3", 30, 60));
153*b0d29bc4SBrooks Davis     passwd_ns::set_mock_users_for_testing(users);
154*b0d29bc4SBrooks Davis 
155*b0d29bc4SBrooks Davis     const passwd_ns::user user = passwd_ns::find_user_by_uid(10);
156*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(10, user.uid);
157*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(15, user.gid);
158*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ("myself1", user.name);
159*b0d29bc4SBrooks Davis 
160*b0d29bc4SBrooks Davis     ATF_REQUIRE_THROW_RE(std::runtime_error, "Failed.*user.*UID 0",
161*b0d29bc4SBrooks Davis                          passwd_ns::find_user_by_uid(0));
162*b0d29bc4SBrooks Davis }
163*b0d29bc4SBrooks Davis 
164*b0d29bc4SBrooks Davis 
ATF_INIT_TEST_CASES(tcs)165*b0d29bc4SBrooks Davis ATF_INIT_TEST_CASES(tcs)
166*b0d29bc4SBrooks Davis {
167*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, user__public_fields);
168*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, user__is_root__true);
169*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, user__is_root__false);
170*b0d29bc4SBrooks Davis 
171*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, current_user);
172*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, current_user__fake);
173*b0d29bc4SBrooks Davis 
174*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, find_user_by_name__ok);
175*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, find_user_by_name__fail);
176*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, find_user_by_name__fake);
177*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, find_user_by_uid__ok);
178*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, find_user_by_uid__fake);
179*b0d29bc4SBrooks Davis }
180