symlink: use throughout gnulib
[gnulib.git] / tests / test-remove.c
1 /* Tests of remove.
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 #include <config.h>
20
21 #include <stdio.h>
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 #define BASE "test-remove.t"
43
44 int
45 main ()
46 {
47   /* Remove any leftovers from a previous partial run.  */
48   ASSERT (system ("rm -rf " BASE "*") == 0);
49
50   /* Setup.  */
51   ASSERT (mkdir (BASE "dir", 0700) == 0);
52   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
53
54   /* Basic error conditions.  */
55   errno = 0;
56   ASSERT (remove ("") == -1);
57   ASSERT (errno == ENOENT);
58   errno = 0;
59   ASSERT (remove ("nosuch") == -1);
60   ASSERT (errno == ENOENT);
61   errno = 0;
62   ASSERT (remove ("nosuch/") == -1);
63   ASSERT (errno == ENOENT);
64   errno = 0;
65   ASSERT (remove (".") == -1);
66   ASSERT (errno == EINVAL || errno == EBUSY);
67   /* Resulting errno after ".." or "/" is too varied to test; it is
68      reasonable to see any of EINVAL, EEXIST, ENOTEMPTY, EACCES.  */
69   ASSERT (remove ("..") == -1);
70   ASSERT (remove ("/") == -1);
71   ASSERT (remove ("///") == -1);
72   errno = 0;
73   ASSERT (remove (BASE "dir/file/") == -1);
74   ASSERT (errno == ENOTDIR);
75
76   /* Non-empty directory.  */
77   errno = 0;
78   ASSERT (remove (BASE "dir") == -1);
79   ASSERT (errno == EEXIST || errno == ENOTEMPTY);
80
81   /* Non-directory.  */
82   ASSERT (remove (BASE "dir/file") == 0);
83
84   /* Empty directory.  */
85   errno = 0;
86   ASSERT (remove (BASE "dir/./") == -1);
87   ASSERT (errno == EINVAL || errno == EBUSY);
88   ASSERT (remove (BASE "dir") == 0);
89
90   /* Test symlink behavior.  Specifying trailing slash should remove
91      referent directory, or cause ENOTDIR failure, but not touch
92      symlink.  */
93   if (symlink (BASE "dir", BASE "link") != 0)
94     {
95       fputs ("skipping test: symlinks not supported on this filesystem\n",
96              stderr);
97       return 77;
98     }
99   ASSERT (mkdir (BASE "dir", 0700) == 0);
100   errno = 0;
101   if (remove (BASE "link/") == 0)
102     {
103       struct stat st;
104       errno = 0;
105       ASSERT (stat (BASE "link", &st) == -1);
106       ASSERT (errno == ENOENT);
107     }
108   else
109     ASSERT (remove (BASE "dir") == 0);
110   {
111     struct stat st;
112     ASSERT (lstat (BASE "link", &st) == 0);
113     ASSERT (S_ISLNK (st.st_mode));
114   }
115   ASSERT (remove (BASE "link") == 0);
116
117   return 0;
118 }