diff options
Diffstat (limited to 'src/regress/lib/libc/sys/t_getgroups.c')
-rw-r--r-- | src/regress/lib/libc/sys/t_getgroups.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_getgroups.c b/src/regress/lib/libc/sys/t_getgroups.c new file mode 100644 index 0000000000..dcef3c16f5 --- /dev/null +++ b/src/regress/lib/libc/sys/t_getgroups.c | |||
@@ -0,0 +1,175 @@ | |||
1 | /* $OpenBSD: t_getgroups.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */ | ||
2 | /* $NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * This code is derived from software contributed to The NetBSD Foundation | ||
9 | * by Jukka Ruohonen. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "macros.h" | ||
34 | |||
35 | #include <sys/cdefs.h> | ||
36 | __RCSID("$NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $"); | ||
37 | |||
38 | #include <sys/wait.h> | ||
39 | |||
40 | #include "atf-c.h" | ||
41 | #include <errno.h> | ||
42 | #include <limits.h> | ||
43 | #include <stdlib.h> | ||
44 | #include <string.h> | ||
45 | #include <unistd.h> | ||
46 | |||
47 | ATF_TC(getgroups_err); | ||
48 | ATF_TC_HEAD(getgroups_err, tc) | ||
49 | { | ||
50 | atf_tc_set_md_var(tc, "descr", "Test errors in getgroups(2)"); | ||
51 | } | ||
52 | |||
53 | ATF_TC_BODY(getgroups_err, tc) | ||
54 | { | ||
55 | gid_t gidset[NGROUPS_MAX]; | ||
56 | |||
57 | errno = 0; | ||
58 | |||
59 | ATF_REQUIRE(getgroups(NGROUPS_MAX, (gid_t *)-1) == -1); | ||
60 | ATF_REQUIRE(errno == EFAULT); | ||
61 | |||
62 | errno = 0; | ||
63 | |||
64 | ATF_REQUIRE(getgroups(-1, gidset) == -1); | ||
65 | ATF_REQUIRE(errno == EINVAL); | ||
66 | } | ||
67 | |||
68 | ATF_TC(getgroups_getgid); | ||
69 | ATF_TC_HEAD(getgroups_getgid, tc) | ||
70 | { | ||
71 | atf_tc_set_md_var(tc, "descr", "Test getgid(2) from getgroups(2)"); | ||
72 | } | ||
73 | |||
74 | ATF_TC_BODY(getgroups_getgid, tc) | ||
75 | { | ||
76 | gid_t gidset[NGROUPS_MAX]; | ||
77 | gid_t gid = getgid(); | ||
78 | int i, n; | ||
79 | |||
80 | /* | ||
81 | * Check that getgid(2) is found from | ||
82 | * the GIDs returned by getgroups(2). | ||
83 | */ | ||
84 | n = getgroups(NGROUPS_MAX, gidset); | ||
85 | |||
86 | for (i = 0; i < n; i++) { | ||
87 | |||
88 | if (gidset[i] == gid) | ||
89 | return; | ||
90 | } | ||
91 | |||
92 | atf_tc_fail("getgid(2) not found from getgroups(2)"); | ||
93 | } | ||
94 | |||
95 | ATF_TC(getgroups_setgid); | ||
96 | ATF_TC_HEAD(getgroups_setgid, tc) | ||
97 | { | ||
98 | atf_tc_set_md_var(tc, "descr", "Test setgid(2) from getgroups(2)"); | ||
99 | atf_tc_set_md_var(tc, "require.user", "root"); | ||
100 | } | ||
101 | |||
102 | ATF_TC_BODY(getgroups_setgid, tc) | ||
103 | { | ||
104 | gid_t gidset[NGROUPS_MAX]; | ||
105 | int i, n, rv, sta; | ||
106 | pid_t pid; | ||
107 | |||
108 | /* | ||
109 | * Check that we can setgid(2) | ||
110 | * to the returned group IDs. | ||
111 | */ | ||
112 | n = getgroups(NGROUPS_MAX, gidset); | ||
113 | ATF_REQUIRE(n >= 0); | ||
114 | |||
115 | for (i = 0; i < n; i++) { | ||
116 | |||
117 | pid = fork(); | ||
118 | ATF_REQUIRE(pid >= 0); | ||
119 | |||
120 | if (pid == 0) { | ||
121 | |||
122 | rv = setgid(gidset[i]); | ||
123 | |||
124 | if (rv != 0) | ||
125 | _exit(EXIT_FAILURE); | ||
126 | |||
127 | _exit(EXIT_SUCCESS); | ||
128 | } | ||
129 | |||
130 | (void)wait(&sta); | ||
131 | |||
132 | if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) | ||
133 | atf_tc_fail("getgroups(2) is inconsistent"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | ATF_TC(getgroups_zero); | ||
138 | ATF_TC_HEAD(getgroups_zero, tc) | ||
139 | { | ||
140 | atf_tc_set_md_var(tc, "descr", "Test getgroups(2) with zero param"); | ||
141 | } | ||
142 | |||
143 | ATF_TC_BODY(getgroups_zero, tc) | ||
144 | { | ||
145 | const gid_t val = 123456789; | ||
146 | gid_t gidset[NGROUPS_MAX]; | ||
147 | size_t i; | ||
148 | |||
149 | /* | ||
150 | * If the first parameter is zero, the number | ||
151 | * of groups should be returned but the supplied | ||
152 | * buffer should remain intact. | ||
153 | */ | ||
154 | for (i = 0; i < __arraycount(gidset); i++) | ||
155 | gidset[i] = val; | ||
156 | |||
157 | ATF_REQUIRE(getgroups(0, gidset) >= 0); | ||
158 | |||
159 | for (i = 0; i < __arraycount(gidset); i++) { | ||
160 | |||
161 | if (gidset[i] != val) | ||
162 | atf_tc_fail("getgroups(2) modified the buffer"); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | ATF_TP_ADD_TCS(tp) | ||
167 | { | ||
168 | |||
169 | ATF_TP_ADD_TC(tp, getgroups_err); | ||
170 | ATF_TP_ADD_TC(tp, getgroups_getgid); | ||
171 | ATF_TP_ADD_TC(tp, getgroups_setgid); | ||
172 | ATF_TP_ADD_TC(tp, getgroups_zero); | ||
173 | |||
174 | return atf_no_error(); | ||
175 | } | ||