tst-putpwent.c 3.9 KB

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