Blame gnulib-tests/test-areadlink.h

Packit Service fdd496
/* Tests of areadlink and friends.
Packit Service fdd496
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
Packit Service fdd496
Packit Service fdd496
/* This file is designed to test areadlink(a),
Packit Service fdd496
   areadlink_with_size(a,b), and areadlinkat(AT_FDCWD,a).  FUNC is the
Packit Service fdd496
   function to test; a length is always supplied, but may be ignored.
Packit Service fdd496
   Assumes that BASE and ASSERT are already defined, and that
Packit Service fdd496
   appropriate headers are already included.  If PRINT, warn before
Packit Service fdd496
   skipping symlink tests with status 77.  */
Packit Service fdd496
Packit Service fdd496
static int
Packit Service fdd496
test_areadlink (char * (*func) (char const *, size_t), bool print)
Packit Service fdd496
{
Packit Service fdd496
  /* Sanity checks of failures.  Mingw lacks symlink, but areadlink can
Packit Service fdd496
     still distinguish between various errors.  */
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func ("no_such", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == ENOENT);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func ("no_such/", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == ENOENT);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func ("", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == ENOENT || errno == EINVAL);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func (".", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == EINVAL);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func ("./", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == EINVAL);
Packit Service fdd496
  ASSERT (close (creat (BASE "file", 0600)) == 0);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func (BASE "file", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == EINVAL);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func (BASE "file/", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */
Packit Service fdd496
  ASSERT (unlink (BASE "file") == 0);
Packit Service fdd496
Packit Service fdd496
  /* Now test actual symlinks.  */
Packit Service fdd496
  if (symlink (BASE "dir", BASE "link"))
Packit Service fdd496
    {
Packit Service fdd496
      if (print)
Packit Service fdd496
        fputs ("skipping test: symlinks not supported on this file system\n",
Packit Service fdd496
               stderr);
Packit Service fdd496
      return 77;
Packit Service fdd496
    }
Packit Service fdd496
  ASSERT (mkdir (BASE "dir", 0700) == 0);
Packit Service fdd496
  errno = 0;
Packit Service fdd496
  ASSERT (func (BASE "link/", 1) == NULL);
Packit Service fdd496
  ASSERT (errno == EINVAL);
Packit Service fdd496
  {
Packit Service fdd496
    /* Too small a guess is okay.  */
Packit Service fdd496
    char *buf = func (BASE "link", 1);
Packit Service fdd496
    ASSERT (buf);
Packit Service fdd496
    ASSERT (strcmp (buf, BASE "dir") == 0);
Packit Service fdd496
    free (buf);
Packit Service fdd496
    /* Too large a guess is okay.  */
Packit Service fdd496
    buf = func (BASE "link", 10000000);
Packit Service fdd496
    ASSERT (buf);
Packit Service fdd496
    ASSERT (strcmp (buf, BASE "dir") == 0);
Packit Service fdd496
    free (buf);
Packit Service fdd496
  }
Packit Service fdd496
  ASSERT (rmdir (BASE "dir") == 0);
Packit Service fdd496
  ASSERT (unlink (BASE "link") == 0);
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
}