tst-putgrent.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Test for processing of invalid group entries. [BZ #18724]
  2. Copyright (C) 2015-2019 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. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <grp.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. static bool errors;
  22. static void
  23. check (struct group e, const char *expected)
  24. {
  25. char *buf;
  26. size_t buf_size;
  27. FILE *f = open_memstream (&buf, &buf_size);
  28. if (f == NULL)
  29. {
  30. printf ("open_memstream: %m\n");
  31. errors = true;
  32. return;
  33. }
  34. int ret = putgrent (&e, f);
  35. if (expected == NULL)
  36. {
  37. if (ret == -1)
  38. {
  39. if (errno != EINVAL)
  40. {
  41. printf ("putgrent: unexpected error code: %m\n");
  42. errors = true;
  43. }
  44. }
  45. else
  46. {
  47. printf ("putgrent: unexpected success (\"%s\", \"%s\")\n",
  48. e.gr_name, e.gr_passwd);
  49. errors = true;
  50. }
  51. }
  52. else
  53. {
  54. /* Expect success. */
  55. size_t expected_length = strlen (expected);
  56. if (ret == 0)
  57. {
  58. long written = ftell (f);
  59. if (written <= 0 || fflush (f) < 0)
  60. {
  61. printf ("stream error: %m\n");
  62. errors = true;
  63. }
  64. else if (buf[written - 1] != '\n')
  65. {
  66. printf ("FAILED: \"%s\" without newline\n", expected);
  67. errors = true;
  68. }
  69. else if (strncmp (buf, expected, written - 1) != 0
  70. || written - 1 != expected_length)
  71. {
  72. buf[written - 1] = '\0';
  73. printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n",
  74. buf, written - 1, expected, expected_length);
  75. errors = true;
  76. }
  77. }
  78. else
  79. {
  80. printf ("FAILED: putgrent (expected \"%s\"): %m\n", expected);
  81. errors = true;
  82. }
  83. }
  84. fclose (f);
  85. free (buf);
  86. }
  87. static int
  88. do_test (void)
  89. {
  90. check ((struct group) {
  91. .gr_name = (char *) "root",
  92. },
  93. "root::0:");
  94. check ((struct group) {
  95. .gr_name = (char *) "root",
  96. .gr_passwd = (char *) "password",
  97. .gr_gid = 1234,
  98. .gr_mem = (char *[2]) {(char *) "member1", NULL}
  99. },
  100. "root:password:1234:member1");
  101. check ((struct group) {
  102. .gr_name = (char *) "root",
  103. .gr_passwd = (char *) "password",
  104. .gr_gid = 1234,
  105. .gr_mem = (char *[3]) {(char *) "member1", (char *) "member2", NULL}
  106. },
  107. "root:password:1234:member1,member2");
  108. /* Bad values. */
  109. {
  110. static const char *const bad_strings[] = {
  111. ":",
  112. "\n",
  113. ":bad",
  114. "\nbad",
  115. "b:ad",
  116. "b\nad",
  117. "bad:",
  118. "bad\n",
  119. "b:a\nd"
  120. ",",
  121. "\n,",
  122. ":,",
  123. ",bad",
  124. "b,ad",
  125. "bad,",
  126. NULL
  127. };
  128. for (const char *const *bad = bad_strings; *bad != NULL; ++bad)
  129. {
  130. char *members[]
  131. = {(char *) "first", (char *) *bad, (char *) "last", NULL};
  132. if (strpbrk (*bad, ":\n") != NULL)
  133. {
  134. check ((struct group) {
  135. .gr_name = (char *) *bad,
  136. }, NULL);
  137. check ((struct group) {
  138. .gr_name = (char *) "root",
  139. .gr_passwd = (char *) *bad,
  140. }, NULL);
  141. }
  142. check ((struct group) {
  143. .gr_name = (char *) "root",
  144. .gr_passwd = (char *) "password",
  145. .gr_mem = members,
  146. }, NULL);
  147. }
  148. }
  149. return errors;
  150. }
  151. #define TEST_FUNCTION do_test ()
  152. #include "../test-skeleton.c"