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