]>
Commit | Line | Data |
---|---|---|
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" |
76877b46 | 9 | #include "macro.h" |
07630cea | 10 | #include "path-util.h" |
5ca9139a | 11 | #include "process-util.h" |
c6878637 | 12 | #include "rm-rf.h" |
84e72b5e | 13 | #include "stat-util.h" |
07630cea LP |
14 | #include "string-util.h" |
15 | #include "strv.h" | |
6d7c4033 | 16 | #include "tests.h" |
07630cea | 17 | #include "util.h" |
76877b46 | 18 | |
3602ca6f ZJS |
19 | static void test_print_paths(void) { |
20 | log_info("DEFAULT_PATH=%s", DEFAULT_PATH); | |
21 | log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH); | |
22 | } | |
23 | ||
858d36c1 YW |
24 | static void test_path_simplify(const char *in, const char *out, const char *out_dot) { |
25 | char *p; | |
26 | ||
771fded3 ZJS |
27 | log_info("/* %s */", __func__); |
28 | ||
858d36c1 YW |
29 | p = strdupa(in); |
30 | assert_se(streq(path_simplify(p, false), out)); | |
31 | ||
32 | p = strdupa(in); | |
33 | assert_se(streq(path_simplify(p, true), out_dot)); | |
34 | } | |
35 | ||
76877b46 | 36 | static void test_path(void) { |
771fded3 ZJS |
37 | log_info("/* %s */", __func__); |
38 | ||
76877b46 ZJS |
39 | assert_se(path_is_absolute("/")); |
40 | assert_se(!path_is_absolute("./")); | |
41 | ||
42 | assert_se(is_path("/dir")); | |
43 | assert_se(is_path("a/b")); | |
44 | assert_se(!is_path(".")); | |
45 | ||
2b6bf07d ZJS |
46 | assert_se(streq(basename("./aa/bb/../file.da."), "file.da.")); |
47 | assert_se(streq(basename("/aa///.file"), ".file")); | |
48 | assert_se(streq(basename("/aa///file..."), "file...")); | |
49 | assert_se(streq(basename("file.../"), "")); | |
76877b46 | 50 | |
858d36c1 YW |
51 | test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc"); |
52 | test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc"); | |
53 | test_path_simplify("///", "/", "/"); | |
54 | test_path_simplify("///.//", "/.", "/"); | |
55 | test_path_simplify("///.//.///", "/./.", "/"); | |
56 | test_path_simplify("////.././///../.", "/.././../.", "/../.."); | |
afbae3e9 TH |
57 | test_path_simplify(".", ".", "."); |
58 | test_path_simplify("./", ".", "."); | |
59 | test_path_simplify(".///.//./.", "./././.", "."); | |
60 | test_path_simplify(".///.//././/", "./././.", "."); | |
858d36c1 YW |
61 | test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", |
62 | "/./aaa/././.bbb/../c./d.dd/..eeee/.", | |
63 | "/aaa/.bbb/../c./d.dd/..eeee"); | |
64 | test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
65 | "/./aaa/././.bbb/../c./d.dd/..eeee/..", | |
66 | "/aaa/.bbb/../c./d.dd/..eeee/.."); | |
67 | test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
68 | "././aaa/././.bbb/../c./d.dd/..eeee/..", | |
69 | "aaa/.bbb/../c./d.dd/..eeee/.."); | |
70 | test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
71 | ".././aaa/././.bbb/../c./d.dd/..eeee/..", | |
72 | "../aaa/.bbb/../c./d.dd/..eeee/.."); | |
3ae5990c ZJS |
73 | |
74 | assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo")); | |
75 | assert_se(PATH_IN_SET("/bin", "/bin")); | |
76 | assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin")); | |
77 | assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar")); | |
78 | assert_se(!PATH_IN_SET("/", "/abc", "/def")); | |
24737c29 ZJS |
79 | |
80 | assert_se(path_equal_ptr(NULL, NULL)); | |
81 | assert_se(path_equal_ptr("/a", "/a")); | |
82 | assert_se(!path_equal_ptr("/a", "/b")); | |
83 | assert_se(!path_equal_ptr("/a", NULL)); | |
84 | assert_se(!path_equal_ptr(NULL, "/a")); | |
727e63e3 LB |
85 | |
86 | assert_se(path_equal_filename("/a/c", "/b/c")); | |
87 | assert_se(path_equal_filename("/a", "/a")); | |
88 | assert_se(!path_equal_filename("/a/b", "/a/c")); | |
89 | assert_se(!path_equal_filename("/b", "/c")); | |
76877b46 ZJS |
90 | } |
91 | ||
353df443 YW |
92 | static void test_path_compare_one(const char *a, const char *b, int expected) { |
93 | int r; | |
94 | ||
95 | assert_se(path_compare(a, a) == 0); | |
96 | assert_se(path_compare(b, b) == 0); | |
97 | ||
98 | r = path_compare(a, b); | |
99 | assert_se((r > 0) == (expected > 0) && (r < 0) == (expected < 0)); | |
100 | r = path_compare(b, a); | |
101 | assert_se((r < 0) == (expected > 0) && (r > 0) == (expected < 0)); | |
102 | ||
103 | assert_se(path_equal(a, a) == 1); | |
104 | assert_se(path_equal(b, b) == 1); | |
105 | assert_se(path_equal(a, b) == (expected == 0)); | |
106 | assert_se(path_equal(b, a) == (expected == 0)); | |
107 | } | |
108 | ||
109 | static void test_path_compare(void) { | |
110 | test_path_compare_one("/goo", "/goo", 0); | |
111 | test_path_compare_one("/goo", "/goo", 0); | |
112 | test_path_compare_one("//goo", "/goo", 0); | |
113 | test_path_compare_one("//goo/////", "/goo", 0); | |
114 | test_path_compare_one("goo/////", "goo", 0); | |
115 | test_path_compare_one("/goo/boo", "/goo//boo", 0); | |
116 | test_path_compare_one("//goo/boo", "/goo/boo//", 0); | |
117 | test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0); | |
118 | test_path_compare_one("/.", "//.///", 0); | |
119 | test_path_compare_one("/x", "x/", 1); | |
120 | test_path_compare_one("x/", "/", -1); | |
121 | test_path_compare_one("/x/./y", "x/y", 1); | |
122 | test_path_compare_one("x/.y", "x/y", -1); | |
123 | test_path_compare_one("foo", "/foo", -1); | |
124 | test_path_compare_one("/foo", "/foo/bar", -1); | |
125 | test_path_compare_one("/foo/aaa", "/foo/b", -1); | |
126 | test_path_compare_one("/foo/aaa", "/foo/b/a", -1); | |
127 | test_path_compare_one("/foo/a", "/foo/aaa", -1); | |
128 | test_path_compare_one("/foo/a/b", "/foo/aaa", -1); | |
129 | } | |
130 | ||
84e72b5e ZJS |
131 | static void test_path_equal_root(void) { |
132 | /* Nail down the details of how path_equal("/", ...) works. */ | |
133 | ||
771fded3 ZJS |
134 | log_info("/* %s */", __func__); |
135 | ||
84e72b5e ZJS |
136 | assert_se(path_equal("/", "/")); |
137 | assert_se(path_equal("/", "//")); | |
138 | ||
353df443 | 139 | assert_se(path_equal("/", "/./")); |
84e72b5e ZJS |
140 | assert_se(!path_equal("/", "/../")); |
141 | ||
142 | assert_se(!path_equal("/", "/.../")); | |
143 | ||
144 | /* Make sure that files_same works as expected. */ | |
145 | ||
e3f791a2 ZJS |
146 | assert_se(files_same("/", "/", 0) > 0); |
147 | assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0); | |
148 | assert_se(files_same("/", "//", 0) > 0); | |
149 | assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0); | |
84e72b5e | 150 | |
e3f791a2 ZJS |
151 | assert_se(files_same("/", "/./", 0) > 0); |
152 | assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0); | |
153 | assert_se(files_same("/", "/../", 0) > 0); | |
154 | assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0); | |
84e72b5e | 155 | |
e3f791a2 ZJS |
156 | assert_se(files_same("/", "/.../", 0) == -ENOENT); |
157 | assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT); | |
84e72b5e ZJS |
158 | |
159 | /* The same for path_equal_or_files_same. */ | |
160 | ||
e3f791a2 ZJS |
161 | assert_se(path_equal_or_files_same("/", "/", 0)); |
162 | assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW)); | |
163 | assert_se(path_equal_or_files_same("/", "//", 0)); | |
164 | assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e | 165 | |
e3f791a2 ZJS |
166 | assert_se(path_equal_or_files_same("/", "/./", 0)); |
167 | assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW)); | |
168 | assert_se(path_equal_or_files_same("/", "/../", 0)); | |
169 | assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e | 170 | |
e3f791a2 ZJS |
171 | assert_se(!path_equal_or_files_same("/", "/.../", 0)); |
172 | assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e ZJS |
173 | } |
174 | ||
92673045 ZJS |
175 | static void test_find_executable_full(void) { |
176 | char *p; | |
177 | ||
178 | log_info("/* %s */", __func__); | |
179 | ||
5ca9139a | 180 | assert_se(find_executable_full("sh", true, &p, NULL) == 0); |
92673045 ZJS |
181 | puts(p); |
182 | assert_se(streq(basename(p), "sh")); | |
183 | free(p); | |
184 | ||
5ca9139a | 185 | assert_se(find_executable_full("sh", false, &p, NULL) == 0); |
92673045 ZJS |
186 | puts(p); |
187 | assert_se(streq(basename(p), "sh")); | |
188 | free(p); | |
189 | ||
190 | _cleanup_free_ char *oldpath = NULL; | |
191 | p = getenv("PATH"); | |
192 | if (p) | |
193 | assert_se(oldpath = strdup(p)); | |
194 | ||
44ee03d1 | 195 | assert_se(unsetenv("PATH") == 0); |
92673045 | 196 | |
5ca9139a | 197 | assert_se(find_executable_full("sh", true, &p, NULL) == 0); |
92673045 ZJS |
198 | puts(p); |
199 | assert_se(streq(basename(p), "sh")); | |
200 | free(p); | |
201 | ||
5ca9139a | 202 | assert_se(find_executable_full("sh", false, &p, NULL) == 0); |
92673045 ZJS |
203 | puts(p); |
204 | assert_se(streq(basename(p), "sh")); | |
205 | free(p); | |
206 | ||
207 | if (oldpath) | |
208 | assert_se(setenv("PATH", oldpath, true) >= 0); | |
209 | } | |
210 | ||
f7bc0c32 | 211 | static void test_find_executable(const char *self) { |
c9d954b2 ZJS |
212 | char *p; |
213 | ||
771fded3 ZJS |
214 | log_info("/* %s */", __func__); |
215 | ||
f7bc0c32 | 216 | assert_se(find_executable("/bin/sh", &p) == 0); |
c9d954b2 | 217 | puts(p); |
85eca92e | 218 | assert_se(path_equal(p, "/bin/sh")); |
c9d954b2 ZJS |
219 | free(p); |
220 | ||
f7bc0c32 | 221 | assert_se(find_executable(self, &p) == 0); |
c9d954b2 | 222 | puts(p); |
92673045 | 223 | assert_se(endswith(p, "/test-path-util")); |
8d95631e | 224 | assert_se(path_is_absolute(p)); |
c9d954b2 ZJS |
225 | free(p); |
226 | ||
f7bc0c32 | 227 | assert_se(find_executable("sh", &p) == 0); |
c9d954b2 | 228 | puts(p); |
8d95631e FB |
229 | assert_se(endswith(p, "/sh")); |
230 | assert_se(path_is_absolute(p)); | |
c9d954b2 ZJS |
231 | free(p); |
232 | ||
92673045 ZJS |
233 | assert_se(find_executable("/bin/touch", &p) == 0); |
234 | assert_se(streq(p, "/bin/touch")); | |
235 | free(p); | |
236 | ||
237 | assert_se(find_executable("touch", &p) == 0); | |
238 | assert_se(path_is_absolute(p)); | |
239 | assert_se(streq(basename(p), "touch")); | |
240 | free(p); | |
241 | ||
f7bc0c32 ZJS |
242 | assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT); |
243 | assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT); | |
92673045 | 244 | assert_se(find_executable("/proc/filesystems", &p) == -EACCES); |
c9d954b2 ZJS |
245 | } |
246 | ||
5ca9139a ZJS |
247 | static void test_find_executable_exec_one(const char *path) { |
248 | _cleanup_free_ char *t = NULL; | |
249 | _cleanup_close_ int fd = -1; | |
250 | pid_t pid; | |
251 | int r; | |
252 | ||
253 | r = find_executable_full(path, false, &t, &fd); | |
254 | ||
255 | log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd); | |
256 | ||
257 | assert_se(fd > STDERR_FILENO); | |
258 | assert_se(path_is_absolute(t)); | |
259 | if (path_is_absolute(path)) | |
260 | assert_se(streq(t, path)); | |
261 | ||
262 | pid = fork(); | |
263 | assert_se(pid >= 0); | |
264 | if (pid == 0) { | |
a6d9111c ZJS |
265 | r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL)); |
266 | log_error_errno(r, "[f]execve: %m"); | |
5ca9139a ZJS |
267 | _exit(EXIT_FAILURE); |
268 | } | |
269 | ||
270 | assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0); | |
271 | } | |
272 | ||
273 | static void test_find_executable_exec(void) { | |
274 | log_info("/* %s */", __func__); | |
275 | ||
276 | test_find_executable_exec_one("touch"); | |
277 | test_find_executable_exec_one("/bin/touch"); | |
a6d9111c ZJS |
278 | |
279 | _cleanup_free_ char *script = NULL; | |
280 | assert_se(get_testdata_dir("test-path-util/script.sh", &script) >= 0); | |
281 | test_find_executable_exec_one(script); | |
5ca9139a ZJS |
282 | } |
283 | ||
fecffe5d | 284 | static void test_prefixes(void) { |
b82f71c7 LP |
285 | static const char* const values[] = { |
286 | "/a/b/c/d", | |
287 | "/a/b/c", | |
288 | "/a/b", | |
289 | "/a", | |
290 | "", | |
291 | NULL | |
292 | }; | |
e203f7c3 | 293 | unsigned i; |
fecffe5d | 294 | char s[PATH_MAX]; |
e203f7c3 | 295 | bool b; |
fecffe5d | 296 | |
771fded3 ZJS |
297 | log_info("/* %s */", __func__); |
298 | ||
e203f7c3 LP |
299 | i = 0; |
300 | PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { | |
fecffe5d LP |
301 | log_error("---%s---", s); |
302 | assert_se(streq(s, values[i++])); | |
303 | } | |
e203f7c3 | 304 | assert_se(values[i] == NULL); |
fecffe5d | 305 | |
e203f7c3 LP |
306 | i = 1; |
307 | PATH_FOREACH_PREFIX(s, "/a/b/c/d") { | |
308 | log_error("---%s---", s); | |
309 | assert_se(streq(s, values[i++])); | |
310 | } | |
fecffe5d LP |
311 | assert_se(values[i] == NULL); |
312 | ||
313 | i = 0; | |
e203f7c3 | 314 | PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") |
fecffe5d | 315 | assert_se(streq(s, values[i++])); |
e203f7c3 | 316 | assert_se(values[i] == NULL); |
fecffe5d | 317 | |
e203f7c3 LP |
318 | i = 1; |
319 | PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") | |
320 | assert_se(streq(s, values[i++])); | |
fecffe5d LP |
321 | assert_se(values[i] == NULL); |
322 | ||
323 | PATH_FOREACH_PREFIX(s, "////") | |
e203f7c3 LP |
324 | assert_not_reached("Wut?"); |
325 | ||
326 | b = false; | |
327 | PATH_FOREACH_PREFIX_MORE(s, "////") { | |
328 | assert_se(!b); | |
fecffe5d | 329 | assert_se(streq(s, "")); |
e203f7c3 LP |
330 | b = true; |
331 | } | |
332 | assert_se(b); | |
fecffe5d LP |
333 | |
334 | PATH_FOREACH_PREFIX(s, "") | |
335 | assert_not_reached("wut?"); | |
336 | ||
e203f7c3 LP |
337 | b = false; |
338 | PATH_FOREACH_PREFIX_MORE(s, "") { | |
8d95631e FB |
339 | assert_se(!b); |
340 | assert_se(streq(s, "")); | |
e203f7c3 LP |
341 | b = true; |
342 | } | |
fecffe5d LP |
343 | } |
344 | ||
0c6ea3a4 | 345 | static void test_path_join(void) { |
771fded3 | 346 | log_info("/* %s */", __func__); |
59ae3a95 | 347 | |
62a85ee0 | 348 | #define test_join(expected, ...) { \ |
59ae3a95 | 349 | _cleanup_free_ char *z = NULL; \ |
62a85ee0 ZJS |
350 | z = path_join(__VA_ARGS__); \ |
351 | log_debug("got \"%s\", expected \"%s\"", z, expected); \ | |
59ae3a95 TA |
352 | assert_se(streq(z, expected)); \ |
353 | } | |
354 | ||
62a85ee0 ZJS |
355 | test_join("/root/a/b/c", "/root", "/a/b", "/c"); |
356 | test_join("/root/a/b/c", "/root", "a/b", "c"); | |
357 | test_join("/root/a/b/c", "/root", "/a/b", "c"); | |
358 | test_join("/root/c", "/root", "/", "c"); | |
359 | test_join("/root/", "/root", "/", NULL); | |
360 | ||
361 | test_join("/a/b/c", "", "/a/b", "/c"); | |
362 | test_join("a/b/c", "", "a/b", "c"); | |
363 | test_join("/a/b/c", "", "/a/b", "c"); | |
364 | test_join("/c", "", "/", "c"); | |
365 | test_join("/", "", "/", NULL); | |
366 | ||
652ef298 ZJS |
367 | test_join("/a/b/c", NULL, "/a/b", "/c"); |
368 | test_join("a/b/c", NULL, "a/b", "c"); | |
369 | test_join("/a/b/c", NULL, "/a/b", "c"); | |
370 | test_join("/c", NULL, "/", "c"); | |
371 | test_join("/", NULL, "/", NULL); | |
372 | ||
62a85ee0 | 373 | test_join("", "", NULL); |
652ef298 ZJS |
374 | test_join("", NULL, ""); |
375 | test_join("", NULL, NULL); | |
62a85ee0 ZJS |
376 | |
377 | test_join("foo/bar", "foo", "bar"); | |
378 | test_join("foo/bar", "", "foo", "bar"); | |
652ef298 | 379 | test_join("foo/bar", NULL, "foo", NULL, "bar"); |
62a85ee0 ZJS |
380 | test_join("foo/bar", "", "foo", "", "bar", ""); |
381 | test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", ""); | |
382 | ||
383 | test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", ""); | |
384 | test_join("/foo/bar/", "/", "foo", "/", "bar", "/"); | |
385 | test_join("foo/bar/baz", "foo", "bar", "baz"); | |
386 | test_join("foo/bar/baz", "foo/", "bar", "/baz"); | |
387 | test_join("foo//bar//baz", "foo/", "/bar/", "/baz"); | |
388 | test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//"); | |
0c6ea3a4 ZJS |
389 | } |
390 | ||
7ae27680 LP |
391 | static void test_path_extend(void) { |
392 | _cleanup_free_ char *p = NULL; | |
393 | ||
394 | log_info("/* %s */", __func__); | |
395 | ||
396 | assert_se(path_extend(&p, "foo", "bar", "baz") == p); | |
397 | assert_se(streq(p, "foo/bar/baz")); | |
398 | ||
399 | assert_se(path_extend(&p, "foo", "bar", "baz") == p); | |
400 | assert_se(streq(p, "foo/bar/baz/foo/bar/baz")); | |
401 | ||
402 | p = mfree(p); | |
403 | assert_se(path_extend(&p, "foo") == p); | |
404 | assert_se(streq(p, "foo")); | |
405 | ||
406 | assert_se(path_extend(&p, "/foo") == p); | |
407 | assert_se(streq(p, "foo/foo")); | |
340cd6b6 YW |
408 | assert_se(path_extend(&p, "/waaaah/wahhh//") == p); |
409 | assert_se(streq(p, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */ | |
410 | assert_se(path_extend(&p, "/aaa/bbb/") == p); | |
411 | assert_se(streq(p, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */ | |
412 | ||
413 | assert_se(free_and_strdup(&p, "/") >= 0); | |
414 | assert_se(path_extend(&p, "foo") == p); | |
415 | assert_se(streq(p, "/foo")); | |
7ae27680 LP |
416 | } |
417 | ||
eb66db55 | 418 | static void test_fsck_exists(void) { |
771fded3 ZJS |
419 | log_info("/* %s */", __func__); |
420 | ||
eb66db55 | 421 | /* Ensure we use a sane default for PATH. */ |
44ee03d1 | 422 | assert_se(unsetenv("PATH") == 0); |
eb66db55 MG |
423 | |
424 | /* fsck.minix is provided by util-linux and will probably exist. */ | |
85eca92e | 425 | assert_se(fsck_exists("minix") == 1); |
eb66db55 | 426 | |
85eca92e LP |
427 | assert_se(fsck_exists("AbCdE") == 0); |
428 | assert_se(fsck_exists("/../bin/") == 0); | |
eb66db55 MG |
429 | } |
430 | ||
fe69c41e YW |
431 | static void test_path_make_relative_one(const char *from, const char *to, const char *expected) { |
432 | _cleanup_free_ char *z = NULL; | |
433 | int r; | |
6b56a651 | 434 | |
fe69c41e | 435 | log_info("/* %s(%s, %s) */", __func__, from, to); |
771fded3 | 436 | |
fe69c41e YW |
437 | r = path_make_relative(from, to, &z); |
438 | assert_se((r >= 0) == !!expected); | |
439 | assert_se(streq_ptr(z, expected)); | |
440 | } | |
6b56a651 | 441 | |
fe69c41e YW |
442 | static void test_make_relative(void) { |
443 | log_info("/* %s */", __func__); | |
6b56a651 | 444 | |
fe69c41e YW |
445 | test_path_make_relative_one("some/relative/path", "/some/path", NULL); |
446 | test_path_make_relative_one("/some/path", "some/relative/path", NULL); | |
447 | test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL); | |
448 | ||
449 | test_path_make_relative_one("/", "/", "."); | |
450 | test_path_make_relative_one("/", "/some/path", "some/path"); | |
451 | test_path_make_relative_one("/some/path", "/some/path", "."); | |
452 | test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir"); | |
453 | test_path_make_relative_one("/some/path", "/", "../.."); | |
454 | test_path_make_relative_one("/some/path", "/some/other/path", "../other/path"); | |
455 | test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path"); | |
456 | test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); | |
6b56a651 TK |
457 | } |
458 | ||
3e8a78c8 MM |
459 | static void test_strv_resolve(void) { |
460 | char tmp_dir[] = "/tmp/test-path-util-XXXXXX"; | |
461 | _cleanup_strv_free_ char **search_dirs = NULL; | |
462 | _cleanup_strv_free_ char **absolute_dirs = NULL; | |
463 | char **d; | |
464 | ||
465 | assert_se(mkdtemp(tmp_dir) != NULL); | |
466 | ||
bea1a013 | 467 | search_dirs = strv_new("/dir1", "/dir2", "/dir3"); |
3e8a78c8 MM |
468 | assert_se(search_dirs); |
469 | STRV_FOREACH(d, search_dirs) { | |
b910cc72 | 470 | char *p = path_join(tmp_dir, *d); |
3e8a78c8 MM |
471 | assert_se(p); |
472 | assert_se(strv_push(&absolute_dirs, p) == 0); | |
473 | } | |
474 | ||
475 | assert_se(mkdir(absolute_dirs[0], 0700) == 0); | |
476 | assert_se(mkdir(absolute_dirs[1], 0700) == 0); | |
477 | assert_se(symlink("dir2", absolute_dirs[2]) == 0); | |
478 | ||
479 | path_strv_resolve(search_dirs, tmp_dir); | |
480 | assert_se(streq(search_dirs[0], "/dir1")); | |
481 | assert_se(streq(search_dirs[1], "/dir2")); | |
482 | assert_se(streq(search_dirs[2], "/dir2")); | |
483 | ||
c6878637 | 484 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
3e8a78c8 MM |
485 | } |
486 | ||
63f11e35 YW |
487 | static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) { |
488 | const char *p, *q; | |
0470289b | 489 | |
63f11e35 | 490 | log_debug("/* %s(%s, %s) */", __func__, path, prefix); |
0470289b | 491 | |
63f11e35 YW |
492 | p = path_startswith(path, prefix); |
493 | assert_se(streq_ptr(p, expected)); | |
494 | if (p) { | |
495 | q = strjoina(skipped, p); | |
496 | assert_se(streq(q, path)); | |
497 | assert_se(p == path + strlen(skipped)); | |
498 | } | |
499 | } | |
0470289b | 500 | |
63f11e35 YW |
501 | static void test_path_startswith(void) { |
502 | log_info("/* %s */", __func__); | |
5895b62f | 503 | |
63f11e35 YW |
504 | test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/"); |
505 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/"); | |
506 | test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/"); | |
507 | test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/"); | |
508 | test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", ""); | |
509 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", ""); | |
510 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", ""); | |
511 | test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", ""); | |
512 | test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", ""); | |
513 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", ""); | |
514 | ||
515 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL, NULL); | |
516 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL, NULL); | |
517 | test_path_startswith_one("/foo/bar/barfoo/", "", NULL, NULL); | |
518 | test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL, NULL); | |
519 | test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL, NULL); | |
520 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL, NULL); | |
521 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL, NULL); | |
522 | test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL, NULL); | |
5895b62f RC |
523 | } |
524 | ||
1d13f648 LP |
525 | static void test_prefix_root_one(const char *r, const char *p, const char *expected) { |
526 | _cleanup_free_ char *s = NULL; | |
527 | const char *t; | |
528 | ||
c6134d3e LP |
529 | assert_se(s = path_join(r, p)); |
530 | assert_se(path_equal_ptr(s, expected)); | |
1d13f648 LP |
531 | |
532 | t = prefix_roota(r, p); | |
533 | assert_se(t); | |
c6134d3e | 534 | assert_se(path_equal_ptr(t, expected)); |
1d13f648 LP |
535 | } |
536 | ||
537 | static void test_prefix_root(void) { | |
771fded3 ZJS |
538 | log_info("/* %s */", __func__); |
539 | ||
1d13f648 LP |
540 | test_prefix_root_one("/", "/foo", "/foo"); |
541 | test_prefix_root_one(NULL, "/foo", "/foo"); | |
542 | test_prefix_root_one("", "/foo", "/foo"); | |
543 | test_prefix_root_one("///", "/foo", "/foo"); | |
544 | test_prefix_root_one("/", "////foo", "/foo"); | |
545 | test_prefix_root_one(NULL, "////foo", "/foo"); | |
f9421dd8 YW |
546 | test_prefix_root_one("/", "foo", "/foo"); |
547 | test_prefix_root_one("", "foo", "foo"); | |
548 | test_prefix_root_one(NULL, "foo", "foo"); | |
1d13f648 LP |
549 | |
550 | test_prefix_root_one("/foo", "/bar", "/foo/bar"); | |
551 | test_prefix_root_one("/foo", "bar", "/foo/bar"); | |
552 | test_prefix_root_one("foo", "bar", "foo/bar"); | |
553 | test_prefix_root_one("/foo/", "/bar", "/foo/bar"); | |
554 | test_prefix_root_one("/foo/", "//bar", "/foo/bar"); | |
555 | test_prefix_root_one("/foo///", "//bar", "/foo/bar"); | |
556 | } | |
557 | ||
63292663 RC |
558 | static void test_file_in_same_dir(void) { |
559 | char *t; | |
560 | ||
771fded3 ZJS |
561 | log_info("/* %s */", __func__); |
562 | ||
63292663 RC |
563 | t = file_in_same_dir("/", "a"); |
564 | assert_se(streq(t, "/a")); | |
565 | free(t); | |
566 | ||
567 | t = file_in_same_dir("/", "/a"); | |
568 | assert_se(streq(t, "/a")); | |
569 | free(t); | |
570 | ||
571 | t = file_in_same_dir("", "a"); | |
572 | assert_se(streq(t, "a")); | |
573 | free(t); | |
574 | ||
575 | t = file_in_same_dir("a/", "a"); | |
576 | assert_se(streq(t, "a/a")); | |
577 | free(t); | |
578 | ||
579 | t = file_in_same_dir("bar/foo", "bar"); | |
580 | assert_se(streq(t, "bar/bar")); | |
581 | free(t); | |
582 | } | |
583 | ||
0ee54dd4 YW |
584 | static void test_path_find_first_component_one( |
585 | const char *path, | |
586 | bool accept_dot_dot, | |
587 | char **expected, | |
588 | int ret) { | |
589 | ||
590 | log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot)); | |
591 | ||
592 | for (const char *p = path;;) { | |
593 | const char *e; | |
594 | int r; | |
595 | ||
596 | r = path_find_first_component(&p, accept_dot_dot, &e); | |
597 | if (r <= 0) { | |
598 | if (r == 0) { | |
599 | if (path) | |
600 | assert_se(p == path + strlen_ptr(path)); | |
601 | else | |
602 | assert_se(!p); | |
603 | assert_se(!e); | |
604 | } | |
605 | assert_se(r == ret); | |
606 | assert_se(strv_isempty(expected)); | |
607 | return; | |
608 | } | |
609 | ||
610 | assert_se(e); | |
611 | assert_se(strcspn(e, "/") == (size_t) r); | |
612 | assert_se(strlen_ptr(*expected) == (size_t) r); | |
613 | assert_se(strneq(e, *expected++, r)); | |
614 | } | |
615 | } | |
616 | ||
617 | static void test_path_find_first_component(void) { | |
618 | _cleanup_free_ char *hoge = NULL; | |
619 | char foo[NAME_MAX * 2]; | |
620 | ||
621 | log_info("/* %s */", __func__); | |
622 | ||
623 | test_path_find_first_component_one(NULL, false, NULL, 0); | |
624 | test_path_find_first_component_one("", false, NULL, 0); | |
625 | test_path_find_first_component_one("/", false, NULL, 0); | |
626 | test_path_find_first_component_one(".", false, NULL, 0); | |
627 | test_path_find_first_component_one("./", false, NULL, 0); | |
628 | test_path_find_first_component_one("./.", false, NULL, 0); | |
629 | test_path_find_first_component_one("..", false, NULL, -EINVAL); | |
630 | test_path_find_first_component_one("/..", false, NULL, -EINVAL); | |
631 | test_path_find_first_component_one("./..", false, NULL, -EINVAL); | |
632 | test_path_find_first_component_one("////./././//.", false, NULL, 0); | |
633 | test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0); | |
634 | test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0); | |
635 | test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL); | |
636 | test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL); | |
637 | test_path_find_first_component_one("a/foo./b", false, STRV_MAKE("a", "foo.", "b"), 0); | |
638 | ||
639 | test_path_find_first_component_one(NULL, true, NULL, 0); | |
640 | test_path_find_first_component_one("", true, NULL, 0); | |
641 | test_path_find_first_component_one("/", true, NULL, 0); | |
642 | test_path_find_first_component_one(".", true, NULL, 0); | |
643 | test_path_find_first_component_one("./", true, NULL, 0); | |
644 | test_path_find_first_component_one("./.", true, NULL, 0); | |
645 | test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0); | |
646 | test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0); | |
647 | test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0); | |
648 | test_path_find_first_component_one("////./././//.", true, NULL, 0); | |
649 | test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0); | |
650 | test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0); | |
651 | test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0); | |
652 | test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0); | |
653 | test_path_find_first_component_one("a/foo./b", true, STRV_MAKE("a", "foo.", "b"), 0); | |
654 | ||
655 | memset(foo, 'a', sizeof(foo) -1); | |
656 | char_array_0(foo); | |
657 | ||
658 | test_path_find_first_component_one(foo, false, NULL, -EINVAL); | |
659 | test_path_find_first_component_one(foo, true, NULL, -EINVAL); | |
660 | ||
661 | hoge = strjoin("a/b/c/", foo, "//d/e/.//f/"); | |
662 | assert_se(hoge); | |
663 | ||
664 | test_path_find_first_component_one(hoge, false, STRV_MAKE("a", "b", "c"), -EINVAL); | |
665 | test_path_find_first_component_one(hoge, true, STRV_MAKE("a", "b", "c"), -EINVAL); | |
666 | } | |
667 | ||
484cd43c YW |
668 | static void test_path_find_last_component_one( |
669 | const char *path, | |
670 | bool accept_dot_dot, | |
671 | char **expected, | |
672 | int ret) { | |
673 | ||
674 | log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot)); | |
675 | ||
676 | for (const char *next = NULL;;) { | |
677 | const char *e; | |
678 | int r; | |
679 | ||
680 | r = path_find_last_component(path, accept_dot_dot, &next, &e); | |
681 | if (r <= 0) { | |
682 | if (r == 0) { | |
683 | assert_se(next == path); | |
684 | assert_se(!e); | |
685 | } | |
686 | assert_se(r == ret); | |
687 | assert_se(strv_isempty(expected)); | |
688 | return; | |
689 | } | |
690 | ||
691 | assert_se(e); | |
692 | assert_se(strcspn(e, "/") == (size_t) r); | |
693 | assert_se(strlen_ptr(*expected) == (size_t) r); | |
694 | assert_se(strneq(e, *expected++, r)); | |
695 | } | |
696 | } | |
697 | ||
698 | static void test_path_find_last_component(void) { | |
699 | _cleanup_free_ char *hoge = NULL; | |
700 | char foo[NAME_MAX * 2]; | |
701 | ||
702 | log_info("/* %s */", __func__); | |
703 | ||
704 | test_path_find_last_component_one(NULL, false, NULL, 0); | |
705 | test_path_find_last_component_one("", false, NULL, 0); | |
706 | test_path_find_last_component_one("/", false, NULL, 0); | |
707 | test_path_find_last_component_one(".", false, NULL, 0); | |
708 | test_path_find_last_component_one("./", false, NULL, 0); | |
709 | test_path_find_last_component_one("./.", false, NULL, 0); | |
710 | test_path_find_last_component_one("..", false, NULL, -EINVAL); | |
711 | test_path_find_last_component_one("/..", false, NULL, -EINVAL); | |
712 | test_path_find_last_component_one("./..", false, NULL, -EINVAL); | |
713 | test_path_find_last_component_one("////./././//.", false, NULL, 0); | |
714 | test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0); | |
715 | test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0); | |
716 | test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL); | |
717 | test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL); | |
718 | ||
719 | test_path_find_last_component_one(NULL, true, NULL, 0); | |
720 | test_path_find_last_component_one("", true, NULL, 0); | |
721 | test_path_find_last_component_one("/", true, NULL, 0); | |
722 | test_path_find_last_component_one(".", true, NULL, 0); | |
723 | test_path_find_last_component_one("./", true, NULL, 0); | |
724 | test_path_find_last_component_one("./.", true, NULL, 0); | |
725 | test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0); | |
726 | test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0); | |
727 | test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0); | |
728 | test_path_find_last_component_one("////./././//.", true, NULL, 0); | |
729 | test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0); | |
730 | test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0); | |
731 | test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0); | |
732 | test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0); | |
733 | ||
734 | memset(foo, 'a', sizeof(foo) -1); | |
735 | char_array_0(foo); | |
736 | ||
737 | test_path_find_last_component_one(foo, false, NULL, -EINVAL); | |
738 | test_path_find_last_component_one(foo, true, NULL, -EINVAL); | |
739 | ||
740 | hoge = strjoin(foo, "/a/b/c/"); | |
741 | assert_se(hoge); | |
742 | ||
743 | test_path_find_last_component_one(hoge, false, STRV_MAKE("c", "b", "a"), -EINVAL); | |
744 | test_path_find_last_component_one(hoge, true, STRV_MAKE("c", "b", "a"), -EINVAL); | |
745 | } | |
746 | ||
b12d25a8 | 747 | static void test_last_path_component(void) { |
77e0a1b5 | 748 | assert_se(last_path_component(NULL) == NULL); |
b12d25a8 ZJS |
749 | assert_se(streq(last_path_component("a/b/c"), "c")); |
750 | assert_se(streq(last_path_component("a/b/c/"), "c/")); | |
751 | assert_se(streq(last_path_component("/"), "/")); | |
752 | assert_se(streq(last_path_component("//"), "/")); | |
753 | assert_se(streq(last_path_component("///"), "/")); | |
754 | assert_se(streq(last_path_component("."), ".")); | |
755 | assert_se(streq(last_path_component("./."), ".")); | |
756 | assert_se(streq(last_path_component("././"), "./")); | |
757 | assert_se(streq(last_path_component("././/"), ".//")); | |
758 | assert_se(streq(last_path_component("/foo/a"), "a")); | |
759 | assert_se(streq(last_path_component("/foo/a/"), "a/")); | |
69f9ccf1 | 760 | assert_se(streq(last_path_component(""), "")); |
8460289f LP |
761 | assert_se(streq(last_path_component("a"), "a")); |
762 | assert_se(streq(last_path_component("a/"), "a/")); | |
763 | assert_se(streq(last_path_component("/a"), "a")); | |
764 | assert_se(streq(last_path_component("/a/"), "a/")); | |
b12d25a8 ZJS |
765 | } |
766 | ||
a60c8eee LP |
767 | static void test_path_extract_filename_one(const char *input, const char *output, int ret) { |
768 | _cleanup_free_ char *k = NULL; | |
769 | int r; | |
770 | ||
771 | r = path_extract_filename(input, &k); | |
ee277c6b LP |
772 | log_info_errno(r, "%s → %s/%m [expected: %s/%s]", |
773 | strnull(input), | |
774 | strnull(k), /* strerror(r) is printed via %m, to avoid that the two strerror()'s overwrite each other's buffers */ | |
775 | strnull(output), ret < 0 ? strerror_safe(ret) : "-"); | |
a60c8eee LP |
776 | assert_se(streq_ptr(k, output)); |
777 | assert_se(r == ret); | |
778 | } | |
779 | ||
780 | static void test_path_extract_filename(void) { | |
771fded3 ZJS |
781 | log_info("/* %s */", __func__); |
782 | ||
a60c8eee LP |
783 | test_path_extract_filename_one(NULL, NULL, -EINVAL); |
784 | test_path_extract_filename_one("a/b/c", "c", 0); | |
ee277c6b | 785 | test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY); |
3cdcbdd3 LP |
786 | test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL); |
787 | test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL); | |
788 | test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL); | |
a60c8eee LP |
789 | test_path_extract_filename_one(".", NULL, -EINVAL); |
790 | test_path_extract_filename_one("./.", NULL, -EINVAL); | |
791 | test_path_extract_filename_one("././", NULL, -EINVAL); | |
792 | test_path_extract_filename_one("././/", NULL, -EINVAL); | |
793 | test_path_extract_filename_one("/foo/a", "a", 0); | |
ee277c6b | 794 | test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY); |
a60c8eee LP |
795 | test_path_extract_filename_one("", NULL, -EINVAL); |
796 | test_path_extract_filename_one("a", "a", 0); | |
ee277c6b | 797 | test_path_extract_filename_one("a/", "a", O_DIRECTORY); |
a60c8eee | 798 | test_path_extract_filename_one("/a", "a", 0); |
ee277c6b LP |
799 | test_path_extract_filename_one("/a/", "a", O_DIRECTORY); |
800 | test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY); | |
a60c8eee LP |
801 | test_path_extract_filename_one("xx/.", NULL, -EINVAL); |
802 | test_path_extract_filename_one("xx/..", NULL, -EINVAL); | |
803 | test_path_extract_filename_one("..", NULL, -EINVAL); | |
804 | test_path_extract_filename_one("/..", NULL, -EINVAL); | |
805 | test_path_extract_filename_one("../", NULL, -EINVAL); | |
806 | test_path_extract_filename_one(".", NULL, -EINVAL); | |
353df443 | 807 | test_path_extract_filename_one("/.", NULL, -EADDRNOTAVAIL); |
a60c8eee LP |
808 | test_path_extract_filename_one("./", NULL, -EINVAL); |
809 | } | |
810 | ||
8dcb891c LP |
811 | static void test_path_extract_directory_one(const char *input, const char *output, int ret) { |
812 | _cleanup_free_ char *k = NULL; | |
813 | int r; | |
814 | ||
815 | r = path_extract_directory(input, &k); | |
816 | log_info_errno(r, "%s → %s/%m [expected: %s/%s]", | |
817 | strnull(input), | |
818 | strnull(k), /* we output strerror_safe(r) via %m here, since otherwise the error buffer might be overwritten twice */ | |
819 | strnull(output), strerror_safe(ret)); | |
820 | assert_se(streq_ptr(k, output)); | |
821 | assert_se(r == ret); | |
822 | ||
823 | /* Extra safety check: let's make sure that if we split out the filename too (and it works) the | |
824 | * joined parts are identical to the original again */ | |
825 | if (r >= 0) { | |
826 | _cleanup_free_ char *f = NULL; | |
827 | ||
828 | r = path_extract_filename(input, &f); | |
829 | if (r >= 0) { | |
830 | _cleanup_free_ char *j = NULL; | |
831 | ||
832 | assert_se(j = path_join(k, f)); | |
833 | assert_se(path_equal(input, j)); | |
834 | } | |
835 | } | |
836 | } | |
837 | ||
838 | static void test_path_extract_directory(void) { | |
839 | log_info("/* %s */", __func__); | |
840 | ||
841 | test_path_extract_directory_one(NULL, NULL, -EINVAL); | |
842 | test_path_extract_directory_one("a/b/c", "a/b", 0); | |
843 | test_path_extract_directory_one("a/b/c/", "a/b", 0); | |
844 | test_path_extract_directory_one("/", NULL, -EADDRNOTAVAIL); | |
845 | test_path_extract_directory_one("//", NULL, -EADDRNOTAVAIL); | |
846 | test_path_extract_directory_one("///", NULL, -EADDRNOTAVAIL); | |
847 | test_path_extract_directory_one(".", NULL, -EDESTADDRREQ); | |
848 | test_path_extract_directory_one("./.", ".", 0); | |
849 | test_path_extract_directory_one("././", ".", 0); | |
850 | test_path_extract_directory_one("././/", ".", 0); | |
851 | test_path_extract_directory_one("/foo/a", "/foo", 0); | |
852 | test_path_extract_directory_one("/foo/a/", "/foo", 0); | |
853 | test_path_extract_directory_one("", NULL, -EINVAL); | |
854 | test_path_extract_directory_one("a", NULL, -EDESTADDRREQ); | |
855 | test_path_extract_directory_one("a/", NULL, -EDESTADDRREQ); | |
856 | test_path_extract_directory_one("/a", "/", 0); | |
857 | test_path_extract_directory_one("/a/", "/", 0); | |
858 | test_path_extract_directory_one("/////////////a/////////////", "/", 0); | |
859 | test_path_extract_directory_one("xx/.", "xx", 0); | |
860 | test_path_extract_directory_one("xx/..", "xx", 0); | |
861 | test_path_extract_directory_one("..", NULL, -EDESTADDRREQ); | |
862 | test_path_extract_directory_one("/..", "/", 0); | |
863 | test_path_extract_directory_one("../", NULL, -EDESTADDRREQ); | |
864 | test_path_extract_directory_one(".", NULL, -EDESTADDRREQ); | |
353df443 | 865 | test_path_extract_directory_one("/.", NULL, -EADDRNOTAVAIL); |
8dcb891c LP |
866 | test_path_extract_directory_one("./", NULL, -EDESTADDRREQ); |
867 | } | |
868 | ||
63292663 | 869 | static void test_filename_is_valid(void) { |
2ef2376d | 870 | char foo[NAME_MAX+2]; |
63292663 | 871 | |
771fded3 ZJS |
872 | log_info("/* %s */", __func__); |
873 | ||
63292663 RC |
874 | assert_se(!filename_is_valid("")); |
875 | assert_se(!filename_is_valid("/bar/foo")); | |
876 | assert_se(!filename_is_valid("/")); | |
877 | assert_se(!filename_is_valid(".")); | |
878 | assert_se(!filename_is_valid("..")); | |
1c322571 ZJS |
879 | assert_se(!filename_is_valid("bar/foo")); |
880 | assert_se(!filename_is_valid("bar/foo/")); | |
881 | assert_se(!filename_is_valid("bar//")); | |
63292663 | 882 | |
2ef2376d LP |
883 | memset(foo, 'a', sizeof(foo) - 1); |
884 | char_array_0(foo); | |
63292663 RC |
885 | |
886 | assert_se(!filename_is_valid(foo)); | |
887 | ||
888 | assert_se(filename_is_valid("foo_bar-333")); | |
889 | assert_se(filename_is_valid("o.o")); | |
890 | } | |
891 | ||
32df2e14 YW |
892 | static void test_path_is_valid_and_safe_one(const char *p, bool ret) { |
893 | log_debug("/* %s(\"%s\")*/", __func__, strnull(p)); | |
894 | ||
895 | assert_se(path_is_valid(p) == ret); | |
896 | if (ret) | |
897 | ret = !streq(p, "..") && | |
898 | !startswith(p, "../") && | |
899 | !endswith(p, "/..") && | |
900 | !strstr(p, "/../"); | |
901 | assert_se(path_is_safe(p) == ret); | |
902 | } | |
903 | ||
904 | static void test_path_is_valid_and_safe(void) { | |
2ef2376d LP |
905 | char foo[PATH_MAX+2]; |
906 | const char *c; | |
907 | ||
908 | log_info("/* %s */", __func__); | |
909 | ||
32df2e14 YW |
910 | test_path_is_valid_and_safe_one("", false); |
911 | test_path_is_valid_and_safe_one("/bar/foo", true); | |
912 | test_path_is_valid_and_safe_one("/bar/foo/", true); | |
913 | test_path_is_valid_and_safe_one("/bar/foo/", true); | |
914 | test_path_is_valid_and_safe_one("//bar//foo//", true); | |
915 | test_path_is_valid_and_safe_one("/", true); | |
916 | test_path_is_valid_and_safe_one("/////", true); | |
917 | test_path_is_valid_and_safe_one("/////.///.////...///..//.", true); | |
918 | test_path_is_valid_and_safe_one(".", true); | |
919 | test_path_is_valid_and_safe_one("..", true); | |
920 | test_path_is_valid_and_safe_one("bar/foo", true); | |
921 | test_path_is_valid_and_safe_one("bar/foo/", true); | |
922 | test_path_is_valid_and_safe_one("bar//", true); | |
2ef2376d LP |
923 | |
924 | memset(foo, 'a', sizeof(foo) -1); | |
925 | char_array_0(foo); | |
926 | ||
32df2e14 | 927 | test_path_is_valid_and_safe_one(foo, false); |
2ef2376d LP |
928 | |
929 | c = strjoina("/xxx/", foo, "/yyy"); | |
32df2e14 | 930 | test_path_is_valid_and_safe_one(c, false); |
2ef2376d | 931 | |
32df2e14 YW |
932 | test_path_is_valid_and_safe_one("foo_bar-333", true); |
933 | test_path_is_valid_and_safe_one("o.o", true); | |
2ef2376d LP |
934 | } |
935 | ||
b05b9cde | 936 | static void test_hidden_or_backup_file(void) { |
771fded3 ZJS |
937 | log_info("/* %s */", __func__); |
938 | ||
b05b9cde ZJS |
939 | assert_se(hidden_or_backup_file(".hidden")); |
940 | assert_se(hidden_or_backup_file("..hidden")); | |
941 | assert_se(!hidden_or_backup_file("hidden.")); | |
942 | ||
943 | assert_se(hidden_or_backup_file("backup~")); | |
944 | assert_se(hidden_or_backup_file(".backup~")); | |
945 | ||
946 | assert_se(hidden_or_backup_file("lost+found")); | |
947 | assert_se(hidden_or_backup_file("aquota.user")); | |
948 | assert_se(hidden_or_backup_file("aquota.group")); | |
949 | ||
950 | assert_se(hidden_or_backup_file("test.rpmnew")); | |
951 | assert_se(hidden_or_backup_file("test.dpkg-old")); | |
952 | assert_se(hidden_or_backup_file("test.dpkg-remove")); | |
953 | assert_se(hidden_or_backup_file("test.swp")); | |
954 | ||
955 | assert_se(!hidden_or_backup_file("test.rpmnew.")); | |
956 | assert_se(!hidden_or_backup_file("test.dpkg-old.foo")); | |
957 | } | |
958 | ||
5a46d55f ZJS |
959 | static void test_systemd_installation_has_version(const char *path) { |
960 | int r; | |
a67c318d | 961 | const unsigned versions[] = {0, 231, PROJECT_VERSION, 999}; |
5a46d55f ZJS |
962 | unsigned i; |
963 | ||
771fded3 ZJS |
964 | log_info("/* %s */", __func__); |
965 | ||
5a46d55f ZJS |
966 | for (i = 0; i < ELEMENTSOF(versions); i++) { |
967 | r = systemd_installation_has_version(path, versions[i]); | |
968 | assert_se(r >= 0); | |
969 | log_info("%s has systemd >= %u: %s", | |
970 | path ?: "Current installation", versions[i], yes_no(r)); | |
971 | } | |
972 | } | |
973 | ||
a119ec7c | 974 | static void test_skip_dev_prefix(void) { |
771fded3 | 975 | log_info("/* %s */", __func__); |
a119ec7c LP |
976 | |
977 | assert_se(streq(skip_dev_prefix("/"), "/")); | |
978 | assert_se(streq(skip_dev_prefix("/dev"), "")); | |
979 | assert_se(streq(skip_dev_prefix("/dev/"), "")); | |
980 | assert_se(streq(skip_dev_prefix("/dev/foo"), "foo")); | |
981 | assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar")); | |
982 | assert_se(streq(skip_dev_prefix("//dev"), "")); | |
983 | assert_se(streq(skip_dev_prefix("//dev//"), "")); | |
984 | assert_se(streq(skip_dev_prefix("/dev///foo"), "foo")); | |
985 | assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar")); | |
986 | assert_se(streq(skip_dev_prefix("//foo"), "//foo")); | |
987 | assert_se(streq(skip_dev_prefix("foo"), "foo")); | |
988 | } | |
989 | ||
57ea45e1 | 990 | static void test_empty_or_root(void) { |
771fded3 ZJS |
991 | log_info("/* %s */", __func__); |
992 | ||
57ea45e1 LP |
993 | assert_se(empty_or_root(NULL)); |
994 | assert_se(empty_or_root("")); | |
995 | assert_se(empty_or_root("/")); | |
996 | assert_se(empty_or_root("//")); | |
997 | assert_se(empty_or_root("///")); | |
998 | assert_se(empty_or_root("/////////////////")); | |
999 | assert_se(!empty_or_root("xxx")); | |
1000 | assert_se(!empty_or_root("/xxx")); | |
1001 | assert_se(!empty_or_root("/xxx/")); | |
1002 | assert_se(!empty_or_root("//yy//")); | |
1003 | } | |
1004 | ||
d898ed65 | 1005 | static void test_path_startswith_set(void) { |
771fded3 | 1006 | log_info("/* %s */", __func__); |
d898ed65 LP |
1007 | |
1008 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "")); | |
1009 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar")); | |
1010 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar")); | |
1011 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar")); | |
1012 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL)); | |
1013 | ||
1014 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
1015 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2")); | |
1016 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2")); | |
1017 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2")); | |
1018 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL)); | |
1019 | ||
1020 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
1021 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL)); | |
1022 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL)); | |
1023 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar")); | |
1024 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL)); | |
1025 | } | |
1026 | ||
cc4d7d81 ZJS |
1027 | static void test_path_startswith_strv(void) { |
1028 | log_info("/* %s */", __func__); | |
1029 | ||
1030 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), "")); | |
1031 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar")); | |
1032 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar")); | |
1033 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar")); | |
1034 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
1035 | ||
1036 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
1037 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2")); | |
1038 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2")); | |
1039 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2")); | |
1040 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
1041 | ||
1042 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
1043 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL)); | |
1044 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL)); | |
1045 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar")); | |
1046 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
1047 | } | |
1048 | ||
7f076504 | 1049 | int main(int argc, char **argv) { |
6d7c4033 | 1050 | test_setup_logging(LOG_DEBUG); |
5a46d55f | 1051 | |
69866062 LP |
1052 | log_info("PATH_MAX=%zu\n" |
1053 | "FILENAME_MAX=%zu\n" | |
1054 | "NAME_MAX=%zu", | |
1055 | (size_t) PATH_MAX, | |
1056 | (size_t) FILENAME_MAX, | |
1057 | (size_t) NAME_MAX); | |
1058 | ||
1059 | assert_cc(FILENAME_MAX == PATH_MAX); | |
1060 | ||
3602ca6f | 1061 | test_print_paths(); |
76877b46 | 1062 | test_path(); |
353df443 | 1063 | test_path_compare(); |
84e72b5e | 1064 | test_path_equal_root(); |
92673045 | 1065 | test_find_executable_full(); |
f7bc0c32 | 1066 | test_find_executable(argv[0]); |
5ca9139a | 1067 | test_find_executable_exec(); |
fecffe5d | 1068 | test_prefixes(); |
0c6ea3a4 | 1069 | test_path_join(); |
7ae27680 | 1070 | test_path_extend(); |
eb66db55 | 1071 | test_fsck_exists(); |
6b56a651 | 1072 | test_make_relative(); |
3e8a78c8 | 1073 | test_strv_resolve(); |
5895b62f | 1074 | test_path_startswith(); |
1d13f648 | 1075 | test_prefix_root(); |
63292663 | 1076 | test_file_in_same_dir(); |
0ee54dd4 | 1077 | test_path_find_first_component(); |
484cd43c | 1078 | test_path_find_last_component(); |
b12d25a8 | 1079 | test_last_path_component(); |
a60c8eee | 1080 | test_path_extract_filename(); |
8dcb891c | 1081 | test_path_extract_directory(); |
63292663 | 1082 | test_filename_is_valid(); |
32df2e14 | 1083 | test_path_is_valid_and_safe(); |
b05b9cde | 1084 | test_hidden_or_backup_file(); |
a119ec7c | 1085 | test_skip_dev_prefix(); |
57ea45e1 | 1086 | test_empty_or_root(); |
d898ed65 | 1087 | test_path_startswith_set(); |
cc4d7d81 | 1088 | test_path_startswith_strv(); |
5895b62f | 1089 | |
5a46d55f ZJS |
1090 | test_systemd_installation_has_version(argv[1]); /* NULL is OK */ |
1091 | ||
76877b46 ZJS |
1092 | return 0; |
1093 | } |