usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-remove.c
blob91e7524e2d4698f65f124e9641272c3868cc9c61
1 /* Tests of remove.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 #include <config.h>
21 #include <stdio.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (remove, int, (char const *));
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>
33 #include "ignore-value.h"
34 #include "macros.h"
36 #define BASE "test-remove.t"
38 int
39 main (void)
41 /* Remove any leftovers from a previous partial run. */
42 ignore_value (system ("rm -rf " BASE "*"));
44 /* Setup. */
45 ASSERT (mkdir (BASE "dir", 0700) == 0);
46 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
48 /* Basic error conditions. */
49 errno = 0;
50 ASSERT (remove ("") == -1);
51 ASSERT (errno == ENOENT);
52 errno = 0;
53 ASSERT (remove ("nosuch") == -1);
54 ASSERT (errno == ENOENT);
55 errno = 0;
56 ASSERT (remove ("nosuch/") == -1);
57 ASSERT (errno == ENOENT);
58 errno = 0;
59 ASSERT (remove (".") == -1);
60 ASSERT (errno == EINVAL || errno == EBUSY);
61 /* Resulting errno after ".." or "/" is too varied to test; it is
62 reasonable to see any of EINVAL, EEXIST, ENOTEMPTY, EACCES. */
63 ASSERT (remove ("..") == -1);
64 ASSERT (remove ("/") == -1);
65 ASSERT (remove ("///") == -1);
66 errno = 0;
67 ASSERT (remove (BASE "dir/file/") == -1);
68 ASSERT (errno == ENOTDIR);
70 /* Non-empty directory. */
71 errno = 0;
72 ASSERT (remove (BASE "dir") == -1);
73 ASSERT (errno == EEXIST || errno == ENOTEMPTY);
75 /* Non-directory. */
76 ASSERT (remove (BASE "dir/file") == 0);
78 /* Empty directory. */
79 errno = 0;
80 ASSERT (remove (BASE "dir/.//") == -1);
81 ASSERT (errno == EINVAL || errno == EBUSY || errno == EEXIST);
82 ASSERT (remove (BASE "dir") == 0);
84 /* Test symlink behavior. Specifying trailing slash should remove
85 referent directory, or cause ENOTDIR failure, but not touch
86 symlink. */
87 if (symlink (BASE "dir", BASE "link") != 0)
89 if (test_exit_status != EXIT_SUCCESS)
90 return test_exit_status;
91 fputs ("skipping test: symlinks not supported on this file system\n",
92 stderr);
93 return 77;
95 ASSERT (mkdir (BASE "dir", 0700) == 0);
96 errno = 0;
97 if (remove (BASE "link/") == 0)
99 struct stat st;
100 errno = 0;
101 ASSERT (stat (BASE "link", &st) == -1);
102 ASSERT (errno == ENOENT);
104 else
105 ASSERT (remove (BASE "dir") == 0);
107 struct stat st;
108 ASSERT (lstat (BASE "link", &st) == 0);
109 ASSERT (S_ISLNK (st.st_mode));
111 ASSERT (remove (BASE "link") == 0);
112 /* Trailing slash on symlink to non-directory is an error. */
113 ASSERT (symlink (BASE "loop", BASE "loop") == 0);
114 errno = 0;
115 ASSERT (remove (BASE "loop/") == -1);
116 ASSERT (errno == ELOOP || errno == ENOTDIR);
117 ASSERT (remove (BASE "loop") == 0);
118 ASSERT (close (creat (BASE "file", 0600)) == 0);
119 ASSERT (symlink (BASE "file", BASE "link") == 0);
120 errno = 0;
121 ASSERT (remove (BASE "link/") == -1);
122 ASSERT (errno == ENOTDIR);
123 ASSERT (remove (BASE "link") == 0);
124 ASSERT (remove (BASE "file") == 0);
126 return test_exit_status;