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)
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 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",
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"));
75 result1
= canonicalize_file_name ("");
76 ASSERT (result1
== NULL
);
77 ASSERT (errno
== ENOENT
);
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
90 result1
= canonicalize_file_name (null_ptr ());
91 ASSERT (result1
== NULL
);
92 ASSERT (errno
== EINVAL
);
96 result2
= canonicalize_filename_mode (NULL
, CAN_EXISTING
);
97 ASSERT (result2
== NULL
);
98 ASSERT (errno
== EINVAL
);
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. */
111 result1
= canonicalize_file_name (BASE
"/tra/");
112 ASSERT (result1
== NULL
);
113 ASSERT (errno
== ENOTDIR
);
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. */
125 result1
= canonicalize_file_name (BASE
"/zzz/..");
126 ASSERT (result1
== NULL
);
127 ASSERT (errno
== ENOENT
);
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",
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);
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);
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);
203 /* Check that a symbolic link to a nonexistent file yields NULL. */
208 result1
= canonicalize_file_name (BASE
"/ouk");
209 ASSERT (result1
== NULL
);
210 ASSERT (errno
== ENOENT
);
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. */
222 result1
= canonicalize_file_name (BASE
"/huk/");
223 ASSERT (result1
== NULL
);
224 ASSERT (errno
== ENOTDIR
);
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. */
236 result1
= canonicalize_file_name (BASE
"/ouk/..");
237 ASSERT (result1
== NULL
);
238 ASSERT (errno
== ENOENT
);
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. */
250 result1
= canonicalize_file_name ("ise");
251 ASSERT (result1
== NULL
);
252 ASSERT (errno
== ELOOP
);
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);
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);
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);
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);
323 /* Check that leading // is honored correctly. */
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
);
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);
346 ASSERT (strcmp (result1
, "//") == 0);
347 ASSERT (strcmp (result2
, "//") == 0);
348 ASSERT (strcmp (result3
, "//") == 0);
349 ASSERT (strcmp (result4
, "//") == 0);
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);