tst-putspent.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Test for processing of invalid shadow 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 <shadow.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 spwd 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 = putspent (&p, f);
  35. if (expected == NULL)
  36. {
  37. if (ret == -1)
  38. {
  39. if (errno != EINVAL)
  40. {
  41. printf ("putspent: unexpected error code: %m\n");
  42. errors = true;
  43. }
  44. }
  45. else
  46. {
  47. printf ("putspent: unexpected success (\"%s\")\n", p.sp_namp);
  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: putspent (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 spwd) {
  89. .sp_namp = (char *) "root",
  90. },
  91. "root::0:0:0:0:0:0:0");
  92. check ((struct spwd) {
  93. .sp_namp = (char *) "root",
  94. .sp_pwdp = (char *) "password",
  95. },
  96. "root:password:0:0:0:0:0:0:0");
  97. check ((struct spwd) {
  98. .sp_namp = (char *) "root",
  99. .sp_pwdp = (char *) "password",
  100. .sp_lstchg = -1,
  101. .sp_min = -1,
  102. .sp_max = -1,
  103. .sp_warn = -1,
  104. .sp_inact = -1,
  105. .sp_expire = -1,
  106. .sp_flag = -1
  107. },
  108. "root:password:::::::");
  109. check ((struct spwd) {
  110. .sp_namp = (char *) "root",
  111. .sp_pwdp = (char *) "password",
  112. .sp_lstchg = 1,
  113. .sp_min = 2,
  114. .sp_max = 3,
  115. .sp_warn = 4,
  116. .sp_inact = 5,
  117. .sp_expire = 6,
  118. .sp_flag = 7
  119. },
  120. "root:password:1:2:3:4:5:6:7");
  121. /* Bad values. */
  122. {
  123. static const char *const bad_strings[] = {
  124. ":",
  125. "\n",
  126. ":bad",
  127. "\nbad",
  128. "b:ad",
  129. "b\nad",
  130. "bad:",
  131. "bad\n",
  132. "b:a\nd",
  133. NULL
  134. };
  135. for (const char *const *bad = bad_strings; *bad != NULL; ++bad)
  136. {
  137. check ((struct spwd) {
  138. .sp_namp = (char *) *bad,
  139. }, NULL);
  140. check ((struct spwd) {
  141. .sp_namp = (char *) "root",
  142. .sp_pwdp = (char *) *bad,
  143. }, NULL);
  144. }
  145. }
  146. return errors;
  147. }
  148. #define TEST_FUNCTION do_test ()
  149. #include "../test-skeleton.c"