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