Blame gnulib-tests/test-areadlink.h

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