Remove building with NOCRYPTO option
[minix3.git] / tests / lib / libc / sys / t_getgroups.c
blob9a8ec8ede27191771169aecb3aaa782371dacc3d
1 /* $NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 $");
34 #include <sys/wait.h>
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
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];
53 errno = 0;
55 ATF_REQUIRE(getgroups(10, (gid_t *)-1) == -1);
56 ATF_REQUIRE(errno == EFAULT);
58 errno = 0;
60 ATF_REQUIRE(getgroups(-1, gidset) == -1);
61 ATF_REQUIRE(errno == EINVAL);
64 ATF_TC(getgroups_getgid);
65 ATF_TC_HEAD(getgroups_getgid, tc)
67 atf_tc_set_md_var(tc, "descr", "Test getgid(2) from getgroups(2)");
70 ATF_TC_BODY(getgroups_getgid, tc)
72 gid_t gidset[NGROUPS_MAX];
73 gid_t gid = getgid();
74 int i, n;
77 * Check that getgid(2) is found from
78 * the GIDs returned by getgroups(2).
80 n = getgroups(NGROUPS_MAX, gidset);
82 for (i = 0; i < n; i++) {
84 if (gidset[i] == gid)
85 return;
88 atf_tc_fail("getgid(2) not found from getgroups(2)");
91 ATF_TC(getgroups_setgid);
92 ATF_TC_HEAD(getgroups_setgid, tc)
94 atf_tc_set_md_var(tc, "descr", "Test setgid(2) from getgroups(2)");
95 atf_tc_set_md_var(tc, "require.user", "root");
98 ATF_TC_BODY(getgroups_setgid, tc)
100 gid_t gidset[NGROUPS_MAX];
101 int i, n, rv, sta;
102 pid_t pid;
105 * Check that we can setgid(2)
106 * to the returned group IDs.
108 n = getgroups(NGROUPS_MAX, gidset);
109 ATF_REQUIRE(n >= 0);
111 for (i = 0; i < n; i++) {
113 pid = fork();
114 ATF_REQUIRE(pid >= 0);
116 if (pid == 0) {
118 rv = setgid(gidset[i]);
120 if (rv != 0)
121 _exit(EXIT_FAILURE);
123 _exit(EXIT_SUCCESS);
126 (void)wait(&sta);
128 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
129 atf_tc_fail("getgroups(2) is inconsistent");
133 ATF_TC(getgroups_zero);
134 ATF_TC_HEAD(getgroups_zero, tc)
136 atf_tc_set_md_var(tc, "descr", "Test getgroups(2) with zero param");
139 ATF_TC_BODY(getgroups_zero, tc)
141 const gid_t val = 123456789;
142 gid_t gidset[NGROUPS_MAX];
143 size_t i;
146 * If the first parameter is zero, the number
147 * of groups should be returned but the supplied
148 * buffer should remain intact.
150 for (i = 0; i < __arraycount(gidset); i++)
151 gidset[i] = val;
153 ATF_REQUIRE(getgroups(0, gidset) >= 0);
155 for (i = 0; i < __arraycount(gidset); i++) {
157 if (gidset[i] != val)
158 atf_tc_fail("getgroups(2) modified the buffer");
162 ATF_TP_ADD_TCS(tp)
165 ATF_TP_ADD_TC(tp, getgroups_err);
166 ATF_TP_ADD_TC(tp, getgroups_getgid);
167 ATF_TP_ADD_TC(tp, getgroups_setgid);
168 ATF_TP_ADD_TC(tp, getgroups_zero);
170 return atf_no_error();