root/maint/gnulib/tests/test-getgroups.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Tests of getgroups.
   2    Copyright (C) 2009-2021 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 <https://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 "signature.h"
  24 SIGNATURE_CHECK (getgroups, int, (int, gid_t[]));
  25 
  26 #include <errno.h>
  27 #include <stdio.h>
  28 #include <stdlib.h>
  29 #include <stdint.h>
  30 
  31 #include "macros.h"
  32 
  33 /* Tell GCC not to warn about the specific edge cases tested here.
  34    GCC >= 10 with glibc >= 2.32 would otherwise trigger warnings, even without
  35    any -W options, because getgroups() is declared with
  36      __attribute__ ((__access__ (__write_only__, 2, 1)))
  37  */
  38 #if __GNUC__ >= 7
  39 # pragma GCC diagnostic ignored "-Wstringop-overflow"
  40 #endif
  41 
  42 int
  43 main (int argc, _GL_UNUSED char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45   int result;
  46   gid_t *groups;
  47 
  48   errno = 0;
  49   result = getgroups (0, NULL);
  50   if (result == -1 && errno == ENOSYS)
  51     {
  52       fputs ("skipping test: no support for groups\n", stderr);
  53       return 77;
  54     }
  55   ASSERT (0 <= result);
  56   ASSERT (result + 1 < SIZE_MAX / sizeof *groups);
  57   groups = malloc ((result + 1) * sizeof *groups);
  58   ASSERT (groups);
  59   groups[result] = -1;
  60   /* Check for EINVAL handling.  Not all processes have supplemental
  61      groups, and getgroups does not have to return the effective gid,
  62      so a result of 0 is reasonable.  Also, we can't test for EINVAL
  63      if result is 1, because of how getgroups treats 0.  */
  64   if (1 < result)
  65     {
  66       errno = 0;
  67       ASSERT (getgroups (result - 1, groups) == -1);
  68       ASSERT (errno == EINVAL);
  69     }
  70   ASSERT (getgroups (result, groups) == result);
  71   ASSERT (getgroups (result + 1, groups) == result);
  72   ASSERT (groups[result] == -1);
  73   errno = 0;
  74   ASSERT (getgroups (-1, NULL) == -1);
  75   ASSERT (errno == EINVAL);
  76 
  77   /* The automated unit test, with no arguments, ends here.  However,
  78      for debugging purposes, you can pass a command-line argument to
  79      list the returned groups.  */
  80   if (1 < argc)
  81     {
  82       int i;
  83       for (i = 0; i < result; i++)
  84         printf ("%d\n", (int) groups[i]);
  85     }
  86   free (groups);
  87   return 0;
  88 }

/* [previous][next][first][last][top][bottom][index][help] */