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