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