2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
32 #include <sys/types.h>
40 #include <atf-c++.hpp>
44 // ------------------------------------------------------------------------
45 // Test cases for the free functions.
46 // ------------------------------------------------------------------------
49 ATF_TEST_CASE_HEAD(euid
)
51 set_md_var("descr", "Tests the euid function");
53 ATF_TEST_CASE_BODY(euid
)
55 using tools::user::euid
;
57 ATF_REQUIRE_EQ(euid(), ::geteuid());
60 ATF_TEST_CASE(is_member_of_group
);
61 ATF_TEST_CASE_HEAD(is_member_of_group
)
63 set_md_var("descr", "Tests the is_member_of_group function");
65 ATF_TEST_CASE_BODY(is_member_of_group
)
67 using tools::user::is_member_of_group
;
69 std::set
< gid_t
> groups
;
72 gid_t gids
[NGROUPS_MAX
];
73 int ngids
= ::getgroups(NGROUPS_MAX
, gids
);
75 ATF_FAIL("Call to ::getgroups failed");
76 for (int i
= 0; i
< ngids
; i
++) {
77 groups
.insert(gids
[i
]);
81 std::cout
<< "User belongs to " << ngids
<< " groups\n";
82 std::cout
<< "Last GID is " << maxgid
<< "\n";
85 const gid_t maxgid_limit
= 1 << 16;
86 if (maxgid
> maxgid_limit
) {
87 std::cout
<< "Test truncated from " << maxgid
<< " groups to "
88 << maxgid_limit
<< " to keep the run time reasonable "
90 maxgid
= maxgid_limit
;
93 for (gid_t g
= 0; g
<= maxgid
; g
++) {
94 if (groups
.find(g
) == groups
.end()) {
95 std::cout
<< "Checking if user does not belong to group "
97 ATF_REQUIRE(!is_member_of_group(g
));
99 std::cout
<< "Checking if user belongs to group " << g
<< "\n";
100 ATF_REQUIRE(is_member_of_group(g
));
105 ATF_TEST_CASE(is_root
);
106 ATF_TEST_CASE_HEAD(is_root
)
108 set_md_var("descr", "Tests the is_root function");
110 ATF_TEST_CASE_BODY(is_root
)
112 using tools::user::is_root
;
114 if (::geteuid() == 0) {
115 ATF_REQUIRE(is_root());
117 ATF_REQUIRE(!is_root());
121 ATF_TEST_CASE(is_unprivileged
);
122 ATF_TEST_CASE_HEAD(is_unprivileged
)
124 set_md_var("descr", "Tests the is_unprivileged function");
126 ATF_TEST_CASE_BODY(is_unprivileged
)
128 using tools::user::is_unprivileged
;
130 if (::geteuid() != 0) {
131 ATF_REQUIRE(is_unprivileged());
133 ATF_REQUIRE(!is_unprivileged());
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
141 ATF_INIT_TEST_CASES(tcs
)
143 // Add the tests for the free functions.
144 ATF_ADD_TEST_CASE(tcs
, euid
);
145 ATF_ADD_TEST_CASE(tcs
, is_member_of_group
);
146 ATF_ADD_TEST_CASE(tcs
, is_root
);
147 ATF_ADD_TEST_CASE(tcs
, is_unprivileged
);