Blame libio/tst-readline.c

Packit Bot 0c2104
/* Test the __libc_readline_unlocked function.
Packit Bot 0c2104
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Bot 0c2104
   This file is part of the GNU C Library.
Packit Bot 0c2104
Packit Bot 0c2104
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot 0c2104
   modify it under the terms of the GNU Lesser General Public
Packit Bot 0c2104
   License as published by the Free Software Foundation; either
Packit Bot 0c2104
   version 2.1 of the License, or (at your option) any later version.
Packit Bot 0c2104
Packit Bot 0c2104
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot 0c2104
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 0c2104
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot 0c2104
   Lesser General Public License for more details.
Packit Bot 0c2104
Packit Bot 0c2104
   You should have received a copy of the GNU Lesser General Public
Packit Bot 0c2104
   License along with the GNU C Library; if not, see
Packit Bot 0c2104
   <http://www.gnu.org/licenses/>.  */
Packit Bot 0c2104
Packit Bot 0c2104
/* Exercise __libc_readline_unlocked with various combinations of line
Packit Bot 0c2104
   lengths, stdio buffer sizes, and line read buffer sizes.  */
Packit Bot 0c2104
Packit Bot 0c2104
#include <errno.h>
Packit Bot 0c2104
#include <stdbool.h>
Packit Bot 0c2104
#include <stdio.h>
Packit Bot 0c2104
#include <string.h>
Packit Bot 0c2104
#include <support/check.h>
Packit Bot 0c2104
#include <support/support.h>
Packit Bot 0c2104
#include <support/temp_file.h>
Packit Bot 0c2104
#include <support/test-driver.h>
Packit Bot 0c2104
#include <support/xmemstream.h>
Packit Bot 0c2104
#include <support/xstdio.h>
Packit Bot 0c2104
#include <support/xunistd.h>
Packit Bot 0c2104
Packit Bot 0c2104
enum
Packit Bot 0c2104
  {
Packit Bot 0c2104
    maximum_line_length = 7,
Packit Bot 0c2104
    number_of_lines = 3,
Packit Bot 0c2104
  };
Packit Bot 0c2104
Packit Bot 0c2104
/* -1: Do not set buffer size.  0: unbuffered.  Otherwise, use this as
Packit Bot 0c2104
   the size of the buffer.  */
Packit Bot 0c2104
static int buffer_size;
Packit Bot 0c2104
Packit Bot 0c2104
/* These size of the buffer used for reading.  Must be at least 2.  */
Packit Bot 0c2104
static int read_size;
Packit Bot 0c2104
Packit Bot 0c2104
/* If a read files with ERANGE, increase the buffer size by this
Packit Bot 0c2104
   amount.  Must be positive.  */
Packit Bot 0c2104
static int read_size_increment;
Packit Bot 0c2104
Packit Bot 0c2104
/* If non-zero, do not reset the read size after an ERANGE error.  */
Packit Bot 0c2104
static int read_size_preserve;
Packit Bot 0c2104
Packit Bot 0c2104
/* If non-zero, no '\n' at the end of the file.  */
Packit Bot 0c2104
static int no_newline_at_eof;
Packit Bot 0c2104
Packit Bot 0c2104
/* Length of the line, or -1 if the line is not present.  */
Packit Bot 0c2104
static int line_lengths[number_of_lines];
Packit Bot 0c2104
Packit Bot 0c2104
/* The name of the test file.  */
Packit Bot 0c2104
static char *test_file_path;
Packit Bot 0c2104
Packit Bot 0c2104
/* The contents of the test file.  */
Packit Bot 0c2104
static char expected_contents[(maximum_line_length + 2) * number_of_lines + 1];
Packit Bot 0c2104
static size_t expected_length;
Packit Bot 0c2104
Packit Bot 0c2104
/* Returns a random byte which is not zero or the line terminator.  */
Packit Bot 0c2104
static char
Packit Bot 0c2104
random_char (void)
Packit Bot 0c2104
{
Packit Bot 0c2104
  static unsigned int rand_state = 1;
Packit Bot 0c2104
  while (true)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      char result = rand_r (&rand_state) >> 16;
Packit Bot 0c2104
      if (result != 0 && result != '\n')
Packit Bot 0c2104
        return result;
Packit Bot 0c2104
    }
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
/* Create the test file.  */
Packit Bot 0c2104
static void
Packit Bot 0c2104
prepare (int argc, char **argv)
Packit Bot 0c2104
{
Packit Bot 0c2104
  int fd = create_temp_file ("tst-readline-", &test_file_path);
Packit Bot 0c2104
  TEST_VERIFY_EXIT (fd >= 0);
Packit Bot 0c2104
  xclose (fd);
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
/* Prepare the test file.  Return false if the test parameters are
Packit Bot 0c2104
   incongruent and the test should be skipped.  */
Packit Bot 0c2104
static bool
Packit Bot 0c2104
write_test_file (void)
Packit Bot 0c2104
{
Packit Bot 0c2104
  expected_length = 0;
Packit Bot 0c2104
  char *p = expected_contents;
Packit Bot 0c2104
  for (int lineno = 0; lineno < number_of_lines; ++lineno)
Packit Bot 0c2104
    for (int i = 0; i < line_lengths[lineno]; ++i)
Packit Bot 0c2104
      *p++ = random_char ();
Packit Bot 0c2104
  expected_length = p - &expected_contents[0];
Packit Bot 0c2104
  if (no_newline_at_eof)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      if (expected_length == 0)
Packit Bot 0c2104
        return false;
Packit Bot 0c2104
      --expected_length;
Packit Bot 0c2104
      --p;
Packit Bot 0c2104
    }
Packit Bot 0c2104
  if (test_verbose > 0)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      printf ("info: writing test file of %zu bytes:\n", expected_length);
Packit Bot 0c2104
      for (int i = 0; i < number_of_lines; ++i)
Packit Bot 0c2104
        printf (" line %d: %d\n", i, line_lengths[i]);
Packit Bot 0c2104
      if (no_newline_at_eof)
Packit Bot 0c2104
        puts ("  (no newline at EOF)");
Packit Bot 0c2104
    }
Packit Bot 0c2104
  TEST_VERIFY_EXIT (expected_length < sizeof (expected_contents));
Packit Bot 0c2104
  *p++ = '\0';
Packit Bot 0c2104
  support_write_file_string (test_file_path, expected_contents);
Packit Bot 0c2104
  return true;
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
/* Run a single test (a combination of a test file and read
Packit Bot 0c2104
   parameters).  */
Packit Bot 0c2104
static void
Packit Bot 0c2104
run_test (void)
Packit Bot 0c2104
{
Packit Bot 0c2104
  TEST_VERIFY_EXIT (read_size_increment > 0);
Packit Bot 0c2104
  if (test_verbose > 0)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      printf ("info: running test: buffer_size=%d read_size=%d\n"
Packit Bot 0c2104
              "  read_size_increment=%d read_size_preserve=%d\n",
Packit Bot 0c2104
              buffer_size, read_size, read_size_increment, read_size_preserve);
Packit Bot 0c2104
    }
Packit Bot 0c2104
Packit Bot 0c2104
  struct xmemstream result;
Packit Bot 0c2104
  xopen_memstream (&result);
Packit Bot 0c2104
Packit Bot 0c2104
  FILE *fp = xfopen (test_file_path, "rce");
Packit Bot 0c2104
  char *fp_buffer = NULL;
Packit Bot 0c2104
  if (buffer_size == 0)
Packit Bot 0c2104
    TEST_VERIFY_EXIT (setvbuf (fp, NULL, _IONBF, 0) == 0);
Packit Bot 0c2104
  if (buffer_size > 0)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      fp_buffer = xmalloc (buffer_size);
Packit Bot 0c2104
      TEST_VERIFY_EXIT (setvbuf (fp, fp_buffer, _IOFBF, buffer_size) == 0);
Packit Bot 0c2104
    }
Packit Bot 0c2104
Packit Bot 0c2104
  char *line_buffer = xmalloc (read_size);
Packit Bot 0c2104
  size_t line_buffer_size = read_size;
Packit Bot 0c2104
Packit Bot 0c2104
  while (true)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      ssize_t ret = __libc_readline_unlocked
Packit Bot 0c2104
        (fp, line_buffer, line_buffer_size);
Packit Bot 0c2104
      if (ret < 0)
Packit Bot 0c2104
        {
Packit Bot 0c2104
          TEST_VERIFY (ret == -1);
Packit Bot 0c2104
          if (errno != ERANGE)
Packit Bot 0c2104
            FAIL_EXIT1 ("__libc_readline_unlocked: %m");
Packit Bot 0c2104
          line_buffer_size += read_size_increment;
Packit Bot 0c2104
          free (line_buffer);
Packit Bot 0c2104
          line_buffer = xmalloc (line_buffer_size);
Packit Bot 0c2104
          /* Try reading this line again.  */
Packit Bot 0c2104
        }
Packit Bot 0c2104
      else if (ret == 0)
Packit Bot 0c2104
        break;
Packit Bot 0c2104
      else
Packit Bot 0c2104
        {
Packit Bot 0c2104
          /* A line has been read.  Save it.  */
Packit Bot 0c2104
          TEST_VERIFY (ret == strlen (line_buffer));
Packit Bot 0c2104
          const char *pnl = strchr (line_buffer, '\n');
Packit Bot 0c2104
          /* If there is a \n, it must be at the end.  */
Packit Bot 0c2104
          TEST_VERIFY (pnl == NULL || pnl == line_buffer + ret - 1);
Packit Bot 0c2104
          fputs (line_buffer, result.out);
Packit Bot 0c2104
Packit Bot 0c2104
          /* Restore the original read size if required.  */
Packit Bot 0c2104
          if (line_buffer_size > read_size && !read_size_preserve)
Packit Bot 0c2104
            {
Packit Bot 0c2104
              line_buffer_size = read_size;
Packit Bot 0c2104
              free (line_buffer);
Packit Bot 0c2104
              line_buffer = xmalloc (line_buffer_size);
Packit Bot 0c2104
            }
Packit Bot 0c2104
        }
Packit Bot 0c2104
    }
Packit Bot 0c2104
Packit Bot 0c2104
  xfclose (fp);
Packit Bot 0c2104
  free (fp_buffer);
Packit Bot 0c2104
  free (line_buffer);
Packit Bot 0c2104
Packit Bot 0c2104
  xfclose_memstream (&result);
Packit Bot 0c2104
  TEST_VERIFY (result.length == expected_length);
Packit Bot 0c2104
  TEST_VERIFY (strcmp (result.buffer, expected_contents) == 0);
Packit Bot 0c2104
  if (test_verbose > 0)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      printf ("info: expected (%zu): [[%s]]\n",
Packit Bot 0c2104
              expected_length, expected_contents);
Packit Bot 0c2104
      printf ("info:   actual (%zu): [[%s]]\n", result.length, result.buffer);
Packit Bot 0c2104
    }
Packit Bot 0c2104
  free (result.buffer);
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
/* Test one test file with multiple read parameters.  */
Packit Bot 0c2104
static void
Packit Bot 0c2104
test_one_file (void)
Packit Bot 0c2104
{
Packit Bot 0c2104
  for (buffer_size = -1; buffer_size <= maximum_line_length + 1; ++buffer_size)
Packit Bot 0c2104
    for (read_size = 2; read_size <= maximum_line_length + 2; ++read_size)
Packit Bot 0c2104
      for (read_size_increment = 1; read_size_increment <= 4;
Packit Bot 0c2104
           ++read_size_increment)
Packit Bot 0c2104
        for (read_size_preserve = 0; read_size_preserve < 2;
Packit Bot 0c2104
             ++read_size_preserve)
Packit Bot 0c2104
          run_test ();
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
Packit Bot 0c2104
static int
Packit Bot 0c2104
do_test (void)
Packit Bot 0c2104
{
Packit Bot 0c2104
  /* Set up the test file contents.  */
Packit Bot 0c2104
  for (line_lengths[0] = -1; line_lengths[0] <= maximum_line_length;
Packit Bot 0c2104
       ++line_lengths[0])
Packit Bot 0c2104
    for (line_lengths[1] = -1; line_lengths[1] <= maximum_line_length;
Packit Bot 0c2104
         ++line_lengths[1])
Packit Bot 0c2104
      for (line_lengths[2] = -1; line_lengths[2] <= maximum_line_length;
Packit Bot 0c2104
           ++line_lengths[2])
Packit Bot 0c2104
        for (no_newline_at_eof = 0; no_newline_at_eof < 2; ++no_newline_at_eof)
Packit Bot 0c2104
          {
Packit Bot 0c2104
            if (!write_test_file ())
Packit Bot 0c2104
              continue;
Packit Bot 0c2104
            test_one_file ();
Packit Bot 0c2104
          }
Packit Bot 0c2104
  free (test_file_path);
Packit Bot 0c2104
  return 0;
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
#define PREPARE prepare
Packit Bot 0c2104
#include <support/test-driver.c>