1 /* Copyright (c) 2007 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/detail/user.h"
28 #include <sys/param.h>
29 #include <sys/types.h>
37 #include "atf-c/detail/test_helpers.h"
39 /* ---------------------------------------------------------------------
40 * Test cases for the free functions.
41 * --------------------------------------------------------------------- */
46 atf_tc_set_md_var(tc, "descr", "Tests the atf_user_euid function");
50 ATF_REQUIRE_EQ(atf_user_euid(), geteuid());
53 ATF_TC(is_member_of_group);
54 ATF_TC_HEAD(is_member_of_group, tc)
56 atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_member_of_group "
59 ATF_TC_BODY(is_member_of_group, tc)
61 gid_t gids[NGROUPS_MAX];
64 const gid_t maxgid_limit = 1 << 16;
69 ngids = getgroups(NGROUPS_MAX, gids);
71 atf_tc_fail("Call to getgroups failed");
73 for (i = 0; i < ngids; i++) {
74 printf("User group %d is %u\n", i, gids[i]);
78 printf("User belongs to %d groups\n", ngids);
79 printf("Last GID is %u\n", maxgid);
82 if (maxgid > maxgid_limit) {
83 printf("Test truncated from %u groups to %u to keep the run time "
84 "reasonable enough\n", maxgid, maxgid_limit);
85 maxgid = maxgid_limit;
88 for (g = 0; g < maxgid; g++) {
92 for (i = 0; !found && i < ngids; i++) {
98 printf("Checking if user belongs to group %d\n", g);
99 ATF_REQUIRE(atf_user_is_member_of_group(g));
101 printf("Checking if user does not belong to group %d\n", g);
102 ATF_REQUIRE(!atf_user_is_member_of_group(g));
108 ATF_TC_HEAD(is_root, tc)
110 atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_root function");
112 ATF_TC_BODY(is_root, tc)
115 ATF_REQUIRE(atf_user_is_root());
117 ATF_REQUIRE(!atf_user_is_root());
120 ATF_TC(is_unprivileged);
121 ATF_TC_HEAD(is_unprivileged, tc)
123 atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_unprivileged "
126 ATF_TC_BODY(is_unprivileged, tc)
129 ATF_REQUIRE(atf_user_is_unprivileged());
131 ATF_REQUIRE(!atf_user_is_unprivileged());
134 /* ---------------------------------------------------------------------
136 * --------------------------------------------------------------------- */
140 ATF_TP_ADD_TC(tp, euid);
141 ATF_TP_ADD_TC(tp, is_member_of_group);
142 ATF_TP_ADD_TC(tp, is_root);
143 ATF_TP_ADD_TC(tp, is_unprivileged);
145 return atf_no_error();