1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32
33 /*
34 AUTHOR: Barrie Kletscher
35 Rewrote : 11-92 by Richard Logan
36 CO-PILOT: Dave Baumgartner
37
38 TEST ITEMS:
39 1. Check to see if getgroups(-1, gidset) fails and sets errno to EINVAL
40 2. Check to see if getgroups(0, gidset) does not return -1 and gidset is
41 not modified.
42 3. Check to see if getgroups(x, gigset) fails and sets errno to EINVAL,
43 where x is one less then what is returned by getgroups(0, gidset).
44 4. Check to see if getgroups() succeeds and gidset contains
45 group id returned from getgid().
46 */
47
48 #include <unistd.h>
49 #include <signal.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <grp.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
55
56 #include "test.h"
57
58 /*
59 * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
60 * rewriting all tests to the new API.
61 */
62 #include "compat_16.h"
63
64 static void setup(void);
65 static void cleanup(void);
66
67 TCID_DEFINE(getgroups01);
68 int TST_TOTAL = 4;
69
70 static GID_T gidset[NGROUPS];
71 static GID_T cmpset[NGROUPS];
72
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 int lc;
76 GID_T group;
77 int i;
78 int entries;
79
80 tst_parse_opts(ac, av, NULL, NULL);
81
82 setup();
83
84 for (lc = 0; TEST_LOOPING(lc); lc++) {
85
86 tst_count = 0;
87
88 TEST(GETGROUPS(cleanup, -1, gidset));
89
90 if (TEST_RETURN == 0) {
91 tst_resm(TFAIL, "getgroups succeeded unexpectedly");
92 } else {
93 if (errno == EINVAL)
94 tst_resm(TPASS,
95 "getgroups failed as expected with EINVAL");
96 else
97 tst_resm(TFAIL | TTERRNO,
98 "getgroups didn't fail as expected with EINVAL");
99 }
100
101 /*
102 * Check that if ngrps is zero that the number of groups is
103 * return and the gidset array is not modified.
104 * This is a POSIX special case.
105 */
106 memset(gidset, 052, NGROUPS * sizeof(GID_T));
107 memset(cmpset, 052, NGROUPS * sizeof(GID_T));
108
109 TEST(GETGROUPS(cleanup, 0, gidset));
110 if (TEST_RETURN == -1) {
111 tst_resm(TFAIL | TTERRNO, "getgroups failed unexpectedly");
112 } else {
113 if (memcmp(cmpset, gidset, NGROUPS * sizeof(GID_T)) != 0)
114 tst_resm(TFAIL,
115 "getgroups modified the gidset array");
116 else
117 tst_resm(TPASS,
118 "getgroups did not modify the gidset "
119 "array");
120 }
121
122 /*
123 * Check to see that is -1 is returned and errno is set to
124 * EINVAL when ngroups is not big enough to hold all groups.
125 */
126 if (TEST_RETURN <= 1) {
127 tst_resm(TCONF,
128 "getgroups returned %ld; unable to test that using ngrps >=1 but less than number of grps",
129 TEST_RETURN);
130 } else {
131 TEST(GETGROUPS(cleanup, TEST_RETURN - 1, gidset));
132 if (TEST_RETURN == -1) {
133 if (errno == EINVAL)
134 tst_resm(TPASS,
135 "getgroups failed as "
136 "expected with EINVAL");
137 else
138 tst_resm(TFAIL | TERRNO,
139 "getgroups didn't fail "
140 "with EINVAL");
141 } else {
142 tst_resm(TFAIL,
143 "getgroups succeeded unexpectedly with %ld",
144 TEST_RETURN);
145 }
146 }
147
148 TEST(GETGROUPS(cleanup, NGROUPS, gidset));
149 if ((entries = TEST_RETURN) == -1) {
150 tst_resm(TFAIL | TTERRNO,
151 "getgroups failed unexpectedly");
152 } else {
153
154 group = getgid();
155
156 for (i = 0; i < entries; i++) {
157 if (gidset[i] == group) {
158 tst_resm(TPASS,
159 "getgroups(NGROUPS,gidset) "
160 "returned %d contains gid %d "
161 "(from getgid)",
162 entries, group);
163 break;
164 }
165 }
166
167 if (i == entries) {
168 tst_resm(TFAIL,
169 "getgroups(NGROUPS,gidset) ret %d, does "
170 "not contain gid %d (from getgid)",
171 entries, group);
172 }
173 }
174
175 }
176
177 cleanup();
178 tst_exit();
179 }
180
setup(void)181 static void setup(void)
182 {
183 tst_sig(FORK, DEF_HANDLER, cleanup);
184
185 TEST_PAUSE;
186
187 gid_t init_gidset[3] = {0, 1, 2};
188 setgroups(3, init_gidset);
189 }
190
cleanup(void)191 static void cleanup(void)
192 {
193 }
194