File: tst-readline.c

package info (click to toggle)
glibc 2.28-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, buster-proposed-updates, experimental, sid
  • size: 272,168 kB
  • sloc: ansic: 1,008,602; asm: 259,607; makefile: 11,271; sh: 10,477; python: 6,910; cpp: 4,992; perl: 2,258; awk: 2,005; yacc: 290; pascal: 182; sed: 73
file content (237 lines) | stat: -rw-r--r-- 7,212 bytes parent folder | download | duplicates (4)
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
/* Test the __libc_readline_unlocked function.
   Copyright (C) 2018 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
   <http://www.gnu.org/licenses/>.  */

/* Exercise __libc_readline_unlocked with various combinations of line
   lengths, stdio buffer sizes, and line read buffer sizes.  */

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xmemstream.h>
#include <support/xstdio.h>
#include <support/xunistd.h>

enum
  {
    maximum_line_length = 7,
    number_of_lines = 3,
  };

/* -1: Do not set buffer size.  0: unbuffered.  Otherwise, use this as
   the size of the buffer.  */
static int buffer_size;

/* These size of the buffer used for reading.  Must be at least 2.  */
static int read_size;

/* If a read files with ERANGE, increase the buffer size by this
   amount.  Must be positive.  */
static int read_size_increment;

/* If non-zero, do not reset the read size after an ERANGE error.  */
static int read_size_preserve;

/* If non-zero, no '\n' at the end of the file.  */
static int no_newline_at_eof;

/* Length of the line, or -1 if the line is not present.  */
static int line_lengths[number_of_lines];

/* The name of the test file.  */
static char *test_file_path;

/* The contents of the test file.  */
static char expected_contents[(maximum_line_length + 2) * number_of_lines + 1];
static size_t expected_length;

/* Returns a random byte which is not zero or the line terminator.  */
static char
random_char (void)
{
  static unsigned int rand_state = 1;
  while (true)
    {
      char result = rand_r (&rand_state) >> 16;
      if (result != 0 && result != '\n')
        return result;
    }
}

/* Create the test file.  */
static void
prepare (int argc, char **argv)
{
  int fd = create_temp_file ("tst-readline-", &test_file_path);
  TEST_VERIFY_EXIT (fd >= 0);
  xclose (fd);
}

/* Prepare the test file.  Return false if the test parameters are
   incongruent and the test should be skipped.  */
static bool
write_test_file (void)
{
  expected_length = 0;
  char *p = expected_contents;
  for (int lineno = 0; lineno < number_of_lines; ++lineno)
    for (int i = 0; i < line_lengths[lineno]; ++i)
      *p++ = random_char ();
  expected_length = p - &expected_contents[0];
  if (no_newline_at_eof)
    {
      if (expected_length == 0)
        return false;
      --expected_length;
      --p;
    }
  if (test_verbose > 0)
    {
      printf ("info: writing test file of %zu bytes:\n", expected_length);
      for (int i = 0; i < number_of_lines; ++i)
        printf (" line %d: %d\n", i, line_lengths[i]);
      if (no_newline_at_eof)
        puts ("  (no newline at EOF)");
    }
  TEST_VERIFY_EXIT (expected_length < sizeof (expected_contents));
  *p++ = '\0';
  support_write_file_string (test_file_path, expected_contents);
  return true;
}

/* Run a single test (a combination of a test file and read
   parameters).  */
static void
run_test (void)
{
  TEST_VERIFY_EXIT (read_size_increment > 0);
  if (test_verbose > 0)
    {
      printf ("info: running test: buffer_size=%d read_size=%d\n"
              "  read_size_increment=%d read_size_preserve=%d\n",
              buffer_size, read_size, read_size_increment, read_size_preserve);
    }

  struct xmemstream result;
  xopen_memstream (&result);

  FILE *fp = xfopen (test_file_path, "rce");
  char *fp_buffer = NULL;
  if (buffer_size == 0)
    TEST_VERIFY_EXIT (setvbuf (fp, NULL, _IONBF, 0) == 0);
  if (buffer_size > 0)
    {
      fp_buffer = xmalloc (buffer_size);
      TEST_VERIFY_EXIT (setvbuf (fp, fp_buffer, _IOFBF, buffer_size) == 0);
    }

  char *line_buffer = xmalloc (read_size);
  size_t line_buffer_size = read_size;

  while (true)
    {
      ssize_t ret = __libc_readline_unlocked
        (fp, line_buffer, line_buffer_size);
      if (ret < 0)
        {
          TEST_VERIFY (ret == -1);
          if (errno != ERANGE)
            FAIL_EXIT1 ("__libc_readline_unlocked: %m");
          line_buffer_size += read_size_increment;
          free (line_buffer);
          line_buffer = xmalloc (line_buffer_size);
          /* Try reading this line again.  */
        }
      else if (ret == 0)
        break;
      else
        {
          /* A line has been read.  Save it.  */
          TEST_VERIFY (ret == strlen (line_buffer));
          const char *pnl = strchr (line_buffer, '\n');
          /* If there is a \n, it must be at the end.  */
          TEST_VERIFY (pnl == NULL || pnl == line_buffer + ret - 1);
          fputs (line_buffer, result.out);

          /* Restore the original read size if required.  */
          if (line_buffer_size > read_size && !read_size_preserve)
            {
              line_buffer_size = read_size;
              free (line_buffer);
              line_buffer = xmalloc (line_buffer_size);
            }
        }
    }

  xfclose (fp);
  free (fp_buffer);
  free (line_buffer);

  xfclose_memstream (&result);
  TEST_VERIFY (result.length == expected_length);
  TEST_VERIFY (strcmp (result.buffer, expected_contents) == 0);
  if (test_verbose > 0)
    {
      printf ("info: expected (%zu): [[%s]]\n",
              expected_length, expected_contents);
      printf ("info:   actual (%zu): [[%s]]\n", result.length, result.buffer);
    }
  free (result.buffer);
}

/* Test one test file with multiple read parameters.  */
static void
test_one_file (void)
{
  for (buffer_size = -1; buffer_size <= maximum_line_length + 1; ++buffer_size)
    for (read_size = 2; read_size <= maximum_line_length + 2; ++read_size)
      for (read_size_increment = 1; read_size_increment <= 4;
           ++read_size_increment)
        for (read_size_preserve = 0; read_size_preserve < 2;
             ++read_size_preserve)
          run_test ();
}


static int
do_test (void)
{
  /* Set up the test file contents.  */
  for (line_lengths[0] = -1; line_lengths[0] <= maximum_line_length;
       ++line_lengths[0])
    for (line_lengths[1] = -1; line_lengths[1] <= maximum_line_length;
         ++line_lengths[1])
      for (line_lengths[2] = -1; line_lengths[2] <= maximum_line_length;
           ++line_lengths[2])
        for (no_newline_at_eof = 0; no_newline_at_eof < 2; ++no_newline_at_eof)
          {
            if (!write_test_file ())
              continue;
            test_one_file ();
          }
  free (test_file_path);
  return 0;
}

#define TIMEOUT 100
#define PREPARE prepare
#include <support/test-driver.c>