1 /* $NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
43 ATF_TC(getgroups_err);
44 ATF_TC_HEAD(getgroups_err, tc)
46 atf_tc_set_md_var(tc, "descr", "Test errors in getgroups(2)");
49 ATF_TC_BODY(getgroups_err, tc)
51 gid_t gidset[NGROUPS_MAX];
55 ATF_REQUIRE(getgroups(10, (gid_t *)-1) == -1);
56 ATF_REQUIRE(errno == EFAULT);
61 atf_tc_expect_fail("Reported as kern/189941");
63 ATF_REQUIRE(getgroups(-1, gidset) == -1);
64 ATF_REQUIRE(errno == EINVAL);
67 ATF_TC(getgroups_getgid);
68 ATF_TC_HEAD(getgroups_getgid, tc)
70 atf_tc_set_md_var(tc, "descr", "Test getgid(2) from getgroups(2)");
73 ATF_TC_BODY(getgroups_getgid, tc)
75 gid_t gidset[NGROUPS_MAX];
80 * Check that getgid(2) is found from
81 * the GIDs returned by getgroups(2).
83 n = getgroups(NGROUPS_MAX, gidset);
85 for (i = 0; i < n; i++) {
91 atf_tc_fail("getgid(2) not found from getgroups(2)");
94 ATF_TC(getgroups_setgid);
95 ATF_TC_HEAD(getgroups_setgid, tc)
97 atf_tc_set_md_var(tc, "descr", "Test setgid(2) from getgroups(2)");
98 atf_tc_set_md_var(tc, "require.user", "root");
101 ATF_TC_BODY(getgroups_setgid, tc)
103 gid_t gidset[NGROUPS_MAX];
108 * Check that we can setgid(2)
109 * to the returned group IDs.
111 n = getgroups(NGROUPS_MAX, gidset);
114 for (i = 0; i < n; i++) {
117 ATF_REQUIRE(pid >= 0);
121 rv = setgid(gidset[i]);
131 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
132 atf_tc_fail("getgroups(2) is inconsistent");
136 ATF_TC(getgroups_zero);
137 ATF_TC_HEAD(getgroups_zero, tc)
139 atf_tc_set_md_var(tc, "descr", "Test getgroups(2) with zero param");
142 ATF_TC_BODY(getgroups_zero, tc)
144 const gid_t val = 123456789;
145 gid_t gidset[NGROUPS_MAX];
149 * If the first parameter is zero, the number
150 * of groups should be returned but the supplied
151 * buffer should remain intact.
153 for (i = 0; i < __arraycount(gidset); i++)
156 ATF_REQUIRE(getgroups(0, gidset) >= 0);
158 for (i = 0; i < __arraycount(gidset); i++) {
160 if (gidset[i] != val)
161 atf_tc_fail("getgroups(2) modified the buffer");
168 ATF_TP_ADD_TC(tp, getgroups_err);
169 ATF_TP_ADD_TC(tp, getgroups_getgid);
170 ATF_TP_ADD_TC(tp, getgroups_setgid);
171 ATF_TP_ADD_TC(tp, getgroups_zero);
173 return atf_no_error();