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