openat-tests: ensure unlinkat behaves like rmdir
[gnulib.git] / tests / test-rmdir.h
1 /* Tests of rmdir.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 /* This file is designed to test both rmdir(n) and
20    unlinkat(AT_FDCWD,n,AT_REMOVEDIR).  FUNC is the function to test.
21    Assumes that BASE and ASSERT are already defined, and that
22    appropriate headers are already included.  */
23
24 static int
25 test_rmdir_func (int (*func) (char const *name))
26 {
27   /* Remove any leftovers from a previous partial run.  */
28   ASSERT (system ("rm -rf " BASE "*") == 0);
29
30   /* Setup.  */
31   ASSERT (mkdir (BASE "dir", 0700) == 0);
32   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
33
34   /* Basic error conditions.  */
35   errno = 0;
36   ASSERT (func ("") == -1);
37   ASSERT (errno == ENOENT);
38   errno = 0;
39   ASSERT (func (BASE "nosuch") == -1);
40   ASSERT (errno == ENOENT);
41   errno = 0;
42   ASSERT (func (BASE "nosuch/") == -1);
43   ASSERT (errno == ENOENT);
44   errno = 0;
45   ASSERT (func (".") == -1);
46   ASSERT (errno == EINVAL || errno == EBUSY);
47   /* Resulting errno after ".." or "/" is too varied to test; it is
48      reasonable to see any of EINVAL, EBUSY, EEXIST, ENOTEMPTY,
49      EACCES, EPERM.  */
50   ASSERT (func ("..") == -1);
51   ASSERT (func ("/") == -1);
52   ASSERT (func ("///") == -1);
53   errno = 0;
54   ASSERT (func (BASE "dir/file/") == -1);
55   ASSERT (errno == ENOTDIR);
56
57   /* Non-empty directory.  */
58   errno = 0;
59   ASSERT (func (BASE "dir") == -1);
60   ASSERT (errno == EEXIST || errno == ENOTEMPTY);
61
62   /* Non-directory.  */
63   errno = 0;
64   ASSERT (func (BASE "dir/file") == -1);
65   ASSERT (errno == ENOTDIR);
66
67   /* Empty directory.  */
68   ASSERT (unlink (BASE "dir/file") == 0);
69   errno = 0;
70   ASSERT (func (BASE "dir/./") == -1);
71   ASSERT (errno == EINVAL || errno == EBUSY);
72   ASSERT (func (BASE "dir") == 0);
73
74   /* Test symlink behavior.  Specifying trailing slash should remove
75      referent directory (POSIX), or cause ENOTDIR failure (Linux), but
76      not touch symlink.  We prefer the Linux behavior for its
77      intuitiveness (especially compared to rmdir("symlink-to-file/")),
78      but not enough to penalize POSIX systems with an rpl_rmdir.  */
79   if (symlink (BASE "dir", BASE "link") != 0)
80     {
81       fputs ("skipping test: symlinks not supported on this filesystem\n",
82              stderr);
83       return 77;
84     }
85   ASSERT (mkdir (BASE "dir", 0700) == 0);
86   errno = 0;
87   if (func (BASE "link/") == 0)
88     {
89       struct stat st;
90       errno = 0;
91       ASSERT (stat (BASE "link", &st) == -1);
92       ASSERT (errno == ENOENT);
93     }
94   else
95     {
96       ASSERT (errno == ENOTDIR);
97       ASSERT (func (BASE "dir") == 0);
98     }
99   ASSERT (unlink (BASE "link") == 0);
100
101   return 0;
102 }