1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
11 #include "path-util.h"
12 #include "process-util.h"
14 #include "stat-util.h"
15 #include "string-util.h"
18 #include "tmpfile-util.h"
21 log_info("default system PATH: %s", default_PATH());
22 log_info("default user PATH: %s", default_user_PATH());
26 assert_se( path_is_absolute("/"));
27 assert_se(!path_is_absolute("./"));
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"));
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"));
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"));
51 assert_se(!is_path("."));
52 assert_se(!is_path(""));
53 assert_se(!is_path(".."));
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"));
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"));
95 assert_se(!is_device_path("."));
96 assert_se(!is_device_path(""));
97 assert_se(!is_device_path(".."));
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"));
138 static void test_path_simplify_one(const char *in
, const char *out
, PathSimplifyFlags flags
) {
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
);
147 TEST(path_simplify
) {
148 _cleanup_free_
char *hoge
= NULL
, *hoge_out
= NULL
;
149 char foo
[NAME_MAX
* 2];
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
);
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);
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);
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);
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);
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);
214 memset(foo
, 'a', sizeof(foo
) -1);
217 test_path_simplify_one(foo
, foo
, 0);
219 hoge
= strjoin("/", foo
);
221 test_path_simplify_one(hoge
, hoge
, 0);
224 hoge
= strjoin("a////.//././//./b///././/./c/////././//./", foo
, "//.//////d/e/.//f/");
227 hoge_out
= strjoin("a/b/c/", foo
, "//.//////d/e/.//f/");
230 test_path_simplify_one(hoge
, hoge_out
, 0);
233 static void test_path_compare_one(const char *a
, const char *b
, int expected
) {
236 assert_se(path_compare(a
, a
) == 0);
237 assert_se(path_compare(b
, b
) == 0);
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));
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));
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);
278 static void test_path_compare_filename_one(const char *a
, const char *b
, int expected
) {
281 assert_se(path_compare_filename(a
, a
) == 0);
282 assert_se(path_compare_filename(b
, b
) == 0);
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));
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));
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);
327 TEST(path_equal_root
) {
328 /* Nail down the details of how path_equal("/", ...) works. */
330 assert_se(path_equal("/", "/"));
331 assert_se(path_equal("/", "//"));
333 assert_se(path_equal("/", "/./"));
334 assert_se(!path_equal("/", "/../"));
336 assert_se(!path_equal("/", "/.../"));
338 /* Make sure that files_same works as expected. */
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);
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);
350 assert_se(inode_same("/", "/.../", 0) == -ENOENT
);
351 assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
353 /* The same for path_equal_or_files_same. */
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
));
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
));
365 assert_se(!path_equal_or_inode_same("/", "/.../", 0));
366 assert_se(!path_equal_or_inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
369 TEST(find_executable_full
) {
371 _cleanup_free_
char *test_file_name
= NULL
;
372 _cleanup_close_
int fd
= -EBADF
;
373 char fn
[] = "/tmp/test-XXXXXX";
375 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
377 ASSERT_OK(path_extract_filename(p
, &bp
));
378 ASSERT_STREQ(bp
, "sh");
382 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
384 ASSERT_OK(path_extract_filename(p
, &bp
));
385 ASSERT_STREQ(bp
, "sh");
389 _cleanup_free_
char *oldpath
= NULL
;
392 assert_se(oldpath
= strdup(p
));
394 assert_se(unsetenv("PATH") == 0);
396 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
398 ASSERT_OK(path_extract_filename(p
, &bp
));
399 ASSERT_STREQ(bp
, "sh");
403 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
405 ASSERT_OK(path_extract_filename(p
, &bp
));
406 ASSERT_STREQ(bp
, "sh");
411 ASSERT_OK_ERRNO(setenv("PATH", oldpath
, true));
413 ASSERT_OK(fd
= mkostemp_safe(fn
));
414 ASSERT_OK_ERRNO(fchmod(fd
, 0755));
416 ASSERT_OK(path_extract_filename(fn
, &test_file_name
));
418 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == 0);
424 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == -ENOENT
);
427 TEST(find_executable
) {
429 _cleanup_free_
char *bp
= NULL
;
431 assert_se(find_executable("/bin/sh", &p
) == 0);
433 assert_se(path_equal(p
, "/bin/sh"));
436 assert_se(find_executable(saved_argv
[0], &p
) == 0);
438 assert_se(endswith(p
, "/test-path-util"));
439 assert_se(path_is_absolute(p
));
442 assert_se(find_executable("sh", &p
) == 0);
444 assert_se(endswith(p
, "/sh"));
445 assert_se(path_is_absolute(p
));
448 assert_se(find_executable("/bin/touch", &p
) == 0);
449 ASSERT_STREQ(p
, "/bin/touch");
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");
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
);
463 static void test_find_executable_exec_one(const char *path
) {
464 _cleanup_free_
char *t
= NULL
;
465 _cleanup_close_
int fd
= -EBADF
;
469 r
= find_executable_full(path
, NULL
, NULL
, false, &t
, &fd
);
471 log_info_errno(r
, "%s: %s → %s: %d/%m", __func__
, path
, t
?: "-", fd
);
473 assert_se(fd
> STDERR_FILENO
);
474 assert_se(path_is_absolute(t
));
475 if (path_is_absolute(path
))
476 ASSERT_STREQ(t
, path
);
481 r
= fexecve_or_execve(fd
, t
, STRV_MAKE(t
, "--version"), STRV_MAKE(NULL
));
482 log_error_errno(r
, "[f]execve: %m");
486 assert_se(wait_for_terminate_and_check(t
, pid
, WAIT_LOG
) == 0);
489 TEST(find_executable_exec
) {
490 test_find_executable_exec_one("touch");
491 test_find_executable_exec_one("/bin/touch");
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
);
499 static const char* const values
[] = {
512 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
513 log_error("---%s---", s
);
514 ASSERT_STREQ(s
, values
[i
++]);
516 ASSERT_NULL(values
[i
]);
519 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
520 log_error("---%s---", s
);
521 ASSERT_STREQ(s
, values
[i
++]);
523 ASSERT_NULL(values
[i
]);
526 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
527 ASSERT_STREQ(s
, values
[i
++]);
528 ASSERT_NULL(values
[i
]);
531 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
532 ASSERT_STREQ(s
, values
[i
++]);
533 ASSERT_NULL(values
[i
]);
535 PATH_FOREACH_PREFIX(s
, "////")
536 assert_not_reached();
539 PATH_FOREACH_PREFIX_MORE(s
, "////") {
546 PATH_FOREACH_PREFIX(s
, "")
547 assert_not_reached();
550 PATH_FOREACH_PREFIX_MORE(s
, "") {
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); \
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
);
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
);
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
);
583 test_join("", "", NULL
);
584 test_join("", NULL
, "");
585 test_join("", NULL
, NULL
);
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", "", "", "");
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//");
602 _cleanup_free_
char *p
= NULL
;
604 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
605 ASSERT_STREQ(p
, "foo/bar/baz");
607 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
608 ASSERT_STREQ(p
, "foo/bar/baz/foo/bar/baz");
611 assert_se(path_extend(&p
, "foo") == p
);
612 ASSERT_STREQ(p
, "foo");
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 */
621 assert_se(free_and_strdup(&p
, "/") >= 0);
622 assert_se(path_extend(&p
, "foo") == p
);
623 ASSERT_STREQ(p
, "/foo");
627 /* Ensure we use a sane default for PATH. */
628 assert_se(unsetenv("PATH") == 0);
630 /* We might or might not find one of these, so keep the test lax. */
631 assert_se(fsck_exists_for_fstype("minix") >= 0);
633 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
634 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
637 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
638 _cleanup_free_
char *z
= NULL
;
641 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
643 r
= path_make_relative(from
, to
, &z
);
644 assert_se((r
>= 0) == !!expected
);
645 ASSERT_STREQ(z
, expected
);
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
);
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");
663 static void test_path_make_relative_parent_one(const char *from
, const char *to
, const char *expected
) {
664 _cleanup_free_
char *z
= NULL
;
667 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
669 r
= path_make_relative_parent(from
, to
, &z
);
670 assert_se((r
>= 0) == !!expected
);
671 ASSERT_STREQ(z
, expected
);
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
);
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");
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
;
695 ASSERT_NOT_NULL(mkdtemp(tmp_dir
));
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
);
702 assert_se(strv_push(&absolute_dirs
, p
) == 0);
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);
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");
714 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
717 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
720 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
722 p
= path_startswith(path
, prefix
);
723 ASSERT_STREQ(p
, expected
);
725 q
= strjoina(skipped
, p
);
726 ASSERT_STREQ(q
, path
);
727 assert_se(p
== path
+ strlen(skipped
));
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/", "");
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/./.", "");
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
);
764 static void test_path_startswith_return_leading_slash_one(const char *path
, const char *prefix
, const char *expected
) {
767 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
769 p
= path_startswith_full(path
, prefix
, PATH_STARTSWITH_RETURN_LEADING_SLASH
);
770 ASSERT_STREQ(p
, expected
);
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", "/");
780 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
781 _cleanup_free_
char *s
= NULL
;
783 assert_se(s
= path_join(r
, p
));
784 assert_se(path_equal(s
, expected
));
786 _cleanup_free_
char *t
= path_join(r
, p
);
788 assert_se(path_equal(t
, expected
));
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");
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");
810 TEST(file_in_same_dir
) {
813 assert_se(file_in_same_dir("/", "a", &t
) == -EADDRNOTAVAIL
);
815 assert_se(file_in_same_dir("/", "/a", &t
) >= 0);
816 ASSERT_STREQ(t
, "/a");
819 assert_se(file_in_same_dir("", "a", &t
) == -EINVAL
);
821 assert_se(file_in_same_dir("a/", "x", &t
) >= 0);
822 ASSERT_STREQ(t
, "x");
825 assert_se(file_in_same_dir("bar/foo", "bar", &t
) >= 0);
826 ASSERT_STREQ(t
, "bar/bar");
830 static void test_path_find_first_component_one(
836 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
838 for (const char *p
= path
;;) {
842 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
846 assert_se(p
== path
+ strlen_ptr(path
));
847 assert_se(isempty(p
));
853 assert_se(strv_isempty(expected
));
858 assert_se(strcspn(e
, "/") == (size_t) r
);
859 assert_se(strlen_ptr(*expected
) == (size_t) r
);
860 assert_se(strneq(e
, *expected
++, r
));
863 log_debug("p=%s", p
);
864 if (!isempty(*expected
))
865 assert_se(startswith(p
, *expected
));
867 assert_se(p
== path
+ strlen_ptr(path
));
868 assert_se(isempty(p
));
873 TEST(path_find_first_component
) {
874 _cleanup_free_
char *hoge
= NULL
;
875 char foo
[NAME_MAX
* 2];
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);
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);
909 memset(foo
, 'a', sizeof(foo
) -1);
912 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
913 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
915 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
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
);
922 static void test_path_find_last_component_one(
928 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
930 for (const char *next
= NULL
;;) {
934 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
937 assert_se(next
== path
);
941 assert_se(strv_isempty(expected
));
946 assert_se(strcspn(e
, "/") == (size_t) r
);
947 assert_se(strlen_ptr(*expected
) == (size_t) r
);
948 assert_se(strneq(e
, *expected
++, r
));
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
));
957 assert_se(next
== path
);
961 TEST(path_find_last_component
) {
962 _cleanup_free_
char *hoge
= NULL
;
963 char foo
[NAME_MAX
* 2];
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
);
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);
995 memset(foo
, 'a', sizeof(foo
) -1);
998 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
999 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
1001 hoge
= strjoin(foo
, "/a/b/c/");
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
);
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/");
1028 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
1029 _cleanup_free_
char *k
= NULL
;
1032 r
= path_extract_filename(input
, &k
);
1033 log_info("%s → %s/%s [expected: %s/%s]",
1035 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
1036 strnull(output
), ret
< 0 ? STRERROR(ret
) : "-");
1037 ASSERT_STREQ(k
, output
);
1038 assert_se(r
== ret
);
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
);
1072 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
1073 _cleanup_free_
char *k
= NULL
;
1076 r
= path_extract_directory(input
, &k
);
1077 log_info("%s → %s/%s [expected: %s/%s]",
1079 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
1080 strnull(output
), STRERROR(ret
));
1081 ASSERT_STREQ(k
, output
);
1082 assert_se(r
== ret
);
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 */
1087 _cleanup_free_
char *f
= NULL
;
1089 r
= path_extract_filename(input
, &f
);
1091 _cleanup_free_
char *j
= NULL
;
1093 assert_se(j
= path_join(k
, f
));
1094 assert_se(path_equal(input
, j
));
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
);
1130 TEST(filename_is_valid
) {
1131 char foo
[NAME_MAX
+2];
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//"));
1142 memset(foo
, 'a', sizeof(foo
) - 1);
1145 assert_se(!filename_is_valid(foo
));
1147 assert_se(filename_is_valid("foo_bar-333"));
1148 assert_se(filename_is_valid("o.o"));
1151 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
1152 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
1154 assert_se(path_is_valid(p
) == ret
);
1156 ret
= !streq(p
, "..") &&
1157 !startswith(p
, "../") &&
1158 !endswith(p
, "/..") &&
1160 assert_se(path_is_safe(p
) == ret
);
1163 TEST(path_is_valid_and_safe
) {
1164 char foo
[PATH_MAX
+2];
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);
1181 memset(foo
, 'a', sizeof(foo
) -1);
1184 test_path_is_valid_and_safe_one(foo
, false);
1186 c
= strjoina("/xxx/", foo
, "/yyy");
1187 test_path_is_valid_and_safe_one(c
, false);
1189 test_path_is_valid_and_safe_one("foo_bar-333", true);
1190 test_path_is_valid_and_safe_one("o.o", true);
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."));
1198 assert_se(hidden_or_backup_file("backup~"));
1199 assert_se(hidden_or_backup_file(".backup~"));
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"));
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"));
1210 assert_se(!hidden_or_backup_file("test.rpmnew."));
1211 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
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");
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//"));
1241 TEST(empty_or_root_harder_to_null
) {
1245 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1249 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1253 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1256 p
= "/../../././//";
1257 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1261 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1262 ASSERT_STREQ(p
, "/usr");
1264 p
= "/usr/../../../";
1265 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1269 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1270 ASSERT_STREQ(p
, "/usr/");
1272 p
= "///././/../../usr////";
1273 ASSERT_OK(empty_or_root_harder_to_null(&p
));
1274 ASSERT_STREQ(p
, "///././/../../usr////");
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
);
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
);
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
);
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
);
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
);
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
);
1317 static void test_path_glob_can_match_one(const char *pattern
, const char *prefix
, const char *expected
) {
1318 _cleanup_free_
char *result
= NULL
;
1320 log_debug("%s(%s, %s, %s)", __func__
, pattern
, prefix
, strnull(expected
));
1322 assert_se(path_glob_can_match(pattern
, prefix
, &result
) == !!expected
);
1323 ASSERT_STREQ(result
, expected
);
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");
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");
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");
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/*");
1356 log_info("PATH_MAX=%zu\n"
1357 "FILENAME_MAX=%zu\n"
1360 (size_t) FILENAME_MAX
,
1363 assert_cc(FILENAME_MAX
== PATH_MAX
);
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//"));
1390 DEFINE_TEST_MAIN(LOG_DEBUG
);