about summary refs log tree commit diff
path: root/stdio-common/tst-getline.c
blob: 29eb7cec0f344872404ca474de181d77df9260be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* Test getline.
   Copyright (C) 2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <support/check.h>
#include <support/test-driver.h>
#include <support/support.h>
#include <support/xstdio.h>
#include <support/xunistd.h>

static struct test_data
{
  /* Input test data for fopencookie stream.  */
  const char *in_data;

  /* The amount of test data left.  */
  size_t in_data_left;

  /* Error number for forcing an error on next read.  */
  int in_error;

  /* Error number for forcing an error (rather than EOF) after all
     bytes read.  */
  int in_error_after;
} the_cookie;

/* Produce a stream of test data based on data in COOKIE, storing up
   to SIZE bytes in BUF.  */

static ssize_t
io_read (void *cookie, char *buf, size_t size)
{
  struct test_data *p = cookie;
  if (p->in_error)
    {
      errno = p->in_error;
      return -1;
    }
  if (size > p->in_data_left)
    size = p->in_data_left;
  memcpy (buf, p->in_data, size);
  p->in_data += size;
  p->in_data_left -= size;
  if (p->in_data_left == 0)
    p->in_error = p->in_error_after;
  return size;
}

/* Set up a test stream with fopencookie.  */

static FILE *
open_test_stream (const char *in_data, size_t size)
{
  static cookie_io_functions_t io_funcs = { .read = io_read };
  the_cookie.in_data = in_data;
  the_cookie.in_data_left = size;
  the_cookie.in_error = 0;
  the_cookie.in_error_after = 0;
  FILE *fp = fopencookie (&the_cookie, "r", io_funcs);
  TEST_VERIFY_EXIT (fp != NULL);
  return fp;
}

/* Set up a test stream with fopencookie, using data from a string
   literal.  */
#define OPEN_TEST_STREAM(IN_DATA) open_test_stream (IN_DATA, sizeof (IN_DATA))

/* Wrap getline to verify that (as per the glibc manual), *LINEPTR is
   returned as non-null and with at least *N bytes (even on error or
   EOF).  Also clear errno for the benefit of tests that check the
   value of errno after the call.  */

ssize_t
wrap_getline (char **lineptr, size_t *n, FILE *stream)
{
  errno = 0;
  ssize_t ret = getline (lineptr, n, stream);
  if (lineptr != NULL && n != NULL)
    {
      TEST_VERIFY (*lineptr != NULL);
      TEST_VERIFY (malloc_usable_size (*lineptr) >= *n);
    }
  return ret;
}

int
do_test (void)
{
  FILE *fp;
  char *lineptr = NULL;
  size_t size = 0;
  ssize_t ret;
  mtrace ();
  /* Test failure with EINVAL (and error indicator for stream set) if
     lineptr is a null pointer.  */
  verbose_printf ("Testing lineptr == NULL\n");
  fp = OPEN_TEST_STREAM ("test");
  ret = wrap_getline (NULL, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (errno, EINVAL);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  /* Test failure with EINVAL (and error indicator for stream set) if
     n is a null pointer.  */
  verbose_printf ("Testing n == NULL\n");
  fp = OPEN_TEST_STREAM ("test");
  ret = wrap_getline (&lineptr, NULL, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (errno, EINVAL);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  /* Test failure with EINVAL (and error indicator for stream set) if
     both lineptr and n are null pointers.  */
  verbose_printf ("Testing lineptr == NULL and n == NULL\n");
  fp = OPEN_TEST_STREAM ("test");
  ret = wrap_getline (NULL, NULL, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (errno, EINVAL);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  /* Test normal line, fitting in available space (including case with
     null bytes).  */
  verbose_printf ("Testing normal nonempty input\n");
  lineptr = xmalloc (10);
  size = 10;
  fp = OPEN_TEST_STREAM ("foo\nbar\0\n\0baz\nte\0st\n");
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "foo\n", 5);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "bar\0\n", 6);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "\0baz\n", 6);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 6);
  TEST_COMPARE_BLOB (lineptr, 7, "te\0st\n", 7);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 1);
  TEST_COMPARE_BLOB (lineptr, 1, "", 1);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (ferror (fp), 0);
  TEST_COMPARE (!!feof (fp), 1);
  fclose (fp);
  /* Test normal line, with reallocation (including case with null bytes).  */
  verbose_printf ("Testing normal nonempty input with reallocation\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = OPEN_TEST_STREAM ("foo\nbar\0\n\0baz\nte\0st\n"
			 "foo\nbar\0\n\0baz\nte\0st\n");
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "foo\n", 5);
  free (lineptr);
  lineptr = NULL;
  size = 0;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "bar\0\n", 6);
  free (lineptr);
  lineptr = NULL;
  size = 0;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "\0baz\n", 6);
  free (lineptr);
  lineptr = NULL;
  size = 0;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 6);
  TEST_COMPARE_BLOB (lineptr, 7, "te\0st\n", 7);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "foo\n", 5);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "bar\0\n", 6);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "\0baz\n", 6);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 6);
  TEST_COMPARE_BLOB (lineptr, 7, "te\0st\n", 7);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 1);
  TEST_COMPARE_BLOB (lineptr, 1, "", 1);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (ferror (fp), 0);
  TEST_COMPARE (!!feof (fp), 1);
  fclose (fp);
  /* Test EOF before delimiter but after some bytes read, fitting in
     available space (including case with null bytes).  */
  verbose_printf ("Testing EOF before delimiter\n");
  free (lineptr);
  lineptr = xmalloc (10);
  size = 10;
  fp = open_test_stream ("foo", 3);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 3);
  TEST_COMPARE_BLOB (lineptr, 4, "foo", 4);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (10);
  size = 10;
  fp = open_test_stream ("bar\0", 4);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "bar\0", 5);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (10);
  size = 10;
  fp = open_test_stream ("\0baz", 4);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "\0baz", 5);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (10);
  size = 10;
  fp = open_test_stream ("te\0st", 5);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "te\0st", 6);
  fclose (fp);
  /* Test EOF before delimiter but after some bytes read, with
     reallocation (including case with null bytes).  */
  verbose_printf ("Testing EOF before delimiter with reallocation\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("foo", 3);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 3);
  TEST_COMPARE_BLOB (lineptr, 4, "foo", 4);
  fclose (fp);
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("bar\0", 4);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "bar\0", 5);
  fclose (fp);
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("\0baz", 4);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "\0baz", 5);
  fclose (fp);
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("te\0st", 5);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "te\0st", 6);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  fp = open_test_stream ("foo", 3);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 3);
  TEST_COMPARE_BLOB (lineptr, 4, "foo", 4);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  fp = open_test_stream ("bar\0", 4);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "bar\0", 5);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  fp = open_test_stream ("\0baz", 4);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 4);
  TEST_COMPARE_BLOB (lineptr, 5, "\0baz", 5);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  fp = open_test_stream ("te\0st", 5);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 5);
  TEST_COMPARE_BLOB (lineptr, 6, "te\0st", 6);
  fclose (fp);
  /* Test EOF with no bytes read (nothing is specified about anything
     written to the buffer), including EOF again when already at end
     of file.  */
  verbose_printf ("Testing EOF with no bytes read\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("", 0);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (ferror (fp), 0);
  TEST_COMPARE (!!feof (fp), 1);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (ferror (fp), 0);
  TEST_COMPARE (!!feof (fp), 1);
  fclose (fp);
  free (lineptr);
  lineptr = xmalloc (1);
  size = 1;
  fp = open_test_stream ("", 0);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (ferror (fp), 0);
  TEST_COMPARE (!!feof (fp), 1);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (ferror (fp), 0);
  TEST_COMPARE (!!feof (fp), 1);
  fclose (fp);
  /* Test error occurring with no bytes read, including calling
     wrap_getline again while the file is in error state.  */
  verbose_printf ("Testing error with no bytes read\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("", 0);
  the_cookie.in_error = EINVAL;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (errno, EINVAL);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  /* Make sure error state is sticky.  */
  the_cookie.in_error = 0;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  /* Test error occurring after some bytes read.  Specifications are
     ambiguous here; at least in the fopencookie case used for
     testing, glibc returns the partial line (but with the error
     indicator on the stream set).  */
  verbose_printf ("Testing error after some bytes read\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = open_test_stream ("foo", 3);
  the_cookie.in_error_after = EINVAL;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, 3);
  TEST_COMPARE_BLOB (lineptr, 4, "foo", 4);
  TEST_COMPARE (errno, EINVAL);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  /* Make sure error state is sticky.  */
  the_cookie.in_error = 0;
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  /* Test EBADF error as a representative example of an fgetc error
     resulting in an error from wrap_getline.  We don't try to cover all
     error cases for fgetc here.  */
  verbose_printf ("Testing EBADF error\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  fp = xfopen ("/dev/null", "r");
  xclose (fileno (fp));
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  /* Test EAGAIN error as an example of an fgetc error on a valid file
     descriptor.  */
  verbose_printf ("Testing EAGAIN error\n");
  free (lineptr);
  lineptr = NULL;
  size = 0;
  int pipefd[2];
  xpipe (pipefd);
  ret = fcntl (pipefd[0], F_SETFL, O_NONBLOCK);
  TEST_VERIFY_EXIT (ret == 0);
  fp = fdopen (pipefd[0], "r");
  TEST_VERIFY_EXIT (fp != NULL);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (errno, EAGAIN);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  /* Make sure error state is sticky (even after more data is
     available to read).  */
  xwrite (pipefd[1], "x\n", 2);
  ret = wrap_getline (&lineptr, &size, fp);
  TEST_COMPARE (ret, -1);
  TEST_COMPARE (!!ferror (fp), 1);
  TEST_COMPARE (feof (fp), 0);
  fclose (fp);
  free (lineptr);
  return 0;
}

#include <support/test-driver.c>