1 /* Test of execution of file name canonicalization.
2 Copyright (C) 2007-2025 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)
26 #include "canonicalize.h"
36 #include "same-inode.h"
37 #include "ignore-value.h"
39 #if GNULIB_defined_canonicalize_file_name
40 # include "null-ptr.h"
45 #define BASE "t-can.tmp"
50 /* Setup some hierarchy to be used by this test. Start by removing
51 any leftovers from a previous partial run. */
54 ignore_value (system ("rm -rf " BASE
" ise"));
55 ASSERT (mkdir (BASE
, 0700) == 0);
56 fd
= creat (BASE
"/tra", 0600);
58 ASSERT (close (fd
) == 0);
61 /* Check // handling (the easy cases, without symlinks).
62 This // handling is not mandated by POSIX. However, many applications
63 expect that canonicalize_filename_mode "canonicalizes" the file name,
64 that is, that different results of canonicalize_filename_mode correspond
65 to different files (except for hard links). */
67 char *result0
= canonicalize_file_name ("/etc/passwd");
68 if (result0
!= NULL
) /* This file does not exist on native Windows. */
72 result
= canonicalize_filename_mode ("/etc/passwd", CAN_MISSING
);
73 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
75 result
= canonicalize_filename_mode ("/etc//passwd", CAN_MISSING
);
76 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
78 result
= canonicalize_filename_mode ("/etc///passwd", CAN_MISSING
);
79 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
81 /* On Windows, the syntax //host/share/filename denotes a file
82 in a directory named 'share', exported from host 'host'.
83 See also m4/double-slash-root.m4. */
84 #if !(defined _WIN32 || defined __CYGWIN__)
85 result
= canonicalize_filename_mode ("//etc/passwd", CAN_MISSING
);
86 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
88 result
= canonicalize_filename_mode ("//etc//passwd", CAN_MISSING
);
89 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
91 result
= canonicalize_filename_mode ("//etc///passwd", CAN_MISSING
);
92 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
95 result
= canonicalize_filename_mode ("///etc/passwd", CAN_MISSING
);
96 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
98 result
= canonicalize_filename_mode ("///etc//passwd", CAN_MISSING
);
99 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
101 result
= canonicalize_filename_mode ("///etc///passwd", CAN_MISSING
);
102 ASSERT (result
!= NULL
&& strcmp (result
, result0
) == 0);
106 /* Check for ., .., intermediate // handling, and for error cases. */
108 char *result1
= canonicalize_file_name (BASE
"//./..//" BASE
"/tra");
109 char *result2
= canonicalize_filename_mode (BASE
"//./..//" BASE
"/tra",
111 ASSERT (result1
!= NULL
);
112 ASSERT (result2
!= NULL
);
113 ASSERT (strcmp (result1
, result2
) == 0);
114 ASSERT (strstr (result1
, "/" BASE
"/tra")
115 == result1
+ strlen (result1
) - strlen ("/" BASE
"/tra"));
120 result1
= canonicalize_file_name ("");
121 ASSERT (result1
== NULL
);
122 ASSERT (errno
== ENOENT
);
125 result2
= canonicalize_filename_mode ("", CAN_EXISTING
);
126 ASSERT (result2
== NULL
);
127 ASSERT (errno
== ENOENT
);
129 /* This test works only if the canonicalize_file_name implementation
130 comes from gnulib. If it comes from libc, we have no way to prevent
131 gcc from "optimizing" the null_ptr function in invalid ways. See
132 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93156>. */
133 #if GNULIB_defined_canonicalize_file_name
135 result1
= canonicalize_file_name (null_ptr ());
136 ASSERT (result1
== NULL
);
137 ASSERT (errno
== EINVAL
);
141 result2
= canonicalize_filename_mode (NULL
, CAN_EXISTING
);
142 ASSERT (result2
== NULL
);
143 ASSERT (errno
== EINVAL
);
146 result2
= canonicalize_filename_mode (".", CAN_MISSING
| CAN_ALL_BUT_LAST
);
147 ASSERT (result2
== NULL
);
148 ASSERT (errno
== EINVAL
);
151 /* Check that a non-directory with trailing slash yields NULL. */
156 result1
= canonicalize_file_name (BASE
"/tra/");
157 ASSERT (result1
== NULL
);
158 ASSERT (errno
== ENOTDIR
);
160 result2
= canonicalize_filename_mode (BASE
"/tra/", CAN_EXISTING
);
161 ASSERT (result2
== NULL
);
162 ASSERT (errno
== ENOTDIR
);
165 /* Check that a missing directory yields NULL. */
170 result1
= canonicalize_file_name (BASE
"/zzz/..");
171 ASSERT (result1
== NULL
);
172 ASSERT (errno
== ENOENT
);
174 result2
= canonicalize_filename_mode (BASE
"/zzz/..", CAN_EXISTING
);
175 ASSERT (result2
== NULL
);
176 ASSERT (errno
== ENOENT
);
179 /* From here on out, tests involve symlinks. */
180 if (symlink (BASE
"/ket", "ise") != 0)
182 ASSERT (remove (BASE
"/tra") == 0);
183 ASSERT (rmdir (BASE
) == 0);
184 if (test_exit_status
!= EXIT_SUCCESS
)
185 return test_exit_status
;
186 fputs ("skipping test: symlinks not supported on this file system\n",
190 ASSERT (symlink ("bef", BASE
"/plo") == 0);
191 ASSERT (symlink ("tra", BASE
"/huk") == 0);
192 ASSERT (symlink ("lum", BASE
"/bef") == 0);
193 ASSERT (symlink ("wum", BASE
"/ouk") == 0);
194 ASSERT (symlink ("../ise", BASE
"/ket") == 0);
195 ASSERT (mkdir (BASE
"/lum", 0700) == 0);
196 ASSERT (symlink ("s", BASE
"/p") == 0);
197 ASSERT (symlink ("d", BASE
"/s") == 0);
198 ASSERT (mkdir (BASE
"/d", 0700) == 0);
199 ASSERT (close (creat (BASE
"/d/2", 0600)) == 0);
200 ASSERT (symlink ("../s/2", BASE
"/d/1") == 0);
201 ASSERT (symlink ("//.//../..", BASE
"/droot") == 0);
203 /* Check that symbolic links are not resolved, with CAN_NOLINKS. */
205 char *result1
= canonicalize_filename_mode (BASE
"/huk", CAN_NOLINKS
);
206 ASSERT (result1
!= NULL
);
207 ASSERT (str_endswith (result1
, "/" BASE
"/huk"));
211 /* Check that the symbolic link to a file can be resolved. */
213 char *result1
= canonicalize_file_name (BASE
"/huk");
214 char *result2
= canonicalize_file_name (BASE
"/tra");
215 char *result3
= canonicalize_filename_mode (BASE
"/huk", CAN_EXISTING
);
216 ASSERT (result1
!= NULL
);
217 ASSERT (result2
!= NULL
);
218 ASSERT (result3
!= NULL
);
219 ASSERT (strcmp (result1
, result2
) == 0);
220 ASSERT (strcmp (result2
, result3
) == 0);
221 ASSERT (str_endswith (result1
, "/" BASE
"/tra"));
227 /* Check that the symbolic link to a directory can be resolved. */
229 char *result1
= canonicalize_file_name (BASE
"/plo");
230 char *result2
= canonicalize_file_name (BASE
"/bef");
231 char *result3
= canonicalize_file_name (BASE
"/lum");
232 char *result4
= canonicalize_filename_mode (BASE
"/plo", CAN_EXISTING
);
233 ASSERT (result1
!= NULL
);
234 ASSERT (result2
!= NULL
);
235 ASSERT (result3
!= NULL
);
236 ASSERT (result4
!= NULL
);
237 ASSERT (strcmp (result1
, result2
) == 0);
238 ASSERT (strcmp (result2
, result3
) == 0);
239 ASSERT (strcmp (result3
, result4
) == 0);
240 ASSERT (str_endswith (result1
, "/" BASE
"/lum"));
247 /* Check that a symbolic link to a nonexistent file yields NULL. */
252 result1
= canonicalize_file_name (BASE
"/ouk");
253 ASSERT (result1
== NULL
);
254 ASSERT (errno
== ENOENT
);
256 result2
= canonicalize_filename_mode (BASE
"/ouk", CAN_EXISTING
);
257 ASSERT (result2
== NULL
);
258 ASSERT (errno
== ENOENT
);
261 /* Check that a non-directory symlink with trailing slash yields NULL,
262 and likewise for other troublesome suffixes. */
264 char const *const file_name
[]
276 BASE
"/huk/././../x",
278 for (int i
= 0; i
< sizeof file_name
/ sizeof *file_name
; i
++)
281 ASSERT (!canonicalize_file_name (file_name
[i
]));
282 ASSERT (errno
== ENOTDIR
);
284 ASSERT (!canonicalize_filename_mode (file_name
[i
], CAN_EXISTING
));
285 ASSERT (errno
== ENOTDIR
);
289 /* Check that a missing directory via symlink yields NULL. */
294 result1
= canonicalize_file_name (BASE
"/ouk/..");
295 ASSERT (result1
== NULL
);
296 ASSERT (errno
== ENOENT
);
298 result2
= canonicalize_filename_mode (BASE
"/ouk/..", CAN_EXISTING
);
299 ASSERT (result2
== NULL
);
300 ASSERT (errno
== ENOENT
);
303 /* Check that a loop of symbolic links is detected. */
308 result1
= canonicalize_file_name ("ise");
309 ASSERT (result1
== NULL
);
310 ASSERT (errno
== ELOOP
);
312 result2
= canonicalize_filename_mode ("ise", CAN_EXISTING
);
313 ASSERT (result2
== NULL
);
314 ASSERT (errno
== ELOOP
);
317 /* Check that alternate modes can resolve missing basenames. */
319 char *result1
= canonicalize_filename_mode (BASE
"/zzz", CAN_ALL_BUT_LAST
);
320 char *result2
= canonicalize_filename_mode (BASE
"/zzz", CAN_MISSING
);
321 char *result3
= canonicalize_filename_mode (BASE
"/zzz/", CAN_ALL_BUT_LAST
);
322 char *result4
= canonicalize_filename_mode (BASE
"/zzz/", CAN_MISSING
);
323 ASSERT (result1
!= NULL
);
324 ASSERT (result2
!= NULL
);
325 ASSERT (result3
!= NULL
);
326 ASSERT (result4
!= NULL
);
327 ASSERT (strcmp (result1
, result2
) == 0);
328 ASSERT (strcmp (result2
, result3
) == 0);
329 ASSERT (strcmp (result3
, result4
) == 0);
330 ASSERT (str_endswith (result1
, "/" BASE
"/zzz"));
337 /* Check that alternate modes can resolve broken symlink basenames. */
339 char *result1
= canonicalize_filename_mode (BASE
"/ouk", CAN_ALL_BUT_LAST
);
340 char *result2
= canonicalize_filename_mode (BASE
"/ouk", CAN_MISSING
);
341 char *result3
= canonicalize_filename_mode (BASE
"/ouk/", CAN_ALL_BUT_LAST
);
342 char *result4
= canonicalize_filename_mode (BASE
"/ouk/", CAN_MISSING
);
343 ASSERT (result1
!= NULL
);
344 ASSERT (result2
!= NULL
);
345 ASSERT (result3
!= NULL
);
346 ASSERT (result4
!= NULL
);
347 ASSERT (strcmp (result1
, result2
) == 0);
348 ASSERT (strcmp (result2
, result3
) == 0);
349 ASSERT (strcmp (result3
, result4
) == 0);
350 ASSERT (str_endswith (result1
, "/" BASE
"/wum"));
357 /* Check that alternate modes can handle missing dirnames. */
359 char *result1
= canonicalize_filename_mode ("t-can.zzz/zzz", CAN_ALL_BUT_LAST
);
360 char *result2
= canonicalize_filename_mode ("t-can.zzz/zzz", CAN_MISSING
);
361 ASSERT (result1
== NULL
);
362 ASSERT (result2
!= NULL
);
363 ASSERT (str_endswith (result2
, "/t-can.zzz/zzz"));
367 /* Ensure that the following is resolved properly.
368 Before 2007-09-27, it would mistakenly report a loop. */
370 char *result1
= canonicalize_filename_mode (BASE
, CAN_EXISTING
);
371 char *result2
= canonicalize_filename_mode (BASE
"/p/1", CAN_EXISTING
);
372 ASSERT (result1
!= NULL
);
373 ASSERT (result2
!= NULL
);
374 ASSERT (strcmp (result2
+ strlen (result1
), "/d/2") == 0);
379 /* Check that leading // within symlinks is honored correctly. */
383 char *result1
= canonicalize_file_name ("//.");
384 char *result2
= canonicalize_filename_mode ("//.", CAN_EXISTING
);
385 char *result3
= canonicalize_file_name (BASE
"/droot");
386 char *result4
= canonicalize_filename_mode (BASE
"/droot", CAN_EXISTING
);
391 ASSERT (stat ("/", &st1
) == 0);
392 ASSERT (stat ("//", &st2
) == 0);
393 bool same
= psame_inode (&st1
, &st2
);
395 /* On IBM z/OS, "/" and "//" both canonicalize to themselves, yet they both
396 have st_dev == st_ino == 1. */
401 ASSERT (strcmp (result1
, "/") == 0);
402 ASSERT (strcmp (result2
, "/") == 0);
403 ASSERT (strcmp (result3
, "/") == 0);
404 ASSERT (strcmp (result4
, "/") == 0);
408 ASSERT (strcmp (result1
, "//") == 0);
409 ASSERT (strcmp (result2
, "//") == 0);
410 ASSERT (strcmp (result3
, "//") == 0);
411 ASSERT (strcmp (result4
, "//") == 0);
419 #if !(defined _WIN32 && !defined __CYGWIN__)
420 /* Check a device file. */
422 char *result1
= canonicalize_file_name ("/dev/null");
423 char *result2
= canonicalize_filename_mode ("/dev/null", CAN_ALL_BUT_LAST
);
424 ASSERT (result1
!= NULL
);
425 ASSERT (result2
!= NULL
);
426 ASSERT (strcmp (result1
, result2
) == 0);
431 ASSERT (remove (BASE
"/droot") == 0);
432 ASSERT (remove (BASE
"/d/1") == 0);
433 ASSERT (remove (BASE
"/d/2") == 0);
434 ASSERT (remove (BASE
"/d") == 0);
435 ASSERT (remove (BASE
"/s") == 0);
436 ASSERT (remove (BASE
"/p") == 0);
437 ASSERT (remove (BASE
"/plo") == 0);
438 ASSERT (remove (BASE
"/huk") == 0);
439 ASSERT (remove (BASE
"/bef") == 0);
440 ASSERT (remove (BASE
"/ouk") == 0);
441 ASSERT (remove (BASE
"/ket") == 0);
442 ASSERT (remove (BASE
"/lum") == 0);
443 ASSERT (remove (BASE
"/tra") == 0);
444 ASSERT (remove (BASE
) == 0);
445 ASSERT (remove ("ise") == 0);
447 return test_exit_status
;