Support compiling without -loldnames on native Windows.
[gnulib.git] / tests / test-canonicalize.c
blobe0b623f19b2c1da31c15bcbc930b5eac97c2ba15
1 /* Test of execution of file name canonicalization.
2 Copyright (C) 2007-2020 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 Bruno Haible <bruno@clisp.org>, 2007. */
19 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
20 may "optimize" the null_ptr function, when its result gets passed to a
21 function that has an argument declared as _GL_ARG_NONNULL. */
22 #define _GL_ARG_NONNULL(params)
24 #include <config.h>
26 #include "canonicalize.h"
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
36 #include "same-inode.h"
37 #include "ignore-value.h"
39 #if GNULIB_defined_canonicalize_file_name
40 # include "null-ptr.h"
41 #endif
43 #include "macros.h"
45 #define BASE "t-can.tmp"
47 int
48 main (void)
50 /* Setup some hierarchy to be used by this test. Start by removing
51 any leftovers from a previous partial run. */
53 int fd;
54 ignore_value (system ("rm -rf " BASE " ise"));
55 ASSERT (mkdir (BASE, 0700) == 0);
56 fd = creat (BASE "/tra", 0600);
57 ASSERT (0 <= fd);
58 ASSERT (close (fd) == 0);
61 /* Check for ., .., intermediate // handling, and for error cases. */
63 char *result1 = canonicalize_file_name (BASE "//./..//" BASE "/tra");
64 char *result2 = canonicalize_filename_mode (BASE "//./..//" BASE "/tra",
65 CAN_EXISTING);
66 ASSERT (result1 != NULL);
67 ASSERT (result2 != NULL);
68 ASSERT (strcmp (result1, result2) == 0);
69 ASSERT (strstr (result1, "/" BASE "/tra")
70 == result1 + strlen (result1) - strlen ("/" BASE "/tra"));
71 free (result1);
72 free (result2);
74 errno = 0;
75 result1 = canonicalize_file_name ("");
76 ASSERT (result1 == NULL);
77 ASSERT (errno == ENOENT);
79 errno = 0;
80 result2 = canonicalize_filename_mode ("", CAN_EXISTING);
81 ASSERT (result2 == NULL);
82 ASSERT (errno == ENOENT);
84 /* This test works only if the canonicalize_file_name implementation
85 comes from gnulib. If it comes from libc, we have no way to prevent
86 gcc from "optimizing" the null_ptr function in invalid ways. See
87 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93156>. */
88 #if GNULIB_defined_canonicalize_file_name
89 errno = 0;
90 result1 = canonicalize_file_name (null_ptr ());
91 ASSERT (result1 == NULL);
92 ASSERT (errno == EINVAL);
93 #endif
95 errno = 0;
96 result2 = canonicalize_filename_mode (NULL, CAN_EXISTING);
97 ASSERT (result2 == NULL);
98 ASSERT (errno == EINVAL);
100 errno = 0;
101 result2 = canonicalize_filename_mode (".", CAN_MISSING | CAN_ALL_BUT_LAST);
102 ASSERT (result2 == NULL);
103 ASSERT (errno == EINVAL);
106 /* Check that a non-directory with trailing slash yields NULL. */
108 char *result1;
109 char *result2;
110 errno = 0;
111 result1 = canonicalize_file_name (BASE "/tra/");
112 ASSERT (result1 == NULL);
113 ASSERT (errno == ENOTDIR);
114 errno = 0;
115 result2 = canonicalize_filename_mode (BASE "/tra/", CAN_EXISTING);
116 ASSERT (result2 == NULL);
117 ASSERT (errno == ENOTDIR);
120 /* Check that a missing directory yields NULL. */
122 char *result1;
123 char *result2;
124 errno = 0;
125 result1 = canonicalize_file_name (BASE "/zzz/..");
126 ASSERT (result1 == NULL);
127 ASSERT (errno == ENOENT);
128 errno = 0;
129 result2 = canonicalize_filename_mode (BASE "/zzz/..", CAN_EXISTING);
130 ASSERT (result2 == NULL);
131 ASSERT (errno == ENOENT);
134 /* From here on out, tests involve symlinks. */
135 if (symlink (BASE "/ket", "ise") != 0)
137 ASSERT (remove (BASE "/tra") == 0);
138 ASSERT (rmdir (BASE) == 0);
139 fputs ("skipping test: symlinks not supported on this file system\n",
140 stderr);
141 return 77;
143 ASSERT (symlink ("bef", BASE "/plo") == 0);
144 ASSERT (symlink ("tra", BASE "/huk") == 0);
145 ASSERT (symlink ("lum", BASE "/bef") == 0);
146 ASSERT (symlink ("wum", BASE "/ouk") == 0);
147 ASSERT (symlink ("../ise", BASE "/ket") == 0);
148 ASSERT (mkdir (BASE "/lum", 0700) == 0);
149 ASSERT (symlink ("s", BASE "/p") == 0);
150 ASSERT (symlink ("d", BASE "/s") == 0);
151 ASSERT (mkdir (BASE "/d", 0700) == 0);
152 ASSERT (close (creat (BASE "/d/2", 0600)) == 0);
153 ASSERT (symlink ("../s/2", BASE "/d/1") == 0);
154 ASSERT (symlink ("//.//../..", BASE "/droot") == 0);
156 /* Check that symbolic links are not resolved, with CAN_NOLINKS. */
158 char *result1 = canonicalize_filename_mode (BASE "/huk", CAN_NOLINKS);
159 ASSERT (result1 != NULL);
160 ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/huk"),
161 "/" BASE "/huk") == 0);
162 free (result1);
165 /* Check that the symbolic link to a file can be resolved. */
167 char *result1 = canonicalize_file_name (BASE "/huk");
168 char *result2 = canonicalize_file_name (BASE "/tra");
169 char *result3 = canonicalize_filename_mode (BASE "/huk", CAN_EXISTING);
170 ASSERT (result1 != NULL);
171 ASSERT (result2 != NULL);
172 ASSERT (result3 != NULL);
173 ASSERT (strcmp (result1, result2) == 0);
174 ASSERT (strcmp (result2, result3) == 0);
175 ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/tra"),
176 "/" BASE "/tra") == 0);
177 free (result1);
178 free (result2);
179 free (result3);
182 /* Check that the symbolic link to a directory can be resolved. */
184 char *result1 = canonicalize_file_name (BASE "/plo");
185 char *result2 = canonicalize_file_name (BASE "/bef");
186 char *result3 = canonicalize_file_name (BASE "/lum");
187 char *result4 = canonicalize_filename_mode (BASE "/plo", CAN_EXISTING);
188 ASSERT (result1 != NULL);
189 ASSERT (result2 != NULL);
190 ASSERT (result3 != NULL);
191 ASSERT (result4 != NULL);
192 ASSERT (strcmp (result1, result2) == 0);
193 ASSERT (strcmp (result2, result3) == 0);
194 ASSERT (strcmp (result3, result4) == 0);
195 ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/lum"),
196 "/" BASE "/lum") == 0);
197 free (result1);
198 free (result2);
199 free (result3);
200 free (result4);
203 /* Check that a symbolic link to a nonexistent file yields NULL. */
205 char *result1;
206 char *result2;
207 errno = 0;
208 result1 = canonicalize_file_name (BASE "/ouk");
209 ASSERT (result1 == NULL);
210 ASSERT (errno == ENOENT);
211 errno = 0;
212 result2 = canonicalize_filename_mode (BASE "/ouk", CAN_EXISTING);
213 ASSERT (result2 == NULL);
214 ASSERT (errno == ENOENT);
217 /* Check that a non-directory symlink with trailing slash yields NULL. */
219 char *result1;
220 char *result2;
221 errno = 0;
222 result1 = canonicalize_file_name (BASE "/huk/");
223 ASSERT (result1 == NULL);
224 ASSERT (errno == ENOTDIR);
225 errno = 0;
226 result2 = canonicalize_filename_mode (BASE "/huk/", CAN_EXISTING);
227 ASSERT (result2 == NULL);
228 ASSERT (errno == ENOTDIR);
231 /* Check that a missing directory via symlink yields NULL. */
233 char *result1;
234 char *result2;
235 errno = 0;
236 result1 = canonicalize_file_name (BASE "/ouk/..");
237 ASSERT (result1 == NULL);
238 ASSERT (errno == ENOENT);
239 errno = 0;
240 result2 = canonicalize_filename_mode (BASE "/ouk/..", CAN_EXISTING);
241 ASSERT (result2 == NULL);
242 ASSERT (errno == ENOENT);
245 /* Check that a loop of symbolic links is detected. */
247 char *result1;
248 char *result2;
249 errno = 0;
250 result1 = canonicalize_file_name ("ise");
251 ASSERT (result1 == NULL);
252 ASSERT (errno == ELOOP);
253 errno = 0;
254 result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
255 ASSERT (result2 == NULL);
256 ASSERT (errno == ELOOP);
259 /* Check that alternate modes can resolve missing basenames. */
261 char *result1 = canonicalize_filename_mode (BASE "/zzz", CAN_ALL_BUT_LAST);
262 char *result2 = canonicalize_filename_mode (BASE "/zzz", CAN_MISSING);
263 char *result3 = canonicalize_filename_mode (BASE "/zzz/", CAN_ALL_BUT_LAST);
264 char *result4 = canonicalize_filename_mode (BASE "/zzz/", CAN_MISSING);
265 ASSERT (result1 != NULL);
266 ASSERT (result2 != NULL);
267 ASSERT (result3 != NULL);
268 ASSERT (result4 != NULL);
269 ASSERT (strcmp (result1, result2) == 0);
270 ASSERT (strcmp (result2, result3) == 0);
271 ASSERT (strcmp (result3, result4) == 0);
272 ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/zzz"),
273 "/" BASE "/zzz") == 0);
274 free (result1);
275 free (result2);
276 free (result3);
277 free (result4);
280 /* Check that alternate modes can resolve broken symlink basenames. */
282 char *result1 = canonicalize_filename_mode (BASE "/ouk", CAN_ALL_BUT_LAST);
283 char *result2 = canonicalize_filename_mode (BASE "/ouk", CAN_MISSING);
284 char *result3 = canonicalize_filename_mode (BASE "/ouk/", CAN_ALL_BUT_LAST);
285 char *result4 = canonicalize_filename_mode (BASE "/ouk/", CAN_MISSING);
286 ASSERT (result1 != NULL);
287 ASSERT (result2 != NULL);
288 ASSERT (result3 != NULL);
289 ASSERT (result4 != NULL);
290 ASSERT (strcmp (result1, result2) == 0);
291 ASSERT (strcmp (result2, result3) == 0);
292 ASSERT (strcmp (result3, result4) == 0);
293 ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/wum"),
294 "/" BASE "/wum") == 0);
295 free (result1);
296 free (result2);
297 free (result3);
298 free (result4);
301 /* Check that alternate modes can handle missing dirnames. */
303 char *result1 = canonicalize_filename_mode ("t-can.zzz/zzz", CAN_ALL_BUT_LAST);
304 char *result2 = canonicalize_filename_mode ("t-can.zzz/zzz", CAN_MISSING);
305 ASSERT (result1 == NULL);
306 ASSERT (result2 != NULL);
307 ASSERT (strcmp (result2 + strlen (result2) - 14, "/t-can.zzz/zzz") == 0);
308 free (result2);
311 /* Ensure that the following is resolved properly.
312 Before 2007-09-27, it would mistakenly report a loop. */
314 char *result1 = canonicalize_filename_mode (BASE, CAN_EXISTING);
315 char *result2 = canonicalize_filename_mode (BASE "/p/1", CAN_EXISTING);
316 ASSERT (result1 != NULL);
317 ASSERT (result2 != NULL);
318 ASSERT (strcmp (result2 + strlen (result1), "/d/2") == 0);
319 free (result1);
320 free (result2);
323 /* Check that leading // is honored correctly. */
325 struct stat st1;
326 struct stat st2;
327 char *result1 = canonicalize_file_name ("//.");
328 char *result2 = canonicalize_filename_mode ("//.", CAN_EXISTING);
329 char *result3 = canonicalize_file_name (BASE "/droot");
330 char *result4 = canonicalize_filename_mode (BASE "/droot", CAN_EXISTING);
331 ASSERT (result1);
332 ASSERT (result2);
333 ASSERT (result3);
334 ASSERT (result4);
335 ASSERT (stat ("/", &st1) == 0);
336 ASSERT (stat ("//", &st2) == 0);
337 if (SAME_INODE (st1, st2))
339 ASSERT (strcmp (result1, "/") == 0);
340 ASSERT (strcmp (result2, "/") == 0);
341 ASSERT (strcmp (result3, "/") == 0);
342 ASSERT (strcmp (result4, "/") == 0);
344 else
346 ASSERT (strcmp (result1, "//") == 0);
347 ASSERT (strcmp (result2, "//") == 0);
348 ASSERT (strcmp (result3, "//") == 0);
349 ASSERT (strcmp (result4, "//") == 0);
351 free (result1);
352 free (result2);
353 free (result3);
354 free (result4);
357 /* Cleanup. */
358 ASSERT (remove (BASE "/droot") == 0);
359 ASSERT (remove (BASE "/d/1") == 0);
360 ASSERT (remove (BASE "/d/2") == 0);
361 ASSERT (remove (BASE "/d") == 0);
362 ASSERT (remove (BASE "/s") == 0);
363 ASSERT (remove (BASE "/p") == 0);
364 ASSERT (remove (BASE "/plo") == 0);
365 ASSERT (remove (BASE "/huk") == 0);
366 ASSERT (remove (BASE "/bef") == 0);
367 ASSERT (remove (BASE "/ouk") == 0);
368 ASSERT (remove (BASE "/ket") == 0);
369 ASSERT (remove (BASE "/lum") == 0);
370 ASSERT (remove (BASE "/tra") == 0);
371 ASSERT (remove (BASE) == 0);
372 ASSERT (remove ("ise") == 0);
374 return 0;