1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/* Test of execution of file name canonicalization.
Copyright (C) 2007-2020 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
may "optimize" the null_ptr function, when its result gets passed to a
function that has an argument declared as _GL_ARG_NONNULL. */
#define _GL_ARG_NONNULL(params)
#include <config.h>
#include "canonicalize.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "same-inode.h"
#include "ignore-value.h"
#if GNULIB_defined_canonicalize_file_name
# include "null-ptr.h"
#endif
#include "macros.h"
#define BASE "t-can.tmp"
int
main (void)
{
/* Setup some hierarchy to be used by this test. Start by removing
any leftovers from a previous partial run. */
{
int fd;
ignore_value (system ("rm -rf " BASE " ise"));
ASSERT (mkdir (BASE, 0700) == 0);
fd = creat (BASE "/tra", 0600);
ASSERT (0 <= fd);
ASSERT (close (fd) == 0);
}
/* Check for ., .., intermediate // handling, and for error cases. */
{
char *result1 = canonicalize_file_name (BASE "//./..//" BASE "/tra");
char *result2 = canonicalize_filename_mode (BASE "//./..//" BASE "/tra",
CAN_EXISTING);
ASSERT (result1 != NULL);
ASSERT (result2 != NULL);
ASSERT (strcmp (result1, result2) == 0);
ASSERT (strstr (result1, "/" BASE "/tra")
== result1 + strlen (result1) - strlen ("/" BASE "/tra"));
free (result1);
free (result2);
errno = 0;
result1 = canonicalize_file_name ("");
ASSERT (result1 == NULL);
ASSERT (errno == ENOENT);
errno = 0;
result2 = canonicalize_filename_mode ("", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ENOENT);
/* This test works only if the canonicalize_file_name implementation
comes from gnulib. If it comes from libc, we have no way to prevent
gcc from "optimizing" the null_ptr function in invalid ways. See
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93156>. */
#if GNULIB_defined_canonicalize_file_name
errno = 0;
result1 = canonicalize_file_name (null_ptr ());
ASSERT (result1 == NULL);
ASSERT (errno == EINVAL);
#endif
errno = 0;
result2 = canonicalize_filename_mode (NULL, CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == EINVAL);
errno = 0;
result2 = canonicalize_filename_mode (".", CAN_MISSING | CAN_ALL_BUT_LAST);
ASSERT (result2 == NULL);
ASSERT (errno == EINVAL);
}
/* Check that a non-directory with trailing slash yields NULL. */
{
char *result1;
char *result2;
errno = 0;
result1 = canonicalize_file_name (BASE "/tra/");
ASSERT (result1 == NULL);
ASSERT (errno == ENOTDIR);
errno = 0;
result2 = canonicalize_filename_mode (BASE "/tra/", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ENOTDIR);
}
/* Check that a missing directory yields NULL. */
{
char *result1;
char *result2;
errno = 0;
result1 = canonicalize_file_name (BASE "/zzz/..");
ASSERT (result1 == NULL);
ASSERT (errno == ENOENT);
errno = 0;
result2 = canonicalize_filename_mode (BASE "/zzz/..", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ENOENT);
}
/* From here on out, tests involve symlinks. */
if (symlink (BASE "/ket", "ise") != 0)
{
ASSERT (remove (BASE "/tra") == 0);
ASSERT (rmdir (BASE) == 0);
fputs ("skipping test: symlinks not supported on this file system\n",
stderr);
return 77;
}
ASSERT (symlink ("bef", BASE "/plo") == 0);
ASSERT (symlink ("tra", BASE "/huk") == 0);
ASSERT (symlink ("lum", BASE "/bef") == 0);
ASSERT (symlink ("wum", BASE "/ouk") == 0);
ASSERT (symlink ("../ise", BASE "/ket") == 0);
ASSERT (mkdir (BASE "/lum", 0700) == 0);
ASSERT (symlink ("s", BASE "/p") == 0);
ASSERT (symlink ("d", BASE "/s") == 0);
ASSERT (mkdir (BASE "/d", 0700) == 0);
ASSERT (close (creat (BASE "/d/2", 0600)) == 0);
ASSERT (symlink ("../s/2", BASE "/d/1") == 0);
ASSERT (symlink ("//.//../..", BASE "/droot") == 0);
/* Check that symbolic links are not resolved, with CAN_NOLINKS. */
{
char *result1 = canonicalize_filename_mode (BASE "/huk", CAN_NOLINKS);
ASSERT (result1 != NULL);
ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/huk"),
"/" BASE "/huk") == 0);
free (result1);
}
/* Check that the symbolic link to a file can be resolved. */
{
char *result1 = canonicalize_file_name (BASE "/huk");
char *result2 = canonicalize_file_name (BASE "/tra");
char *result3 = canonicalize_filename_mode (BASE "/huk", CAN_EXISTING);
ASSERT (result1 != NULL);
ASSERT (result2 != NULL);
ASSERT (result3 != NULL);
ASSERT (strcmp (result1, result2) == 0);
ASSERT (strcmp (result2, result3) == 0);
ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/tra"),
"/" BASE "/tra") == 0);
free (result1);
free (result2);
free (result3);
}
/* Check that the symbolic link to a directory can be resolved. */
{
char *result1 = canonicalize_file_name (BASE "/plo");
char *result2 = canonicalize_file_name (BASE "/bef");
char *result3 = canonicalize_file_name (BASE "/lum");
char *result4 = canonicalize_filename_mode (BASE "/plo", CAN_EXISTING);
ASSERT (result1 != NULL);
ASSERT (result2 != NULL);
ASSERT (result3 != NULL);
ASSERT (result4 != NULL);
ASSERT (strcmp (result1, result2) == 0);
ASSERT (strcmp (result2, result3) == 0);
ASSERT (strcmp (result3, result4) == 0);
ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/lum"),
"/" BASE "/lum") == 0);
free (result1);
free (result2);
free (result3);
free (result4);
}
/* Check that a symbolic link to a nonexistent file yields NULL. */
{
char *result1;
char *result2;
errno = 0;
result1 = canonicalize_file_name (BASE "/ouk");
ASSERT (result1 == NULL);
ASSERT (errno == ENOENT);
errno = 0;
result2 = canonicalize_filename_mode (BASE "/ouk", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ENOENT);
}
/* Check that a non-directory symlink with trailing slash yields NULL. */
{
char *result1;
char *result2;
errno = 0;
result1 = canonicalize_file_name (BASE "/huk/");
ASSERT (result1 == NULL);
ASSERT (errno == ENOTDIR);
errno = 0;
result2 = canonicalize_filename_mode (BASE "/huk/", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ENOTDIR);
}
/* Check that a missing directory via symlink yields NULL. */
{
char *result1;
char *result2;
errno = 0;
result1 = canonicalize_file_name (BASE "/ouk/..");
ASSERT (result1 == NULL);
ASSERT (errno == ENOENT);
errno = 0;
result2 = canonicalize_filename_mode (BASE "/ouk/..", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ENOENT);
}
/* Check that a loop of symbolic links is detected. */
{
char *result1;
char *result2;
errno = 0;
result1 = canonicalize_file_name ("ise");
ASSERT (result1 == NULL);
ASSERT (errno == ELOOP);
errno = 0;
result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
ASSERT (result2 == NULL);
ASSERT (errno == ELOOP);
}
/* Check that alternate modes can resolve missing basenames. */
{
char *result1 = canonicalize_filename_mode (BASE "/zzz", CAN_ALL_BUT_LAST);
char *result2 = canonicalize_filename_mode (BASE "/zzz", CAN_MISSING);
char *result3 = canonicalize_filename_mode (BASE "/zzz/", CAN_ALL_BUT_LAST);
char *result4 = canonicalize_filename_mode (BASE "/zzz/", CAN_MISSING);
ASSERT (result1 != NULL);
ASSERT (result2 != NULL);
ASSERT (result3 != NULL);
ASSERT (result4 != NULL);
ASSERT (strcmp (result1, result2) == 0);
ASSERT (strcmp (result2, result3) == 0);
ASSERT (strcmp (result3, result4) == 0);
ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/zzz"),
"/" BASE "/zzz") == 0);
free (result1);
free (result2);
free (result3);
free (result4);
}
/* Check that alternate modes can resolve broken symlink basenames. */
{
char *result1 = canonicalize_filename_mode (BASE "/ouk", CAN_ALL_BUT_LAST);
char *result2 = canonicalize_filename_mode (BASE "/ouk", CAN_MISSING);
char *result3 = canonicalize_filename_mode (BASE "/ouk/", CAN_ALL_BUT_LAST);
char *result4 = canonicalize_filename_mode (BASE "/ouk/", CAN_MISSING);
ASSERT (result1 != NULL);
ASSERT (result2 != NULL);
ASSERT (result3 != NULL);
ASSERT (result4 != NULL);
ASSERT (strcmp (result1, result2) == 0);
ASSERT (strcmp (result2, result3) == 0);
ASSERT (strcmp (result3, result4) == 0);
ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/wum"),
"/" BASE "/wum") == 0);
free (result1);
free (result2);
free (result3);
free (result4);
}
/* Check that alternate modes can handle missing dirnames. */
{
char *result1 = canonicalize_filename_mode ("t-can.zzz/zzz", CAN_ALL_BUT_LAST);
char *result2 = canonicalize_filename_mode ("t-can.zzz/zzz", CAN_MISSING);
ASSERT (result1 == NULL);
ASSERT (result2 != NULL);
ASSERT (strcmp (result2 + strlen (result2) - 14, "/t-can.zzz/zzz") == 0);
free (result2);
}
/* Ensure that the following is resolved properly.
Before 2007-09-27, it would mistakenly report a loop. */
{
char *result1 = canonicalize_filename_mode (BASE, CAN_EXISTING);
char *result2 = canonicalize_filename_mode (BASE "/p/1", CAN_EXISTING);
ASSERT (result1 != NULL);
ASSERT (result2 != NULL);
ASSERT (strcmp (result2 + strlen (result1), "/d/2") == 0);
free (result1);
free (result2);
}
/* Check that leading // is honored correctly. */
{
struct stat st1;
struct stat st2;
char *result1 = canonicalize_file_name ("//.");
char *result2 = canonicalize_filename_mode ("//.", CAN_EXISTING);
char *result3 = canonicalize_file_name (BASE "/droot");
char *result4 = canonicalize_filename_mode (BASE "/droot", CAN_EXISTING);
ASSERT (result1);
ASSERT (result2);
ASSERT (result3);
ASSERT (result4);
ASSERT (stat ("/", &st1) == 0);
ASSERT (stat ("//", &st2) == 0);
if (SAME_INODE (st1, st2))
{
ASSERT (strcmp (result1, "/") == 0);
ASSERT (strcmp (result2, "/") == 0);
ASSERT (strcmp (result3, "/") == 0);
ASSERT (strcmp (result4, "/") == 0);
}
else
{
ASSERT (strcmp (result1, "//") == 0);
ASSERT (strcmp (result2, "//") == 0);
ASSERT (strcmp (result3, "//") == 0);
ASSERT (strcmp (result4, "//") == 0);
}
free (result1);
free (result2);
free (result3);
free (result4);
}
/* Cleanup. */
ASSERT (remove (BASE "/droot") == 0);
ASSERT (remove (BASE "/d/1") == 0);
ASSERT (remove (BASE "/d/2") == 0);
ASSERT (remove (BASE "/d") == 0);
ASSERT (remove (BASE "/s") == 0);
ASSERT (remove (BASE "/p") == 0);
ASSERT (remove (BASE "/plo") == 0);
ASSERT (remove (BASE "/huk") == 0);
ASSERT (remove (BASE "/bef") == 0);
ASSERT (remove (BASE "/ouk") == 0);
ASSERT (remove (BASE "/ket") == 0);
ASSERT (remove (BASE "/lum") == 0);
ASSERT (remove (BASE "/tra") == 0);
ASSERT (remove (BASE) == 0);
ASSERT (remove ("ise") == 0);
return 0;
}
|