]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-util.c
cbe0341fe4b9c9bff648ea49fb049183657ccbf4
[thirdparty/systemd.git] / src / test / test-path-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "argv-util.h"
9 #include "exec-util.h"
10 #include "fd-util.h"
11 #include "path-util.h"
12 #include "process-util.h"
13 #include "rm-rf.h"
14 #include "stat-util.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "tests.h"
18 #include "tmpfile-util.h"
19
20 TEST(print_paths) {
21 log_info("default system PATH: %s", default_PATH());
22 log_info("default user PATH: %s", default_user_PATH());
23 }
24
25 TEST(path) {
26 assert_se( path_is_absolute("/"));
27 assert_se(!path_is_absolute("./"));
28
29 assert_se( PATH_IN_SET("/bin", "/", "/bin", "/foo"));
30 assert_se( PATH_IN_SET("/bin", "/bin"));
31 assert_se( PATH_IN_SET("/bin", "/foo/bar", "/bin"));
32 assert_se( PATH_IN_SET("/", "/", "/", "/foo/bar"));
33 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
34
35 assert_se( path_equal(NULL, NULL));
36 assert_se( path_equal("/a", "/a"));
37 assert_se(!path_equal("/a", "/b"));
38 assert_se(!path_equal("/a", NULL));
39 assert_se(!path_equal(NULL, "/a"));
40 assert_se(!path_equal("a", NULL));
41 assert_se(!path_equal(NULL, "a"));
42 }
43
44 TEST(is_path) {
45 assert_se(!is_path("foo"));
46 assert_se(!is_path("dos.ext"));
47 assert_se( is_path("/dir"));
48 assert_se( is_path("a/b"));
49 assert_se( is_path("a/b.ext"));
50
51 assert_se(!is_path("."));
52 assert_se(!is_path(""));
53 assert_se(!is_path(".."));
54
55 assert_se( is_path("/dev"));
56 assert_se( is_path("/./dev"));
57 assert_se( is_path("/./dev/."));
58 assert_se( is_path("/./dev."));
59 assert_se( is_path("//dev"));
60 assert_se( is_path("///dev"));
61 assert_se( is_path("/dev/"));
62 assert_se( is_path("///dev/"));
63 assert_se( is_path("/./dev/"));
64 assert_se( is_path("/../dev/"));
65 assert_se( is_path("/dev/sda"));
66 assert_se( is_path("/dev/sda5"));
67 assert_se( is_path("/dev/sda5b3"));
68 assert_se( is_path("/dev/sda5b3/idontexit"));
69 assert_se( is_path("/../dev/sda"));
70 assert_se( is_path("/../../dev/sda5"));
71 assert_se( is_path("/../../../dev/sda5b3"));
72 assert_se( is_path("/.././.././dev/sda5b3/idontexit"));
73 assert_se( is_path("/sys"));
74 assert_se( is_path("/sys/"));
75 assert_se( is_path("/./sys"));
76 assert_se( is_path("/./sys/."));
77 assert_se( is_path("/./sys."));
78 assert_se( is_path("/sys/what"));
79 assert_se( is_path("/sys/something/.."));
80 assert_se( is_path("/sys/something/../"));
81 assert_se( is_path("/sys////"));
82 assert_se( is_path("/sys////."));
83 assert_se( is_path("/sys/.."));
84 assert_se( is_path("/sys/../"));
85 assert_se( is_path("/usr/../dev/sda"));
86 }
87
88 TEST(is_device_path) {
89 assert_se(!is_device_path("foo"));
90 assert_se(!is_device_path("dos.ext"));
91 assert_se(!is_device_path("/dir"));
92 assert_se(!is_device_path("a/b"));
93 assert_se(!is_device_path("a/b.ext"));
94
95 assert_se(!is_device_path("."));
96 assert_se(!is_device_path(""));
97 assert_se(!is_device_path(".."));
98
99 assert_se(!is_device_path("/dev"));
100 assert_se(!is_device_path("/./dev"));
101 assert_se(!is_device_path("/./dev/."));
102 assert_se(!is_device_path("/./dev."));
103 assert_se( is_device_path("/./dev/foo"));
104 assert_se( is_device_path("/./dev/./foo"));
105 assert_se(!is_device_path("/./dev./foo"));
106 assert_se(!is_device_path("//dev"));
107 assert_se(!is_device_path("///dev"));
108 assert_se(!is_device_path("/dev/"));
109 assert_se(!is_device_path("///dev/"));
110 assert_se(!is_device_path("/./dev/"));
111 assert_se(!is_device_path("/../dev/"));
112 assert_se( is_device_path("/dev/sda"));
113 assert_se( is_device_path("/dev/sda5"));
114 assert_se( is_device_path("/dev/sda5b3"));
115 assert_se( is_device_path("/dev/sda5b3/idontexit"));
116 assert_se(!is_device_path("/../dev/sda"));
117 assert_se(!is_device_path("/../../dev/sda5"));
118 assert_se(!is_device_path("/../../../dev/sda5b3"));
119 assert_se(!is_device_path("/.././.././dev/sda5b3/idontexit"));
120 assert_se(!is_device_path("/sys"));
121 assert_se(!is_device_path("/sys/"));
122 assert_se(!is_device_path("/./sys"));
123 assert_se(!is_device_path("/./sys/."));
124 assert_se(!is_device_path("/./sys."));
125 assert_se( is_device_path("/./sys/foo"));
126 assert_se( is_device_path("/./sys/./foo"));
127 assert_se(!is_device_path("/./sys./foo"));
128 assert_se( is_device_path("/sys/what"));
129 assert_se( is_device_path("/sys/something/.."));
130 assert_se( is_device_path("/sys/something/../"));
131 assert_se(!is_device_path("/sys////"));
132 assert_se(!is_device_path("/sys////."));
133 assert_se( is_device_path("/sys/.."));
134 assert_se( is_device_path("/sys/../"));
135 assert_se(!is_device_path("/usr/../dev/sda"));
136 }
137
138 static void test_path_simplify_one(const char *in, const char *out, PathSimplifyFlags flags) {
139 char *p;
140
141 p = strdupa_safe(in);
142 path_simplify_full(p, flags);
143 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in, p, out);
144 ASSERT_STREQ(p, out);
145 }
146
147 TEST(path_simplify) {
148 _cleanup_free_ char *hoge = NULL, *hoge_out = NULL;
149 char foo[NAME_MAX * 2];
150
151 test_path_simplify_one("", "", 0);
152 test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc", 0);
153 test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc", 0);
154 test_path_simplify_one("///", "/", 0);
155 test_path_simplify_one("///", "/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
156 test_path_simplify_one("///.//", "/", 0);
157 test_path_simplify_one("///.//.///", "/", 0);
158 test_path_simplify_one("////.././///../.", "/", 0);
159 test_path_simplify_one(".", ".", 0);
160 test_path_simplify_one("./", ".", 0);
161 test_path_simplify_one("./", "./", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
162 test_path_simplify_one(".///.//./.", ".", 0);
163 test_path_simplify_one(".///.//././/", ".", 0);
164 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
165 "/aaa/.bbb/../c./d.dd/..eeee", 0);
166 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
167 "/aaa/.bbb/../c./d.dd/..eeee/..", 0);
168 test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
169 "aaa/.bbb/../c./d.dd/..eeee/..", 0);
170 test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
171 "../aaa/.bbb/../c./d.dd/..eeee/..", 0);
172 test_path_simplify_one("abc///", "abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
173
174 test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
175 test_path_simplify_one("/../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
176 test_path_simplify_one("/../abc///", "/abc", 0);
177 test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
178 test_path_simplify_one("/../abc///..", "/abc/..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
179 test_path_simplify_one("/../abc///../", "/abc/../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
180 test_path_simplify_one("/../abc///../", "/abc/..", 0);
181
182 test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
183 test_path_simplify_one("/../../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
184 test_path_simplify_one("/../../abc///", "/abc", 0);
185 test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
186 test_path_simplify_one("/../../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
187 test_path_simplify_one("/../../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
188 test_path_simplify_one("/../../abc///../../", "/abc/../..", 0);
189
190 test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
191 test_path_simplify_one("/.././../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
192 test_path_simplify_one("/.././../abc///", "/abc", 0);
193 test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
194 test_path_simplify_one("/.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
195 test_path_simplify_one("/.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
196 test_path_simplify_one("/.././../abc///../../", "/abc/../..", 0);
197
198 test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
199 test_path_simplify_one("/./.././../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
200 test_path_simplify_one("/./.././../abc///", "/abc", 0);
201 test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
202 test_path_simplify_one("/./.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
203 test_path_simplify_one("/./.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
204 test_path_simplify_one("/./.././../abc///../../", "/abc/../..", 0);
205
206 test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
207 test_path_simplify_one("/.../abc///", "/.../abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
208 test_path_simplify_one("/.../abc///", "/.../abc", 0);
209 test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
210 test_path_simplify_one("/.../abc///...", "/.../abc/...", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
211 test_path_simplify_one("/.../abc///.../", "/.../abc/.../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
212 test_path_simplify_one("/.../abc///.../", "/.../abc/...", 0);
213
214 memset(foo, 'a', sizeof(foo) -1);
215 char_array_0(foo);
216
217 test_path_simplify_one(foo, foo, 0);
218
219 hoge = strjoin("/", foo);
220 assert_se(hoge);
221 test_path_simplify_one(hoge, hoge, 0);
222 hoge = mfree(hoge);
223
224 hoge = strjoin("a////.//././//./b///././/./c/////././//./", foo, "//.//////d/e/.//f/");
225 assert_se(hoge);
226
227 hoge_out = strjoin("a/b/c/", foo, "//.//////d/e/.//f/");
228 assert_se(hoge_out);
229
230 test_path_simplify_one(hoge, hoge_out, 0);
231 }
232
233 static void test_path_compare_one(const char *a, const char *b, int expected) {
234 int r;
235
236 assert_se(path_compare(a, a) == 0);
237 assert_se(path_compare(b, b) == 0);
238
239 r = path_compare(a, b);
240 assert_se((r > 0) == (expected > 0) && (r < 0) == (expected < 0));
241 r = path_compare(b, a);
242 assert_se((r < 0) == (expected > 0) && (r > 0) == (expected < 0));
243
244 assert_se(path_equal(a, a) == 1);
245 assert_se(path_equal(b, b) == 1);
246 assert_se(path_equal(a, b) == (expected == 0));
247 assert_se(path_equal(b, a) == (expected == 0));
248 }
249
250 TEST(path_compare) {
251 test_path_compare_one("/goo", "/goo", 0);
252 test_path_compare_one("/goo", "/goo", 0);
253 test_path_compare_one("//goo", "/goo", 0);
254 test_path_compare_one("//goo/////", "/goo", 0);
255 test_path_compare_one("goo/////", "goo", 0);
256 test_path_compare_one("/goo/boo", "/goo//boo", 0);
257 test_path_compare_one("//goo/boo", "/goo/boo//", 0);
258 test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
259 test_path_compare_one("/.", "//.///", 0);
260 test_path_compare_one("/x", "x/", 1);
261 test_path_compare_one("x/", "/", -1);
262 test_path_compare_one("/x/./y", "x/y", 1);
263 test_path_compare_one("/x/./y", "/x/y", 0);
264 test_path_compare_one("/x/./././y", "/x/y/././.", 0);
265 test_path_compare_one("./x/./././y", "./x/y/././.", 0);
266 test_path_compare_one(".", "./.", 0);
267 test_path_compare_one(".", "././.", 0);
268 test_path_compare_one("./..", ".", 1);
269 test_path_compare_one("x/.y", "x/y", -1);
270 test_path_compare_one("foo", "/foo", -1);
271 test_path_compare_one("/foo", "/foo/bar", -1);
272 test_path_compare_one("/foo/aaa", "/foo/b", -1);
273 test_path_compare_one("/foo/aaa", "/foo/b/a", -1);
274 test_path_compare_one("/foo/a", "/foo/aaa", -1);
275 test_path_compare_one("/foo/a/b", "/foo/aaa", -1);
276 }
277
278 static void test_path_compare_filename_one(const char *a, const char *b, int expected) {
279 int r;
280
281 assert_se(path_compare_filename(a, a) == 0);
282 assert_se(path_compare_filename(b, b) == 0);
283
284 r = path_compare_filename(a, b);
285 assert_se((r > 0) == (expected > 0) && (r < 0) == (expected < 0));
286 r = path_compare_filename(b, a);
287 assert_se((r < 0) == (expected > 0) && (r > 0) == (expected < 0));
288
289 assert_se(path_equal_filename(a, a) == 1);
290 assert_se(path_equal_filename(b, b) == 1);
291 assert_se(path_equal_filename(a, b) == (expected == 0));
292 assert_se(path_equal_filename(b, a) == (expected == 0));
293 }
294
295 TEST(path_compare_filename) {
296 test_path_compare_filename_one("/goo", "/goo", 0);
297 test_path_compare_filename_one("/goo", "/goo", 0);
298 test_path_compare_filename_one("//goo", "/goo", 0);
299 test_path_compare_filename_one("//goo/////", "/goo", 0);
300 test_path_compare_filename_one("goo/////", "goo", 0);
301 test_path_compare_filename_one("/goo/boo", "/goo//boo", 0);
302 test_path_compare_filename_one("//goo/boo", "/goo/boo//", 0);
303 test_path_compare_filename_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
304 test_path_compare_filename_one("/.", "//.///", -1);
305 test_path_compare_filename_one("/x", "x/", 0);
306 test_path_compare_filename_one("x/", "/", 1);
307 test_path_compare_filename_one("/x/./y", "x/y", 0);
308 test_path_compare_filename_one("/x/./y", "/x/y", 0);
309 test_path_compare_filename_one("/x/./././y", "/x/y/././.", 0);
310 test_path_compare_filename_one("./x/./././y", "./x/y/././.", 0);
311 test_path_compare_filename_one(".", "./.", -1);
312 test_path_compare_filename_one(".", "././.", -1);
313 test_path_compare_filename_one("./..", ".", 1);
314 test_path_compare_filename_one("x/.y", "x/y", -1);
315 test_path_compare_filename_one("foo", "/foo", 0);
316 test_path_compare_filename_one("/foo", "/foo/bar", 1);
317 test_path_compare_filename_one("/foo/aaa", "/foo/b", -1);
318 test_path_compare_filename_one("/foo/aaa", "/foo/b/a", 1);
319 test_path_compare_filename_one("/foo/a", "/foo/aaa", -1);
320 test_path_compare_filename_one("/foo/a/b", "/foo/aaa", 1);
321 test_path_compare_filename_one("/a/c", "/b/c", 0);
322 test_path_compare_filename_one("/a", "/a", 0);
323 test_path_compare_filename_one("/a/b", "/a/c", -1);
324 test_path_compare_filename_one("/b", "/c", -1);
325 }
326
327 TEST(path_equal_root) {
328 /* Nail down the details of how path_equal("/", ...) works. */
329
330 assert_se(path_equal("/", "/"));
331 assert_se(path_equal("/", "//"));
332
333 assert_se(path_equal("/", "/./"));
334 assert_se(!path_equal("/", "/../"));
335
336 assert_se(!path_equal("/", "/.../"));
337
338 /* Make sure that files_same works as expected. */
339
340 assert_se(inode_same("/", "/", 0) > 0);
341 assert_se(inode_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
342 assert_se(inode_same("/", "//", 0) > 0);
343 assert_se(inode_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
344
345 assert_se(inode_same("/", "/./", 0) > 0);
346 assert_se(inode_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
347 assert_se(inode_same("/", "/../", 0) > 0);
348 assert_se(inode_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
349
350 assert_se(inode_same("/", "/.../", 0) == -ENOENT);
351 assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
352
353 /* The same for path_equal_or_files_same. */
354
355 assert_se(path_equal_or_inode_same("/", "/", 0));
356 assert_se(path_equal_or_inode_same("/", "/", AT_SYMLINK_NOFOLLOW));
357 assert_se(path_equal_or_inode_same("/", "//", 0));
358 assert_se(path_equal_or_inode_same("/", "//", AT_SYMLINK_NOFOLLOW));
359
360 assert_se(path_equal_or_inode_same("/", "/./", 0));
361 assert_se(path_equal_or_inode_same("/", "/./", AT_SYMLINK_NOFOLLOW));
362 assert_se(path_equal_or_inode_same("/", "/../", 0));
363 assert_se(path_equal_or_inode_same("/", "/../", AT_SYMLINK_NOFOLLOW));
364
365 assert_se(!path_equal_or_inode_same("/", "/.../", 0));
366 assert_se(!path_equal_or_inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
367 }
368
369 TEST(find_executable_full) {
370 char *p, *bp;
371 _cleanup_free_ char *test_file_name = NULL;
372 _cleanup_close_ int fd = -EBADF;
373 char fn[] = "/tmp/test-XXXXXX";
374
375 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
376 puts(p);
377 ASSERT_OK(path_extract_filename(p, &bp));
378 ASSERT_STREQ(bp, "sh");
379 free(p);
380 free(bp);
381
382 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
383 puts(p);
384 ASSERT_OK(path_extract_filename(p, &bp));
385 ASSERT_STREQ(bp, "sh");
386 free(p);
387 free(bp);
388
389 _cleanup_free_ char *oldpath = NULL;
390 p = getenv("PATH");
391 if (p)
392 assert_se(oldpath = strdup(p));
393
394 assert_se(unsetenv("PATH") == 0);
395
396 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
397 puts(p);
398 ASSERT_OK(path_extract_filename(p, &bp));
399 ASSERT_STREQ(bp, "sh");
400 free(p);
401 free(bp);
402
403 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
404 puts(p);
405 ASSERT_OK(path_extract_filename(p, &bp));
406 ASSERT_STREQ(bp, "sh");
407 free(p);
408 free(bp);
409
410 if (oldpath)
411 ASSERT_OK_ERRNO(setenv("PATH", oldpath, true));
412
413 ASSERT_OK(fd = mkostemp_safe(fn));
414 ASSERT_OK_ERRNO(fchmod(fd, 0755));
415
416 ASSERT_OK(path_extract_filename(fn, &test_file_name));
417
418 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == 0);
419 puts(p);
420 ASSERT_STREQ(p, fn);
421 free(p);
422
423 (void) unlink(fn);
424 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == -ENOENT);
425 }
426
427 TEST(find_executable) {
428 char *p;
429 _cleanup_free_ char *bp = NULL;
430
431 assert_se(find_executable("/bin/sh", &p) == 0);
432 puts(p);
433 assert_se(path_equal(p, "/bin/sh"));
434 free(p);
435
436 assert_se(find_executable(saved_argv[0], &p) == 0);
437 puts(p);
438 assert_se(endswith(p, "/test-path-util"));
439 assert_se(path_is_absolute(p));
440 free(p);
441
442 assert_se(find_executable("sh", &p) == 0);
443 puts(p);
444 assert_se(endswith(p, "/sh"));
445 assert_se(path_is_absolute(p));
446 free(p);
447
448 assert_se(find_executable("/bin/touch", &p) == 0);
449 ASSERT_STREQ(p, "/bin/touch");
450 free(p);
451
452 assert_se(find_executable("touch", &p) == 0);
453 assert_se(path_is_absolute(p));
454 ASSERT_OK(path_extract_filename(p, &bp));
455 ASSERT_STREQ(bp, "touch");
456 free(p);
457
458 assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT);
459 assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT);
460 assert_se(find_executable("/proc/filesystems", &p) == -EACCES);
461 }
462
463 static void test_find_executable_exec_one(const char *path) {
464 _cleanup_free_ char *t = NULL;
465 _cleanup_close_ int fd = -EBADF;
466 pid_t pid;
467 int r;
468
469 r = find_executable_full(path, NULL, NULL, false, &t, &fd);
470
471 log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd);
472
473 assert_se(fd > STDERR_FILENO);
474 assert_se(path_is_absolute(t));
475 if (path_is_absolute(path))
476 ASSERT_STREQ(t, path);
477
478 pid = fork();
479 assert_se(pid >= 0);
480 if (pid == 0) {
481 r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL));
482 log_error_errno(r, "[f]execve: %m");
483 _exit(EXIT_FAILURE);
484 }
485
486 assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0);
487 }
488
489 TEST(find_executable_exec) {
490 test_find_executable_exec_one("touch");
491 test_find_executable_exec_one("/bin/touch");
492
493 _cleanup_free_ char *script = NULL;
494 assert_se(get_testdata_dir("test-path-util/script.sh", &script) >= 0);
495 test_find_executable_exec_one(script);
496 }
497
498 TEST(prefixes) {
499 static const char* const values[] = {
500 "/a/b/c/d",
501 "/a/b/c",
502 "/a/b",
503 "/a",
504 "",
505 NULL
506 };
507 unsigned i;
508 char s[PATH_MAX];
509 bool b;
510
511 i = 0;
512 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
513 log_error("---%s---", s);
514 ASSERT_STREQ(s, values[i++]);
515 }
516 ASSERT_NULL(values[i]);
517
518 i = 1;
519 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
520 log_error("---%s---", s);
521 ASSERT_STREQ(s, values[i++]);
522 }
523 ASSERT_NULL(values[i]);
524
525 i = 0;
526 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
527 ASSERT_STREQ(s, values[i++]);
528 ASSERT_NULL(values[i]);
529
530 i = 1;
531 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
532 ASSERT_STREQ(s, values[i++]);
533 ASSERT_NULL(values[i]);
534
535 PATH_FOREACH_PREFIX(s, "////")
536 assert_not_reached();
537
538 b = false;
539 PATH_FOREACH_PREFIX_MORE(s, "////") {
540 assert_se(!b);
541 ASSERT_STREQ(s, "");
542 b = true;
543 }
544 assert_se(b);
545
546 PATH_FOREACH_PREFIX(s, "")
547 assert_not_reached();
548
549 b = false;
550 PATH_FOREACH_PREFIX_MORE(s, "") {
551 assert_se(!b);
552 ASSERT_STREQ(s, "");
553 b = true;
554 }
555 }
556
557 TEST(path_join) {
558 #define test_join(expected, ...) { \
559 _cleanup_free_ char *z = NULL; \
560 z = path_join(__VA_ARGS__); \
561 log_debug("got \"%s\", expected \"%s\"", z, expected); \
562 ASSERT_STREQ(z, expected); \
563 }
564
565 test_join("/root/a/b/c", "/root", "/a/b", "/c");
566 test_join("/root/a/b/c", "/root", "a/b", "c");
567 test_join("/root/a/b/c", "/root", "/a/b", "c");
568 test_join("/root/c", "/root", "/", "c");
569 test_join("/root/", "/root", "/", NULL);
570
571 test_join("/a/b/c", "", "/a/b", "/c");
572 test_join("a/b/c", "", "a/b", "c");
573 test_join("/a/b/c", "", "/a/b", "c");
574 test_join("/c", "", "/", "c");
575 test_join("/", "", "/", NULL);
576
577 test_join("/a/b/c", NULL, "/a/b", "/c");
578 test_join("a/b/c", NULL, "a/b", "c");
579 test_join("/a/b/c", NULL, "/a/b", "c");
580 test_join("/c", NULL, "/", "c");
581 test_join("/", NULL, "/", NULL);
582
583 test_join("", "", NULL);
584 test_join("", NULL, "");
585 test_join("", NULL, NULL);
586
587 test_join("foo/bar", "foo", "bar");
588 test_join("foo/bar", "", "foo", "bar");
589 test_join("foo/bar", NULL, "foo", NULL, "bar");
590 test_join("foo/bar", "", "foo", "", "bar", "");
591 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
592
593 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
594 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
595 test_join("foo/bar/baz", "foo", "bar", "baz");
596 test_join("foo/bar/baz", "foo/", "bar", "/baz");
597 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
598 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
599 }
600
601 TEST(path_extend) {
602 _cleanup_free_ char *p = NULL;
603
604 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
605 ASSERT_STREQ(p, "foo/bar/baz");
606
607 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
608 ASSERT_STREQ(p, "foo/bar/baz/foo/bar/baz");
609
610 p = mfree(p);
611 assert_se(path_extend(&p, "foo") == p);
612 ASSERT_STREQ(p, "foo");
613
614 assert_se(path_extend(&p, "/foo") == p);
615 ASSERT_STREQ(p, "foo/foo");
616 assert_se(path_extend(&p, "/waaaah/wahhh//") == p);
617 ASSERT_STREQ(p, "foo/foo/waaaah/wahhh//"); /* path_extend() does not drop redundant slashes */
618 assert_se(path_extend(&p, "/aaa/bbb/") == p);
619 ASSERT_STREQ(p, "foo/foo/waaaah/wahhh///aaa/bbb/"); /* but not add an extra slash */
620
621 assert_se(free_and_strdup(&p, "/") >= 0);
622 assert_se(path_extend(&p, "foo") == p);
623 ASSERT_STREQ(p, "/foo");
624 }
625
626 TEST(fsck_exists) {
627 /* Ensure we use a sane default for PATH. */
628 assert_se(unsetenv("PATH") == 0);
629
630 /* We might or might not find one of these, so keep the test lax. */
631 assert_se(fsck_exists_for_fstype("minix") >= 0);
632
633 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
634 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
635 }
636
637 static void test_path_make_relative_one(const char *from, const char *to, const char *expected) {
638 _cleanup_free_ char *z = NULL;
639 int r;
640
641 log_info("/* %s(%s, %s) */", __func__, from, to);
642
643 r = path_make_relative(from, to, &z);
644 assert_se((r >= 0) == !!expected);
645 ASSERT_STREQ(z, expected);
646 }
647
648 TEST(path_make_relative) {
649 test_path_make_relative_one("some/relative/path", "/some/path", NULL);
650 test_path_make_relative_one("/some/path", "some/relative/path", NULL);
651 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL);
652
653 test_path_make_relative_one("/", "/", ".");
654 test_path_make_relative_one("/", "/some/path", "some/path");
655 test_path_make_relative_one("/some/path", "/some/path", ".");
656 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
657 test_path_make_relative_one("/some/path", "/", "../..");
658 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
659 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
660 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
661 }
662
663 static void test_path_make_relative_parent_one(const char *from, const char *to, const char *expected) {
664 _cleanup_free_ char *z = NULL;
665 int r;
666
667 log_info("/* %s(%s, %s) */", __func__, from, to);
668
669 r = path_make_relative_parent(from, to, &z);
670 assert_se((r >= 0) == !!expected);
671 ASSERT_STREQ(z, expected);
672 }
673
674 TEST(path_make_relative_parent) {
675 test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL);
676 test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL);
677 test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL);
678 test_path_make_relative_parent_one("/", "/aaa", NULL);
679
680 test_path_make_relative_parent_one("/hoge", "/", ".");
681 test_path_make_relative_parent_one("/hoge", "/some/path", "some/path");
682 test_path_make_relative_parent_one("/some/path/hoge", "/some/path", ".");
683 test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir");
684 test_path_make_relative_parent_one("/some/path/hoge", "/", "../..");
685 test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path");
686 test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path");
687 test_path_make_relative_parent_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//hoge", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
688 }
689
690 TEST(path_strv_resolve) {
691 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
692 _cleanup_strv_free_ char **search_dirs = NULL;
693 _cleanup_strv_free_ char **absolute_dirs = NULL;
694
695 ASSERT_NOT_NULL(mkdtemp(tmp_dir));
696
697 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
698 assert_se(search_dirs);
699 STRV_FOREACH(d, search_dirs) {
700 char *p = path_join(tmp_dir, *d);
701 assert_se(p);
702 assert_se(strv_push(&absolute_dirs, p) == 0);
703 }
704
705 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
706 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
707 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
708
709 path_strv_resolve(search_dirs, tmp_dir);
710 ASSERT_STREQ(search_dirs[0], "/dir1");
711 ASSERT_STREQ(search_dirs[1], "/dir2");
712 ASSERT_STREQ(search_dirs[2], "/dir2");
713
714 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
715 }
716
717 static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) {
718 const char *p, *q;
719
720 log_debug("/* %s(%s, %s) */", __func__, path, prefix);
721
722 p = path_startswith(path, prefix);
723 ASSERT_STREQ(p, expected);
724 if (p) {
725 q = strjoina(skipped, p);
726 ASSERT_STREQ(q, path);
727 assert_se(p == path + strlen(skipped));
728 }
729 }
730
731 TEST(path_startswith) {
732 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
733 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
734 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
735 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
736 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
737 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
738 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
739 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
740 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
741 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
742
743 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo", "/foo/./", "bar///barfoo/./.");
744 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/", "/foo/./", "bar///barfoo/./.");
745 test_path_startswith_one("/foo/./bar///barfoo/./.", "/", "/", "foo/./bar///barfoo/./.");
746 test_path_startswith_one("/foo/./bar///barfoo/./.", "////", "/", "foo/./bar///barfoo/./.");
747 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo//bar/////barfoo///", "/foo/./bar///barfoo/./.", "");
748 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo////", "/foo/./bar///barfoo/./.", "");
749 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar///barfoo/", "/foo/./bar///barfoo/./.", "");
750 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo////bar/barfoo/", "/foo/./bar///barfoo/./.", "");
751 test_path_startswith_one("/foo/./bar///barfoo/./.", "////foo/bar/barfoo/", "/foo/./bar///barfoo/./.", "");
752 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo", "/foo/./bar///barfoo/./.", "");
753
754 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL, NULL);
755 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL, NULL);
756 test_path_startswith_one("/foo/bar/barfoo/", "", NULL, NULL);
757 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL, NULL);
758 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL, NULL);
759 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL, NULL);
760 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL, NULL);
761 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL, NULL);
762 }
763
764 static void test_path_startswith_return_leading_slash_one(const char *path, const char *prefix, const char *expected) {
765 const char *p;
766
767 log_debug("/* %s(%s, %s) */", __func__, path, prefix);
768
769 p = path_startswith_full(path, prefix, PATH_STARTSWITH_RETURN_LEADING_SLASH);
770 ASSERT_STREQ(p, expected);
771 }
772
773 TEST(path_startswith_return_leading_slash) {
774 test_path_startswith_return_leading_slash_one("/foo/bar", "/", "/foo/bar");
775 test_path_startswith_return_leading_slash_one("/foo/bar", "/foo", "/bar");
776 test_path_startswith_return_leading_slash_one("/foo/bar", "/foo/bar", NULL);
777 test_path_startswith_return_leading_slash_one("/foo/bar/", "/foo/bar", "/");
778 }
779
780 static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
781 _cleanup_free_ char *s = NULL;
782
783 assert_se(s = path_join(r, p));
784 assert_se(path_equal(s, expected));
785
786 _cleanup_free_ char *t = path_join(r, p);
787 assert_se(t);
788 assert_se(path_equal(t, expected));
789 }
790
791 TEST(prefix_root) {
792 test_prefix_root_one("/", "/foo", "/foo");
793 test_prefix_root_one(NULL, "/foo", "/foo");
794 test_prefix_root_one("", "/foo", "/foo");
795 test_prefix_root_one("///", "/foo", "/foo");
796 test_prefix_root_one("/", "////foo", "/foo");
797 test_prefix_root_one(NULL, "////foo", "/foo");
798 test_prefix_root_one("/", "foo", "/foo");
799 test_prefix_root_one("", "foo", "foo");
800 test_prefix_root_one(NULL, "foo", "foo");
801
802 test_prefix_root_one("/foo", "/bar", "/foo/bar");
803 test_prefix_root_one("/foo", "bar", "/foo/bar");
804 test_prefix_root_one("foo", "bar", "foo/bar");
805 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
806 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
807 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
808 }
809
810 TEST(file_in_same_dir) {
811 char *t;
812
813 assert_se(file_in_same_dir("/", "a", &t) == -EADDRNOTAVAIL);
814
815 assert_se(file_in_same_dir("/", "/a", &t) >= 0);
816 ASSERT_STREQ(t, "/a");
817 free(t);
818
819 assert_se(file_in_same_dir("", "a", &t) == -EINVAL);
820
821 assert_se(file_in_same_dir("a/", "x", &t) >= 0);
822 ASSERT_STREQ(t, "x");
823 free(t);
824
825 assert_se(file_in_same_dir("bar/foo", "bar", &t) >= 0);
826 ASSERT_STREQ(t, "bar/bar");
827 free(t);
828 }
829
830 static void test_path_find_first_component_one(
831 const char *path,
832 bool accept_dot_dot,
833 char **expected,
834 int ret) {
835
836 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
837
838 for (const char *p = path;;) {
839 const char *e;
840 int r;
841
842 r = path_find_first_component(&p, accept_dot_dot, &e);
843 if (r <= 0) {
844 if (r == 0) {
845 if (path) {
846 assert_se(p == path + strlen_ptr(path));
847 assert_se(isempty(p));
848 } else
849 assert_se(!p);
850 assert_se(!e);
851 }
852 assert_se(r == ret);
853 assert_se(strv_isempty(expected));
854 return;
855 }
856
857 assert_se(e);
858 assert_se(strcspn(e, "/") == (size_t) r);
859 assert_se(strlen_ptr(*expected) == (size_t) r);
860 assert_se(strneq(e, *expected++, r));
861
862 assert_se(p);
863 log_debug("p=%s", p);
864 if (!isempty(*expected))
865 assert_se(startswith(p, *expected));
866 else if (ret >= 0) {
867 assert_se(p == path + strlen_ptr(path));
868 assert_se(isempty(p));
869 }
870 }
871 }
872
873 TEST(path_find_first_component) {
874 _cleanup_free_ char *hoge = NULL;
875 char foo[NAME_MAX * 2];
876
877 test_path_find_first_component_one(NULL, false, NULL, 0);
878 test_path_find_first_component_one("", false, NULL, 0);
879 test_path_find_first_component_one("/", false, NULL, 0);
880 test_path_find_first_component_one(".", false, NULL, 0);
881 test_path_find_first_component_one("./", false, NULL, 0);
882 test_path_find_first_component_one("./.", false, NULL, 0);
883 test_path_find_first_component_one("..", false, NULL, -EINVAL);
884 test_path_find_first_component_one("/..", false, NULL, -EINVAL);
885 test_path_find_first_component_one("./..", false, NULL, -EINVAL);
886 test_path_find_first_component_one("////./././//.", false, NULL, 0);
887 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
888 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
889 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL);
890 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL);
891 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
892
893 test_path_find_first_component_one(NULL, true, NULL, 0);
894 test_path_find_first_component_one("", true, NULL, 0);
895 test_path_find_first_component_one("/", true, NULL, 0);
896 test_path_find_first_component_one(".", true, NULL, 0);
897 test_path_find_first_component_one("./", true, NULL, 0);
898 test_path_find_first_component_one("./.", true, NULL, 0);
899 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
900 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
901 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
902 test_path_find_first_component_one("////./././//.", true, NULL, 0);
903 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
904 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
905 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
906 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
907 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
908
909 memset(foo, 'a', sizeof(foo) -1);
910 char_array_0(foo);
911
912 test_path_find_first_component_one(foo, false, NULL, -EINVAL);
913 test_path_find_first_component_one(foo, true, NULL, -EINVAL);
914
915 hoge = strjoin("a/b/c/", foo, "//d/e/.//f/");
916 assert_se(hoge);
917
918 test_path_find_first_component_one(hoge, false, STRV_MAKE("a", "b", "c"), -EINVAL);
919 test_path_find_first_component_one(hoge, true, STRV_MAKE("a", "b", "c"), -EINVAL);
920 }
921
922 static void test_path_find_last_component_one(
923 const char *path,
924 bool accept_dot_dot,
925 char **expected,
926 int ret) {
927
928 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
929
930 for (const char *next = NULL;;) {
931 const char *e;
932 int r;
933
934 r = path_find_last_component(path, accept_dot_dot, &next, &e);
935 if (r <= 0) {
936 if (r == 0) {
937 assert_se(next == path);
938 assert_se(!e);
939 }
940 assert_se(r == ret);
941 assert_se(strv_isempty(expected));
942 return;
943 }
944
945 assert_se(e);
946 assert_se(strcspn(e, "/") == (size_t) r);
947 assert_se(strlen_ptr(*expected) == (size_t) r);
948 assert_se(strneq(e, *expected++, r));
949
950 assert_se(next);
951 log_debug("path=%s\nnext=%s", path, next);
952 if (!isempty(*expected)) {
953 assert_se(next < path + strlen(path));
954 assert_se(next >= path + strlen(*expected));
955 assert_se(startswith(next - strlen(*expected), *expected));
956 } else if (ret >= 0)
957 assert_se(next == path);
958 }
959 }
960
961 TEST(path_find_last_component) {
962 _cleanup_free_ char *hoge = NULL;
963 char foo[NAME_MAX * 2];
964
965 test_path_find_last_component_one(NULL, false, NULL, 0);
966 test_path_find_last_component_one("", false, NULL, 0);
967 test_path_find_last_component_one("/", false, NULL, 0);
968 test_path_find_last_component_one(".", false, NULL, 0);
969 test_path_find_last_component_one("./", false, NULL, 0);
970 test_path_find_last_component_one("./.", false, NULL, 0);
971 test_path_find_last_component_one("..", false, NULL, -EINVAL);
972 test_path_find_last_component_one("/..", false, NULL, -EINVAL);
973 test_path_find_last_component_one("./..", false, NULL, -EINVAL);
974 test_path_find_last_component_one("////./././//.", false, NULL, 0);
975 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
976 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
977 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL);
978 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL);
979
980 test_path_find_last_component_one(NULL, true, NULL, 0);
981 test_path_find_last_component_one("", true, NULL, 0);
982 test_path_find_last_component_one("/", true, NULL, 0);
983 test_path_find_last_component_one(".", true, NULL, 0);
984 test_path_find_last_component_one("./", true, NULL, 0);
985 test_path_find_last_component_one("./.", true, NULL, 0);
986 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
987 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
988 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
989 test_path_find_last_component_one("////./././//.", true, NULL, 0);
990 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
991 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
992 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
993 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
994
995 memset(foo, 'a', sizeof(foo) -1);
996 char_array_0(foo);
997
998 test_path_find_last_component_one(foo, false, NULL, -EINVAL);
999 test_path_find_last_component_one(foo, true, NULL, -EINVAL);
1000
1001 hoge = strjoin(foo, "/a/b/c/");
1002 assert_se(hoge);
1003
1004 test_path_find_last_component_one(hoge, false, STRV_MAKE("c", "b", "a"), -EINVAL);
1005 test_path_find_last_component_one(hoge, true, STRV_MAKE("c", "b", "a"), -EINVAL);
1006 }
1007
1008 TEST(last_path_component) {
1009 ASSERT_NULL(last_path_component(NULL));
1010 ASSERT_STREQ(last_path_component("a/b/c"), "c");
1011 ASSERT_STREQ(last_path_component("a/b/c/"), "c/");
1012 ASSERT_STREQ(last_path_component("/"), "/");
1013 ASSERT_STREQ(last_path_component("//"), "/");
1014 ASSERT_STREQ(last_path_component("///"), "/");
1015 ASSERT_STREQ(last_path_component("."), ".");
1016 ASSERT_STREQ(last_path_component("./."), ".");
1017 ASSERT_STREQ(last_path_component("././"), "./");
1018 ASSERT_STREQ(last_path_component("././/"), ".//");
1019 ASSERT_STREQ(last_path_component("/foo/a"), "a");
1020 ASSERT_STREQ(last_path_component("/foo/a/"), "a/");
1021 ASSERT_STREQ(last_path_component(""), "");
1022 ASSERT_STREQ(last_path_component("a"), "a");
1023 ASSERT_STREQ(last_path_component("a/"), "a/");
1024 ASSERT_STREQ(last_path_component("/a"), "a");
1025 ASSERT_STREQ(last_path_component("/a/"), "a/");
1026 }
1027
1028 static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
1029 _cleanup_free_ char *k = NULL;
1030 int r;
1031
1032 r = path_extract_filename(input, &k);
1033 log_info("%s → %s/%s [expected: %s/%s]",
1034 strnull(input),
1035 strnull(k), r < 0 ? STRERROR(r) : "-",
1036 strnull(output), ret < 0 ? STRERROR(ret) : "-");
1037 ASSERT_STREQ(k, output);
1038 assert_se(r == ret);
1039 }
1040
1041 TEST(path_extract_filename) {
1042 test_path_extract_filename_one(NULL, NULL, -EINVAL);
1043 test_path_extract_filename_one("a/b/c", "c", 0);
1044 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY);
1045 test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL);
1046 test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL);
1047 test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL);
1048 test_path_extract_filename_one("/.", NULL, -EADDRNOTAVAIL);
1049 test_path_extract_filename_one(".", NULL, -EADDRNOTAVAIL);
1050 test_path_extract_filename_one("./", NULL, -EADDRNOTAVAIL);
1051 test_path_extract_filename_one("./.", NULL, -EADDRNOTAVAIL);
1052 test_path_extract_filename_one("././", NULL, -EADDRNOTAVAIL);
1053 test_path_extract_filename_one("././/", NULL, -EADDRNOTAVAIL);
1054 test_path_extract_filename_one("/foo/a", "a", 0);
1055 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY);
1056 test_path_extract_filename_one("", NULL, -EINVAL);
1057 test_path_extract_filename_one("a", "a", 0);
1058 test_path_extract_filename_one("a/", "a", O_DIRECTORY);
1059 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY);
1060 test_path_extract_filename_one("/a", "a", 0);
1061 test_path_extract_filename_one("/a/", "a", O_DIRECTORY);
1062 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY);
1063 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY);
1064 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY);
1065 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY);
1066 test_path_extract_filename_one("xx/..", NULL, -EINVAL);
1067 test_path_extract_filename_one("..", NULL, -EINVAL);
1068 test_path_extract_filename_one("/..", NULL, -EINVAL);
1069 test_path_extract_filename_one("../", NULL, -EINVAL);
1070 }
1071
1072 static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
1073 _cleanup_free_ char *k = NULL;
1074 int r;
1075
1076 r = path_extract_directory(input, &k);
1077 log_info("%s → %s/%s [expected: %s/%s]",
1078 strnull(input),
1079 strnull(k), r < 0 ? STRERROR(r) : "-",
1080 strnull(output), STRERROR(ret));
1081 ASSERT_STREQ(k, output);
1082 assert_se(r == ret);
1083
1084 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
1085 * joined parts are identical to the original again */
1086 if (r >= 0) {
1087 _cleanup_free_ char *f = NULL;
1088
1089 r = path_extract_filename(input, &f);
1090 if (r >= 0) {
1091 _cleanup_free_ char *j = NULL;
1092
1093 assert_se(j = path_join(k, f));
1094 assert_se(path_equal(input, j));
1095 }
1096 }
1097 }
1098
1099 TEST(path_extract_directory) {
1100 test_path_extract_directory_one(NULL, NULL, -EINVAL);
1101 test_path_extract_directory_one("a/b/c", "a/b", 0);
1102 test_path_extract_directory_one("a/b/c/", "a/b", 0);
1103 test_path_extract_directory_one("/", NULL, -EADDRNOTAVAIL);
1104 test_path_extract_directory_one("//", NULL, -EADDRNOTAVAIL);
1105 test_path_extract_directory_one("///", NULL, -EADDRNOTAVAIL);
1106 test_path_extract_directory_one("/.", NULL, -EADDRNOTAVAIL);
1107 test_path_extract_directory_one(".", NULL, -EADDRNOTAVAIL);
1108 test_path_extract_directory_one("./", NULL, -EADDRNOTAVAIL);
1109 test_path_extract_directory_one("./.", NULL, -EADDRNOTAVAIL);
1110 test_path_extract_directory_one("././", NULL, -EADDRNOTAVAIL);
1111 test_path_extract_directory_one("././/", NULL, -EADDRNOTAVAIL);
1112 test_path_extract_directory_one("/foo/a", "/foo", 0);
1113 test_path_extract_directory_one("/foo/a/", "/foo", 0);
1114 test_path_extract_directory_one("", NULL, -EINVAL);
1115 test_path_extract_directory_one("a", NULL, -EDESTADDRREQ);
1116 test_path_extract_directory_one("a/", NULL, -EDESTADDRREQ);
1117 test_path_extract_directory_one("a/././//.", NULL, -EDESTADDRREQ);
1118 test_path_extract_directory_one("/a", "/", 0);
1119 test_path_extract_directory_one("/a/", "/", 0);
1120 test_path_extract_directory_one("/a//./.", "/", 0);
1121 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
1122 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
1123 test_path_extract_directory_one("xx/.", NULL, -EDESTADDRREQ);
1124 test_path_extract_directory_one("xx/..", NULL, -EINVAL);
1125 test_path_extract_directory_one("..", NULL, -EINVAL);
1126 test_path_extract_directory_one("/..", NULL, -EINVAL);
1127 test_path_extract_directory_one("../", NULL, -EINVAL);
1128 }
1129
1130 TEST(filename_is_valid) {
1131 char foo[NAME_MAX+2];
1132
1133 assert_se(!filename_is_valid(""));
1134 assert_se(!filename_is_valid("/bar/foo"));
1135 assert_se(!filename_is_valid("/"));
1136 assert_se(!filename_is_valid("."));
1137 assert_se(!filename_is_valid(".."));
1138 assert_se(!filename_is_valid("bar/foo"));
1139 assert_se(!filename_is_valid("bar/foo/"));
1140 assert_se(!filename_is_valid("bar//"));
1141
1142 memset(foo, 'a', sizeof(foo) - 1);
1143 char_array_0(foo);
1144
1145 assert_se(!filename_is_valid(foo));
1146
1147 assert_se(filename_is_valid("foo_bar-333"));
1148 assert_se(filename_is_valid("o.o"));
1149 }
1150
1151 static void test_path_is_valid_and_safe_one(const char *p, bool ret) {
1152 log_debug("/* %s(\"%s\") */", __func__, strnull(p));
1153
1154 assert_se(path_is_valid(p) == ret);
1155 if (ret)
1156 ret = !streq(p, "..") &&
1157 !startswith(p, "../") &&
1158 !endswith(p, "/..") &&
1159 !strstr(p, "/../");
1160 assert_se(path_is_safe(p) == ret);
1161 }
1162
1163 TEST(path_is_valid_and_safe) {
1164 char foo[PATH_MAX+2];
1165 const char *c;
1166
1167 test_path_is_valid_and_safe_one("", false);
1168 test_path_is_valid_and_safe_one("/bar/foo", true);
1169 test_path_is_valid_and_safe_one("/bar/foo/", true);
1170 test_path_is_valid_and_safe_one("/bar/foo/", true);
1171 test_path_is_valid_and_safe_one("//bar//foo//", true);
1172 test_path_is_valid_and_safe_one("/", true);
1173 test_path_is_valid_and_safe_one("/////", true);
1174 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
1175 test_path_is_valid_and_safe_one(".", true);
1176 test_path_is_valid_and_safe_one("..", true);
1177 test_path_is_valid_and_safe_one("bar/foo", true);
1178 test_path_is_valid_and_safe_one("bar/foo/", true);
1179 test_path_is_valid_and_safe_one("bar//", true);
1180
1181 memset(foo, 'a', sizeof(foo) -1);
1182 char_array_0(foo);
1183
1184 test_path_is_valid_and_safe_one(foo, false);
1185
1186 c = strjoina("/xxx/", foo, "/yyy");
1187 test_path_is_valid_and_safe_one(c, false);
1188
1189 test_path_is_valid_and_safe_one("foo_bar-333", true);
1190 test_path_is_valid_and_safe_one("o.o", true);
1191 }
1192
1193 TEST(hidden_or_backup_file) {
1194 assert_se(hidden_or_backup_file(".hidden"));
1195 assert_se(hidden_or_backup_file("..hidden"));
1196 assert_se(!hidden_or_backup_file("hidden."));
1197
1198 assert_se(hidden_or_backup_file("backup~"));
1199 assert_se(hidden_or_backup_file(".backup~"));
1200
1201 assert_se(hidden_or_backup_file("lost+found"));
1202 assert_se(hidden_or_backup_file("aquota.user"));
1203 assert_se(hidden_or_backup_file("aquota.group"));
1204
1205 assert_se(hidden_or_backup_file("test.rpmnew"));
1206 assert_se(hidden_or_backup_file("test.dpkg-old"));
1207 assert_se(hidden_or_backup_file("test.dpkg-remove"));
1208 assert_se(hidden_or_backup_file("test.swp"));
1209
1210 assert_se(!hidden_or_backup_file("test.rpmnew."));
1211 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1212 }
1213
1214 TEST(skip_dev_prefix) {
1215 ASSERT_STREQ(skip_dev_prefix("/"), "/");
1216 ASSERT_STREQ(skip_dev_prefix("/dev"), "");
1217 ASSERT_STREQ(skip_dev_prefix("/dev/"), "");
1218 ASSERT_STREQ(skip_dev_prefix("/dev/foo"), "foo");
1219 ASSERT_STREQ(skip_dev_prefix("/dev/foo/bar"), "foo/bar");
1220 ASSERT_STREQ(skip_dev_prefix("//dev"), "");
1221 ASSERT_STREQ(skip_dev_prefix("//dev//"), "");
1222 ASSERT_STREQ(skip_dev_prefix("/dev///foo"), "foo");
1223 ASSERT_STREQ(skip_dev_prefix("///dev///foo///bar"), "foo///bar");
1224 ASSERT_STREQ(skip_dev_prefix("//foo"), "//foo");
1225 ASSERT_STREQ(skip_dev_prefix("foo"), "foo");
1226 }
1227
1228 TEST(empty_or_root) {
1229 assert_se(empty_or_root(NULL));
1230 assert_se(empty_or_root(""));
1231 assert_se(empty_or_root("/"));
1232 assert_se(empty_or_root("//"));
1233 assert_se(empty_or_root("///"));
1234 assert_se(empty_or_root("/////////////////"));
1235 assert_se(!empty_or_root("xxx"));
1236 assert_se(!empty_or_root("/xxx"));
1237 assert_se(!empty_or_root("/xxx/"));
1238 assert_se(!empty_or_root("//yy//"));
1239 }
1240
1241 TEST(empty_or_root_harder_to_null) {
1242 const char *p;
1243
1244 p = NULL;
1245 ASSERT_OK(empty_or_root_harder_to_null(&p));
1246 ASSERT_NULL(p);
1247
1248 p = "/";
1249 ASSERT_OK(empty_or_root_harder_to_null(&p));
1250 ASSERT_NULL(p);
1251
1252 p = "////////";
1253 ASSERT_OK(empty_or_root_harder_to_null(&p));
1254 ASSERT_NULL(p);
1255
1256 p = "/../../././//";
1257 ASSERT_OK(empty_or_root_harder_to_null(&p));
1258 ASSERT_NULL(p);
1259
1260 p = "/usr";
1261 ASSERT_OK(empty_or_root_harder_to_null(&p));
1262 ASSERT_STREQ(p, "/usr");
1263
1264 p = "/usr/../../../";
1265 ASSERT_OK(empty_or_root_harder_to_null(&p));
1266 ASSERT_NULL(p);
1267
1268 p = "/usr/";
1269 ASSERT_OK(empty_or_root_harder_to_null(&p));
1270 ASSERT_STREQ(p, "/usr/");
1271
1272 p = "///././/../../usr////";
1273 ASSERT_OK(empty_or_root_harder_to_null(&p));
1274 ASSERT_STREQ(p, "///././/../../usr////");
1275 }
1276
1277 TEST(path_startswith_set) {
1278 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "");
1279 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar");
1280 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar");
1281 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar");
1282 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL);
1283
1284 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL);
1285 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2");
1286 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2");
1287 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2");
1288 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL);
1289
1290 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL);
1291 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL);
1292 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL);
1293 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar");
1294 ASSERT_STREQ(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL);
1295 }
1296
1297 TEST(path_startswith_strv) {
1298 ASSERT_STREQ(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), "");
1299 ASSERT_STREQ(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar");
1300 ASSERT_STREQ(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar");
1301 ASSERT_STREQ(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar");
1302 ASSERT_STREQ(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL);
1303
1304 ASSERT_STREQ(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL);
1305 ASSERT_STREQ(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2");
1306 ASSERT_STREQ(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2");
1307 ASSERT_STREQ(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2");
1308 ASSERT_STREQ(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL);
1309
1310 ASSERT_STREQ(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL);
1311 ASSERT_STREQ(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL);
1312 ASSERT_STREQ(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL);
1313 ASSERT_STREQ(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar");
1314 ASSERT_STREQ(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL);
1315 }
1316
1317 static void test_path_glob_can_match_one(const char *pattern, const char *prefix, const char *expected) {
1318 _cleanup_free_ char *result = NULL;
1319
1320 log_debug("%s(%s, %s, %s)", __func__, pattern, prefix, strnull(expected));
1321
1322 assert_se(path_glob_can_match(pattern, prefix, &result) == !!expected);
1323 ASSERT_STREQ(result, expected);
1324 }
1325
1326 TEST(path_glob_can_match) {
1327 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL);
1328 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1329 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa");
1330 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa");
1331 test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa");
1332
1333 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL);
1334 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1335 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa");
1336 test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa");
1337 test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa");
1338
1339 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL);
1340 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa");
1341 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa");
1342 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa");
1343 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa");
1344 test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa");
1345
1346 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL);
1347 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb");
1348 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL);
1349 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*");
1350 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*");
1351 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*");
1352 test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*");
1353 }
1354
1355 TEST(print_MAX) {
1356 log_info("PATH_MAX=%zu\n"
1357 "FILENAME_MAX=%zu\n"
1358 "NAME_MAX=%zu",
1359 (size_t) PATH_MAX,
1360 (size_t) FILENAME_MAX,
1361 (size_t) NAME_MAX);
1362
1363 assert_cc(FILENAME_MAX == PATH_MAX);
1364 }
1365
1366 TEST(path_implies_directory) {
1367 assert_se(!path_implies_directory(NULL));
1368 assert_se(!path_implies_directory(""));
1369 assert_se(path_implies_directory("/"));
1370 assert_se(path_implies_directory("////"));
1371 assert_se(path_implies_directory("////.///"));
1372 assert_se(path_implies_directory("////./"));
1373 assert_se(path_implies_directory("////."));
1374 assert_se(path_implies_directory("."));
1375 assert_se(path_implies_directory("./"));
1376 assert_se(path_implies_directory("/."));
1377 assert_se(path_implies_directory(".."));
1378 assert_se(path_implies_directory("../"));
1379 assert_se(path_implies_directory("/.."));
1380 assert_se(!path_implies_directory("a"));
1381 assert_se(!path_implies_directory("ab"));
1382 assert_se(path_implies_directory("ab/"));
1383 assert_se(!path_implies_directory("ab/a"));
1384 assert_se(path_implies_directory("ab/a/"));
1385 assert_se(path_implies_directory("ab/a/.."));
1386 assert_se(path_implies_directory("ab/a/."));
1387 assert_se(path_implies_directory("ab/a//"));
1388 }
1389
1390 DEFINE_TEST_MAIN(LOG_DEBUG);