Blame support/tst-xreadlink.c

Packit 6c4009
/* Test the xreadlink function.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char *dir = support_create_temp_directory ("tst-xreadlink-");
Packit 6c4009
  char *symlink_name = xasprintf ("%s/symlink", dir);
Packit 6c4009
  add_temp_file (symlink_name);
Packit 6c4009
Packit 6c4009
  /* The limit 10000 is arbitrary and simply there to prevent an
Packit 6c4009
     attempt to exhaust all available disk space.  */
Packit 6c4009
  for (int size = 1; size < 10000; ++size)
Packit 6c4009
    {
Packit 6c4009
      char *contents = xmalloc (size + 1);
Packit 6c4009
      for (int i = 0; i < size; ++i)
Packit 6c4009
        contents[i] = 'a' + (rand () % 26);
Packit 6c4009
      contents[size] = '\0';
Packit 6c4009
      if (symlink (contents, symlink_name) != 0)
Packit 6c4009
        {
Packit 6c4009
          if (errno == ENAMETOOLONG)
Packit 6c4009
            {
Packit 6c4009
              printf ("info: ENAMETOOLONG failure at %d bytes\n", size);
Packit 6c4009
              free (contents);
Packit 6c4009
              break;
Packit 6c4009
            }
Packit 6c4009
          FAIL_EXIT1 ("symlink (%d bytes): %m", size);
Packit 6c4009
        }
Packit 6c4009
Packit 6c4009
      char *readlink_result = xreadlink (symlink_name);
Packit 6c4009
      TEST_VERIFY (strcmp (readlink_result, contents) == 0);
Packit 6c4009
      free (readlink_result);
Packit 6c4009
      xunlink (symlink_name);
Packit 6c4009
      free (contents);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Create an empty file to suppress the temporary file deletion
Packit 6c4009
     warning.  */
Packit 6c4009
  xclose (xopen (symlink_name, O_WRONLY | O_CREAT, 0));
Packit 6c4009
Packit 6c4009
  free (symlink_name);
Packit 6c4009
  free (dir);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>