tst-sgetsgent.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Test large input for sgetsgent (bug 30151).
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <gshadow.h>
  16. #include <stddef.h>
  17. #include <support/check.h>
  18. #include <support/support.h>
  19. #include <support/xmemstream.h>
  20. #include <stdlib.h>
  21. static int
  22. do_test (void)
  23. {
  24. /* Create a shadow group with 1000 members. */
  25. struct xmemstream mem;
  26. xopen_memstream (&mem);
  27. const char *passwd = "k+zD0nucwfxAo3sw1NXUj6K5vt5M16+X0TVGdE1uFvq5R8V7efJ";
  28. fprintf (mem.out, "group-name:%s::m0", passwd);
  29. for (int i = 1; i < 1000; ++i)
  30. fprintf (mem.out, ",m%d", i);
  31. xfclose_memstream (&mem);
  32. /* Call sgetsgent. */
  33. char *input = mem.buffer;
  34. struct sgrp *e = sgetsgent (input);
  35. TEST_VERIFY_EXIT (e != NULL);
  36. TEST_COMPARE_STRING (e->sg_namp, "group-name");
  37. TEST_COMPARE_STRING (e->sg_passwd, passwd);
  38. /* No administrators. */
  39. TEST_COMPARE_STRING (e->sg_adm[0], NULL);
  40. /* Check the members list. */
  41. for (int i = 0; i < 1000; ++i)
  42. {
  43. char *member = xasprintf ("m%d", i);
  44. TEST_COMPARE_STRING (e->sg_mem[i], member);
  45. free (member);
  46. }
  47. TEST_COMPARE_STRING (e->sg_mem[1000], NULL);
  48. /* Check that putsgent brings back the input string. */
  49. xopen_memstream (&mem);
  50. TEST_COMPARE (putsgent (e, mem.out), 0);
  51. xfclose_memstream (&mem);
  52. /* Compare without the trailing '\n' that putsgent added. */
  53. TEST_COMPARE (mem.buffer[mem.length - 1], '\n');
  54. mem.buffer[mem.length - 1] = '\0';
  55. TEST_COMPARE_STRING (mem.buffer, input);
  56. free (mem.buffer);
  57. free (input);
  58. return 0;
  59. }
  60. #include <support/test-driver.c>