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