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(NULL
, NULL
));
41 assert_se( path_equal("/a", "/a"));
42 assert_se(!path_equal("/a", "/b"));
43 assert_se(!path_equal("/a", NULL
));
44 assert_se(!path_equal(NULL
, "/a"));
45 assert_se(!path_equal("a", NULL
));
46 assert_se(!path_equal(NULL
, "a"));
50 assert_se(!is_path("foo"));
51 assert_se(!is_path("dos.ext"));
52 assert_se( is_path("/dir"));
53 assert_se( is_path("a/b"));
54 assert_se( is_path("a/b.ext"));
56 assert_se(!is_path("."));
57 assert_se(!is_path(""));
58 assert_se(!is_path(".."));
60 assert_se( is_path("/dev"));
61 assert_se( is_path("/./dev"));
62 assert_se( is_path("/./dev/."));
63 assert_se( is_path("/./dev."));
64 assert_se( is_path("//dev"));
65 assert_se( is_path("///dev"));
66 assert_se( is_path("/dev/"));
67 assert_se( is_path("///dev/"));
68 assert_se( is_path("/./dev/"));
69 assert_se( is_path("/../dev/"));
70 assert_se( is_path("/dev/sda"));
71 assert_se( is_path("/dev/sda5"));
72 assert_se( is_path("/dev/sda5b3"));
73 assert_se( is_path("/dev/sda5b3/idontexit"));
74 assert_se( is_path("/../dev/sda"));
75 assert_se( is_path("/../../dev/sda5"));
76 assert_se( is_path("/../../../dev/sda5b3"));
77 assert_se( is_path("/.././.././dev/sda5b3/idontexit"));
78 assert_se( is_path("/sys"));
79 assert_se( is_path("/sys/"));
80 assert_se( is_path("/./sys"));
81 assert_se( is_path("/./sys/."));
82 assert_se( is_path("/./sys."));
83 assert_se( is_path("/sys/what"));
84 assert_se( is_path("/sys/something/.."));
85 assert_se( is_path("/sys/something/../"));
86 assert_se( is_path("/sys////"));
87 assert_se( is_path("/sys////."));
88 assert_se( is_path("/sys/.."));
89 assert_se( is_path("/sys/../"));
90 assert_se( is_path("/usr/../dev/sda"));
93 TEST(is_device_path
) {
94 assert_se(!is_device_path("foo"));
95 assert_se(!is_device_path("dos.ext"));
96 assert_se(!is_device_path("/dir"));
97 assert_se(!is_device_path("a/b"));
98 assert_se(!is_device_path("a/b.ext"));
100 assert_se(!is_device_path("."));
101 assert_se(!is_device_path(""));
102 assert_se(!is_device_path(".."));
104 assert_se(!is_device_path("/dev"));
105 assert_se(!is_device_path("/./dev"));
106 assert_se(!is_device_path("/./dev/."));
107 assert_se(!is_device_path("/./dev."));
108 assert_se( is_device_path("/./dev/foo"));
109 assert_se( is_device_path("/./dev/./foo"));
110 assert_se(!is_device_path("/./dev./foo"));
111 assert_se(!is_device_path("//dev"));
112 assert_se(!is_device_path("///dev"));
113 assert_se(!is_device_path("/dev/"));
114 assert_se(!is_device_path("///dev/"));
115 assert_se(!is_device_path("/./dev/"));
116 assert_se(!is_device_path("/../dev/"));
117 assert_se( is_device_path("/dev/sda"));
118 assert_se( is_device_path("/dev/sda5"));
119 assert_se( is_device_path("/dev/sda5b3"));
120 assert_se( is_device_path("/dev/sda5b3/idontexit"));
121 assert_se(!is_device_path("/../dev/sda"));
122 assert_se(!is_device_path("/../../dev/sda5"));
123 assert_se(!is_device_path("/../../../dev/sda5b3"));
124 assert_se(!is_device_path("/.././.././dev/sda5b3/idontexit"));
125 assert_se(!is_device_path("/sys"));
126 assert_se(!is_device_path("/sys/"));
127 assert_se(!is_device_path("/./sys"));
128 assert_se(!is_device_path("/./sys/."));
129 assert_se(!is_device_path("/./sys."));
130 assert_se( is_device_path("/./sys/foo"));
131 assert_se( is_device_path("/./sys/./foo"));
132 assert_se(!is_device_path("/./sys./foo"));
133 assert_se( is_device_path("/sys/what"));
134 assert_se( is_device_path("/sys/something/.."));
135 assert_se( is_device_path("/sys/something/../"));
136 assert_se(!is_device_path("/sys////"));
137 assert_se(!is_device_path("/sys////."));
138 assert_se( is_device_path("/sys/.."));
139 assert_se( is_device_path("/sys/../"));
140 assert_se(!is_device_path("/usr/../dev/sda"));
143 static void test_path_simplify_one(const char *in
, const char *out
, PathSimplifyFlags flags
) {
146 p
= strdupa_safe(in
);
147 path_simplify_full(p
, flags
);
148 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in
, p
, out
);
149 assert_se(streq(p
, out
));
152 TEST(path_simplify
) {
153 _cleanup_free_
char *hoge
= NULL
, *hoge_out
= NULL
;
154 char foo
[NAME_MAX
* 2];
156 test_path_simplify_one("", "", 0);
157 test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc", 0);
158 test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc", 0);
159 test_path_simplify_one("///", "/", 0);
160 test_path_simplify_one("///", "/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
161 test_path_simplify_one("///.//", "/", 0);
162 test_path_simplify_one("///.//.///", "/", 0);
163 test_path_simplify_one("////.././///../.", "/", 0);
164 test_path_simplify_one(".", ".", 0);
165 test_path_simplify_one("./", ".", 0);
166 test_path_simplify_one("./", "./", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
167 test_path_simplify_one(".///.//./.", ".", 0);
168 test_path_simplify_one(".///.//././/", ".", 0);
169 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
170 "/aaa/.bbb/../c./d.dd/..eeee", 0);
171 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
172 "/aaa/.bbb/../c./d.dd/..eeee/..", 0);
173 test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
174 "aaa/.bbb/../c./d.dd/..eeee/..", 0);
175 test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
176 "../aaa/.bbb/../c./d.dd/..eeee/..", 0);
177 test_path_simplify_one("abc///", "abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
179 test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
180 test_path_simplify_one("/../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
181 test_path_simplify_one("/../abc///", "/abc", 0);
182 test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
183 test_path_simplify_one("/../abc///..", "/abc/..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
184 test_path_simplify_one("/../abc///../", "/abc/../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
185 test_path_simplify_one("/../abc///../", "/abc/..", 0);
187 test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
188 test_path_simplify_one("/../../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
189 test_path_simplify_one("/../../abc///", "/abc", 0);
190 test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
191 test_path_simplify_one("/../../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
192 test_path_simplify_one("/../../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
193 test_path_simplify_one("/../../abc///../../", "/abc/../..", 0);
195 test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
196 test_path_simplify_one("/.././../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
197 test_path_simplify_one("/.././../abc///", "/abc", 0);
198 test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
199 test_path_simplify_one("/.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
200 test_path_simplify_one("/.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
201 test_path_simplify_one("/.././../abc///../../", "/abc/../..", 0);
203 test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
204 test_path_simplify_one("/./.././../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
205 test_path_simplify_one("/./.././../abc///", "/abc", 0);
206 test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
207 test_path_simplify_one("/./.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
208 test_path_simplify_one("/./.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
209 test_path_simplify_one("/./.././../abc///../../", "/abc/../..", 0);
211 test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
212 test_path_simplify_one("/.../abc///", "/.../abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
213 test_path_simplify_one("/.../abc///", "/.../abc", 0);
214 test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
215 test_path_simplify_one("/.../abc///...", "/.../abc/...", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
216 test_path_simplify_one("/.../abc///.../", "/.../abc/.../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH
);
217 test_path_simplify_one("/.../abc///.../", "/.../abc/...", 0);
219 memset(foo
, 'a', sizeof(foo
) -1);
222 test_path_simplify_one(foo
, foo
, 0);
224 hoge
= strjoin("/", foo
);
226 test_path_simplify_one(hoge
, hoge
, 0);
229 hoge
= strjoin("a////.//././//./b///././/./c/////././//./", foo
, "//.//////d/e/.//f/");
232 hoge_out
= strjoin("a/b/c/", foo
, "//.//////d/e/.//f/");
235 test_path_simplify_one(hoge
, hoge_out
, 0);
238 static void test_path_compare_one(const char *a
, const char *b
, int expected
) {
241 assert_se(path_compare(a
, a
) == 0);
242 assert_se(path_compare(b
, b
) == 0);
244 r
= path_compare(a
, b
);
245 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
246 r
= path_compare(b
, a
);
247 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
249 assert_se(path_equal(a
, a
) == 1);
250 assert_se(path_equal(b
, b
) == 1);
251 assert_se(path_equal(a
, b
) == (expected
== 0));
252 assert_se(path_equal(b
, a
) == (expected
== 0));
256 test_path_compare_one("/goo", "/goo", 0);
257 test_path_compare_one("/goo", "/goo", 0);
258 test_path_compare_one("//goo", "/goo", 0);
259 test_path_compare_one("//goo/////", "/goo", 0);
260 test_path_compare_one("goo/////", "goo", 0);
261 test_path_compare_one("/goo/boo", "/goo//boo", 0);
262 test_path_compare_one("//goo/boo", "/goo/boo//", 0);
263 test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
264 test_path_compare_one("/.", "//.///", 0);
265 test_path_compare_one("/x", "x/", 1);
266 test_path_compare_one("x/", "/", -1);
267 test_path_compare_one("/x/./y", "x/y", 1);
268 test_path_compare_one("/x/./y", "/x/y", 0);
269 test_path_compare_one("/x/./././y", "/x/y/././.", 0);
270 test_path_compare_one("./x/./././y", "./x/y/././.", 0);
271 test_path_compare_one(".", "./.", 0);
272 test_path_compare_one(".", "././.", 0);
273 test_path_compare_one("./..", ".", 1);
274 test_path_compare_one("x/.y", "x/y", -1);
275 test_path_compare_one("foo", "/foo", -1);
276 test_path_compare_one("/foo", "/foo/bar", -1);
277 test_path_compare_one("/foo/aaa", "/foo/b", -1);
278 test_path_compare_one("/foo/aaa", "/foo/b/a", -1);
279 test_path_compare_one("/foo/a", "/foo/aaa", -1);
280 test_path_compare_one("/foo/a/b", "/foo/aaa", -1);
283 static void test_path_compare_filename_one(const char *a
, const char *b
, int expected
) {
286 assert_se(path_compare_filename(a
, a
) == 0);
287 assert_se(path_compare_filename(b
, b
) == 0);
289 r
= path_compare_filename(a
, b
);
290 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
291 r
= path_compare_filename(b
, a
);
292 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
294 assert_se(path_equal_filename(a
, a
) == 1);
295 assert_se(path_equal_filename(b
, b
) == 1);
296 assert_se(path_equal_filename(a
, b
) == (expected
== 0));
297 assert_se(path_equal_filename(b
, a
) == (expected
== 0));
300 TEST(path_compare_filename
) {
301 test_path_compare_filename_one("/goo", "/goo", 0);
302 test_path_compare_filename_one("/goo", "/goo", 0);
303 test_path_compare_filename_one("//goo", "/goo", 0);
304 test_path_compare_filename_one("//goo/////", "/goo", 0);
305 test_path_compare_filename_one("goo/////", "goo", 0);
306 test_path_compare_filename_one("/goo/boo", "/goo//boo", 0);
307 test_path_compare_filename_one("//goo/boo", "/goo/boo//", 0);
308 test_path_compare_filename_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
309 test_path_compare_filename_one("/.", "//.///", -1);
310 test_path_compare_filename_one("/x", "x/", 0);
311 test_path_compare_filename_one("x/", "/", 1);
312 test_path_compare_filename_one("/x/./y", "x/y", 0);
313 test_path_compare_filename_one("/x/./y", "/x/y", 0);
314 test_path_compare_filename_one("/x/./././y", "/x/y/././.", 0);
315 test_path_compare_filename_one("./x/./././y", "./x/y/././.", 0);
316 test_path_compare_filename_one(".", "./.", -1);
317 test_path_compare_filename_one(".", "././.", -1);
318 test_path_compare_filename_one("./..", ".", 1);
319 test_path_compare_filename_one("x/.y", "x/y", -1);
320 test_path_compare_filename_one("foo", "/foo", 0);
321 test_path_compare_filename_one("/foo", "/foo/bar", 1);
322 test_path_compare_filename_one("/foo/aaa", "/foo/b", -1);
323 test_path_compare_filename_one("/foo/aaa", "/foo/b/a", 1);
324 test_path_compare_filename_one("/foo/a", "/foo/aaa", -1);
325 test_path_compare_filename_one("/foo/a/b", "/foo/aaa", 1);
326 test_path_compare_filename_one("/a/c", "/b/c", 0);
327 test_path_compare_filename_one("/a", "/a", 0);
328 test_path_compare_filename_one("/a/b", "/a/c", -1);
329 test_path_compare_filename_one("/b", "/c", -1);
332 TEST(path_equal_root
) {
333 /* Nail down the details of how path_equal("/", ...) works. */
335 assert_se(path_equal("/", "/"));
336 assert_se(path_equal("/", "//"));
338 assert_se(path_equal("/", "/./"));
339 assert_se(!path_equal("/", "/../"));
341 assert_se(!path_equal("/", "/.../"));
343 /* Make sure that files_same works as expected. */
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) > 0);
351 assert_se(inode_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
352 assert_se(inode_same("/", "/../", 0) > 0);
353 assert_se(inode_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
355 assert_se(inode_same("/", "/.../", 0) == -ENOENT
);
356 assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
358 /* The same for path_equal_or_files_same. */
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
));
367 assert_se(path_equal_or_inode_same("/", "/../", 0));
368 assert_se(path_equal_or_inode_same("/", "/../", AT_SYMLINK_NOFOLLOW
));
370 assert_se(!path_equal_or_inode_same("/", "/.../", 0));
371 assert_se(!path_equal_or_inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
374 TEST(find_executable_full
) {
376 char* test_file_name
;
377 _cleanup_close_
int fd
= -EBADF
;
378 char fn
[] = "/tmp/test-XXXXXX";
380 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
382 assert_se(streq(basename(p
), "sh"));
385 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
387 assert_se(streq(basename(p
), "sh"));
390 _cleanup_free_
char *oldpath
= NULL
;
393 assert_se(oldpath
= strdup(p
));
395 assert_se(unsetenv("PATH") == 0);
397 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
399 assert_se(streq(basename(p
), "sh"));
402 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
404 assert_se(streq(basename(p
), "sh"));
408 assert_se(setenv("PATH", oldpath
, true) >= 0);
410 assert_se((fd
= mkostemp_safe(fn
)) >= 0);
411 assert_se(fchmod(fd
, 0755) >= 0);
413 test_file_name
= basename(fn
);
415 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == 0);
417 assert_se(streq(p
, fn
));
421 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == -ENOENT
);
424 TEST(find_executable
) {
427 assert_se(find_executable("/bin/sh", &p
) == 0);
429 assert_se(path_equal(p
, "/bin/sh"));
432 assert_se(find_executable(saved_argv
[0], &p
) == 0);
434 assert_se(endswith(p
, "/test-path-util"));
435 assert_se(path_is_absolute(p
));
438 assert_se(find_executable("sh", &p
) == 0);
440 assert_se(endswith(p
, "/sh"));
441 assert_se(path_is_absolute(p
));
444 assert_se(find_executable("/bin/touch", &p
) == 0);
445 assert_se(streq(p
, "/bin/touch"));
448 assert_se(find_executable("touch", &p
) == 0);
449 assert_se(path_is_absolute(p
));
450 assert_se(streq(basename(p
), "touch"));
453 assert_se(find_executable("xxxx-xxxx", &p
) == -ENOENT
);
454 assert_se(find_executable("/some/dir/xxxx-xxxx", &p
) == -ENOENT
);
455 assert_se(find_executable("/proc/filesystems", &p
) == -EACCES
);
458 static void test_find_executable_exec_one(const char *path
) {
459 _cleanup_free_
char *t
= NULL
;
460 _cleanup_close_
int fd
= -EBADF
;
464 r
= find_executable_full(path
, NULL
, NULL
, false, &t
, &fd
);
466 log_info_errno(r
, "%s: %s → %s: %d/%m", __func__
, path
, t
?: "-", fd
);
468 assert_se(fd
> STDERR_FILENO
);
469 assert_se(path_is_absolute(t
));
470 if (path_is_absolute(path
))
471 assert_se(streq(t
, path
));
476 r
= fexecve_or_execve(fd
, t
, STRV_MAKE(t
, "--version"), STRV_MAKE(NULL
));
477 log_error_errno(r
, "[f]execve: %m");
481 assert_se(wait_for_terminate_and_check(t
, pid
, WAIT_LOG
) == 0);
484 TEST(find_executable_exec
) {
485 test_find_executable_exec_one("touch");
486 test_find_executable_exec_one("/bin/touch");
488 _cleanup_free_
char *script
= NULL
;
489 assert_se(get_testdata_dir("test-path-util/script.sh", &script
) >= 0);
490 test_find_executable_exec_one(script
);
494 static const char* const values
[] = {
507 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
508 log_error("---%s---", s
);
509 assert_se(streq(s
, values
[i
++]));
511 ASSERT_NULL(values
[i
]);
514 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
515 log_error("---%s---", s
);
516 assert_se(streq(s
, values
[i
++]));
518 ASSERT_NULL(values
[i
]);
521 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
522 assert_se(streq(s
, values
[i
++]));
523 ASSERT_NULL(values
[i
]);
526 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
527 assert_se(streq(s
, values
[i
++]));
528 ASSERT_NULL(values
[i
]);
530 PATH_FOREACH_PREFIX(s
, "////")
531 assert_not_reached();
534 PATH_FOREACH_PREFIX_MORE(s
, "////") {
536 assert_se(streq(s
, ""));
541 PATH_FOREACH_PREFIX(s
, "")
542 assert_not_reached();
545 PATH_FOREACH_PREFIX_MORE(s
, "") {
547 assert_se(streq(s
, ""));
553 #define test_join(expected, ...) { \
554 _cleanup_free_ char *z = NULL; \
555 z = path_join(__VA_ARGS__); \
556 log_debug("got \"%s\", expected \"%s\"", z, expected); \
557 assert_se(streq(z, expected)); \
560 test_join("/root/a/b/c", "/root", "/a/b", "/c");
561 test_join("/root/a/b/c", "/root", "a/b", "c");
562 test_join("/root/a/b/c", "/root", "/a/b", "c");
563 test_join("/root/c", "/root", "/", "c");
564 test_join("/root/", "/root", "/", NULL
);
566 test_join("/a/b/c", "", "/a/b", "/c");
567 test_join("a/b/c", "", "a/b", "c");
568 test_join("/a/b/c", "", "/a/b", "c");
569 test_join("/c", "", "/", "c");
570 test_join("/", "", "/", NULL
);
572 test_join("/a/b/c", NULL
, "/a/b", "/c");
573 test_join("a/b/c", NULL
, "a/b", "c");
574 test_join("/a/b/c", NULL
, "/a/b", "c");
575 test_join("/c", NULL
, "/", "c");
576 test_join("/", NULL
, "/", NULL
);
578 test_join("", "", NULL
);
579 test_join("", NULL
, "");
580 test_join("", NULL
, NULL
);
582 test_join("foo/bar", "foo", "bar");
583 test_join("foo/bar", "", "foo", "bar");
584 test_join("foo/bar", NULL
, "foo", NULL
, "bar");
585 test_join("foo/bar", "", "foo", "", "bar", "");
586 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
588 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
589 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
590 test_join("foo/bar/baz", "foo", "bar", "baz");
591 test_join("foo/bar/baz", "foo/", "bar", "/baz");
592 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
593 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
597 _cleanup_free_
char *p
= NULL
;
599 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
600 assert_se(streq(p
, "foo/bar/baz"));
602 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
603 assert_se(streq(p
, "foo/bar/baz/foo/bar/baz"));
606 assert_se(path_extend(&p
, "foo") == p
);
607 assert_se(streq(p
, "foo"));
609 assert_se(path_extend(&p
, "/foo") == p
);
610 assert_se(streq(p
, "foo/foo"));
611 assert_se(path_extend(&p
, "/waaaah/wahhh//") == p
);
612 assert_se(streq(p
, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
613 assert_se(path_extend(&p
, "/aaa/bbb/") == p
);
614 assert_se(streq(p
, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
616 assert_se(free_and_strdup(&p
, "/") >= 0);
617 assert_se(path_extend(&p
, "foo") == p
);
618 assert_se(streq(p
, "/foo"));
622 /* Ensure we use a sane default for PATH. */
623 assert_se(unsetenv("PATH") == 0);
625 /* We might or might not find one of these, so keep the test lax. */
626 assert_se(fsck_exists_for_fstype("minix") >= 0);
628 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
629 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
632 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
633 _cleanup_free_
char *z
= NULL
;
636 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
638 r
= path_make_relative(from
, to
, &z
);
639 assert_se((r
>= 0) == !!expected
);
640 assert_se(streq_ptr(z
, expected
));
643 TEST(path_make_relative
) {
644 test_path_make_relative_one("some/relative/path", "/some/path", NULL
);
645 test_path_make_relative_one("/some/path", "some/relative/path", NULL
);
646 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL
);
648 test_path_make_relative_one("/", "/", ".");
649 test_path_make_relative_one("/", "/some/path", "some/path");
650 test_path_make_relative_one("/some/path", "/some/path", ".");
651 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
652 test_path_make_relative_one("/some/path", "/", "../..");
653 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
654 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
655 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
658 static void test_path_make_relative_parent_one(const char *from
, const char *to
, const char *expected
) {
659 _cleanup_free_
char *z
= NULL
;
662 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
664 r
= path_make_relative_parent(from
, to
, &z
);
665 assert_se((r
>= 0) == !!expected
);
666 assert_se(streq_ptr(z
, expected
));
669 TEST(path_make_relative_parent
) {
670 test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL
);
671 test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL
);
672 test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL
);
673 test_path_make_relative_parent_one("/", "/aaa", NULL
);
675 test_path_make_relative_parent_one("/hoge", "/", ".");
676 test_path_make_relative_parent_one("/hoge", "/some/path", "some/path");
677 test_path_make_relative_parent_one("/some/path/hoge", "/some/path", ".");
678 test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir");
679 test_path_make_relative_parent_one("/some/path/hoge", "/", "../..");
680 test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path");
681 test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path");
682 test_path_make_relative_parent_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//hoge", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
685 TEST(path_strv_resolve
) {
686 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
687 _cleanup_strv_free_
char **search_dirs
= NULL
;
688 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
690 assert_se(mkdtemp(tmp_dir
) != NULL
);
692 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
693 assert_se(search_dirs
);
694 STRV_FOREACH(d
, search_dirs
) {
695 char *p
= path_join(tmp_dir
, *d
);
697 assert_se(strv_push(&absolute_dirs
, p
) == 0);
700 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
701 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
702 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
704 path_strv_resolve(search_dirs
, tmp_dir
);
705 assert_se(streq(search_dirs
[0], "/dir1"));
706 assert_se(streq(search_dirs
[1], "/dir2"));
707 assert_se(streq(search_dirs
[2], "/dir2"));
709 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
712 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
715 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
717 p
= path_startswith(path
, prefix
);
718 assert_se(streq_ptr(p
, expected
));
720 q
= strjoina(skipped
, p
);
721 assert_se(streq(q
, path
));
722 assert_se(p
== path
+ strlen(skipped
));
726 TEST(path_startswith
) {
727 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
728 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
729 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
730 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
731 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
732 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
733 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
734 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
735 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
736 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
738 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo", "/foo/./", "bar///barfoo/./.");
739 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/", "/foo/./", "bar///barfoo/./.");
740 test_path_startswith_one("/foo/./bar///barfoo/./.", "/", "/", "foo/./bar///barfoo/./.");
741 test_path_startswith_one("/foo/./bar///barfoo/./.", "////", "/", "foo/./bar///barfoo/./.");
742 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo//bar/////barfoo///", "/foo/./bar///barfoo/./.", "");
743 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo////", "/foo/./bar///barfoo/./.", "");
744 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar///barfoo/", "/foo/./bar///barfoo/./.", "");
745 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo////bar/barfoo/", "/foo/./bar///barfoo/./.", "");
746 test_path_startswith_one("/foo/./bar///barfoo/./.", "////foo/bar/barfoo/", "/foo/./bar///barfoo/./.", "");
747 test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo", "/foo/./bar///barfoo/./.", "");
749 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL
, NULL
);
750 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL
, NULL
);
751 test_path_startswith_one("/foo/bar/barfoo/", "", NULL
, NULL
);
752 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL
, NULL
);
753 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL
, NULL
);
754 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL
, NULL
);
755 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL
, NULL
);
756 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL
, NULL
);
759 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
760 _cleanup_free_
char *s
= NULL
;
763 assert_se(s
= path_join(r
, p
));
764 assert_se(path_equal(s
, expected
));
766 t
= prefix_roota(r
, p
);
768 assert_se(path_equal(t
, expected
));
772 test_prefix_root_one("/", "/foo", "/foo");
773 test_prefix_root_one(NULL
, "/foo", "/foo");
774 test_prefix_root_one("", "/foo", "/foo");
775 test_prefix_root_one("///", "/foo", "/foo");
776 test_prefix_root_one("/", "////foo", "/foo");
777 test_prefix_root_one(NULL
, "////foo", "/foo");
778 test_prefix_root_one("/", "foo", "/foo");
779 test_prefix_root_one("", "foo", "foo");
780 test_prefix_root_one(NULL
, "foo", "foo");
782 test_prefix_root_one("/foo", "/bar", "/foo/bar");
783 test_prefix_root_one("/foo", "bar", "/foo/bar");
784 test_prefix_root_one("foo", "bar", "foo/bar");
785 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
786 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
787 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
790 TEST(file_in_same_dir
) {
793 assert_se(file_in_same_dir("/", "a", &t
) == -EADDRNOTAVAIL
);
795 assert_se(file_in_same_dir("/", "/a", &t
) >= 0);
796 assert_se(streq(t
, "/a"));
799 assert_se(file_in_same_dir("", "a", &t
) == -EINVAL
);
801 assert_se(file_in_same_dir("a/", "x", &t
) >= 0);
802 assert_se(streq(t
, "x"));
805 assert_se(file_in_same_dir("bar/foo", "bar", &t
) >= 0);
806 assert_se(streq(t
, "bar/bar"));
810 static void test_path_find_first_component_one(
816 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
818 for (const char *p
= path
;;) {
822 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
826 assert_se(p
== path
+ strlen_ptr(path
));
827 assert_se(isempty(p
));
833 assert_se(strv_isempty(expected
));
838 assert_se(strcspn(e
, "/") == (size_t) r
);
839 assert_se(strlen_ptr(*expected
) == (size_t) r
);
840 assert_se(strneq(e
, *expected
++, r
));
843 log_debug("p=%s", p
);
844 if (!isempty(*expected
))
845 assert_se(startswith(p
, *expected
));
847 assert_se(p
== path
+ strlen_ptr(path
));
848 assert_se(isempty(p
));
853 TEST(path_find_first_component
) {
854 _cleanup_free_
char *hoge
= NULL
;
855 char foo
[NAME_MAX
* 2];
857 test_path_find_first_component_one(NULL
, false, NULL
, 0);
858 test_path_find_first_component_one("", false, NULL
, 0);
859 test_path_find_first_component_one("/", false, NULL
, 0);
860 test_path_find_first_component_one(".", false, NULL
, 0);
861 test_path_find_first_component_one("./", false, NULL
, 0);
862 test_path_find_first_component_one("./.", false, NULL
, 0);
863 test_path_find_first_component_one("..", false, NULL
, -EINVAL
);
864 test_path_find_first_component_one("/..", false, NULL
, -EINVAL
);
865 test_path_find_first_component_one("./..", false, NULL
, -EINVAL
);
866 test_path_find_first_component_one("////./././//.", false, NULL
, 0);
867 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
868 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
869 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL
);
870 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL
);
871 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
873 test_path_find_first_component_one(NULL
, true, NULL
, 0);
874 test_path_find_first_component_one("", true, NULL
, 0);
875 test_path_find_first_component_one("/", true, NULL
, 0);
876 test_path_find_first_component_one(".", true, NULL
, 0);
877 test_path_find_first_component_one("./", true, NULL
, 0);
878 test_path_find_first_component_one("./.", true, NULL
, 0);
879 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
880 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
881 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
882 test_path_find_first_component_one("////./././//.", true, NULL
, 0);
883 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
884 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
885 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
886 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
887 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
889 memset(foo
, 'a', sizeof(foo
) -1);
892 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
893 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
895 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
898 test_path_find_first_component_one(hoge
, false, STRV_MAKE("a", "b", "c"), -EINVAL
);
899 test_path_find_first_component_one(hoge
, true, STRV_MAKE("a", "b", "c"), -EINVAL
);
902 static void test_path_find_last_component_one(
908 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
910 for (const char *next
= NULL
;;) {
914 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
917 assert_se(next
== path
);
921 assert_se(strv_isempty(expected
));
926 assert_se(strcspn(e
, "/") == (size_t) r
);
927 assert_se(strlen_ptr(*expected
) == (size_t) r
);
928 assert_se(strneq(e
, *expected
++, r
));
931 log_debug("path=%s\nnext=%s", path
, next
);
932 if (!isempty(*expected
)) {
933 assert_se(next
< path
+ strlen(path
));
934 assert_se(next
>= path
+ strlen(*expected
));
935 assert_se(startswith(next
- strlen(*expected
), *expected
));
937 assert_se(next
== path
);
941 TEST(path_find_last_component
) {
942 _cleanup_free_
char *hoge
= NULL
;
943 char foo
[NAME_MAX
* 2];
945 test_path_find_last_component_one(NULL
, false, NULL
, 0);
946 test_path_find_last_component_one("", false, NULL
, 0);
947 test_path_find_last_component_one("/", false, NULL
, 0);
948 test_path_find_last_component_one(".", false, NULL
, 0);
949 test_path_find_last_component_one("./", false, NULL
, 0);
950 test_path_find_last_component_one("./.", false, NULL
, 0);
951 test_path_find_last_component_one("..", false, NULL
, -EINVAL
);
952 test_path_find_last_component_one("/..", false, NULL
, -EINVAL
);
953 test_path_find_last_component_one("./..", false, NULL
, -EINVAL
);
954 test_path_find_last_component_one("////./././//.", false, NULL
, 0);
955 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
956 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
957 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL
);
958 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL
);
960 test_path_find_last_component_one(NULL
, true, NULL
, 0);
961 test_path_find_last_component_one("", true, NULL
, 0);
962 test_path_find_last_component_one("/", true, NULL
, 0);
963 test_path_find_last_component_one(".", true, NULL
, 0);
964 test_path_find_last_component_one("./", true, NULL
, 0);
965 test_path_find_last_component_one("./.", true, NULL
, 0);
966 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
967 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
968 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
969 test_path_find_last_component_one("////./././//.", true, NULL
, 0);
970 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
971 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
972 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
973 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
975 memset(foo
, 'a', sizeof(foo
) -1);
978 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
979 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
981 hoge
= strjoin(foo
, "/a/b/c/");
984 test_path_find_last_component_one(hoge
, false, STRV_MAKE("c", "b", "a"), -EINVAL
);
985 test_path_find_last_component_one(hoge
, true, STRV_MAKE("c", "b", "a"), -EINVAL
);
988 TEST(last_path_component
) {
989 assert_se(last_path_component(NULL
) == NULL
);
990 assert_se(streq(last_path_component("a/b/c"), "c"));
991 assert_se(streq(last_path_component("a/b/c/"), "c/"));
992 assert_se(streq(last_path_component("/"), "/"));
993 assert_se(streq(last_path_component("//"), "/"));
994 assert_se(streq(last_path_component("///"), "/"));
995 assert_se(streq(last_path_component("."), "."));
996 assert_se(streq(last_path_component("./."), "."));
997 assert_se(streq(last_path_component("././"), "./"));
998 assert_se(streq(last_path_component("././/"), ".//"));
999 assert_se(streq(last_path_component("/foo/a"), "a"));
1000 assert_se(streq(last_path_component("/foo/a/"), "a/"));
1001 assert_se(streq(last_path_component(""), ""));
1002 assert_se(streq(last_path_component("a"), "a"));
1003 assert_se(streq(last_path_component("a/"), "a/"));
1004 assert_se(streq(last_path_component("/a"), "a"));
1005 assert_se(streq(last_path_component("/a/"), "a/"));
1008 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
1009 _cleanup_free_
char *k
= NULL
;
1012 r
= path_extract_filename(input
, &k
);
1013 log_info("%s → %s/%s [expected: %s/%s]",
1015 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
1016 strnull(output
), ret
< 0 ? STRERROR(ret
) : "-");
1017 assert_se(streq_ptr(k
, output
));
1018 assert_se(r
== ret
);
1021 TEST(path_extract_filename
) {
1022 test_path_extract_filename_one(NULL
, NULL
, -EINVAL
);
1023 test_path_extract_filename_one("a/b/c", "c", 0);
1024 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY
);
1025 test_path_extract_filename_one("/", NULL
, -EADDRNOTAVAIL
);
1026 test_path_extract_filename_one("//", NULL
, -EADDRNOTAVAIL
);
1027 test_path_extract_filename_one("///", NULL
, -EADDRNOTAVAIL
);
1028 test_path_extract_filename_one("/.", NULL
, -EADDRNOTAVAIL
);
1029 test_path_extract_filename_one(".", NULL
, -EADDRNOTAVAIL
);
1030 test_path_extract_filename_one("./", NULL
, -EADDRNOTAVAIL
);
1031 test_path_extract_filename_one("./.", NULL
, -EADDRNOTAVAIL
);
1032 test_path_extract_filename_one("././", NULL
, -EADDRNOTAVAIL
);
1033 test_path_extract_filename_one("././/", NULL
, -EADDRNOTAVAIL
);
1034 test_path_extract_filename_one("/foo/a", "a", 0);
1035 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY
);
1036 test_path_extract_filename_one("", NULL
, -EINVAL
);
1037 test_path_extract_filename_one("a", "a", 0);
1038 test_path_extract_filename_one("a/", "a", O_DIRECTORY
);
1039 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY
);
1040 test_path_extract_filename_one("/a", "a", 0);
1041 test_path_extract_filename_one("/a/", "a", O_DIRECTORY
);
1042 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY
);
1043 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY
);
1044 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY
);
1045 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY
);
1046 test_path_extract_filename_one("xx/..", NULL
, -EINVAL
);
1047 test_path_extract_filename_one("..", NULL
, -EINVAL
);
1048 test_path_extract_filename_one("/..", NULL
, -EINVAL
);
1049 test_path_extract_filename_one("../", NULL
, -EINVAL
);
1052 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
1053 _cleanup_free_
char *k
= NULL
;
1056 r
= path_extract_directory(input
, &k
);
1057 log_info("%s → %s/%s [expected: %s/%s]",
1059 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
1060 strnull(output
), STRERROR(ret
));
1061 assert_se(streq_ptr(k
, output
));
1062 assert_se(r
== ret
);
1064 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
1065 * joined parts are identical to the original again */
1067 _cleanup_free_
char *f
= NULL
;
1069 r
= path_extract_filename(input
, &f
);
1071 _cleanup_free_
char *j
= NULL
;
1073 assert_se(j
= path_join(k
, f
));
1074 assert_se(path_equal(input
, j
));
1079 TEST(path_extract_directory
) {
1080 test_path_extract_directory_one(NULL
, NULL
, -EINVAL
);
1081 test_path_extract_directory_one("a/b/c", "a/b", 0);
1082 test_path_extract_directory_one("a/b/c/", "a/b", 0);
1083 test_path_extract_directory_one("/", NULL
, -EADDRNOTAVAIL
);
1084 test_path_extract_directory_one("//", NULL
, -EADDRNOTAVAIL
);
1085 test_path_extract_directory_one("///", NULL
, -EADDRNOTAVAIL
);
1086 test_path_extract_directory_one("/.", NULL
, -EADDRNOTAVAIL
);
1087 test_path_extract_directory_one(".", NULL
, -EADDRNOTAVAIL
);
1088 test_path_extract_directory_one("./", NULL
, -EADDRNOTAVAIL
);
1089 test_path_extract_directory_one("./.", NULL
, -EADDRNOTAVAIL
);
1090 test_path_extract_directory_one("././", NULL
, -EADDRNOTAVAIL
);
1091 test_path_extract_directory_one("././/", NULL
, -EADDRNOTAVAIL
);
1092 test_path_extract_directory_one("/foo/a", "/foo", 0);
1093 test_path_extract_directory_one("/foo/a/", "/foo", 0);
1094 test_path_extract_directory_one("", NULL
, -EINVAL
);
1095 test_path_extract_directory_one("a", NULL
, -EDESTADDRREQ
);
1096 test_path_extract_directory_one("a/", NULL
, -EDESTADDRREQ
);
1097 test_path_extract_directory_one("a/././//.", NULL
, -EDESTADDRREQ
);
1098 test_path_extract_directory_one("/a", "/", 0);
1099 test_path_extract_directory_one("/a/", "/", 0);
1100 test_path_extract_directory_one("/a//./.", "/", 0);
1101 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
1102 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
1103 test_path_extract_directory_one("xx/.", NULL
, -EDESTADDRREQ
);
1104 test_path_extract_directory_one("xx/..", NULL
, -EINVAL
);
1105 test_path_extract_directory_one("..", NULL
, -EINVAL
);
1106 test_path_extract_directory_one("/..", NULL
, -EINVAL
);
1107 test_path_extract_directory_one("../", NULL
, -EINVAL
);
1110 TEST(filename_is_valid
) {
1111 char foo
[NAME_MAX
+2];
1113 assert_se(!filename_is_valid(""));
1114 assert_se(!filename_is_valid("/bar/foo"));
1115 assert_se(!filename_is_valid("/"));
1116 assert_se(!filename_is_valid("."));
1117 assert_se(!filename_is_valid(".."));
1118 assert_se(!filename_is_valid("bar/foo"));
1119 assert_se(!filename_is_valid("bar/foo/"));
1120 assert_se(!filename_is_valid("bar//"));
1122 memset(foo
, 'a', sizeof(foo
) - 1);
1125 assert_se(!filename_is_valid(foo
));
1127 assert_se(filename_is_valid("foo_bar-333"));
1128 assert_se(filename_is_valid("o.o"));
1131 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
1132 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
1134 assert_se(path_is_valid(p
) == ret
);
1136 ret
= !streq(p
, "..") &&
1137 !startswith(p
, "../") &&
1138 !endswith(p
, "/..") &&
1140 assert_se(path_is_safe(p
) == ret
);
1143 TEST(path_is_valid_and_safe
) {
1144 char foo
[PATH_MAX
+2];
1147 test_path_is_valid_and_safe_one("", false);
1148 test_path_is_valid_and_safe_one("/bar/foo", true);
1149 test_path_is_valid_and_safe_one("/bar/foo/", true);
1150 test_path_is_valid_and_safe_one("/bar/foo/", true);
1151 test_path_is_valid_and_safe_one("//bar//foo//", true);
1152 test_path_is_valid_and_safe_one("/", true);
1153 test_path_is_valid_and_safe_one("/////", true);
1154 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
1155 test_path_is_valid_and_safe_one(".", true);
1156 test_path_is_valid_and_safe_one("..", true);
1157 test_path_is_valid_and_safe_one("bar/foo", true);
1158 test_path_is_valid_and_safe_one("bar/foo/", true);
1159 test_path_is_valid_and_safe_one("bar//", true);
1161 memset(foo
, 'a', sizeof(foo
) -1);
1164 test_path_is_valid_and_safe_one(foo
, false);
1166 c
= strjoina("/xxx/", foo
, "/yyy");
1167 test_path_is_valid_and_safe_one(c
, false);
1169 test_path_is_valid_and_safe_one("foo_bar-333", true);
1170 test_path_is_valid_and_safe_one("o.o", true);
1173 TEST(hidden_or_backup_file
) {
1174 assert_se(hidden_or_backup_file(".hidden"));
1175 assert_se(hidden_or_backup_file("..hidden"));
1176 assert_se(!hidden_or_backup_file("hidden."));
1178 assert_se(hidden_or_backup_file("backup~"));
1179 assert_se(hidden_or_backup_file(".backup~"));
1181 assert_se(hidden_or_backup_file("lost+found"));
1182 assert_se(hidden_or_backup_file("aquota.user"));
1183 assert_se(hidden_or_backup_file("aquota.group"));
1185 assert_se(hidden_or_backup_file("test.rpmnew"));
1186 assert_se(hidden_or_backup_file("test.dpkg-old"));
1187 assert_se(hidden_or_backup_file("test.dpkg-remove"));
1188 assert_se(hidden_or_backup_file("test.swp"));
1190 assert_se(!hidden_or_backup_file("test.rpmnew."));
1191 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1194 TEST(skip_dev_prefix
) {
1195 assert_se(streq(skip_dev_prefix("/"), "/"));
1196 assert_se(streq(skip_dev_prefix("/dev"), ""));
1197 assert_se(streq(skip_dev_prefix("/dev/"), ""));
1198 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
1199 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
1200 assert_se(streq(skip_dev_prefix("//dev"), ""));
1201 assert_se(streq(skip_dev_prefix("//dev//"), ""));
1202 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
1203 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
1204 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
1205 assert_se(streq(skip_dev_prefix("foo"), "foo"));
1208 TEST(empty_or_root
) {
1209 assert_se(empty_or_root(NULL
));
1210 assert_se(empty_or_root(""));
1211 assert_se(empty_or_root("/"));
1212 assert_se(empty_or_root("//"));
1213 assert_se(empty_or_root("///"));
1214 assert_se(empty_or_root("/////////////////"));
1215 assert_se(!empty_or_root("xxx"));
1216 assert_se(!empty_or_root("/xxx"));
1217 assert_se(!empty_or_root("/xxx/"));
1218 assert_se(!empty_or_root("//yy//"));
1221 TEST(path_startswith_set
) {
1222 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
1223 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
1224 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
1225 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
1226 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL
));
1228 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1229 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
1230 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1231 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1232 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL
));
1234 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1235 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL
));
1236 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL
));
1237 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1238 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL
));
1241 TEST(path_startswith_strv
) {
1242 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1243 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1244 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1245 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1246 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1248 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1249 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1250 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1251 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1252 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1254 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1255 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL
));
1256 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL
));
1257 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1258 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1261 static void test_path_glob_can_match_one(const char *pattern
, const char *prefix
, const char *expected
) {
1262 _cleanup_free_
char *result
= NULL
;
1264 log_debug("%s(%s, %s, %s)", __func__
, pattern
, prefix
, strnull(expected
));
1266 assert_se(path_glob_can_match(pattern
, prefix
, &result
) == !!expected
);
1267 assert_se(streq_ptr(result
, expected
));
1270 TEST(path_glob_can_match
) {
1271 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL
);
1272 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1273 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa");
1274 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa");
1275 test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa");
1277 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL
);
1278 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1279 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa");
1280 test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa");
1281 test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa");
1283 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL
);
1284 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa");
1285 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa");
1286 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa");
1287 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa");
1288 test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa");
1290 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL
);
1291 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb");
1292 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL
);
1293 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*");
1294 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*");
1295 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*");
1296 test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*");
1300 log_info("PATH_MAX=%zu\n"
1301 "FILENAME_MAX=%zu\n"
1304 (size_t) FILENAME_MAX
,
1307 assert_cc(FILENAME_MAX
== PATH_MAX
);
1310 TEST(path_implies_directory
) {
1311 assert_se(!path_implies_directory(NULL
));
1312 assert_se(!path_implies_directory(""));
1313 assert_se(path_implies_directory("/"));
1314 assert_se(path_implies_directory("////"));
1315 assert_se(path_implies_directory("////.///"));
1316 assert_se(path_implies_directory("////./"));
1317 assert_se(path_implies_directory("////."));
1318 assert_se(path_implies_directory("."));
1319 assert_se(path_implies_directory("./"));
1320 assert_se(path_implies_directory("/."));
1321 assert_se(path_implies_directory(".."));
1322 assert_se(path_implies_directory("../"));
1323 assert_se(path_implies_directory("/.."));
1324 assert_se(!path_implies_directory("a"));
1325 assert_se(!path_implies_directory("ab"));
1326 assert_se(path_implies_directory("ab/"));
1327 assert_se(!path_implies_directory("ab/a"));
1328 assert_se(path_implies_directory("ab/a/"));
1329 assert_se(path_implies_directory("ab/a/.."));
1330 assert_se(path_implies_directory("ab/a/."));
1331 assert_se(path_implies_directory("ab/a//"));
1334 DEFINE_TEST_MAIN(LOG_DEBUG
);