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