maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-renameat.c
blob65260488ed14f4ef3d3c8a32592ea98289e4b31b
1 /* Tests of renameat.
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 (renameat, int, (int, char const *, int, char const *));
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
34 #include "filenamecat.h"
35 #include "ignore-value.h"
36 #include "macros.h"
38 #define BASE "test-renameat.t"
40 #include "test-rename.h"
42 static int dfd1 = AT_FDCWD;
43 static int dfd2 = AT_FDCWD;
45 /* Wrapper to test renameat like rename. */
46 static int
47 do_rename (char const *name1, char const *name2)
49 return renameat (dfd1, name1, dfd2, name2);
52 int
53 main (void)
55 int i;
56 int dfd;
57 char *cwd;
58 int result;
60 /* Clean up any trash from prior testsuite runs. */
61 ignore_value (system ("rm -rf " BASE "*"));
63 /* Test behaviour for invalid file descriptors. */
65 errno = 0;
66 ASSERT (renameat (-1, "foo", AT_FDCWD, "bar") == -1);
67 ASSERT (errno == EBADF);
70 close (99);
71 errno = 0;
72 ASSERT (renameat (99, "foo", AT_FDCWD, "bar") == -1);
73 ASSERT (errno == EBADF);
75 ASSERT (close (creat (BASE "oo", 0600)) == 0);
77 errno = 0;
78 ASSERT (renameat (AT_FDCWD, BASE "oo", -1, "bar") == -1);
79 ASSERT (errno == EBADF);
82 errno = 0;
83 ASSERT (renameat (AT_FDCWD, BASE "oo", 99, "bar") == -1);
84 ASSERT (errno == EBADF);
86 ASSERT (unlink (BASE "oo") == 0);
88 /* Test basic rename functionality, using current directory. */
89 result = test_rename (do_rename, false);
90 dfd1 = open (".", O_RDONLY);
91 ASSERT (0 <= dfd1);
92 ASSERT (test_rename (do_rename, false) == result);
93 dfd2 = dfd1;
94 ASSERT (test_rename (do_rename, false) == result);
95 dfd1 = AT_FDCWD;
96 ASSERT (test_rename (do_rename, false) == result);
97 ASSERT (close (dfd2) == 0);
99 /* Create locations to manipulate. */
100 ASSERT (mkdir (BASE "sub1", 0700) == 0);
101 ASSERT (mkdir (BASE "sub2", 0700) == 0);
102 dfd = creat (BASE "00", 0600);
103 ASSERT (0 <= dfd);
104 ASSERT (close (dfd) == 0);
105 cwd = getcwd (NULL, 0);
106 ASSERT (cwd);
108 dfd = open (BASE "sub1", O_RDONLY);
109 ASSERT (0 <= dfd);
110 ASSERT (chdir (BASE "sub2") == 0);
112 /* There are 16 possible scenarios, based on whether an fd is
113 AT_FDCWD or real, and whether a file is absolute or relative.
115 To ensure that we test all of the code paths (rather than
116 triggering early normalization optimizations), we use a loop to
117 repeatedly rename a file in the parent directory, use an fd open
118 on subdirectory 1, all while executing in subdirectory 2; all
119 relative names are thus given with a leading "../". Finally, the
120 last scenario (two relative paths given, neither one AT_FDCWD)
121 has two paths, based on whether the two fds are equivalent, so we
122 do the other variant after the loop. */
123 for (i = 0; i < 16; i++)
125 int fd1 = (i & 8) ? dfd : AT_FDCWD;
126 char *file1 = file_name_concat ((i & 4) ? ".." : cwd, BASE "xx", NULL);
127 int fd2 = (i & 2) ? dfd : AT_FDCWD;
128 char *file2 = file_name_concat ((i & 1) ? ".." : cwd, BASE "xx", NULL);
130 ASSERT (sprintf (strchr (file1, '\0') - 2, "%02d", i) == 2);
131 ASSERT (sprintf (strchr (file2, '\0') - 2, "%02d", i + 1) == 2);
132 ASSERT (renameat (fd1, file1, fd2, file2) == 0);
133 free (file1);
134 free (file2);
136 dfd2 = open ("..", O_RDONLY);
137 ASSERT (0 <= dfd2);
138 ASSERT (renameat (dfd, "../" BASE "16", dfd2, BASE "17") == 0);
139 ASSERT (close (dfd2) == 0);
141 /* Now we change back to the parent directory, and set dfd to ".";
142 using dfd in remaining tests will expose any bugs if emulation
143 via /proc/self/fd doesn't check for empty names. */
144 ASSERT (chdir ("..") == 0);
145 ASSERT (close (dfd) == 0);
146 dfd = open (".", O_RDONLY);
147 ASSERT (0 <= dfd);
149 ASSERT (close (creat (BASE "sub2/file", 0600)) == 0);
150 errno = 0;
151 ASSERT (renameat (dfd, BASE "sub1", dfd, BASE "sub2") == -1);
152 ASSERT (errno == EEXIST || errno == ENOTEMPTY);
153 ASSERT (unlink (BASE "sub2/file") == 0);
154 errno = 0;
155 ASSERT (renameat (dfd, BASE "sub2", dfd, BASE "sub1/.") == -1);
156 ASSERT (errno == EINVAL || errno == EISDIR || errno == EBUSY
157 || errno == ENOTEMPTY || errno == EEXIST
158 || errno == ENOENT /* WSL */);
159 errno = 0;
160 ASSERT (renameat (dfd, BASE "sub2/.", dfd, BASE "sub1") == -1);
161 ASSERT (errno == EINVAL || errno == EBUSY || errno == EEXIST
162 || errno == ENOENT /* WSL */);
163 errno = 0;
164 ASSERT (renameat (dfd, BASE "17", dfd, BASE "sub1") == -1);
165 ASSERT (errno == EISDIR);
166 errno = 0;
167 ASSERT (renameat (dfd, BASE "nosuch", dfd, BASE "18") == -1);
168 ASSERT (errno == ENOENT);
169 errno = 0;
170 ASSERT (renameat (dfd, "", dfd, BASE "17") == -1);
171 ASSERT (errno == ENOENT);
172 errno = 0;
173 ASSERT (renameat (dfd, BASE "17", dfd, "") == -1);
174 ASSERT (errno == ENOENT);
175 errno = 0;
176 ASSERT (renameat (dfd, BASE "sub2", dfd, BASE "17") == -1);
177 ASSERT (errno == ENOTDIR);
178 errno = 0;
179 ASSERT (renameat (dfd, BASE "17/", dfd, BASE "18") == -1);
180 ASSERT (errno == ENOTDIR);
181 errno = 0;
182 ASSERT (renameat (dfd, BASE "17", dfd, BASE "18/") == -1);
183 ASSERT (errno == ENOTDIR || errno == ENOENT);
185 /* Finally, make sure we can overwrite existing files. */
186 ASSERT (close (creat (BASE "sub2/file", 0600)) == 0);
187 errno = 0;
188 ASSERT (renameat (dfd, BASE "sub2", dfd, BASE "sub1") == 0);
189 ASSERT (renameat (dfd, BASE "sub1/file", dfd, BASE "17") == 0);
191 /* Cleanup. */
192 ASSERT (close (dfd) == 0);
193 errno = 0;
194 ASSERT (unlink (BASE "sub1/file") == -1);
195 ASSERT (errno == ENOENT);
196 ASSERT (unlink (BASE "17") == 0);
197 ASSERT (rmdir (BASE "sub1") == 0);
198 errno = 0;
199 ASSERT (rmdir (BASE "sub2") == -1);
200 ASSERT (errno == ENOENT);
201 free (cwd);
203 if (result == 77)
204 fputs ("skipping test: symlinks not supported on this file system\n",
205 stderr);
206 return (result ? result : test_exit_status);