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