xref: /minix3/external/bsd/atf/dist/tools/user_test.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc extern "C" {
31*0a6a1f1dSLionel Sambuc #include <sys/param.h>
32*0a6a1f1dSLionel Sambuc #include <sys/types.h>
33*0a6a1f1dSLionel Sambuc #include <limits.h>
34*0a6a1f1dSLionel Sambuc #include <unistd.h>
35*0a6a1f1dSLionel Sambuc }
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc #include <iostream>
38*0a6a1f1dSLionel Sambuc #include <set>
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc #include <atf-c++.hpp>
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc #include "user.hpp"
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
45*0a6a1f1dSLionel Sambuc // Test cases for the free functions.
46*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(euid);
ATF_TEST_CASE_HEAD(euid)49*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(euid)
50*0a6a1f1dSLionel Sambuc {
51*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the euid function");
52*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(euid)53*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(euid)
54*0a6a1f1dSLionel Sambuc {
55*0a6a1f1dSLionel Sambuc     using tools::user::euid;
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(euid(), ::geteuid());
58*0a6a1f1dSLionel Sambuc }
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(is_member_of_group);
ATF_TEST_CASE_HEAD(is_member_of_group)61*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(is_member_of_group)
62*0a6a1f1dSLionel Sambuc {
63*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the is_member_of_group function");
64*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(is_member_of_group)65*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(is_member_of_group)
66*0a6a1f1dSLionel Sambuc {
67*0a6a1f1dSLionel Sambuc     using tools::user::is_member_of_group;
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc     std::set< gid_t > groups;
70*0a6a1f1dSLionel Sambuc     gid_t maxgid = 0;
71*0a6a1f1dSLionel Sambuc     {
72*0a6a1f1dSLionel Sambuc         gid_t gids[NGROUPS_MAX];
73*0a6a1f1dSLionel Sambuc         int ngids = ::getgroups(NGROUPS_MAX, gids);
74*0a6a1f1dSLionel Sambuc         if (ngids == -1)
75*0a6a1f1dSLionel Sambuc             ATF_FAIL("Call to ::getgroups failed");
76*0a6a1f1dSLionel Sambuc         for (int i = 0; i < ngids; i++) {
77*0a6a1f1dSLionel Sambuc             groups.insert(gids[i]);
78*0a6a1f1dSLionel Sambuc             if (gids[i] > maxgid)
79*0a6a1f1dSLionel Sambuc                 maxgid = gids[i];
80*0a6a1f1dSLionel Sambuc         }
81*0a6a1f1dSLionel Sambuc         std::cout << "User belongs to " << ngids << " groups\n";
82*0a6a1f1dSLionel Sambuc         std::cout << "Last GID is " << maxgid << "\n";
83*0a6a1f1dSLionel Sambuc     }
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc     const gid_t maxgid_limit = 1 << 16;
86*0a6a1f1dSLionel Sambuc     if (maxgid > maxgid_limit) {
87*0a6a1f1dSLionel Sambuc         std::cout << "Test truncated from " << maxgid << " groups to "
88*0a6a1f1dSLionel Sambuc                   << maxgid_limit << " to keep the run time reasonable "
89*0a6a1f1dSLionel Sambuc             "enough\n";
90*0a6a1f1dSLionel Sambuc         maxgid = maxgid_limit;
91*0a6a1f1dSLionel Sambuc     }
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc     for (gid_t g = 0; g <= maxgid; g++) {
94*0a6a1f1dSLionel Sambuc         if (groups.find(g) == groups.end()) {
95*0a6a1f1dSLionel Sambuc             std::cout << "Checking if user does not belong to group "
96*0a6a1f1dSLionel Sambuc                       << g << "\n";
97*0a6a1f1dSLionel Sambuc             ATF_REQUIRE(!is_member_of_group(g));
98*0a6a1f1dSLionel Sambuc         } else {
99*0a6a1f1dSLionel Sambuc             std::cout << "Checking if user belongs to group " << g << "\n";
100*0a6a1f1dSLionel Sambuc             ATF_REQUIRE(is_member_of_group(g));
101*0a6a1f1dSLionel Sambuc         }
102*0a6a1f1dSLionel Sambuc     }
103*0a6a1f1dSLionel Sambuc }
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(is_root);
ATF_TEST_CASE_HEAD(is_root)106*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(is_root)
107*0a6a1f1dSLionel Sambuc {
108*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the is_root function");
109*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(is_root)110*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(is_root)
111*0a6a1f1dSLionel Sambuc {
112*0a6a1f1dSLionel Sambuc     using tools::user::is_root;
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc     if (::geteuid() == 0) {
115*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(is_root());
116*0a6a1f1dSLionel Sambuc     } else {
117*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!is_root());
118*0a6a1f1dSLionel Sambuc     }
119*0a6a1f1dSLionel Sambuc }
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(is_unprivileged);
ATF_TEST_CASE_HEAD(is_unprivileged)122*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(is_unprivileged)
123*0a6a1f1dSLionel Sambuc {
124*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the is_unprivileged function");
125*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(is_unprivileged)126*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(is_unprivileged)
127*0a6a1f1dSLionel Sambuc {
128*0a6a1f1dSLionel Sambuc     using tools::user::is_unprivileged;
129*0a6a1f1dSLionel Sambuc 
130*0a6a1f1dSLionel Sambuc     if (::geteuid() != 0) {
131*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(is_unprivileged());
132*0a6a1f1dSLionel Sambuc     } else {
133*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!is_unprivileged());
134*0a6a1f1dSLionel Sambuc     }
135*0a6a1f1dSLionel Sambuc }
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
138*0a6a1f1dSLionel Sambuc // Main.
139*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
140*0a6a1f1dSLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)141*0a6a1f1dSLionel Sambuc ATF_INIT_TEST_CASES(tcs)
142*0a6a1f1dSLionel Sambuc {
143*0a6a1f1dSLionel Sambuc     // Add the tests for the free functions.
144*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, euid);
145*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_member_of_group);
146*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_root);
147*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_unprivileged);
148*0a6a1f1dSLionel Sambuc }
149