tst-readline.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Test the __libc_readline_unlocked function.
  2. Copyright (C) 2018-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. /* Exercise __libc_readline_unlocked with various combinations of line
  16. lengths, stdio buffer sizes, and line read buffer sizes. */
  17. #include <errno.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <support/check.h>
  22. #include <support/support.h>
  23. #include <support/temp_file.h>
  24. #include <support/test-driver.h>
  25. #include <support/xmemstream.h>
  26. #include <support/xstdio.h>
  27. #include <support/xunistd.h>
  28. enum
  29. {
  30. maximum_line_length = 7,
  31. number_of_lines = 3,
  32. };
  33. /* -1: Do not set buffer size. 0: unbuffered. Otherwise, use this as
  34. the size of the buffer. */
  35. static int buffer_size;
  36. /* These size of the buffer used for reading. Must be at least 2. */
  37. static int read_size;
  38. /* If a read files with ERANGE, increase the buffer size by this
  39. amount. Must be positive. */
  40. static int read_size_increment;
  41. /* If non-zero, do not reset the read size after an ERANGE error. */
  42. static int read_size_preserve;
  43. /* If non-zero, no '\n' at the end of the file. */
  44. static int no_newline_at_eof;
  45. /* Length of the line, or -1 if the line is not present. */
  46. static int line_lengths[number_of_lines];
  47. /* The name of the test file. */
  48. static char *test_file_path;
  49. /* The contents of the test file. */
  50. static char expected_contents[(maximum_line_length + 2) * number_of_lines + 1];
  51. static size_t expected_length;
  52. /* Returns a random byte which is not zero or the line terminator. */
  53. static char
  54. random_char (void)
  55. {
  56. static unsigned int rand_state = 1;
  57. while (true)
  58. {
  59. char result = rand_r (&rand_state) >> 16;
  60. if (result != 0 && result != '\n')
  61. return result;
  62. }
  63. }
  64. /* Create the test file. */
  65. static void
  66. prepare (int argc, char **argv)
  67. {
  68. int fd = create_temp_file ("tst-readline-", &test_file_path);
  69. TEST_VERIFY_EXIT (fd >= 0);
  70. xclose (fd);
  71. }
  72. /* Prepare the test file. Return false if the test parameters are
  73. incongruent and the test should be skipped. */
  74. static bool
  75. write_test_file (void)
  76. {
  77. expected_length = 0;
  78. char *p = expected_contents;
  79. for (int lineno = 0; lineno < number_of_lines; ++lineno)
  80. for (int i = 0; i < line_lengths[lineno]; ++i)
  81. *p++ = random_char ();
  82. expected_length = p - &expected_contents[0];
  83. if (no_newline_at_eof)
  84. {
  85. if (expected_length == 0)
  86. return false;
  87. --expected_length;
  88. --p;
  89. }
  90. if (test_verbose > 0)
  91. {
  92. printf ("info: writing test file of %zu bytes:\n", expected_length);
  93. for (int i = 0; i < number_of_lines; ++i)
  94. printf (" line %d: %d\n", i, line_lengths[i]);
  95. if (no_newline_at_eof)
  96. puts (" (no newline at EOF)");
  97. }
  98. TEST_VERIFY_EXIT (expected_length < sizeof (expected_contents));
  99. *p++ = '\0';
  100. support_write_file_string (test_file_path, expected_contents);
  101. return true;
  102. }
  103. /* Run a single test (a combination of a test file and read
  104. parameters). */
  105. static void
  106. run_test (void)
  107. {
  108. TEST_VERIFY_EXIT (read_size_increment > 0);
  109. if (test_verbose > 0)
  110. {
  111. printf ("info: running test: buffer_size=%d read_size=%d\n"
  112. " read_size_increment=%d read_size_preserve=%d\n",
  113. buffer_size, read_size, read_size_increment, read_size_preserve);
  114. }
  115. struct xmemstream result;
  116. xopen_memstream (&result);
  117. FILE *fp = xfopen (test_file_path, "rce");
  118. char *fp_buffer = NULL;
  119. if (buffer_size == 0)
  120. TEST_VERIFY_EXIT (setvbuf (fp, NULL, _IONBF, 0) == 0);
  121. if (buffer_size > 0)
  122. {
  123. fp_buffer = xmalloc (buffer_size);
  124. TEST_VERIFY_EXIT (setvbuf (fp, fp_buffer, _IOFBF, buffer_size) == 0);
  125. }
  126. char *line_buffer = xmalloc (read_size);
  127. size_t line_buffer_size = read_size;
  128. while (true)
  129. {
  130. ssize_t ret = __libc_readline_unlocked
  131. (fp, line_buffer, line_buffer_size);
  132. if (ret < 0)
  133. {
  134. TEST_VERIFY (ret == -1);
  135. if (errno != ERANGE)
  136. FAIL_EXIT1 ("__libc_readline_unlocked: %m");
  137. line_buffer_size += read_size_increment;
  138. free (line_buffer);
  139. line_buffer = xmalloc (line_buffer_size);
  140. /* Try reading this line again. */
  141. }
  142. else if (ret == 0)
  143. break;
  144. else
  145. {
  146. /* A line has been read. Save it. */
  147. TEST_VERIFY (ret == strlen (line_buffer));
  148. const char *pnl = strchr (line_buffer, '\n');
  149. /* If there is a \n, it must be at the end. */
  150. TEST_VERIFY (pnl == NULL || pnl == line_buffer + ret - 1);
  151. fputs (line_buffer, result.out);
  152. /* Restore the original read size if required. */
  153. if (line_buffer_size > read_size && !read_size_preserve)
  154. {
  155. line_buffer_size = read_size;
  156. free (line_buffer);
  157. line_buffer = xmalloc (line_buffer_size);
  158. }
  159. }
  160. }
  161. xfclose (fp);
  162. free (fp_buffer);
  163. free (line_buffer);
  164. xfclose_memstream (&result);
  165. TEST_VERIFY (result.length == expected_length);
  166. TEST_VERIFY (strcmp (result.buffer, expected_contents) == 0);
  167. if (test_verbose > 0)
  168. {
  169. printf ("info: expected (%zu): [[%s]]\n",
  170. expected_length, expected_contents);
  171. printf ("info: actual (%zu): [[%s]]\n", result.length, result.buffer);
  172. }
  173. free (result.buffer);
  174. }
  175. /* Test one test file with multiple read parameters. */
  176. static void
  177. test_one_file (void)
  178. {
  179. for (buffer_size = -1; buffer_size <= maximum_line_length + 1; ++buffer_size)
  180. for (read_size = 2; read_size <= maximum_line_length + 2; ++read_size)
  181. for (read_size_increment = 1; read_size_increment <= 4;
  182. ++read_size_increment)
  183. for (read_size_preserve = 0; read_size_preserve < 2;
  184. ++read_size_preserve)
  185. run_test ();
  186. }
  187. static int
  188. do_test (void)
  189. {
  190. /* Set up the test file contents. */
  191. for (line_lengths[0] = -1; line_lengths[0] <= maximum_line_length;
  192. ++line_lengths[0])
  193. for (line_lengths[1] = -1; line_lengths[1] <= maximum_line_length;
  194. ++line_lengths[1])
  195. for (line_lengths[2] = -1; line_lengths[2] <= maximum_line_length;
  196. ++line_lengths[2])
  197. for (no_newline_at_eof = 0; no_newline_at_eof < 2; ++no_newline_at_eof)
  198. {
  199. if (!write_test_file ())
  200. continue;
  201. test_one_file ();
  202. }
  203. free (test_file_path);
  204. return 0;
  205. }
  206. #define TIMEOUT 100
  207. #define PREPARE prepare
  208. #include <support/test-driver.c>