Blame gnulib-tests/test-rmdir.h

Packit Service 2723c6
/* Tests of rmdir.
Packit Service 2723c6
   Copyright (C) 2009-2018 Free Software Foundation, Inc.
Packit Service 2723c6
Packit Service 2723c6
   This program is free software: you can redistribute it and/or modify
Packit Service 2723c6
   it under the terms of the GNU General Public License as published by
Packit Service 2723c6
   the Free Software Foundation; either version 3 of the License, or
Packit Service 2723c6
   (at your option) any later version.
Packit Service 2723c6
Packit Service 2723c6
   This program is distributed in the hope that it will be useful,
Packit Service 2723c6
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2723c6
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 2723c6
   GNU General Public License for more details.
Packit Service 2723c6
Packit Service 2723c6
   You should have received a copy of the GNU General Public License
Packit Service 2723c6
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 2723c6
Packit Service 2723c6
/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
Packit Service 2723c6
Packit Service 2723c6
/* This file is designed to test both rmdir(n) and
Packit Service 2723c6
   unlinkat(AT_FDCWD,n,AT_REMOVEDIR).  FUNC is the function to test.
Packit Service 2723c6
   Assumes that BASE and ASSERT are already defined, and that
Packit Service 2723c6
   appropriate headers are already included.  If PRINT, then warn
Packit Service 2723c6
   before returning status 77 when symlinks are unsupported.  */
Packit Service 2723c6
Packit Service 2723c6
static int
Packit Service 2723c6
test_rmdir_func (int (*func) (char const *name), bool print)
Packit Service 2723c6
{
Packit Service 2723c6
  /* Setup.  */
Packit Service 2723c6
  ASSERT (mkdir (BASE "dir", 0700) == 0);
Packit Service 2723c6
  ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
Packit Service 2723c6
Packit Service 2723c6
  /* Basic error conditions.  */
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func ("") == -1);
Packit Service 2723c6
  ASSERT (errno == ENOENT);
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (BASE "nosuch") == -1);
Packit Service 2723c6
  ASSERT (errno == ENOENT);
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (BASE "nosuch/") == -1);
Packit Service 2723c6
  ASSERT (errno == ENOENT);
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (".") == -1);
Packit Service 2723c6
  ASSERT (errno == EINVAL || errno == EBUSY);
Packit Service 2723c6
  /* Resulting errno after ".." or "/" is too varied to test; it is
Packit Service 2723c6
     reasonable to see any of EINVAL, EBUSY, EEXIST, ENOTEMPTY,
Packit Service 2723c6
     EACCES, EPERM.  */
Packit Service 2723c6
  ASSERT (func ("..") == -1);
Packit Service 2723c6
  ASSERT (func ("/") == -1);
Packit Service 2723c6
  ASSERT (func ("///") == -1);
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (BASE "dir/file/") == -1);
Packit Service 2723c6
  ASSERT (errno == ENOTDIR);
Packit Service 2723c6
Packit Service 2723c6
  /* Non-empty directory.  */
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (BASE "dir") == -1);
Packit Service 2723c6
  ASSERT (errno == EEXIST || errno == ENOTEMPTY);
Packit Service 2723c6
Packit Service 2723c6
  /* Non-directory.  */
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (BASE "dir/file") == -1);
Packit Service 2723c6
  ASSERT (errno == ENOTDIR);
Packit Service 2723c6
Packit Service 2723c6
  /* Empty directory.  */
Packit Service 2723c6
  ASSERT (unlink (BASE "dir/file") == 0);
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  ASSERT (func (BASE "dir/.//") == -1);
Packit Service 2723c6
  ASSERT (errno == EINVAL || errno == EBUSY || errno == EEXIST
Packit Service 2723c6
          || errno == ENOTEMPTY);
Packit Service 2723c6
  ASSERT (func (BASE "dir") == 0);
Packit Service 2723c6
Packit Service 2723c6
  /* Test symlink behavior.  Specifying trailing slash should remove
Packit Service 2723c6
     referent directory (POSIX), or cause ENOTDIR failure (Linux), but
Packit Service 2723c6
     not touch symlink.  We prefer the Linux behavior for its
Packit Service 2723c6
     intuitiveness (especially compared to rmdir("symlink-to-file/")),
Packit Service 2723c6
     but not enough to penalize POSIX systems with an rpl_rmdir.  */
Packit Service 2723c6
  if (symlink (BASE "dir", BASE "link") != 0)
Packit Service 2723c6
    {
Packit Service 2723c6
      if (print)
Packit Service 2723c6
        fputs ("skipping test: symlinks not supported on this file system\n",
Packit Service 2723c6
               stderr);
Packit Service 2723c6
      return 77;
Packit Service 2723c6
    }
Packit Service 2723c6
  ASSERT (mkdir (BASE "dir", 0700) == 0);
Packit Service 2723c6
  errno = 0;
Packit Service 2723c6
  if (func (BASE "link/") == 0)
Packit Service 2723c6
    {
Packit Service 2723c6
      struct stat st;
Packit Service 2723c6
      errno = 0;
Packit Service 2723c6
      ASSERT (stat (BASE "link", &st) == -1);
Packit Service 2723c6
      ASSERT (errno == ENOENT);
Packit Service 2723c6
    }
Packit Service 2723c6
  else
Packit Service 2723c6
    {
Packit Service 2723c6
      ASSERT (errno == ENOTDIR);
Packit Service 2723c6
      ASSERT (func (BASE "dir") == 0);
Packit Service 2723c6
    }
Packit Service 2723c6
  ASSERT (unlink (BASE "link") == 0);
Packit Service 2723c6
Packit Service 2723c6
  return 0;
Packit Service 2723c6
}