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