Blame gnulib-tests/test-symlink.h

Packit 709fb3
/* Tests of symlink.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
Packit 709fb3
Packit 709fb3
/* This file is designed to test both symlink(a,b) and
Packit 709fb3
   symlinkat(a,AT_FDCWD,b).  FUNC is the function to test.  Assumes
Packit 709fb3
   that BASE and ASSERT are already defined, and that appropriate
Packit 709fb3
   headers are already included.  If PRINT, warn before skipping
Packit 709fb3
   symlink tests with status 77.  */
Packit 709fb3
Packit 709fb3
static int
Packit 709fb3
test_symlink (int (*func) (char const *, char const *), bool print)
Packit 709fb3
{
Packit 709fb3
  if (func ("nowhere", BASE "link1"))
Packit 709fb3
    {
Packit 709fb3
      if (print)
Packit 709fb3
        fputs ("skipping test: symlinks not supported on this file system\n",
Packit 709fb3
               stderr);
Packit 709fb3
      return 77;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Some systems allow the creation of 0-length symlinks as a synonym
Packit 709fb3
     for "."; but most reject it.  */
Packit 709fb3
  {
Packit 709fb3
    int status;
Packit 709fb3
    errno = 0;
Packit 709fb3
    status = func ("", BASE "link2");
Packit 709fb3
    if (status == -1)
Packit 709fb3
      ASSERT (errno == ENOENT || errno == EINVAL);
Packit 709fb3
    else
Packit 709fb3
      {
Packit 709fb3
        ASSERT (status == 0);
Packit 709fb3
        ASSERT (unlink (BASE "link2") == 0);
Packit 709fb3
      }
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Sanity checks of failures.  */
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", "") == -1);
Packit 709fb3
  ASSERT (errno == ENOENT);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", ".") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST || errno == EINVAL);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("somewhere", BASE "link1") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", BASE "link2/") == -1);
Packit 709fb3
  ASSERT (errno == ENOTDIR || errno == ENOENT);
Packit 709fb3
  ASSERT (mkdir (BASE "dir", 0700) == 0);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", BASE "dir") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", BASE "dir/") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST || errno == EINVAL);
Packit 709fb3
  ASSERT (close (creat (BASE "file", 0600)) == 0);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", BASE "file") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func ("nowhere", BASE "file/") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
Packit 709fb3
Packit 709fb3
  /* Trailing slash must always be rejected.  */
Packit 709fb3
  ASSERT (unlink (BASE "link1") == 0);
Packit 709fb3
  ASSERT (func (BASE "link2", BASE "link1") == 0);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (func (BASE "nowhere", BASE "link1/") == -1);
Packit 709fb3
  ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (unlink (BASE "link2") == -1);
Packit 709fb3
  ASSERT (errno == ENOENT);
Packit 709fb3
Packit 709fb3
  /* Cleanup.  */
Packit 709fb3
  ASSERT (rmdir (BASE "dir") == 0);
Packit 709fb3
  ASSERT (unlink (BASE "file") == 0);
Packit 709fb3
  ASSERT (unlink (BASE "link1") == 0);
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}