1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #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_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(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.../"), ""));
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"));
40 assert_se(path_equal_ptr(NULL
, NULL
));
41 assert_se(path_equal_ptr("/a", "/a"));
42 assert_se(!path_equal_ptr("/a", "/b"));
43 assert_se(!path_equal_ptr("/a", NULL
));
44 assert_se(!path_equal_ptr(NULL
, "/a"));
48 assert_se(!is_path("foo"));
49 assert_se(!is_path("dos.ext"));
50 assert_se( is_path("/dir"));
51 assert_se( is_path("a/b"));
52 assert_se( is_path("a/b.ext"));
54 assert_se(!is_path("."));
55 assert_se(!is_path(""));
56 assert_se(!is_path(".."));
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/"));
66 assert_se( is_path("/./dev/"));
67 assert_se( is_path("/../dev/"));
68 assert_se( is_path("/dev/sda"));
69 assert_se( is_path("/dev/sda5"));
70 assert_se( is_path("/dev/sda5b3"));
71 assert_se( is_path("/dev/sda5b3/idontexit"));
72 assert_se( is_path("/../dev/sda"));
73 assert_se( is_path("/../../dev/sda5"));
74 assert_se( is_path("/../../../dev/sda5b3"));
75 assert_se( is_path("/.././.././dev/sda5b3/idontexit"));
76 assert_se( is_path("/sys"));
77 assert_se( is_path("/sys/"));
78 assert_se( is_path("/./sys"));
79 assert_se( is_path("/./sys/."));
80 assert_se( is_path("/./sys."));
81 assert_se( is_path("/sys/what"));
82 assert_se( is_path("/sys/something/.."));
83 assert_se( is_path("/sys/something/../"));
84 assert_se( is_path("/sys////"));
85 assert_se( is_path("/sys////."));
86 assert_se( is_path("/sys/.."));
87 assert_se( is_path("/sys/../"));
88 assert_se( is_path("/usr/../dev/sda"));
91 TEST(is_device_path
) {
92 assert_se(!is_device_path("foo"));
93 assert_se(!is_device_path("dos.ext"));
94 assert_se(!is_device_path("/dir"));
95 assert_se(!is_device_path("a/b"));
96 assert_se(!is_device_path("a/b.ext"));
98 assert_se(!is_device_path("."));
99 assert_se(!is_device_path(""));
100 assert_se(!is_device_path(".."));
102 assert_se(!is_device_path("/dev"));
103 assert_se(!is_device_path("/./dev"));
104 assert_se(!is_device_path("/./dev/."));
105 assert_se(!is_device_path("/./dev."));
106 assert_se( is_device_path("/./dev/foo"));
107 assert_se( is_device_path("/./dev/./foo"));
108 assert_se(!is_device_path("/./dev./foo"));
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/"));
113 assert_se(!is_device_path("/./dev/"));
114 assert_se(!is_device_path("/../dev/"));
115 assert_se( is_device_path("/dev/sda"));
116 assert_se( is_device_path("/dev/sda5"));
117 assert_se( is_device_path("/dev/sda5b3"));
118 assert_se( is_device_path("/dev/sda5b3/idontexit"));
119 assert_se(!is_device_path("/../dev/sda"));
120 assert_se(!is_device_path("/../../dev/sda5"));
121 assert_se(!is_device_path("/../../../dev/sda5b3"));
122 assert_se(!is_device_path("/.././.././dev/sda5b3/idontexit"));
123 assert_se(!is_device_path("/sys"));
124 assert_se(!is_device_path("/sys/"));
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/foo"));
129 assert_se( is_device_path("/./sys/./foo"));
130 assert_se(!is_device_path("/./sys./foo"));
131 assert_se( is_device_path("/sys/what"));
132 assert_se( is_device_path("/sys/something/.."));
133 assert_se( is_device_path("/sys/something/../"));
134 assert_se(!is_device_path("/sys////"));
135 assert_se(!is_device_path("/sys////."));
136 assert_se( is_device_path("/sys/.."));
137 assert_se( is_device_path("/sys/../"));
138 assert_se(!is_device_path("/usr/../dev/sda"));
141 static void test_path_simplify_one(const char *in
, const char *out
, PathSimplifyFlags flags
) {
144 p
= strdupa_safe(in
);
145 path_simplify_full(p
, flags
);
146 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in
, p
, out
);
147 assert_se(streq(p
, out
));
150 TEST(path_simplify
) {
151 _cleanup_free_
char *hoge
= NULL
, *hoge_out
= NULL
;
152 char foo
[NAME_MAX
* 2];
154 test_path_simplify_one("", "", 0);
155 test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc", 0);
156 test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc", 0);
157 test_path_simplify_one("///", "/", 0);
158 test_path_simplify_one("///", "/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
159 test_path_simplify_one("///.//", "/", 0);
160 test_path_simplify_one("///.//.///", "/", 0);
161 test_path_simplify_one("////.././///../.", "/", 0);
162 test_path_simplify_one(".", ".", 0);
163 test_path_simplify_one("./", ".", 0);
164 test_path_simplify_one("./", "./", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
165 test_path_simplify_one(".///.//./.", ".", 0);
166 test_path_simplify_one(".///.//././/", ".", 0);
167 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
168 "/aaa/.bbb/../c./d.dd/..eeee", 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("abc///", "abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
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", 0);
180 test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
181 test_path_simplify_one("/../abc///..", "/abc/..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
182 test_path_simplify_one("/../abc///../", "/abc/../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
183 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", 0);
188 test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
189 test_path_simplify_one("/../../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
190 test_path_simplify_one("/../../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
191 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", 0);
196 test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
197 test_path_simplify_one("/.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
198 test_path_simplify_one("/.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
199 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", 0);
204 test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
205 test_path_simplify_one("/./.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
206 test_path_simplify_one("/./.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
207 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", 0);
212 test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
213 test_path_simplify_one("/.../abc///...", "/.../abc/...", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
214 test_path_simplify_one("/.../abc///.../", "/.../abc/.../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
215 test_path_simplify_one("/.../abc///.../", "/.../abc/...", 0);
217 memset(foo
, 'a', sizeof(foo
) -1);
220 test_path_simplify_one(foo
, foo
, 0);
222 hoge
= strjoin("/", foo
);
224 test_path_simplify_one(hoge
, hoge
, 0);
227 hoge
= strjoin("a////.//././//./b///././/./c/////././//./", foo
, "//.//////d/e/.//f/");
230 hoge_out
= strjoin("a/b/c/", foo
, "//.//////d/e/.//f/");
233 test_path_simplify_one(hoge
, hoge_out
, 0);
236 static void test_path_compare_one(const char *a
, const char *b
, int expected
) {
239 assert_se(path_compare(a
, a
) == 0);
240 assert_se(path_compare(b
, b
) == 0);
242 r
= path_compare(a
, b
);
243 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
244 r
= path_compare(b
, a
);
245 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
247 assert_se(path_equal(a
, a
) == 1);
248 assert_se(path_equal(b
, b
) == 1);
249 assert_se(path_equal(a
, b
) == (expected
== 0));
250 assert_se(path_equal(b
, a
) == (expected
== 0));
254 test_path_compare_one("/goo", "/goo", 0);
255 test_path_compare_one("/goo", "/goo", 0);
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/boo", "/goo//boo", 0);
260 test_path_compare_one("//goo/boo", "/goo/boo//", 0);
261 test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
262 test_path_compare_one("/.", "//.///", 0);
263 test_path_compare_one("/x", "x/", 1);
264 test_path_compare_one("x/", "/", -1);
265 test_path_compare_one("/x/./y", "x/y", 1);
266 test_path_compare_one("/x/./y", "/x/y", 0);
267 test_path_compare_one("/x/./././y", "/x/y/././.", 0);
268 test_path_compare_one("./x/./././y", "./x/y/././.", 0);
269 test_path_compare_one(".", "./.", 0);
270 test_path_compare_one(".", "././.", 0);
271 test_path_compare_one("./..", ".", 1);
272 test_path_compare_one("x/.y", "x/y", -1);
273 test_path_compare_one("foo", "/foo", -1);
274 test_path_compare_one("/foo", "/foo/bar", -1);
275 test_path_compare_one("/foo/aaa", "/foo/b", -1);
276 test_path_compare_one("/foo/aaa", "/foo/b/a", -1);
277 test_path_compare_one("/foo/a", "/foo/aaa", -1);
278 test_path_compare_one("/foo/a/b", "/foo/aaa", -1);
281 static void test_path_compare_filename_one(const char *a
, const char *b
, int expected
) {
284 assert_se(path_compare_filename(a
, a
) == 0);
285 assert_se(path_compare_filename(b
, b
) == 0);
287 r
= path_compare_filename(a
, b
);
288 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
289 r
= path_compare_filename(b
, a
);
290 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
292 assert_se(path_equal_filename(a
, a
) == 1);
293 assert_se(path_equal_filename(b
, b
) == 1);
294 assert_se(path_equal_filename(a
, b
) == (expected
== 0));
295 assert_se(path_equal_filename(b
, a
) == (expected
== 0));
298 TEST(path_compare_filename
) {
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", "/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/boo", "/goo//boo", 0);
305 test_path_compare_filename_one("//goo/boo", "/goo/boo//", 0);
306 test_path_compare_filename_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
307 test_path_compare_filename_one("/.", "//.///", -1);
308 test_path_compare_filename_one("/x", "x/", 0);
309 test_path_compare_filename_one("x/", "/", 1);
310 test_path_compare_filename_one("/x/./y", "x/y", 0);
311 test_path_compare_filename_one("/x/./y", "/x/y", 0);
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(".", "./.", -1);
315 test_path_compare_filename_one(".", "././.", -1);
316 test_path_compare_filename_one("./..", ".", 1);
317 test_path_compare_filename_one("x/.y", "x/y", -1);
318 test_path_compare_filename_one("foo", "/foo", 0);
319 test_path_compare_filename_one("/foo", "/foo/bar", 1);
320 test_path_compare_filename_one("/foo/aaa", "/foo/b", -1);
321 test_path_compare_filename_one("/foo/aaa", "/foo/b/a", 1);
322 test_path_compare_filename_one("/foo/a", "/foo/aaa", -1);
323 test_path_compare_filename_one("/foo/a/b", "/foo/aaa", 1);
324 test_path_compare_filename_one("/a/c", "/b/c", 0);
325 test_path_compare_filename_one("/a", "/a", 0);
326 test_path_compare_filename_one("/a/b", "/a/c", -1);
327 test_path_compare_filename_one("/b", "/c", -1);
330 TEST(path_equal_root
) {
331 /* Nail down the details of how path_equal("/", ...) works. */
333 assert_se(path_equal("/", "/"));
334 assert_se(path_equal("/", "//"));
336 assert_se(path_equal("/", "/./"));
337 assert_se(!path_equal("/", "/../"));
339 assert_se(!path_equal("/", "/.../"));
341 /* Make sure that files_same works as expected. */
343 assert_se(inode_same("/", "/", 0) > 0);
344 assert_se(inode_same("/", "/", AT_SYMLINK_NOFOLLOW
) > 0);
345 assert_se(inode_same("/", "//", 0) > 0);
346 assert_se(inode_same("/", "//", AT_SYMLINK_NOFOLLOW
) > 0);
348 assert_se(inode_same("/", "/./", 0) > 0);
349 assert_se(inode_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
350 assert_se(inode_same("/", "/../", 0) > 0);
351 assert_se(inode_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
353 assert_se(inode_same("/", "/.../", 0) == -ENOENT
);
354 assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
356 /* The same for path_equal_or_files_same. */
358 assert_se(path_equal_or_inode_same("/", "/", 0));
359 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
));
363 assert_se(path_equal_or_inode_same("/", "/./", 0));
364 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
));
368 assert_se(!path_equal_or_inode_same("/", "/.../", 0));
369 assert_se(!path_equal_or_inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
372 TEST(find_executable_full
) {
374 char* test_file_name
;
375 _cleanup_close_
int fd
= -EBADF
;
376 char fn
[] = "/tmp/test-XXXXXX";
378 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
380 assert_se(streq(basename(p
), "sh"));
383 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
385 assert_se(streq(basename(p
), "sh"));
388 _cleanup_free_
char *oldpath
= NULL
;
391 assert_se(oldpath
= strdup(p
));
393 assert_se(unsetenv("PATH") == 0);
395 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
397 assert_se(streq(basename(p
), "sh"));
400 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
402 assert_se(streq(basename(p
), "sh"));
406 assert_se(setenv("PATH", oldpath
, true) >= 0);
408 assert_se((fd
= mkostemp_safe(fn
)) >= 0);
409 assert_se(fchmod(fd
, 0755) >= 0);
411 test_file_name
= basename(fn
);
413 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == 0);
415 assert_se(streq(p
, fn
));
419 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == -ENOENT
);
422 TEST(find_executable
) {
425 assert_se(find_executable("/bin/sh", &p
) == 0);
427 assert_se(path_equal(p
, "/bin/sh"));
430 assert_se(find_executable(saved_argv
[0], &p
) == 0);
432 assert_se(endswith(p
, "/test-path-util"));
433 assert_se(path_is_absolute(p
));
436 assert_se(find_executable("sh", &p
) == 0);
438 assert_se(endswith(p
, "/sh"));
439 assert_se(path_is_absolute(p
));
442 assert_se(find_executable("/bin/touch", &p
) == 0);
443 assert_se(streq(p
, "/bin/touch"));
446 assert_se(find_executable("touch", &p
) == 0);
447 assert_se(path_is_absolute(p
));
448 assert_se(streq(basename(p
), "touch"));
451 assert_se(find_executable("xxxx-xxxx", &p
) == -ENOENT
);
452 assert_se(find_executable("/some/dir/xxxx-xxxx", &p
) == -ENOENT
);
453 assert_se(find_executable("/proc/filesystems", &p
) == -EACCES
);
456 static void test_find_executable_exec_one(const char *path
) {
457 _cleanup_free_
char *t
= NULL
;
458 _cleanup_close_
int fd
= -EBADF
;
462 r
= find_executable_full(path
, NULL
, NULL
, false, &t
, &fd
);
464 log_info_errno(r
, "%s: %s → %s: %d/%m", __func__
, path
, t
?: "-", fd
);
466 assert_se(fd
> STDERR_FILENO
);
467 assert_se(path_is_absolute(t
));
468 if (path_is_absolute(path
))
469 assert_se(streq(t
, path
));
474 r
= fexecve_or_execve(fd
, t
, STRV_MAKE(t
, "--version"), STRV_MAKE(NULL
));
475 log_error_errno(r
, "[f]execve: %m");
479 assert_se(wait_for_terminate_and_check(t
, pid
, WAIT_LOG
) == 0);
482 TEST(find_executable_exec
) {
483 test_find_executable_exec_one("touch");
484 test_find_executable_exec_one("/bin/touch");
486 _cleanup_free_
char *script
= NULL
;
487 assert_se(get_testdata_dir("test-path-util/script.sh", &script
) >= 0);
488 test_find_executable_exec_one(script
);
492 static const char* const values
[] = {
505 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
506 log_error("---%s---", s
);
507 assert_se(streq(s
, values
[i
++]));
509 assert_se(values
[i
] == NULL
);
512 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
513 log_error("---%s---", s
);
514 assert_se(streq(s
, values
[i
++]));
516 assert_se(values
[i
] == NULL
);
519 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
520 assert_se(streq(s
, values
[i
++]));
521 assert_se(values
[i
] == NULL
);
524 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
525 assert_se(streq(s
, values
[i
++]));
526 assert_se(values
[i
] == NULL
);
528 PATH_FOREACH_PREFIX(s
, "////")
529 assert_not_reached();
532 PATH_FOREACH_PREFIX_MORE(s
, "////") {
534 assert_se(streq(s
, ""));
539 PATH_FOREACH_PREFIX(s
, "")
540 assert_not_reached();
543 PATH_FOREACH_PREFIX_MORE(s
, "") {
545 assert_se(streq(s
, ""));
551 #define test_join(expected, ...) { \
552 _cleanup_free_ char *z = NULL; \
553 z = path_join(__VA_ARGS__); \
554 log_debug("got \"%s\", expected \"%s\"", z, expected); \
555 assert_se(streq(z, expected)); \
558 test_join("/root/a/b/c", "/root", "/a/b", "/c");
559 test_join("/root/a/b/c", "/root", "a/b", "c");
560 test_join("/root/a/b/c", "/root", "/a/b", "c");
561 test_join("/root/c", "/root", "/", "c");
562 test_join("/root/", "/root", "/", NULL
);
564 test_join("/a/b/c", "", "/a/b", "/c");
565 test_join("a/b/c", "", "a/b", "c");
566 test_join("/a/b/c", "", "/a/b", "c");
567 test_join("/c", "", "/", "c");
568 test_join("/", "", "/", NULL
);
570 test_join("/a/b/c", NULL
, "/a/b", "/c");
571 test_join("a/b/c", NULL
, "a/b", "c");
572 test_join("/a/b/c", NULL
, "/a/b", "c");
573 test_join("/c", NULL
, "/", "c");
574 test_join("/", NULL
, "/", NULL
);
576 test_join("", "", NULL
);
577 test_join("", NULL
, "");
578 test_join("", NULL
, NULL
);
580 test_join("foo/bar", "foo", "bar");
581 test_join("foo/bar", "", "foo", "bar");
582 test_join("foo/bar", NULL
, "foo", NULL
, "bar");
583 test_join("foo/bar", "", "foo", "", "bar", "");
584 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
586 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
587 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
588 test_join("foo/bar/baz", "foo", "bar", "baz");
589 test_join("foo/bar/baz", "foo/", "bar", "/baz");
590 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
591 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
595 _cleanup_free_
char *p
= NULL
;
597 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
598 assert_se(streq(p
, "foo/bar/baz"));
600 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
601 assert_se(streq(p
, "foo/bar/baz/foo/bar/baz"));
604 assert_se(path_extend(&p
, "foo") == p
);
605 assert_se(streq(p
, "foo"));
607 assert_se(path_extend(&p
, "/foo") == p
);
608 assert_se(streq(p
, "foo/foo"));
609 assert_se(path_extend(&p
, "/waaaah/wahhh//") == p
);
610 assert_se(streq(p
, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
611 assert_se(path_extend(&p
, "/aaa/bbb/") == p
);
612 assert_se(streq(p
, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
614 assert_se(free_and_strdup(&p
, "/") >= 0);
615 assert_se(path_extend(&p
, "foo") == p
);
616 assert_se(streq(p
, "/foo"));
620 /* Ensure we use a sane default for PATH. */
621 assert_se(unsetenv("PATH") == 0);
623 /* We might or might not find one of these, so keep the test lax. */
624 assert_se(fsck_exists_for_fstype("minix") >= 0);
626 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
627 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
630 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
631 _cleanup_free_
char *z
= NULL
;
634 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
636 r
= path_make_relative(from
, to
, &z
);
637 assert_se((r
>= 0) == !!expected
);
638 assert_se(streq_ptr(z
, expected
));
641 TEST(path_make_relative
) {
642 test_path_make_relative_one("some/relative/path", "/some/path", NULL
);
643 test_path_make_relative_one("/some/path", "some/relative/path", NULL
);
644 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL
);
646 test_path_make_relative_one("/", "/", ".");
647 test_path_make_relative_one("/", "/some/path", "some/path");
648 test_path_make_relative_one("/some/path", "/some/path", ".");
649 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
650 test_path_make_relative_one("/some/path", "/", "../..");
651 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
652 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
653 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
656 static void test_path_make_relative_parent_one(const char *from
, const char *to
, const char *expected
) {
657 _cleanup_free_
char *z
= NULL
;
660 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
662 r
= path_make_relative_parent(from
, to
, &z
);
663 assert_se((r
>= 0) == !!expected
);
664 assert_se(streq_ptr(z
, expected
));
667 TEST(path_make_relative_parent
) {
668 test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL
);
669 test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL
);
670 test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL
);
671 test_path_make_relative_parent_one("/", "/aaa", NULL
);
673 test_path_make_relative_parent_one("/hoge", "/", ".");
674 test_path_make_relative_parent_one("/hoge", "/some/path", "some/path");
675 test_path_make_relative_parent_one("/some/path/hoge", "/some/path", ".");
676 test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir");
677 test_path_make_relative_parent_one("/some/path/hoge", "/", "../..");
678 test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path");
679 test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path");
680 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 TEST(path_strv_resolve
) {
684 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
685 _cleanup_strv_free_
char **search_dirs
= NULL
;
686 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
688 assert_se(mkdtemp(tmp_dir
) != NULL
);
690 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
691 assert_se(search_dirs
);
692 STRV_FOREACH(d
, search_dirs
) {
693 char *p
= path_join(tmp_dir
, *d
);
695 assert_se(strv_push(&absolute_dirs
, p
) == 0);
698 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
699 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
700 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
702 path_strv_resolve(search_dirs
, tmp_dir
);
703 assert_se(streq(search_dirs
[0], "/dir1"));
704 assert_se(streq(search_dirs
[1], "/dir2"));
705 assert_se(streq(search_dirs
[2], "/dir2"));
707 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
710 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
713 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
715 p
= path_startswith(path
, prefix
);
716 assert_se(streq_ptr(p
, expected
));
718 q
= strjoina(skipped
, p
);
719 assert_se(streq(q
, path
));
720 assert_se(p
== path
+ strlen(skipped
));
724 TEST(path_startswith
) {
725 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
726 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
727 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
728 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
729 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
730 test_path_startswith_one("/foo/bar/barfoo/", "/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/", "");
736 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo", "/foo/./", "bar///barfoo/./.");
737 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/", "/foo/./", "bar///barfoo/./.");
738 test_path_startswith_one("/foo/./bar///barfoo/./.", "/", "/", "foo/./bar///barfoo/./.");
739 test_path_startswith_one("/foo/./bar///barfoo/./.", "////", "/", "foo/./bar///barfoo/./.");
740 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo//bar/////barfoo///", "/foo/./bar///barfoo/./.", "");
741 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo////", "/foo/./bar///barfoo/./.", "");
742 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/./.", "");
747 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL
, NULL
);
748 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL
, NULL
);
749 test_path_startswith_one("/foo/bar/barfoo/", "", NULL
, NULL
);
750 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL
, NULL
);
751 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL
, NULL
);
752 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL
, NULL
);
753 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL
, NULL
);
754 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL
, NULL
);
757 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
758 _cleanup_free_
char *s
= NULL
;
761 assert_se(s
= path_join(r
, p
));
762 assert_se(path_equal_ptr(s
, expected
));
764 t
= prefix_roota(r
, p
);
766 assert_se(path_equal_ptr(t
, expected
));
770 test_prefix_root_one("/", "/foo", "/foo");
771 test_prefix_root_one(NULL
, "/foo", "/foo");
772 test_prefix_root_one("", "/foo", "/foo");
773 test_prefix_root_one("///", "/foo", "/foo");
774 test_prefix_root_one("/", "////foo", "/foo");
775 test_prefix_root_one(NULL
, "////foo", "/foo");
776 test_prefix_root_one("/", "foo", "/foo");
777 test_prefix_root_one("", "foo", "foo");
778 test_prefix_root_one(NULL
, "foo", "foo");
780 test_prefix_root_one("/foo", "/bar", "/foo/bar");
781 test_prefix_root_one("/foo", "bar", "/foo/bar");
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");
788 TEST(file_in_same_dir
) {
791 assert_se(file_in_same_dir("/", "a", &t
) == -EADDRNOTAVAIL
);
793 assert_se(file_in_same_dir("/", "/a", &t
) >= 0);
794 assert_se(streq(t
, "/a"));
797 assert_se(file_in_same_dir("", "a", &t
) == -EINVAL
);
799 assert_se(file_in_same_dir("a/", "x", &t
) >= 0);
800 assert_se(streq(t
, "x"));
803 assert_se(file_in_same_dir("bar/foo", "bar", &t
) >= 0);
804 assert_se(streq(t
, "bar/bar"));
808 static void test_path_find_first_component_one(
814 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
816 for (const char *p
= path
;;) {
820 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
824 assert_se(p
== path
+ strlen_ptr(path
));
825 assert_se(isempty(p
));
831 assert_se(strv_isempty(expected
));
836 assert_se(strcspn(e
, "/") == (size_t) r
);
837 assert_se(strlen_ptr(*expected
) == (size_t) r
);
838 assert_se(strneq(e
, *expected
++, r
));
841 log_debug("p=%s", p
);
842 if (!isempty(*expected
))
843 assert_se(startswith(p
, *expected
));
845 assert_se(p
== path
+ strlen_ptr(path
));
846 assert_se(isempty(p
));
851 TEST(path_find_first_component
) {
852 _cleanup_free_
char *hoge
= NULL
;
853 char foo
[NAME_MAX
* 2];
855 test_path_find_first_component_one(NULL
, false, NULL
, 0);
856 test_path_find_first_component_one("", false, NULL
, 0);
857 test_path_find_first_component_one("/", 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
, -EINVAL
);
862 test_path_find_first_component_one("/..", false, NULL
, -EINVAL
);
863 test_path_find_first_component_one("./..", false, NULL
, -EINVAL
);
864 test_path_find_first_component_one("////./././//.", false, NULL
, 0);
865 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
866 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
867 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL
);
868 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL
);
869 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
871 test_path_find_first_component_one(NULL
, true, NULL
, 0);
872 test_path_find_first_component_one("", true, NULL
, 0);
873 test_path_find_first_component_one("/", 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, STRV_MAKE(".."), 0);
878 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
879 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
880 test_path_find_first_component_one("////./././//.", true, NULL
, 0);
881 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
882 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
883 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
884 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
885 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
887 memset(foo
, 'a', sizeof(foo
) -1);
890 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
891 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
893 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
896 test_path_find_first_component_one(hoge
, false, STRV_MAKE("a", "b", "c"), -EINVAL
);
897 test_path_find_first_component_one(hoge
, true, STRV_MAKE("a", "b", "c"), -EINVAL
);
900 static void test_path_find_last_component_one(
906 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
908 for (const char *next
= NULL
;;) {
912 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
915 assert_se(next
== path
);
919 assert_se(strv_isempty(expected
));
924 assert_se(strcspn(e
, "/") == (size_t) r
);
925 assert_se(strlen_ptr(*expected
) == (size_t) r
);
926 assert_se(strneq(e
, *expected
++, r
));
929 log_debug("path=%s\nnext=%s", path
, next
);
930 if (!isempty(*expected
)) {
931 assert_se(next
< path
+ strlen(path
));
932 assert_se(next
>= path
+ strlen(*expected
));
933 assert_se(startswith(next
- strlen(*expected
), *expected
));
935 assert_se(next
== path
);
939 TEST(path_find_last_component
) {
940 _cleanup_free_
char *hoge
= NULL
;
941 char foo
[NAME_MAX
* 2];
943 test_path_find_last_component_one(NULL
, false, NULL
, 0);
944 test_path_find_last_component_one("", false, NULL
, 0);
945 test_path_find_last_component_one("/", 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
, -EINVAL
);
950 test_path_find_last_component_one("/..", false, NULL
, -EINVAL
);
951 test_path_find_last_component_one("./..", false, NULL
, -EINVAL
);
952 test_path_find_last_component_one("////./././//.", false, NULL
, 0);
953 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
954 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
955 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL
);
956 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL
);
958 test_path_find_last_component_one(NULL
, true, NULL
, 0);
959 test_path_find_last_component_one("", true, NULL
, 0);
960 test_path_find_last_component_one("/", 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, STRV_MAKE(".."), 0);
965 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
966 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
967 test_path_find_last_component_one("////./././//.", true, NULL
, 0);
968 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
969 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
970 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
971 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
973 memset(foo
, 'a', sizeof(foo
) -1);
976 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
977 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
979 hoge
= strjoin(foo
, "/a/b/c/");
982 test_path_find_last_component_one(hoge
, false, STRV_MAKE("c", "b", "a"), -EINVAL
);
983 test_path_find_last_component_one(hoge
, true, STRV_MAKE("c", "b", "a"), -EINVAL
);
986 TEST(last_path_component
) {
987 assert_se(last_path_component(NULL
) == NULL
);
988 assert_se(streq(last_path_component("a/b/c"), "c"));
989 assert_se(streq(last_path_component("a/b/c/"), "c/"));
990 assert_se(streq(last_path_component("/"), "/"));
991 assert_se(streq(last_path_component("//"), "/"));
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("/foo/a"), "a"));
998 assert_se(streq(last_path_component("/foo/a/"), "a/"));
999 assert_se(streq(last_path_component(""), ""));
1000 assert_se(streq(last_path_component("a"), "a"));
1001 assert_se(streq(last_path_component("a/"), "a/"));
1002 assert_se(streq(last_path_component("/a"), "a"));
1003 assert_se(streq(last_path_component("/a/"), "a/"));
1006 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
1007 _cleanup_free_
char *k
= NULL
;
1010 r
= path_extract_filename(input
, &k
);
1011 log_info("%s → %s/%s [expected: %s/%s]",
1013 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
1014 strnull(output
), ret
< 0 ? STRERROR(ret
) : "-");
1015 assert_se(streq_ptr(k
, output
));
1016 assert_se(r
== ret
);
1019 TEST(path_extract_filename
) {
1020 test_path_extract_filename_one(NULL
, NULL
, -EINVAL
);
1021 test_path_extract_filename_one("a/b/c", "c", 0);
1022 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY
);
1023 test_path_extract_filename_one("/", NULL
, -EADDRNOTAVAIL
);
1024 test_path_extract_filename_one("//", NULL
, -EADDRNOTAVAIL
);
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("/foo/a", "a", 0);
1033 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY
);
1034 test_path_extract_filename_one("", NULL
, -EINVAL
);
1035 test_path_extract_filename_one("a", "a", 0);
1036 test_path_extract_filename_one("a/", "a", O_DIRECTORY
);
1037 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY
);
1038 test_path_extract_filename_one("/a", "a", 0);
1039 test_path_extract_filename_one("/a/", "a", O_DIRECTORY
);
1040 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY
);
1041 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY
);
1042 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY
);
1043 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY
);
1044 test_path_extract_filename_one("xx/..", NULL
, -EINVAL
);
1045 test_path_extract_filename_one("..", NULL
, -EINVAL
);
1046 test_path_extract_filename_one("/..", NULL
, -EINVAL
);
1047 test_path_extract_filename_one("../", NULL
, -EINVAL
);
1050 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
1051 _cleanup_free_
char *k
= NULL
;
1054 r
= path_extract_directory(input
, &k
);
1055 log_info("%s → %s/%s [expected: %s/%s]",
1057 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
1058 strnull(output
), STRERROR(ret
));
1059 assert_se(streq_ptr(k
, output
));
1060 assert_se(r
== ret
);
1062 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
1063 * joined parts are identical to the original again */
1065 _cleanup_free_
char *f
= NULL
;
1067 r
= path_extract_filename(input
, &f
);
1069 _cleanup_free_
char *j
= NULL
;
1071 assert_se(j
= path_join(k
, f
));
1072 assert_se(path_equal(input
, j
));
1077 TEST(path_extract_directory
) {
1078 test_path_extract_directory_one(NULL
, NULL
, -EINVAL
);
1079 test_path_extract_directory_one("a/b/c", "a/b", 0);
1080 test_path_extract_directory_one("a/b/c/", "a/b", 0);
1081 test_path_extract_directory_one("/", NULL
, -EADDRNOTAVAIL
);
1082 test_path_extract_directory_one("//", NULL
, -EADDRNOTAVAIL
);
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("/foo/a", "/foo", 0);
1091 test_path_extract_directory_one("/foo/a/", "/foo", 0);
1092 test_path_extract_directory_one("", NULL
, -EINVAL
);
1093 test_path_extract_directory_one("a", NULL
, -EDESTADDRREQ
);
1094 test_path_extract_directory_one("a/", NULL
, -EDESTADDRREQ
);
1095 test_path_extract_directory_one("a/././//.", NULL
, -EDESTADDRREQ
);
1096 test_path_extract_directory_one("/a", "/", 0);
1097 test_path_extract_directory_one("/a/", "/", 0);
1098 test_path_extract_directory_one("/a//./.", "/", 0);
1099 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
1100 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
1101 test_path_extract_directory_one("xx/.", NULL
, -EDESTADDRREQ
);
1102 test_path_extract_directory_one("xx/..", NULL
, -EINVAL
);
1103 test_path_extract_directory_one("..", NULL
, -EINVAL
);
1104 test_path_extract_directory_one("/..", NULL
, -EINVAL
);
1105 test_path_extract_directory_one("../", NULL
, -EINVAL
);
1108 TEST(filename_is_valid
) {
1109 char foo
[NAME_MAX
+2];
1111 assert_se(!filename_is_valid(""));
1112 assert_se(!filename_is_valid("/bar/foo"));
1113 assert_se(!filename_is_valid("/"));
1114 assert_se(!filename_is_valid("."));
1115 assert_se(!filename_is_valid(".."));
1116 assert_se(!filename_is_valid("bar/foo"));
1117 assert_se(!filename_is_valid("bar/foo/"));
1118 assert_se(!filename_is_valid("bar//"));
1120 memset(foo
, 'a', sizeof(foo
) - 1);
1123 assert_se(!filename_is_valid(foo
));
1125 assert_se(filename_is_valid("foo_bar-333"));
1126 assert_se(filename_is_valid("o.o"));
1129 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
1130 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
1132 assert_se(path_is_valid(p
) == ret
);
1134 ret
= !streq(p
, "..") &&
1135 !startswith(p
, "../") &&
1136 !endswith(p
, "/..") &&
1138 assert_se(path_is_safe(p
) == ret
);
1141 TEST(path_is_valid_and_safe
) {
1142 char foo
[PATH_MAX
+2];
1145 test_path_is_valid_and_safe_one("", false);
1146 test_path_is_valid_and_safe_one("/bar/foo", true);
1147 test_path_is_valid_and_safe_one("/bar/foo/", true);
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("/", true);
1151 test_path_is_valid_and_safe_one("/////", 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("bar/foo", true);
1156 test_path_is_valid_and_safe_one("bar/foo/", true);
1157 test_path_is_valid_and_safe_one("bar//", true);
1159 memset(foo
, 'a', sizeof(foo
) -1);
1162 test_path_is_valid_and_safe_one(foo
, false);
1164 c
= strjoina("/xxx/", foo
, "/yyy");
1165 test_path_is_valid_and_safe_one(c
, false);
1167 test_path_is_valid_and_safe_one("foo_bar-333", true);
1168 test_path_is_valid_and_safe_one("o.o", true);
1171 TEST(hidden_or_backup_file
) {
1172 assert_se(hidden_or_backup_file(".hidden"));
1173 assert_se(hidden_or_backup_file("..hidden"));
1174 assert_se(!hidden_or_backup_file("hidden."));
1176 assert_se(hidden_or_backup_file("backup~"));
1177 assert_se(hidden_or_backup_file(".backup~"));
1179 assert_se(hidden_or_backup_file("lost+found"));
1180 assert_se(hidden_or_backup_file("aquota.user"));
1181 assert_se(hidden_or_backup_file("aquota.group"));
1183 assert_se(hidden_or_backup_file("test.rpmnew"));
1184 assert_se(hidden_or_backup_file("test.dpkg-old"));
1185 assert_se(hidden_or_backup_file("test.dpkg-remove"));
1186 assert_se(hidden_or_backup_file("test.swp"));
1188 assert_se(!hidden_or_backup_file("test.rpmnew."));
1189 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1192 TEST(skip_dev_prefix
) {
1193 assert_se(streq(skip_dev_prefix("/"), "/"));
1194 assert_se(streq(skip_dev_prefix("/dev"), ""));
1195 assert_se(streq(skip_dev_prefix("/dev/"), ""));
1196 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
1197 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
1198 assert_se(streq(skip_dev_prefix("//dev"), ""));
1199 assert_se(streq(skip_dev_prefix("//dev//"), ""));
1200 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
1201 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
1202 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
1203 assert_se(streq(skip_dev_prefix("foo"), "foo"));
1206 TEST(empty_or_root
) {
1207 assert_se(empty_or_root(NULL
));
1208 assert_se(empty_or_root(""));
1209 assert_se(empty_or_root("/"));
1210 assert_se(empty_or_root("//"));
1211 assert_se(empty_or_root("///"));
1212 assert_se(empty_or_root("/////////////////"));
1213 assert_se(!empty_or_root("xxx"));
1214 assert_se(!empty_or_root("/xxx"));
1215 assert_se(!empty_or_root("/xxx/"));
1216 assert_se(!empty_or_root("//yy//"));
1219 TEST(path_startswith_set
) {
1220 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
1221 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
1222 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
1223 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
1224 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL
));
1226 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1227 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
1228 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1229 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1230 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL
));
1232 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1233 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL
));
1234 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL
));
1235 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1236 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL
));
1239 TEST(path_startswith_strv
) {
1240 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1241 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1242 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1243 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1244 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1246 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1247 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1248 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1249 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1250 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1252 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1253 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL
));
1254 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL
));
1255 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1256 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1259 static void test_path_glob_can_match_one(const char *pattern
, const char *prefix
, const char *expected
) {
1260 _cleanup_free_
char *result
= NULL
;
1262 log_debug("%s(%s, %s, %s)", __func__
, pattern
, prefix
, strnull(expected
));
1264 assert_se(path_glob_can_match(pattern
, prefix
, &result
) == !!expected
);
1265 assert_se(streq_ptr(result
, expected
));
1268 TEST(path_glob_can_match
) {
1269 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL
);
1270 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1271 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa");
1272 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa");
1273 test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa");
1275 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL
);
1276 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1277 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa");
1278 test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa");
1279 test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa");
1281 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL
);
1282 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa");
1283 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa");
1284 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa");
1285 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa");
1286 test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa");
1288 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL
);
1289 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb");
1290 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL
);
1291 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*");
1292 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*");
1293 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*");
1294 test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*");
1298 log_info("PATH_MAX=%zu\n"
1299 "FILENAME_MAX=%zu\n"
1302 (size_t) FILENAME_MAX
,
1305 assert_cc(FILENAME_MAX
== PATH_MAX
);
1308 TEST(path_implies_directory
) {
1309 assert_se(!path_implies_directory(NULL
));
1310 assert_se(!path_implies_directory(""));
1311 assert_se(path_implies_directory("/"));
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("a"));
1323 assert_se(!path_implies_directory("ab"));
1324 assert_se(path_implies_directory("ab/"));
1325 assert_se(!path_implies_directory("ab/a"));
1326 assert_se(path_implies_directory("ab/a/"));
1327 assert_se(path_implies_directory("ab/a/.."));
1328 assert_se(path_implies_directory("ab/a/."));
1329 assert_se(path_implies_directory("ab/a//"));
1332 DEFINE_TEST_MAIN(LOG_DEBUG
);