getgroups: fix logic error
[gnulib.git] / tests / test-getgroups.c
1 /* Tests of getgroups.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 int
40 main (int argc, char **argv _UNUSED_PARAMETER_)
41 {
42   int result;
43   GETGROUPS_T *groups;
44
45   errno = 0;
46   result = getgroups (0, NULL);
47   if (result == -1 && errno == ENOSYS)
48     {
49       fputs ("skipping test: no support for groups\n", stderr);
50       return 77;
51     }
52   ASSERT (0 <= result);
53   ASSERT (result + 1 < SIZE_MAX / sizeof *groups);
54   groups = malloc (result + 1 * sizeof *groups);
55   ASSERT (groups);
56   groups[result] = -1;
57   /* Check for EINVAL handling.  Not all processes have supplemental
58      groups, and getgroups does not have to return the effective gid,
59      so a result of 0 is reasonable.  Also, we can't test for EINVAL
60      if result is 1, because of how getgroups treats 0.  */
61   if (1 < result)
62     {
63       errno = 0;
64       ASSERT (getgroups (result - 1, groups) == -1);
65       ASSERT (errno == EINVAL);
66     }
67   ASSERT (getgroups (result, groups) == result);
68   ASSERT (getgroups (result + 1, groups) == result);
69   ASSERT (groups[result] == -1);
70   errno = 0;
71   ASSERT (getgroups (-1, NULL) == -1);
72   ASSERT (errno == EINVAL);
73
74   /* The automated unit test, with no arguments, ends here.  However,
75      for debugging purposes, you can pass a command-line argument to
76      list the returned groups.  */
77   if (1 < argc)
78     {
79       int i;
80       for (i = 0; i < result; i++)
81         printf ("%d\n", (int) groups[i]);
82     }
83   return 0;
84 }