Blame gshadow/tst-fgetsgent_r.c

Packit Bot 121953
/* Test for fgetsgent_r and buffer sizes.
Packit Bot 121953
   Copyright (C) 2020 Free Software Foundation, Inc.
Packit Bot 121953
   This file is part of the GNU C Library.
Packit Bot 121953
Packit Bot 121953
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot 121953
   modify it under the terms of the GNU Lesser General Public
Packit Bot 121953
   License as published by the Free Software Foundation; either
Packit Bot 121953
   version 2.1 of the License, or (at your option) any later version.
Packit Bot 121953
Packit Bot 121953
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot 121953
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 121953
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot 121953
   Lesser General Public License for more details.
Packit Bot 121953
Packit Bot 121953
   You should have received a copy of the GNU Lesser General Public
Packit Bot 121953
   License along with the GNU C Library; if not, see
Packit Bot 121953
   <https://www.gnu.org/licenses/>.  */
Packit Bot 121953
Packit Bot 121953
#include <array_length.h>
Packit Bot 121953
#include <errno.h>
Packit Bot 121953
#include <gshadow.h>
Packit Bot 121953
#include <stdbool.h>
Packit Bot 121953
#include <stdlib.h>
Packit Bot 121953
#include <support/check.h>
Packit Bot 121953
#include <support/support.h>
Packit Bot 121953
#include <support/temp_file.h>
Packit Bot 121953
#include <support/xmemstream.h>
Packit Bot 121953
#include <support/xstdio.h>
Packit Bot 121953
Packit Bot 121953
/* Turn a parsed struct back into a line string.  The returned string
Packit Bot 121953
   should be freed.  */
Packit Bot 121953
static char *
Packit Bot 121953
format_ent (const struct sgrp *e)
Packit Bot 121953
{
Packit Bot 121953
  struct xmemstream stream;
Packit Bot 121953
  xopen_memstream (&stream);
Packit Bot 121953
  TEST_COMPARE (putsgent (e, stream.out), 0);
Packit Bot 121953
  xfclose_memstream (&stream);
Packit Bot 121953
  return stream.buffer;
Packit Bot 121953
}
Packit Bot 121953
Packit Bot 121953
/* An entry in the input file along with the expected output.  */
Packit Bot 121953
struct input
Packit Bot 121953
{
Packit Bot 121953
  const char *line;		/* Line in the file.  */
Packit Bot 121953
  const char *expected;		/* Expected output.  NULL if skipped.  */
Packit Bot 121953
};
Packit Bot 121953
Packit Bot 121953
const struct input inputs[] =
Packit Bot 121953
  {
Packit Bot 121953
   /* Regular entries.  */
Packit Bot 121953
   { "g1:x1::\n", "g1:x1::\n" },
Packit Bot 121953
   { "g2:x2:a1:\n", "g2:x2:a1:\n" },
Packit Bot 121953
   { "g3:x3:a2:u1\n", "g3:x3:a2:u1\n" },
Packit Bot 121953
   { "g4:x4:a3,a4:u2,u3,u4\n", "g4:x4:a3,a4:u2,u3,u4\n" },
Packit Bot 121953
Packit Bot 121953
   /* Comments and empty lines.  */
Packit Bot 121953
   { "\n", NULL },
Packit Bot 121953
   { " \n", NULL },
Packit Bot 121953
   { "\t\n", NULL },
Packit Bot 121953
   { "#g:x::\n", NULL },
Packit Bot 121953
   { " #g:x::\n", NULL },
Packit Bot 121953
   { "\t#g:x::\n", NULL },
Packit Bot 121953
   { " \t#g:x::\n", NULL },
Packit Bot 121953
Packit Bot 121953
   /* Marker for synchronization.  */
Packit Bot 121953
   { "g5:x5::\n", "g5:x5::\n" },
Packit Bot 121953
Packit Bot 121953
   /* Leading whitespace.  */
Packit Bot 121953
   { " g6:x6::\n", "g6:x6::\n" },
Packit Bot 121953
   { "\tg7:x7::\n", "g7:x7::\n" },
Packit Bot 121953
Packit Bot 121953
   /* This is expected to trigger buffer exhaustion during parsing
Packit Bot 121953
      (bug 20338).  */
Packit Bot 121953
   {
Packit Bot 121953
    "g8:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:u5,u6,u7,u8,u9:\n",
Packit Bot 121953
    "g8:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:u5,u6,u7,u8,u9:\n",
Packit Bot 121953
   },
Packit Bot 121953
   {
Packit Bot 121953
    "g9:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::a5,a6,a7,a8,a9,a10\n",
Packit Bot 121953
    "g9:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::a5,a6,a7,a8,a9,a10\n",
Packit Bot 121953
   },
Packit Bot 121953
  };
Packit Bot 121953
Packit Bot 121953
/* Writes the test data to a temporary file and returns its name.  The
Packit Bot 121953
   returned pointer should be freed.  */
Packit Bot 121953
static char *
Packit Bot 121953
create_test_file (void)
Packit Bot 121953
{
Packit Bot 121953
  char *path;
Packit Bot 121953
  int fd = create_temp_file ("tst-fgetsgent_r-", &path);
Packit Bot 121953
  FILE *fp = fdopen (fd, "w");
Packit Bot 121953
  TEST_VERIFY_EXIT (fp != NULL);
Packit Bot 121953
Packit Bot 121953
  for (size_t i = 0; i < array_length (inputs); ++i)
Packit Bot 121953
    fputs (inputs[i].line, fp);
Packit Bot 121953
Packit Bot 121953
  xfclose (fp);
Packit Bot 121953
  return path;
Packit Bot 121953
}
Packit Bot 121953
Packit Bot 121953
/* Read the test file with the indicated start buffer size.  Return
Packit Bot 121953
   true if the buffer size had to be increased during reading.  */
Packit Bot 121953
static bool
Packit Bot 121953
run_test (const char *path, size_t buffer_size)
Packit Bot 121953
{
Packit Bot 121953
  bool resized = false;
Packit Bot 121953
  FILE *fp = xfopen (path, "r");
Packit Bot 121953
Packit Bot 121953
  /* This avoids repeated lseek system calls (bug 26257).  */
Packit Bot 121953
  TEST_COMPARE (fseeko64 (fp, 0, SEEK_SET), 0);
Packit Bot 121953
Packit Bot 121953
  size_t i = 0;
Packit Bot 121953
  while (true)
Packit Bot 121953
    {
Packit Bot 121953
      /* Skip over unused expected entries.  */
Packit Bot 121953
      while (i < array_length (inputs) && inputs[i].expected == NULL)
Packit Bot 121953
	++i;
Packit Bot 121953
Packit Bot 121953
      /* Store the data on the heap, to help valgrind to detect
Packit Bot 121953
	 invalid accesses.  */
Packit Bot 121953
      struct sgrp *result_storage = xmalloc (sizeof (*result_storage));
Packit Bot 121953
      char *buffer = xmalloc (buffer_size);
Packit Bot 121953
      struct sgrp **result_pointer_storage
Packit Bot 121953
	= xmalloc (sizeof (*result_pointer_storage));
Packit Bot 121953
Packit Bot 121953
      int ret = fgetsgent_r (fp, result_storage, buffer, buffer_size,
Packit Bot 121953
			     result_pointer_storage);
Packit Bot 121953
      if (ret == 0)
Packit Bot 121953
	{
Packit Bot 121953
	  TEST_VERIFY (*result_pointer_storage != NULL);
Packit Bot 121953
	  TEST_VERIFY (i < array_length (inputs));
Packit Bot 121953
	  if (*result_pointer_storage != NULL
Packit Bot 121953
	      && i < array_length (inputs))
Packit Bot 121953
	    {
Packit Bot 121953
	      char * actual = format_ent (*result_pointer_storage);
Packit Bot 121953
	      TEST_COMPARE_STRING (inputs[i].expected, actual);
Packit Bot 121953
	      free (actual);
Packit Bot 121953
	      ++i;
Packit Bot 121953
	    }
Packit Bot 121953
	  else
Packit Bot 121953
	    break;
Packit Bot 121953
	}
Packit Bot 121953
      else
Packit Bot 121953
	{
Packit Bot 121953
	  TEST_VERIFY (*result_pointer_storage == NULL);
Packit Bot 121953
	  TEST_COMPARE (ret, errno);
Packit Bot 121953
Packit Bot 121953
	  if (ret == ENOENT)
Packit Bot 121953
	    {
Packit Bot 121953
	      TEST_COMPARE (i, array_length (inputs));
Packit Bot 121953
	      free (result_pointer_storage);
Packit Bot 121953
	      free (buffer);
Packit Bot 121953
	      free (result_storage);
Packit Bot 121953
	      break;
Packit Bot 121953
	    }
Packit Bot 121953
	  else if (ret == ERANGE)
Packit Bot 121953
	    {
Packit Bot 121953
	      resized = true;
Packit Bot 121953
	      ++buffer_size;
Packit Bot 121953
	    }
Packit Bot 121953
	  else
Packit Bot 121953
	    FAIL_EXIT1 ("read failure: %m");
Packit Bot 121953
	}
Packit Bot 121953
Packit Bot 121953
      free (result_pointer_storage);
Packit Bot 121953
      free (buffer);
Packit Bot 121953
      free (result_storage);
Packit Bot 121953
    }
Packit Bot 121953
Packit Bot 9a19b9
  xfclose (fp);
Packit Bot 121953
  return resized;
Packit Bot 121953
}
Packit Bot 121953
Packit Bot 121953
static int
Packit Bot 121953
do_test (void)
Packit Bot 121953
{
Packit Bot 121953
  char *path = create_test_file ();
Packit Bot 121953
Packit Bot 121953
  for (size_t buffer_size = 3; ; ++buffer_size)
Packit Bot 121953
    {
Packit Bot 121953
      bool resized = run_test (path, buffer_size);
Packit Bot 121953
      if (!resized)
Packit Bot 121953
	break;
Packit Bot 121953
    }
Packit Bot 121953
Packit Bot 121953
  free (path);
Packit Bot 121953
Packit Bot 121953
  return 0;
Packit Bot 121953
}
Packit Bot 121953
Packit Bot 121953
#include <support/test-driver.c>