]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/tst-readline.c
nptl/tst-cancel25 needs to be an internal test
[thirdparty/glibc.git] / libio / tst-readline.c
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
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /* Exercise __libc_readline_unlocked with various combinations of line
20 lengths, stdio buffer sizes, and line read buffer sizes. */
21
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <support/check.h>
27 #include <support/support.h>
28 #include <support/temp_file.h>
29 #include <support/test-driver.h>
30 #include <support/xmemstream.h>
31 #include <support/xstdio.h>
32 #include <support/xunistd.h>
33
34 enum
35 {
36 maximum_line_length = 7,
37 number_of_lines = 3,
38 };
39
40 /* -1: Do not set buffer size. 0: unbuffered. Otherwise, use this as
41 the size of the buffer. */
42 static int buffer_size;
43
44 /* These size of the buffer used for reading. Must be at least 2. */
45 static int read_size;
46
47 /* If a read files with ERANGE, increase the buffer size by this
48 amount. Must be positive. */
49 static int read_size_increment;
50
51 /* If non-zero, do not reset the read size after an ERANGE error. */
52 static int read_size_preserve;
53
54 /* If non-zero, no '\n' at the end of the file. */
55 static int no_newline_at_eof;
56
57 /* Length of the line, or -1 if the line is not present. */
58 static int line_lengths[number_of_lines];
59
60 /* The name of the test file. */
61 static char *test_file_path;
62
63 /* The contents of the test file. */
64 static char expected_contents[(maximum_line_length + 2) * number_of_lines + 1];
65 static size_t expected_length;
66
67 /* Returns a random byte which is not zero or the line terminator. */
68 static char
69 random_char (void)
70 {
71 static unsigned int rand_state = 1;
72 while (true)
73 {
74 char result = rand_r (&rand_state) >> 16;
75 if (result != 0 && result != '\n')
76 return result;
77 }
78 }
79
80 /* Create the test file. */
81 static void
82 prepare (int argc, char **argv)
83 {
84 int fd = create_temp_file ("tst-readline-", &test_file_path);
85 TEST_VERIFY_EXIT (fd >= 0);
86 xclose (fd);
87 }
88
89 /* Prepare the test file. Return false if the test parameters are
90 incongruent and the test should be skipped. */
91 static bool
92 write_test_file (void)
93 {
94 expected_length = 0;
95 char *p = expected_contents;
96 for (int lineno = 0; lineno < number_of_lines; ++lineno)
97 for (int i = 0; i < line_lengths[lineno]; ++i)
98 *p++ = random_char ();
99 expected_length = p - &expected_contents[0];
100 if (no_newline_at_eof)
101 {
102 if (expected_length == 0)
103 return false;
104 --expected_length;
105 --p;
106 }
107 if (test_verbose > 0)
108 {
109 printf ("info: writing test file of %zu bytes:\n", expected_length);
110 for (int i = 0; i < number_of_lines; ++i)
111 printf (" line %d: %d\n", i, line_lengths[i]);
112 if (no_newline_at_eof)
113 puts (" (no newline at EOF)");
114 }
115 TEST_VERIFY_EXIT (expected_length < sizeof (expected_contents));
116 *p++ = '\0';
117 support_write_file_string (test_file_path, expected_contents);
118 return true;
119 }
120
121 /* Run a single test (a combination of a test file and read
122 parameters). */
123 static void
124 run_test (void)
125 {
126 TEST_VERIFY_EXIT (read_size_increment > 0);
127 if (test_verbose > 0)
128 {
129 printf ("info: running test: buffer_size=%d read_size=%d\n"
130 " read_size_increment=%d read_size_preserve=%d\n",
131 buffer_size, read_size, read_size_increment, read_size_preserve);
132 }
133
134 struct xmemstream result;
135 xopen_memstream (&result);
136
137 FILE *fp = xfopen (test_file_path, "rce");
138 char *fp_buffer = NULL;
139 if (buffer_size == 0)
140 TEST_VERIFY_EXIT (setvbuf (fp, NULL, _IONBF, 0) == 0);
141 if (buffer_size > 0)
142 {
143 fp_buffer = xmalloc (buffer_size);
144 TEST_VERIFY_EXIT (setvbuf (fp, fp_buffer, _IOFBF, buffer_size) == 0);
145 }
146
147 char *line_buffer = xmalloc (read_size);
148 size_t line_buffer_size = read_size;
149
150 while (true)
151 {
152 ssize_t ret = __libc_readline_unlocked
153 (fp, line_buffer, line_buffer_size);
154 if (ret < 0)
155 {
156 TEST_VERIFY (ret == -1);
157 if (errno != ERANGE)
158 FAIL_EXIT1 ("__libc_readline_unlocked: %m");
159 line_buffer_size += read_size_increment;
160 free (line_buffer);
161 line_buffer = xmalloc (line_buffer_size);
162 /* Try reading this line again. */
163 }
164 else if (ret == 0)
165 break;
166 else
167 {
168 /* A line has been read. Save it. */
169 TEST_VERIFY (ret == strlen (line_buffer));
170 const char *pnl = strchr (line_buffer, '\n');
171 /* If there is a \n, it must be at the end. */
172 TEST_VERIFY (pnl == NULL || pnl == line_buffer + ret - 1);
173 fputs (line_buffer, result.out);
174
175 /* Restore the original read size if required. */
176 if (line_buffer_size > read_size && !read_size_preserve)
177 {
178 line_buffer_size = read_size;
179 free (line_buffer);
180 line_buffer = xmalloc (line_buffer_size);
181 }
182 }
183 }
184
185 xfclose (fp);
186 free (fp_buffer);
187 free (line_buffer);
188
189 xfclose_memstream (&result);
190 TEST_VERIFY (result.length == expected_length);
191 TEST_VERIFY (strcmp (result.buffer, expected_contents) == 0);
192 if (test_verbose > 0)
193 {
194 printf ("info: expected (%zu): [[%s]]\n",
195 expected_length, expected_contents);
196 printf ("info: actual (%zu): [[%s]]\n", result.length, result.buffer);
197 }
198 free (result.buffer);
199 }
200
201 /* Test one test file with multiple read parameters. */
202 static void
203 test_one_file (void)
204 {
205 for (buffer_size = -1; buffer_size <= maximum_line_length + 1; ++buffer_size)
206 for (read_size = 2; read_size <= maximum_line_length + 2; ++read_size)
207 for (read_size_increment = 1; read_size_increment <= 4;
208 ++read_size_increment)
209 for (read_size_preserve = 0; read_size_preserve < 2;
210 ++read_size_preserve)
211 run_test ();
212 }
213
214
215 static int
216 do_test (void)
217 {
218 /* Set up the test file contents. */
219 for (line_lengths[0] = -1; line_lengths[0] <= maximum_line_length;
220 ++line_lengths[0])
221 for (line_lengths[1] = -1; line_lengths[1] <= maximum_line_length;
222 ++line_lengths[1])
223 for (line_lengths[2] = -1; line_lengths[2] <= maximum_line_length;
224 ++line_lengths[2])
225 for (no_newline_at_eof = 0; no_newline_at_eof < 2; ++no_newline_at_eof)
226 {
227 if (!write_test_file ())
228 continue;
229 test_one_file ();
230 }
231 free (test_file_path);
232 return 0;
233 }
234
235 #define TIMEOUT 100
236 #define PREPARE prepare
237 #include <support/test-driver.c>