Blame gl/tests/test-symlink.h

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