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