]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
Merge pull request #31902 from YHNdnzj/swap-followup
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
76877b46 2
a696dbef 3#include <stdio.h>
07630cea 4#include <unistd.h>
a696dbef 5
b5efdb8a 6#include "alloc-util.h"
a6d9111c 7#include "exec-util.h"
3ffd4af2 8#include "fd-util.h"
8c35c10d 9#include "fs-util.h"
76877b46 10#include "macro.h"
07630cea 11#include "path-util.h"
5ca9139a 12#include "process-util.h"
c6878637 13#include "rm-rf.h"
84e72b5e 14#include "stat-util.h"
07630cea
LP
15#include "string-util.h"
16#include "strv.h"
6d7c4033 17#include "tests.h"
8c35c10d 18#include "tmpfile-util.h"
76877b46 19
4f7452a8 20TEST(print_paths) {
3602ca6f
ZJS
21 log_info("DEFAULT_PATH=%s", DEFAULT_PATH);
22 log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH);
23}
24
4f7452a8 25TEST(path) {
76877b46
ZJS
26 assert_se(path_is_absolute("/"));
27 assert_se(!path_is_absolute("./"));
28
2b6bf07d
ZJS
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.../"), ""));
76877b46 33
3ae5990c
ZJS
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"));
24737c29
ZJS
39
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"));
76877b46 45}
bf9a49a5
ZJS
46
47TEST(is_path) {
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"));
53
54 assert_se(!is_path("."));
55 assert_se(!is_path(""));
56 assert_se(!is_path(".."));
57
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"));
89}
90
91TEST(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"));
97
98 assert_se(!is_device_path("."));
99 assert_se(!is_device_path(""));
100 assert_se(!is_device_path(".."));
101
8f1998b8
ZJS
102 assert_se(!is_device_path("/dev"));
103 assert_se(!is_device_path("/./dev"));
104 assert_se(!is_device_path("/./dev/."));
bf9a49a5
ZJS
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"));
8f1998b8
ZJS
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/"));
bf9a49a5
ZJS
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"));
8f1998b8
ZJS
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/."));
bf9a49a5
ZJS
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/../"));
8f1998b8
ZJS
134 assert_se(!is_device_path("/sys////"));
135 assert_se(!is_device_path("/sys////."));
bf9a49a5
ZJS
136 assert_se( is_device_path("/sys/.."));
137 assert_se( is_device_path("/sys/../"));
138 assert_se(!is_device_path("/usr/../dev/sda"));
139}
76877b46 140
4541d045 141static void test_path_simplify_one(const char *in, const char *out, PathSimplifyFlags flags) {
cb71ed91
YW
142 char *p;
143
2f82562b 144 p = strdupa_safe(in);
4541d045 145 path_simplify_full(p, flags);
cb71ed91
YW
146 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in, p, out);
147 assert_se(streq(p, out));
148}
149
4f7452a8 150TEST(path_simplify) {
cb71ed91
YW
151 _cleanup_free_ char *hoge = NULL, *hoge_out = NULL;
152 char foo[NAME_MAX * 2];
153
4541d045
DDM
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);
003ccba6 161 test_path_simplify_one("////.././///../.", "/", 0);
4541d045
DDM
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);
cb71ed91 167 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
4541d045 168 "/aaa/.bbb/../c./d.dd/..eeee", 0);
cb71ed91 169 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
4541d045 170 "/aaa/.bbb/../c./d.dd/..eeee/..", 0);
cb71ed91 171 test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
4541d045 172 "aaa/.bbb/../c./d.dd/..eeee/..", 0);
cb71ed91 173 test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
4541d045
DDM
174 "../aaa/.bbb/../c./d.dd/..eeee/..", 0);
175 test_path_simplify_one("abc///", "abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
cb71ed91 176
003ccba6
ZJS
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);
184
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);
192
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);
200
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);
208
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);
216
cb71ed91
YW
217 memset(foo, 'a', sizeof(foo) -1);
218 char_array_0(foo);
219
4541d045 220 test_path_simplify_one(foo, foo, 0);
cb71ed91
YW
221
222 hoge = strjoin("/", foo);
223 assert_se(hoge);
4541d045 224 test_path_simplify_one(hoge, hoge, 0);
cb71ed91
YW
225 hoge = mfree(hoge);
226
227 hoge = strjoin("a////.//././//./b///././/./c/////././//./", foo, "//.//////d/e/.//f/");
228 assert_se(hoge);
229
230 hoge_out = strjoin("a/b/c/", foo, "//.//////d/e/.//f/");
231 assert_se(hoge_out);
232
4541d045 233 test_path_simplify_one(hoge, hoge_out, 0);
cb71ed91
YW
234}
235
353df443
YW
236static void test_path_compare_one(const char *a, const char *b, int expected) {
237 int r;
238
239 assert_se(path_compare(a, a) == 0);
240 assert_se(path_compare(b, b) == 0);
241
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));
246
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));
251}
252
4f7452a8 253TEST(path_compare) {
353df443
YW
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);
6d216bdd
ZJS
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);
353df443
YW
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);
279}
280
6808e004
YW
281static void test_path_compare_filename_one(const char *a, const char *b, int expected) {
282 int r;
283
284 assert_se(path_compare_filename(a, a) == 0);
285 assert_se(path_compare_filename(b, b) == 0);
286
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));
291
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));
296}
297
298TEST(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);
328}
329
4f7452a8 330TEST(path_equal_root) {
84e72b5e
ZJS
331 /* Nail down the details of how path_equal("/", ...) works. */
332
333 assert_se(path_equal("/", "/"));
334 assert_se(path_equal("/", "//"));
335
353df443 336 assert_se(path_equal("/", "/./"));
84e72b5e
ZJS
337 assert_se(!path_equal("/", "/../"));
338
339 assert_se(!path_equal("/", "/.../"));
340
341 /* Make sure that files_same works as expected. */
342
563e6846
LP
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);
84e72b5e 347
563e6846
LP
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);
84e72b5e 352
563e6846
LP
353 assert_se(inode_same("/", "/.../", 0) == -ENOENT);
354 assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
84e72b5e
ZJS
355
356 /* The same for path_equal_or_files_same. */
357
563e6846
LP
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));
84e72b5e 362
563e6846
LP
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));
84e72b5e 367
563e6846
LP
368 assert_se(!path_equal_or_inode_same("/", "/.../", 0));
369 assert_se(!path_equal_or_inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
84e72b5e
ZJS
370}
371
4f7452a8 372TEST(find_executable_full) {
92673045 373 char *p;
8c35c10d 374 char* test_file_name;
254d1313 375 _cleanup_close_ int fd = -EBADF;
8c35c10d 376 char fn[] = "/tmp/test-XXXXXX";
92673045 377
8c35c10d 378 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
379 puts(p);
380 assert_se(streq(basename(p), "sh"));
381 free(p);
382
8c35c10d 383 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
384 puts(p);
385 assert_se(streq(basename(p), "sh"));
386 free(p);
387
388 _cleanup_free_ char *oldpath = NULL;
389 p = getenv("PATH");
390 if (p)
391 assert_se(oldpath = strdup(p));
392
44ee03d1 393 assert_se(unsetenv("PATH") == 0);
92673045 394
8c35c10d 395 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
396 puts(p);
397 assert_se(streq(basename(p), "sh"));
398 free(p);
399
8c35c10d 400 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
401 puts(p);
402 assert_se(streq(basename(p), "sh"));
403 free(p);
404
405 if (oldpath)
406 assert_se(setenv("PATH", oldpath, true) >= 0);
8c35c10d 407
408 assert_se((fd = mkostemp_safe(fn)) >= 0);
409 assert_se(fchmod(fd, 0755) >= 0);
410
411 test_file_name = basename(fn);
412
413 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == 0);
414 puts(p);
415 assert_se(streq(p, fn));
416 free(p);
417
418 (void) unlink(fn);
419 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == -ENOENT);
92673045
ZJS
420}
421
4f7452a8 422TEST(find_executable) {
c9d954b2
ZJS
423 char *p;
424
f7bc0c32 425 assert_se(find_executable("/bin/sh", &p) == 0);
c9d954b2 426 puts(p);
85eca92e 427 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
428 free(p);
429
4f7452a8 430 assert_se(find_executable(saved_argv[0], &p) == 0);
c9d954b2 431 puts(p);
92673045 432 assert_se(endswith(p, "/test-path-util"));
8d95631e 433 assert_se(path_is_absolute(p));
c9d954b2
ZJS
434 free(p);
435
f7bc0c32 436 assert_se(find_executable("sh", &p) == 0);
c9d954b2 437 puts(p);
8d95631e
FB
438 assert_se(endswith(p, "/sh"));
439 assert_se(path_is_absolute(p));
c9d954b2
ZJS
440 free(p);
441
92673045
ZJS
442 assert_se(find_executable("/bin/touch", &p) == 0);
443 assert_se(streq(p, "/bin/touch"));
444 free(p);
445
446 assert_se(find_executable("touch", &p) == 0);
447 assert_se(path_is_absolute(p));
448 assert_se(streq(basename(p), "touch"));
449 free(p);
450
f7bc0c32
ZJS
451 assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT);
452 assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT);
92673045 453 assert_se(find_executable("/proc/filesystems", &p) == -EACCES);
c9d954b2
ZJS
454}
455
5ca9139a
ZJS
456static void test_find_executable_exec_one(const char *path) {
457 _cleanup_free_ char *t = NULL;
254d1313 458 _cleanup_close_ int fd = -EBADF;
5ca9139a
ZJS
459 pid_t pid;
460 int r;
461
8c35c10d 462 r = find_executable_full(path, NULL, NULL, false, &t, &fd);
5ca9139a
ZJS
463
464 log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd);
465
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));
470
471 pid = fork();
472 assert_se(pid >= 0);
473 if (pid == 0) {
a6d9111c
ZJS
474 r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL));
475 log_error_errno(r, "[f]execve: %m");
5ca9139a
ZJS
476 _exit(EXIT_FAILURE);
477 }
478
479 assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0);
480}
481
4f7452a8 482TEST(find_executable_exec) {
5ca9139a
ZJS
483 test_find_executable_exec_one("touch");
484 test_find_executable_exec_one("/bin/touch");
a6d9111c
ZJS
485
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);
5ca9139a
ZJS
489}
490
4f7452a8 491TEST(prefixes) {
b82f71c7
LP
492 static const char* const values[] = {
493 "/a/b/c/d",
494 "/a/b/c",
495 "/a/b",
496 "/a",
497 "",
498 NULL
499 };
e203f7c3 500 unsigned i;
fecffe5d 501 char s[PATH_MAX];
e203f7c3 502 bool b;
fecffe5d 503
e203f7c3
LP
504 i = 0;
505 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
506 log_error("---%s---", s);
507 assert_se(streq(s, values[i++]));
508 }
e203f7c3 509 assert_se(values[i] == NULL);
fecffe5d 510
e203f7c3
LP
511 i = 1;
512 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
513 log_error("---%s---", s);
514 assert_se(streq(s, values[i++]));
515 }
fecffe5d
LP
516 assert_se(values[i] == NULL);
517
518 i = 0;
e203f7c3 519 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 520 assert_se(streq(s, values[i++]));
e203f7c3 521 assert_se(values[i] == NULL);
fecffe5d 522
e203f7c3
LP
523 i = 1;
524 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
525 assert_se(streq(s, values[i++]));
fecffe5d
LP
526 assert_se(values[i] == NULL);
527
528 PATH_FOREACH_PREFIX(s, "////")
04499a70 529 assert_not_reached();
e203f7c3
LP
530
531 b = false;
532 PATH_FOREACH_PREFIX_MORE(s, "////") {
533 assert_se(!b);
fecffe5d 534 assert_se(streq(s, ""));
e203f7c3
LP
535 b = true;
536 }
537 assert_se(b);
fecffe5d
LP
538
539 PATH_FOREACH_PREFIX(s, "")
04499a70 540 assert_not_reached();
fecffe5d 541
e203f7c3
LP
542 b = false;
543 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
544 assert_se(!b);
545 assert_se(streq(s, ""));
e203f7c3
LP
546 b = true;
547 }
fecffe5d
LP
548}
549
4f7452a8 550TEST(path_join) {
62a85ee0 551#define test_join(expected, ...) { \
59ae3a95 552 _cleanup_free_ char *z = NULL; \
62a85ee0
ZJS
553 z = path_join(__VA_ARGS__); \
554 log_debug("got \"%s\", expected \"%s\"", z, expected); \
59ae3a95
TA
555 assert_se(streq(z, expected)); \
556 }
557
62a85ee0
ZJS
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);
563
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);
569
652ef298
ZJS
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);
575
62a85ee0 576 test_join("", "", NULL);
652ef298
ZJS
577 test_join("", NULL, "");
578 test_join("", NULL, NULL);
62a85ee0
ZJS
579
580 test_join("foo/bar", "foo", "bar");
581 test_join("foo/bar", "", "foo", "bar");
652ef298 582 test_join("foo/bar", NULL, "foo", NULL, "bar");
62a85ee0
ZJS
583 test_join("foo/bar", "", "foo", "", "bar", "");
584 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
585
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//");
0c6ea3a4
ZJS
592}
593
4f7452a8 594TEST(path_extend) {
7ae27680
LP
595 _cleanup_free_ char *p = NULL;
596
7ae27680
LP
597 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
598 assert_se(streq(p, "foo/bar/baz"));
599
600 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
601 assert_se(streq(p, "foo/bar/baz/foo/bar/baz"));
602
603 p = mfree(p);
604 assert_se(path_extend(&p, "foo") == p);
605 assert_se(streq(p, "foo"));
606
607 assert_se(path_extend(&p, "/foo") == p);
608 assert_se(streq(p, "foo/foo"));
340cd6b6
YW
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 */
613
614 assert_se(free_and_strdup(&p, "/") >= 0);
615 assert_se(path_extend(&p, "foo") == p);
616 assert_se(streq(p, "/foo"));
7ae27680
LP
617}
618
4f7452a8 619TEST(fsck_exists) {
eb66db55 620 /* Ensure we use a sane default for PATH. */
44ee03d1 621 assert_se(unsetenv("PATH") == 0);
eb66db55 622
210dcd8f
LB
623 /* We might or might not find one of these, so keep the test lax. */
624 assert_se(fsck_exists_for_fstype("minix") >= 0);
eb66db55 625
13556724
JK
626 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
627 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
eb66db55
MG
628}
629
fe69c41e
YW
630static void test_path_make_relative_one(const char *from, const char *to, const char *expected) {
631 _cleanup_free_ char *z = NULL;
632 int r;
6b56a651 633
fe69c41e 634 log_info("/* %s(%s, %s) */", __func__, from, to);
771fded3 635
fe69c41e
YW
636 r = path_make_relative(from, to, &z);
637 assert_se((r >= 0) == !!expected);
638 assert_se(streq_ptr(z, expected));
639}
6b56a651 640
4f7452a8 641TEST(path_make_relative) {
fe69c41e
YW
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);
645
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");
6b56a651
TK
654}
655
d4f60bdc
YW
656static void test_path_make_relative_parent_one(const char *from, const char *to, const char *expected) {
657 _cleanup_free_ char *z = NULL;
658 int r;
659
660 log_info("/* %s(%s, %s) */", __func__, from, to);
661
662 r = path_make_relative_parent(from, to, &z);
663 assert_se((r >= 0) == !!expected);
664 assert_se(streq_ptr(z, expected));
665}
666
667TEST(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);
672
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");
681}
682
4f7452a8 683TEST(path_strv_resolve) {
3e8a78c8
MM
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;
3e8a78c8
MM
687
688 assert_se(mkdtemp(tmp_dir) != NULL);
689
bea1a013 690 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
3e8a78c8
MM
691 assert_se(search_dirs);
692 STRV_FOREACH(d, search_dirs) {
b910cc72 693 char *p = path_join(tmp_dir, *d);
3e8a78c8
MM
694 assert_se(p);
695 assert_se(strv_push(&absolute_dirs, p) == 0);
696 }
697
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);
701
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"));
706
c6878637 707 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
708}
709
63f11e35
YW
710static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) {
711 const char *p, *q;
0470289b 712
63f11e35 713 log_debug("/* %s(%s, %s) */", __func__, path, prefix);
0470289b 714
63f11e35
YW
715 p = path_startswith(path, prefix);
716 assert_se(streq_ptr(p, expected));
717 if (p) {
718 q = strjoina(skipped, p);
719 assert_se(streq(q, path));
720 assert_se(p == path + strlen(skipped));
721 }
722}
0470289b 723
4f7452a8 724TEST(path_startswith) {
63f11e35
YW
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/", "");
735
d7aef227
YW
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/./.", "");
746
63f11e35
YW
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);
5895b62f
RC
755}
756
1d13f648
LP
757static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
758 _cleanup_free_ char *s = NULL;
759 const char *t;
760
c6134d3e
LP
761 assert_se(s = path_join(r, p));
762 assert_se(path_equal_ptr(s, expected));
1d13f648
LP
763
764 t = prefix_roota(r, p);
765 assert_se(t);
c6134d3e 766 assert_se(path_equal_ptr(t, expected));
1d13f648
LP
767}
768
4f7452a8 769TEST(prefix_root) {
1d13f648
LP
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");
f9421dd8
YW
776 test_prefix_root_one("/", "foo", "/foo");
777 test_prefix_root_one("", "foo", "foo");
778 test_prefix_root_one(NULL, "foo", "foo");
1d13f648
LP
779
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");
786}
787
4f7452a8 788TEST(file_in_same_dir) {
63292663
RC
789 char *t;
790
162f6477 791 assert_se(file_in_same_dir("/", "a", &t) == -EADDRNOTAVAIL);
63292663 792
162f6477 793 assert_se(file_in_same_dir("/", "/a", &t) >= 0);
63292663
RC
794 assert_se(streq(t, "/a"));
795 free(t);
796
162f6477 797 assert_se(file_in_same_dir("", "a", &t) == -EINVAL);
63292663 798
162f6477
LP
799 assert_se(file_in_same_dir("a/", "x", &t) >= 0);
800 assert_se(streq(t, "x"));
63292663
RC
801 free(t);
802
162f6477 803 assert_se(file_in_same_dir("bar/foo", "bar", &t) >= 0);
63292663
RC
804 assert_se(streq(t, "bar/bar"));
805 free(t);
806}
807
0ee54dd4
YW
808static void test_path_find_first_component_one(
809 const char *path,
810 bool accept_dot_dot,
811 char **expected,
812 int ret) {
813
814 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
815
816 for (const char *p = path;;) {
817 const char *e;
818 int r;
819
820 r = path_find_first_component(&p, accept_dot_dot, &e);
821 if (r <= 0) {
822 if (r == 0) {
de68bf78 823 if (path) {
0ee54dd4 824 assert_se(p == path + strlen_ptr(path));
de68bf78
YW
825 assert_se(isempty(p));
826 } else
0ee54dd4
YW
827 assert_se(!p);
828 assert_se(!e);
829 }
830 assert_se(r == ret);
831 assert_se(strv_isempty(expected));
832 return;
833 }
834
835 assert_se(e);
836 assert_se(strcspn(e, "/") == (size_t) r);
837 assert_se(strlen_ptr(*expected) == (size_t) r);
838 assert_se(strneq(e, *expected++, r));
de68bf78
YW
839
840 assert_se(p);
841 log_debug("p=%s", p);
842 if (!isempty(*expected))
843 assert_se(startswith(p, *expected));
844 else if (ret >= 0) {
845 assert_se(p == path + strlen_ptr(path));
846 assert_se(isempty(p));
847 }
0ee54dd4
YW
848 }
849}
850
4f7452a8 851TEST(path_find_first_component) {
0ee54dd4
YW
852 _cleanup_free_ char *hoge = NULL;
853 char foo[NAME_MAX * 2];
854
0ee54dd4
YW
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);
de68bf78 869 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
0ee54dd4
YW
870
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);
de68bf78 885 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
0ee54dd4
YW
886
887 memset(foo, 'a', sizeof(foo) -1);
888 char_array_0(foo);
889
890 test_path_find_first_component_one(foo, false, NULL, -EINVAL);
891 test_path_find_first_component_one(foo, true, NULL, -EINVAL);
892
893 hoge = strjoin("a/b/c/", foo, "//d/e/.//f/");
894 assert_se(hoge);
895
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);
898}
899
484cd43c
YW
900static void test_path_find_last_component_one(
901 const char *path,
902 bool accept_dot_dot,
903 char **expected,
904 int ret) {
905
906 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
907
908 for (const char *next = NULL;;) {
909 const char *e;
910 int r;
911
912 r = path_find_last_component(path, accept_dot_dot, &next, &e);
913 if (r <= 0) {
914 if (r == 0) {
915 assert_se(next == path);
916 assert_se(!e);
917 }
918 assert_se(r == ret);
919 assert_se(strv_isempty(expected));
920 return;
921 }
922
923 assert_se(e);
924 assert_se(strcspn(e, "/") == (size_t) r);
925 assert_se(strlen_ptr(*expected) == (size_t) r);
926 assert_se(strneq(e, *expected++, r));
de68bf78
YW
927
928 assert_se(next);
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));
934 } else if (ret >= 0)
935 assert_se(next == path);
484cd43c
YW
936 }
937}
938
4f7452a8 939TEST(path_find_last_component) {
484cd43c
YW
940 _cleanup_free_ char *hoge = NULL;
941 char foo[NAME_MAX * 2];
942
484cd43c
YW
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);
957
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);
972
973 memset(foo, 'a', sizeof(foo) -1);
974 char_array_0(foo);
975
976 test_path_find_last_component_one(foo, false, NULL, -EINVAL);
977 test_path_find_last_component_one(foo, true, NULL, -EINVAL);
978
979 hoge = strjoin(foo, "/a/b/c/");
980 assert_se(hoge);
981
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);
984}
985
4f7452a8 986TEST(last_path_component) {
77e0a1b5 987 assert_se(last_path_component(NULL) == NULL);
b12d25a8
ZJS
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/"));
69f9ccf1 999 assert_se(streq(last_path_component(""), ""));
8460289f
LP
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/"));
b12d25a8
ZJS
1004}
1005
a60c8eee
LP
1006static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
1007 _cleanup_free_ char *k = NULL;
1008 int r;
1009
1010 r = path_extract_filename(input, &k);
a6e016af
ZJS
1011 log_info("%s → %s/%s [expected: %s/%s]",
1012 strnull(input),
1013 strnull(k), r < 0 ? STRERROR(r) : "-",
1014 strnull(output), ret < 0 ? STRERROR(ret) : "-");
a60c8eee
LP
1015 assert_se(streq_ptr(k, output));
1016 assert_se(r == ret);
1017}
1018
4f7452a8 1019TEST(path_extract_filename) {
a60c8eee
LP
1020 test_path_extract_filename_one(NULL, NULL, -EINVAL);
1021 test_path_extract_filename_one("a/b/c", "c", 0);
ee277c6b 1022 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY);
3cdcbdd3
LP
1023 test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL);
1024 test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL);
1025 test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL);
01950464
YW
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);
a60c8eee 1032 test_path_extract_filename_one("/foo/a", "a", 0);
ee277c6b 1033 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY);
a60c8eee
LP
1034 test_path_extract_filename_one("", NULL, -EINVAL);
1035 test_path_extract_filename_one("a", "a", 0);
ee277c6b 1036 test_path_extract_filename_one("a/", "a", O_DIRECTORY);
01950464 1037 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY);
a60c8eee 1038 test_path_extract_filename_one("/a", "a", 0);
ee277c6b 1039 test_path_extract_filename_one("/a/", "a", O_DIRECTORY);
01950464 1040 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY);
ee277c6b 1041 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY);
01950464
YW
1042 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY);
1043 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY);
a60c8eee
LP
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);
a60c8eee
LP
1048}
1049
8dcb891c
LP
1050static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
1051 _cleanup_free_ char *k = NULL;
1052 int r;
1053
1054 r = path_extract_directory(input, &k);
a6e016af
ZJS
1055 log_info("%s → %s/%s [expected: %s/%s]",
1056 strnull(input),
1057 strnull(k), r < 0 ? STRERROR(r) : "-",
1058 strnull(output), STRERROR(ret));
8dcb891c
LP
1059 assert_se(streq_ptr(k, output));
1060 assert_se(r == ret);
1061
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 */
1064 if (r >= 0) {
1065 _cleanup_free_ char *f = NULL;
1066
1067 r = path_extract_filename(input, &f);
1068 if (r >= 0) {
1069 _cleanup_free_ char *j = NULL;
1070
1071 assert_se(j = path_join(k, f));
1072 assert_se(path_equal(input, j));
1073 }
1074 }
1075}
1076
4f7452a8 1077TEST(path_extract_directory) {
8dcb891c
LP
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);
01950464
YW
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);
8dcb891c
LP
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);
01950464 1095 test_path_extract_directory_one("a/././//.", NULL, -EDESTADDRREQ);
8dcb891c
LP
1096 test_path_extract_directory_one("/a", "/", 0);
1097 test_path_extract_directory_one("/a/", "/", 0);
01950464 1098 test_path_extract_directory_one("/a//./.", "/", 0);
8dcb891c 1099 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
01950464
YW
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);
8dcb891c
LP
1106}
1107
4f7452a8 1108TEST(filename_is_valid) {
2ef2376d 1109 char foo[NAME_MAX+2];
63292663
RC
1110
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(".."));
1c322571
ZJS
1116 assert_se(!filename_is_valid("bar/foo"));
1117 assert_se(!filename_is_valid("bar/foo/"));
1118 assert_se(!filename_is_valid("bar//"));
63292663 1119
2ef2376d
LP
1120 memset(foo, 'a', sizeof(foo) - 1);
1121 char_array_0(foo);
63292663
RC
1122
1123 assert_se(!filename_is_valid(foo));
1124
1125 assert_se(filename_is_valid("foo_bar-333"));
1126 assert_se(filename_is_valid("o.o"));
1127}
1128
32df2e14 1129static void test_path_is_valid_and_safe_one(const char *p, bool ret) {
7802194a 1130 log_debug("/* %s(\"%s\") */", __func__, strnull(p));
32df2e14
YW
1131
1132 assert_se(path_is_valid(p) == ret);
1133 if (ret)
1134 ret = !streq(p, "..") &&
1135 !startswith(p, "../") &&
1136 !endswith(p, "/..") &&
1137 !strstr(p, "/../");
1138 assert_se(path_is_safe(p) == ret);
1139}
1140
4f7452a8 1141TEST(path_is_valid_and_safe) {
2ef2376d
LP
1142 char foo[PATH_MAX+2];
1143 const char *c;
1144
32df2e14
YW
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);
2ef2376d
LP
1158
1159 memset(foo, 'a', sizeof(foo) -1);
1160 char_array_0(foo);
1161
32df2e14 1162 test_path_is_valid_and_safe_one(foo, false);
2ef2376d
LP
1163
1164 c = strjoina("/xxx/", foo, "/yyy");
32df2e14 1165 test_path_is_valid_and_safe_one(c, false);
2ef2376d 1166
32df2e14
YW
1167 test_path_is_valid_and_safe_one("foo_bar-333", true);
1168 test_path_is_valid_and_safe_one("o.o", true);
2ef2376d
LP
1169}
1170
4f7452a8 1171TEST(hidden_or_backup_file) {
b05b9cde
ZJS
1172 assert_se(hidden_or_backup_file(".hidden"));
1173 assert_se(hidden_or_backup_file("..hidden"));
1174 assert_se(!hidden_or_backup_file("hidden."));
1175
1176 assert_se(hidden_or_backup_file("backup~"));
1177 assert_se(hidden_or_backup_file(".backup~"));
1178
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"));
1182
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"));
1187
1188 assert_se(!hidden_or_backup_file("test.rpmnew."));
1189 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1190}
1191
4f7452a8 1192TEST(skip_dev_prefix) {
a119ec7c
LP
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"));
1204}
1205
4f7452a8 1206TEST(empty_or_root) {
57ea45e1
LP
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//"));
1217}
1218
4f7452a8 1219TEST(path_startswith_set) {
d898ed65
LP
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));
1225
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));
1231
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));
1237}
1238
4f7452a8 1239TEST(path_startswith_strv) {
cc4d7d81
ZJS
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));
1245
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));
1251
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));
1257}
1258
3b703fe2
YW
1259static void test_path_glob_can_match_one(const char *pattern, const char *prefix, const char *expected) {
1260 _cleanup_free_ char *result = NULL;
1261
1262 log_debug("%s(%s, %s, %s)", __func__, pattern, prefix, strnull(expected));
1263
1264 assert_se(path_glob_can_match(pattern, prefix, &result) == !!expected);
1265 assert_se(streq_ptr(result, expected));
1266}
1267
1268TEST(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");
1274
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");
1280
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");
1287
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/*");
1295}
1296
4f7452a8 1297TEST(print_MAX) {
69866062
LP
1298 log_info("PATH_MAX=%zu\n"
1299 "FILENAME_MAX=%zu\n"
1300 "NAME_MAX=%zu",
1301 (size_t) PATH_MAX,
1302 (size_t) FILENAME_MAX,
1303 (size_t) NAME_MAX);
1304
1305 assert_cc(FILENAME_MAX == PATH_MAX);
76877b46 1306}
4f7452a8 1307
dd92ba8a
LP
1308TEST(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//"));
1330}
1331
4f7452a8 1332DEFINE_TEST_MAIN(LOG_DEBUG);