]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
76877b46 | 2 | |
a696dbef | 3 | #include <stdio.h> |
07630cea | 4 | #include <unistd.h> |
a696dbef | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
3ffd4af2 | 7 | #include "fd-util.h" |
76877b46 | 8 | #include "macro.h" |
049af8ad | 9 | #include "mountpoint-util.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 | ||
2230852b MS |
24 | #define test_path_compare(a, b, result) { \ |
25 | assert_se(path_compare(a, b) == result); \ | |
26 | assert_se(path_compare(b, a) == -result); \ | |
27 | assert_se(path_equal(a, b) == !result); \ | |
28 | assert_se(path_equal(b, a) == !result); \ | |
29 | } | |
76877b46 | 30 | |
858d36c1 YW |
31 | static void test_path_simplify(const char *in, const char *out, const char *out_dot) { |
32 | char *p; | |
33 | ||
771fded3 ZJS |
34 | log_info("/* %s */", __func__); |
35 | ||
858d36c1 YW |
36 | p = strdupa(in); |
37 | assert_se(streq(path_simplify(p, false), out)); | |
38 | ||
39 | p = strdupa(in); | |
40 | assert_se(streq(path_simplify(p, true), out_dot)); | |
41 | } | |
42 | ||
76877b46 | 43 | static void test_path(void) { |
3f72b427 LP |
44 | _cleanup_close_ int fd = -1; |
45 | ||
771fded3 ZJS |
46 | log_info("/* %s */", __func__); |
47 | ||
2230852b MS |
48 | test_path_compare("/goo", "/goo", 0); |
49 | test_path_compare("/goo", "/goo", 0); | |
50 | test_path_compare("//goo", "/goo", 0); | |
51 | test_path_compare("//goo/////", "/goo", 0); | |
52 | test_path_compare("goo/////", "goo", 0); | |
76877b46 | 53 | |
2230852b MS |
54 | test_path_compare("/goo/boo", "/goo//boo", 0); |
55 | test_path_compare("//goo/boo", "/goo/boo//", 0); | |
76877b46 | 56 | |
2230852b | 57 | test_path_compare("/", "///", 0); |
76877b46 | 58 | |
2230852b MS |
59 | test_path_compare("/x", "x/", 1); |
60 | test_path_compare("x/", "/", -1); | |
76877b46 | 61 | |
2230852b MS |
62 | test_path_compare("/x/./y", "x/y", 1); |
63 | test_path_compare("x/.y", "x/y", -1); | |
64 | ||
65 | test_path_compare("foo", "/foo", -1); | |
66 | test_path_compare("/foo", "/foo/bar", -1); | |
67 | test_path_compare("/foo/aaa", "/foo/b", -1); | |
68 | test_path_compare("/foo/aaa", "/foo/b/a", -1); | |
69 | test_path_compare("/foo/a", "/foo/aaa", -1); | |
70 | test_path_compare("/foo/a/b", "/foo/aaa", -1); | |
76877b46 ZJS |
71 | |
72 | assert_se(path_is_absolute("/")); | |
73 | assert_se(!path_is_absolute("./")); | |
74 | ||
75 | assert_se(is_path("/dir")); | |
76 | assert_se(is_path("a/b")); | |
77 | assert_se(!is_path(".")); | |
78 | ||
2b6bf07d ZJS |
79 | assert_se(streq(basename("./aa/bb/../file.da."), "file.da.")); |
80 | assert_se(streq(basename("/aa///.file"), ".file")); | |
81 | assert_se(streq(basename("/aa///file..."), "file...")); | |
82 | assert_se(streq(basename("file.../"), "")); | |
76877b46 | 83 | |
3f72b427 LP |
84 | fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY); |
85 | assert_se(fd >= 0); | |
5d409034 | 86 | assert_se(fd_is_mount_point(fd, "/", 0) > 0); |
76877b46 | 87 | |
858d36c1 YW |
88 | test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc"); |
89 | test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc"); | |
90 | test_path_simplify("///", "/", "/"); | |
91 | test_path_simplify("///.//", "/.", "/"); | |
92 | test_path_simplify("///.//.///", "/./.", "/"); | |
93 | test_path_simplify("////.././///../.", "/.././../.", "/../.."); | |
afbae3e9 TH |
94 | test_path_simplify(".", ".", "."); |
95 | test_path_simplify("./", ".", "."); | |
96 | test_path_simplify(".///.//./.", "./././.", "."); | |
97 | test_path_simplify(".///.//././/", "./././.", "."); | |
858d36c1 YW |
98 | test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", |
99 | "/./aaa/././.bbb/../c./d.dd/..eeee/.", | |
100 | "/aaa/.bbb/../c./d.dd/..eeee"); | |
101 | test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
102 | "/./aaa/././.bbb/../c./d.dd/..eeee/..", | |
103 | "/aaa/.bbb/../c./d.dd/..eeee/.."); | |
104 | test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
105 | "././aaa/././.bbb/../c./d.dd/..eeee/..", | |
106 | "aaa/.bbb/../c./d.dd/..eeee/.."); | |
107 | test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
108 | ".././aaa/././.bbb/../c./d.dd/..eeee/..", | |
109 | "../aaa/.bbb/../c./d.dd/..eeee/.."); | |
3ae5990c ZJS |
110 | |
111 | assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo")); | |
112 | assert_se(PATH_IN_SET("/bin", "/bin")); | |
113 | assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin")); | |
114 | assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar")); | |
115 | assert_se(!PATH_IN_SET("/", "/abc", "/def")); | |
24737c29 ZJS |
116 | |
117 | assert_se(path_equal_ptr(NULL, NULL)); | |
118 | assert_se(path_equal_ptr("/a", "/a")); | |
119 | assert_se(!path_equal_ptr("/a", "/b")); | |
120 | assert_se(!path_equal_ptr("/a", NULL)); | |
121 | assert_se(!path_equal_ptr(NULL, "/a")); | |
76877b46 ZJS |
122 | } |
123 | ||
84e72b5e ZJS |
124 | static void test_path_equal_root(void) { |
125 | /* Nail down the details of how path_equal("/", ...) works. */ | |
126 | ||
771fded3 ZJS |
127 | log_info("/* %s */", __func__); |
128 | ||
84e72b5e ZJS |
129 | assert_se(path_equal("/", "/")); |
130 | assert_se(path_equal("/", "//")); | |
131 | ||
132 | assert_se(!path_equal("/", "/./")); | |
133 | assert_se(!path_equal("/", "/../")); | |
134 | ||
135 | assert_se(!path_equal("/", "/.../")); | |
136 | ||
137 | /* Make sure that files_same works as expected. */ | |
138 | ||
e3f791a2 ZJS |
139 | assert_se(files_same("/", "/", 0) > 0); |
140 | assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0); | |
141 | assert_se(files_same("/", "//", 0) > 0); | |
142 | assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0); | |
84e72b5e | 143 | |
e3f791a2 ZJS |
144 | assert_se(files_same("/", "/./", 0) > 0); |
145 | assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0); | |
146 | assert_se(files_same("/", "/../", 0) > 0); | |
147 | assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0); | |
84e72b5e | 148 | |
e3f791a2 ZJS |
149 | assert_se(files_same("/", "/.../", 0) == -ENOENT); |
150 | assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT); | |
84e72b5e ZJS |
151 | |
152 | /* The same for path_equal_or_files_same. */ | |
153 | ||
e3f791a2 ZJS |
154 | assert_se(path_equal_or_files_same("/", "/", 0)); |
155 | assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW)); | |
156 | assert_se(path_equal_or_files_same("/", "//", 0)); | |
157 | assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e | 158 | |
e3f791a2 ZJS |
159 | assert_se(path_equal_or_files_same("/", "/./", 0)); |
160 | assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW)); | |
161 | assert_se(path_equal_or_files_same("/", "/../", 0)); | |
162 | assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e | 163 | |
e3f791a2 ZJS |
164 | assert_se(!path_equal_or_files_same("/", "/.../", 0)); |
165 | assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e ZJS |
166 | } |
167 | ||
92673045 ZJS |
168 | static void test_find_executable_full(void) { |
169 | char *p; | |
170 | ||
171 | log_info("/* %s */", __func__); | |
172 | ||
5ca9139a | 173 | assert_se(find_executable_full("sh", true, &p, NULL) == 0); |
92673045 ZJS |
174 | puts(p); |
175 | assert_se(streq(basename(p), "sh")); | |
176 | free(p); | |
177 | ||
5ca9139a | 178 | assert_se(find_executable_full("sh", false, &p, NULL) == 0); |
92673045 ZJS |
179 | puts(p); |
180 | assert_se(streq(basename(p), "sh")); | |
181 | free(p); | |
182 | ||
183 | _cleanup_free_ char *oldpath = NULL; | |
184 | p = getenv("PATH"); | |
185 | if (p) | |
186 | assert_se(oldpath = strdup(p)); | |
187 | ||
188 | assert_se(unsetenv("PATH") >= 0); | |
189 | ||
5ca9139a | 190 | assert_se(find_executable_full("sh", true, &p, NULL) == 0); |
92673045 ZJS |
191 | puts(p); |
192 | assert_se(streq(basename(p), "sh")); | |
193 | free(p); | |
194 | ||
5ca9139a | 195 | assert_se(find_executable_full("sh", false, &p, NULL) == 0); |
92673045 ZJS |
196 | puts(p); |
197 | assert_se(streq(basename(p), "sh")); | |
198 | free(p); | |
199 | ||
200 | if (oldpath) | |
201 | assert_se(setenv("PATH", oldpath, true) >= 0); | |
202 | } | |
203 | ||
f7bc0c32 | 204 | static void test_find_executable(const char *self) { |
c9d954b2 ZJS |
205 | char *p; |
206 | ||
771fded3 ZJS |
207 | log_info("/* %s */", __func__); |
208 | ||
f7bc0c32 | 209 | assert_se(find_executable("/bin/sh", &p) == 0); |
c9d954b2 | 210 | puts(p); |
85eca92e | 211 | assert_se(path_equal(p, "/bin/sh")); |
c9d954b2 ZJS |
212 | free(p); |
213 | ||
f7bc0c32 | 214 | assert_se(find_executable(self, &p) == 0); |
c9d954b2 | 215 | puts(p); |
92673045 | 216 | assert_se(endswith(p, "/test-path-util")); |
8d95631e | 217 | assert_se(path_is_absolute(p)); |
c9d954b2 ZJS |
218 | free(p); |
219 | ||
f7bc0c32 | 220 | assert_se(find_executable("sh", &p) == 0); |
c9d954b2 | 221 | puts(p); |
8d95631e FB |
222 | assert_se(endswith(p, "/sh")); |
223 | assert_se(path_is_absolute(p)); | |
c9d954b2 ZJS |
224 | free(p); |
225 | ||
92673045 ZJS |
226 | assert_se(find_executable("/bin/touch", &p) == 0); |
227 | assert_se(streq(p, "/bin/touch")); | |
228 | free(p); | |
229 | ||
230 | assert_se(find_executable("touch", &p) == 0); | |
231 | assert_se(path_is_absolute(p)); | |
232 | assert_se(streq(basename(p), "touch")); | |
233 | free(p); | |
234 | ||
f7bc0c32 ZJS |
235 | assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT); |
236 | assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT); | |
92673045 | 237 | assert_se(find_executable("/proc/filesystems", &p) == -EACCES); |
c9d954b2 ZJS |
238 | } |
239 | ||
5ca9139a ZJS |
240 | static void test_find_executable_exec_one(const char *path) { |
241 | _cleanup_free_ char *t = NULL; | |
242 | _cleanup_close_ int fd = -1; | |
243 | pid_t pid; | |
244 | int r; | |
245 | ||
246 | r = find_executable_full(path, false, &t, &fd); | |
247 | ||
248 | log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd); | |
249 | ||
250 | assert_se(fd > STDERR_FILENO); | |
251 | assert_se(path_is_absolute(t)); | |
252 | if (path_is_absolute(path)) | |
253 | assert_se(streq(t, path)); | |
254 | ||
255 | pid = fork(); | |
256 | assert_se(pid >= 0); | |
257 | if (pid == 0) { | |
258 | fexecve(fd, STRV_MAKE(t, "--version"), STRV_MAKE(NULL)); | |
259 | log_error_errno(errno, "fexecve: %m"); | |
260 | _exit(EXIT_FAILURE); | |
261 | } | |
262 | ||
263 | assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0); | |
264 | } | |
265 | ||
266 | static void test_find_executable_exec(void) { | |
267 | log_info("/* %s */", __func__); | |
268 | ||
269 | test_find_executable_exec_one("touch"); | |
270 | test_find_executable_exec_one("/bin/touch"); | |
271 | } | |
272 | ||
fecffe5d | 273 | static void test_prefixes(void) { |
b82f71c7 LP |
274 | static const char* const values[] = { |
275 | "/a/b/c/d", | |
276 | "/a/b/c", | |
277 | "/a/b", | |
278 | "/a", | |
279 | "", | |
280 | NULL | |
281 | }; | |
e203f7c3 | 282 | unsigned i; |
fecffe5d | 283 | char s[PATH_MAX]; |
e203f7c3 | 284 | bool b; |
fecffe5d | 285 | |
771fded3 ZJS |
286 | log_info("/* %s */", __func__); |
287 | ||
e203f7c3 LP |
288 | i = 0; |
289 | PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { | |
fecffe5d LP |
290 | log_error("---%s---", s); |
291 | assert_se(streq(s, values[i++])); | |
292 | } | |
e203f7c3 | 293 | assert_se(values[i] == NULL); |
fecffe5d | 294 | |
e203f7c3 LP |
295 | i = 1; |
296 | PATH_FOREACH_PREFIX(s, "/a/b/c/d") { | |
297 | log_error("---%s---", s); | |
298 | assert_se(streq(s, values[i++])); | |
299 | } | |
fecffe5d LP |
300 | assert_se(values[i] == NULL); |
301 | ||
302 | i = 0; | |
e203f7c3 | 303 | PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") |
fecffe5d | 304 | assert_se(streq(s, values[i++])); |
e203f7c3 | 305 | assert_se(values[i] == NULL); |
fecffe5d | 306 | |
e203f7c3 LP |
307 | i = 1; |
308 | PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") | |
309 | assert_se(streq(s, values[i++])); | |
fecffe5d LP |
310 | assert_se(values[i] == NULL); |
311 | ||
312 | PATH_FOREACH_PREFIX(s, "////") | |
e203f7c3 LP |
313 | assert_not_reached("Wut?"); |
314 | ||
315 | b = false; | |
316 | PATH_FOREACH_PREFIX_MORE(s, "////") { | |
317 | assert_se(!b); | |
fecffe5d | 318 | assert_se(streq(s, "")); |
e203f7c3 LP |
319 | b = true; |
320 | } | |
321 | assert_se(b); | |
fecffe5d LP |
322 | |
323 | PATH_FOREACH_PREFIX(s, "") | |
324 | assert_not_reached("wut?"); | |
325 | ||
e203f7c3 LP |
326 | b = false; |
327 | PATH_FOREACH_PREFIX_MORE(s, "") { | |
8d95631e FB |
328 | assert_se(!b); |
329 | assert_se(streq(s, "")); | |
e203f7c3 LP |
330 | b = true; |
331 | } | |
fecffe5d LP |
332 | } |
333 | ||
0c6ea3a4 | 334 | static void test_path_join(void) { |
771fded3 | 335 | log_info("/* %s */", __func__); |
59ae3a95 | 336 | |
62a85ee0 | 337 | #define test_join(expected, ...) { \ |
59ae3a95 | 338 | _cleanup_free_ char *z = NULL; \ |
62a85ee0 ZJS |
339 | z = path_join(__VA_ARGS__); \ |
340 | log_debug("got \"%s\", expected \"%s\"", z, expected); \ | |
59ae3a95 TA |
341 | assert_se(streq(z, expected)); \ |
342 | } | |
343 | ||
62a85ee0 ZJS |
344 | test_join("/root/a/b/c", "/root", "/a/b", "/c"); |
345 | test_join("/root/a/b/c", "/root", "a/b", "c"); | |
346 | test_join("/root/a/b/c", "/root", "/a/b", "c"); | |
347 | test_join("/root/c", "/root", "/", "c"); | |
348 | test_join("/root/", "/root", "/", NULL); | |
349 | ||
350 | test_join("/a/b/c", "", "/a/b", "/c"); | |
351 | test_join("a/b/c", "", "a/b", "c"); | |
352 | test_join("/a/b/c", "", "/a/b", "c"); | |
353 | test_join("/c", "", "/", "c"); | |
354 | test_join("/", "", "/", NULL); | |
355 | ||
652ef298 ZJS |
356 | test_join("/a/b/c", NULL, "/a/b", "/c"); |
357 | test_join("a/b/c", NULL, "a/b", "c"); | |
358 | test_join("/a/b/c", NULL, "/a/b", "c"); | |
359 | test_join("/c", NULL, "/", "c"); | |
360 | test_join("/", NULL, "/", NULL); | |
361 | ||
62a85ee0 | 362 | test_join("", "", NULL); |
652ef298 ZJS |
363 | test_join("", NULL, ""); |
364 | test_join("", NULL, NULL); | |
62a85ee0 ZJS |
365 | |
366 | test_join("foo/bar", "foo", "bar"); | |
367 | test_join("foo/bar", "", "foo", "bar"); | |
652ef298 | 368 | test_join("foo/bar", NULL, "foo", NULL, "bar"); |
62a85ee0 ZJS |
369 | test_join("foo/bar", "", "foo", "", "bar", ""); |
370 | test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", ""); | |
371 | ||
372 | test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", ""); | |
373 | test_join("/foo/bar/", "/", "foo", "/", "bar", "/"); | |
374 | test_join("foo/bar/baz", "foo", "bar", "baz"); | |
375 | test_join("foo/bar/baz", "foo/", "bar", "/baz"); | |
376 | test_join("foo//bar//baz", "foo/", "/bar/", "/baz"); | |
377 | test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//"); | |
0c6ea3a4 ZJS |
378 | } |
379 | ||
eb66db55 | 380 | static void test_fsck_exists(void) { |
771fded3 ZJS |
381 | log_info("/* %s */", __func__); |
382 | ||
eb66db55 MG |
383 | /* Ensure we use a sane default for PATH. */ |
384 | unsetenv("PATH"); | |
385 | ||
386 | /* fsck.minix is provided by util-linux and will probably exist. */ | |
85eca92e | 387 | assert_se(fsck_exists("minix") == 1); |
eb66db55 | 388 | |
85eca92e LP |
389 | assert_se(fsck_exists("AbCdE") == 0); |
390 | assert_se(fsck_exists("/../bin/") == 0); | |
eb66db55 MG |
391 | } |
392 | ||
6b56a651 TK |
393 | static void test_make_relative(void) { |
394 | char *result; | |
395 | ||
771fded3 ZJS |
396 | log_info("/* %s */", __func__); |
397 | ||
6b56a651 TK |
398 | assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0); |
399 | assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0); | |
2a5beb66 | 400 | assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0); |
6b56a651 | 401 | |
59ae3a95 TA |
402 | #define test(from_dir, to_path, expected) { \ |
403 | _cleanup_free_ char *z = NULL; \ | |
404 | path_make_relative(from_dir, to_path, &z); \ | |
405 | assert_se(streq(z, expected)); \ | |
6b56a651 TK |
406 | } |
407 | ||
408 | test("/", "/", "."); | |
409 | test("/", "/some/path", "some/path"); | |
410 | test("/some/path", "/some/path", "."); | |
411 | test("/some/path", "/some/path/in/subdir", "in/subdir"); | |
412 | test("/some/path", "/", "../.."); | |
413 | test("/some/path", "/some/other/path", "../other/path"); | |
2a5beb66 | 414 | test("/some/path/./dot", "/some/further/path", "../../further/path"); |
48054262 | 415 | test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); |
6b56a651 TK |
416 | } |
417 | ||
3e8a78c8 MM |
418 | static void test_strv_resolve(void) { |
419 | char tmp_dir[] = "/tmp/test-path-util-XXXXXX"; | |
420 | _cleanup_strv_free_ char **search_dirs = NULL; | |
421 | _cleanup_strv_free_ char **absolute_dirs = NULL; | |
422 | char **d; | |
423 | ||
424 | assert_se(mkdtemp(tmp_dir) != NULL); | |
425 | ||
bea1a013 | 426 | search_dirs = strv_new("/dir1", "/dir2", "/dir3"); |
3e8a78c8 MM |
427 | assert_se(search_dirs); |
428 | STRV_FOREACH(d, search_dirs) { | |
b910cc72 | 429 | char *p = path_join(tmp_dir, *d); |
3e8a78c8 MM |
430 | assert_se(p); |
431 | assert_se(strv_push(&absolute_dirs, p) == 0); | |
432 | } | |
433 | ||
434 | assert_se(mkdir(absolute_dirs[0], 0700) == 0); | |
435 | assert_se(mkdir(absolute_dirs[1], 0700) == 0); | |
436 | assert_se(symlink("dir2", absolute_dirs[2]) == 0); | |
437 | ||
438 | path_strv_resolve(search_dirs, tmp_dir); | |
439 | assert_se(streq(search_dirs[0], "/dir1")); | |
440 | assert_se(streq(search_dirs[1], "/dir2")); | |
441 | assert_se(streq(search_dirs[2], "/dir2")); | |
442 | ||
c6878637 | 443 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
3e8a78c8 MM |
444 | } |
445 | ||
5895b62f | 446 | static void test_path_startswith(void) { |
0470289b ZJS |
447 | const char *p; |
448 | ||
771fded3 ZJS |
449 | log_info("/* %s */", __func__); |
450 | ||
0470289b ZJS |
451 | p = path_startswith("/foo/bar/barfoo/", "/foo"); |
452 | assert_se(streq_ptr(p, "bar/barfoo/")); | |
453 | ||
454 | p = path_startswith("/foo/bar/barfoo/", "/foo/"); | |
455 | assert_se(streq_ptr(p, "bar/barfoo/")); | |
456 | ||
457 | p = path_startswith("/foo/bar/barfoo/", "/"); | |
458 | assert_se(streq_ptr(p, "foo/bar/barfoo/")); | |
459 | ||
460 | p = path_startswith("/foo/bar/barfoo/", "////"); | |
461 | assert_se(streq_ptr(p, "foo/bar/barfoo/")); | |
462 | ||
463 | p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///"); | |
464 | assert_se(streq_ptr(p, "")); | |
465 | ||
466 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////"); | |
467 | assert_se(streq_ptr(p, "")); | |
468 | ||
469 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/"); | |
470 | assert_se(streq_ptr(p, "")); | |
471 | ||
472 | p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/"); | |
473 | assert_se(streq_ptr(p, "")); | |
474 | ||
475 | p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/"); | |
476 | assert_se(streq_ptr(p, "")); | |
477 | ||
478 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo"); | |
479 | assert_se(streq_ptr(p, "")); | |
5895b62f RC |
480 | |
481 | assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/")); | |
482 | assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa")); | |
483 | assert_se(!path_startswith("/foo/bar/barfoo/", "")); | |
484 | assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo")); | |
485 | assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/")); | |
486 | } | |
487 | ||
1d13f648 LP |
488 | static void test_prefix_root_one(const char *r, const char *p, const char *expected) { |
489 | _cleanup_free_ char *s = NULL; | |
490 | const char *t; | |
491 | ||
c6134d3e LP |
492 | assert_se(s = path_join(r, p)); |
493 | assert_se(path_equal_ptr(s, expected)); | |
1d13f648 LP |
494 | |
495 | t = prefix_roota(r, p); | |
496 | assert_se(t); | |
c6134d3e | 497 | assert_se(path_equal_ptr(t, expected)); |
1d13f648 LP |
498 | } |
499 | ||
500 | static void test_prefix_root(void) { | |
771fded3 ZJS |
501 | log_info("/* %s */", __func__); |
502 | ||
1d13f648 LP |
503 | test_prefix_root_one("/", "/foo", "/foo"); |
504 | test_prefix_root_one(NULL, "/foo", "/foo"); | |
505 | test_prefix_root_one("", "/foo", "/foo"); | |
506 | test_prefix_root_one("///", "/foo", "/foo"); | |
507 | test_prefix_root_one("/", "////foo", "/foo"); | |
508 | test_prefix_root_one(NULL, "////foo", "/foo"); | |
f9421dd8 YW |
509 | test_prefix_root_one("/", "foo", "/foo"); |
510 | test_prefix_root_one("", "foo", "foo"); | |
511 | test_prefix_root_one(NULL, "foo", "foo"); | |
1d13f648 LP |
512 | |
513 | test_prefix_root_one("/foo", "/bar", "/foo/bar"); | |
514 | test_prefix_root_one("/foo", "bar", "/foo/bar"); | |
515 | test_prefix_root_one("foo", "bar", "foo/bar"); | |
516 | test_prefix_root_one("/foo/", "/bar", "/foo/bar"); | |
517 | test_prefix_root_one("/foo/", "//bar", "/foo/bar"); | |
518 | test_prefix_root_one("/foo///", "//bar", "/foo/bar"); | |
519 | } | |
520 | ||
63292663 RC |
521 | static void test_file_in_same_dir(void) { |
522 | char *t; | |
523 | ||
771fded3 ZJS |
524 | log_info("/* %s */", __func__); |
525 | ||
63292663 RC |
526 | t = file_in_same_dir("/", "a"); |
527 | assert_se(streq(t, "/a")); | |
528 | free(t); | |
529 | ||
530 | t = file_in_same_dir("/", "/a"); | |
531 | assert_se(streq(t, "/a")); | |
532 | free(t); | |
533 | ||
534 | t = file_in_same_dir("", "a"); | |
535 | assert_se(streq(t, "a")); | |
536 | free(t); | |
537 | ||
538 | t = file_in_same_dir("a/", "a"); | |
539 | assert_se(streq(t, "a/a")); | |
540 | free(t); | |
541 | ||
542 | t = file_in_same_dir("bar/foo", "bar"); | |
543 | assert_se(streq(t, "bar/bar")); | |
544 | free(t); | |
545 | } | |
546 | ||
b12d25a8 | 547 | static void test_last_path_component(void) { |
77e0a1b5 | 548 | assert_se(last_path_component(NULL) == NULL); |
b12d25a8 ZJS |
549 | assert_se(streq(last_path_component("a/b/c"), "c")); |
550 | assert_se(streq(last_path_component("a/b/c/"), "c/")); | |
551 | assert_se(streq(last_path_component("/"), "/")); | |
552 | assert_se(streq(last_path_component("//"), "/")); | |
553 | assert_se(streq(last_path_component("///"), "/")); | |
554 | assert_se(streq(last_path_component("."), ".")); | |
555 | assert_se(streq(last_path_component("./."), ".")); | |
556 | assert_se(streq(last_path_component("././"), "./")); | |
557 | assert_se(streq(last_path_component("././/"), ".//")); | |
558 | assert_se(streq(last_path_component("/foo/a"), "a")); | |
559 | assert_se(streq(last_path_component("/foo/a/"), "a/")); | |
69f9ccf1 | 560 | assert_se(streq(last_path_component(""), "")); |
8460289f LP |
561 | assert_se(streq(last_path_component("a"), "a")); |
562 | assert_se(streq(last_path_component("a/"), "a/")); | |
563 | assert_se(streq(last_path_component("/a"), "a")); | |
564 | assert_se(streq(last_path_component("/a/"), "a/")); | |
b12d25a8 ZJS |
565 | } |
566 | ||
a60c8eee LP |
567 | static void test_path_extract_filename_one(const char *input, const char *output, int ret) { |
568 | _cleanup_free_ char *k = NULL; | |
569 | int r; | |
570 | ||
571 | r = path_extract_filename(input, &k); | |
4bbccb02 | 572 | log_info("%s → %s/%s [expected: %s/%s]", strnull(input), strnull(k), strerror_safe(r), strnull(output), strerror_safe(ret)); |
a60c8eee LP |
573 | assert_se(streq_ptr(k, output)); |
574 | assert_se(r == ret); | |
575 | } | |
576 | ||
577 | static void test_path_extract_filename(void) { | |
771fded3 ZJS |
578 | log_info("/* %s */", __func__); |
579 | ||
a60c8eee LP |
580 | test_path_extract_filename_one(NULL, NULL, -EINVAL); |
581 | test_path_extract_filename_one("a/b/c", "c", 0); | |
582 | test_path_extract_filename_one("a/b/c/", "c", 0); | |
583 | test_path_extract_filename_one("/", NULL, -EINVAL); | |
584 | test_path_extract_filename_one("//", NULL, -EINVAL); | |
585 | test_path_extract_filename_one("///", NULL, -EINVAL); | |
586 | test_path_extract_filename_one(".", NULL, -EINVAL); | |
587 | test_path_extract_filename_one("./.", NULL, -EINVAL); | |
588 | test_path_extract_filename_one("././", NULL, -EINVAL); | |
589 | test_path_extract_filename_one("././/", NULL, -EINVAL); | |
590 | test_path_extract_filename_one("/foo/a", "a", 0); | |
591 | test_path_extract_filename_one("/foo/a/", "a", 0); | |
592 | test_path_extract_filename_one("", NULL, -EINVAL); | |
593 | test_path_extract_filename_one("a", "a", 0); | |
594 | test_path_extract_filename_one("a/", "a", 0); | |
595 | test_path_extract_filename_one("/a", "a", 0); | |
596 | test_path_extract_filename_one("/a/", "a", 0); | |
597 | test_path_extract_filename_one("/////////////a/////////////", "a", 0); | |
598 | test_path_extract_filename_one("xx/.", NULL, -EINVAL); | |
599 | test_path_extract_filename_one("xx/..", NULL, -EINVAL); | |
600 | test_path_extract_filename_one("..", NULL, -EINVAL); | |
601 | test_path_extract_filename_one("/..", NULL, -EINVAL); | |
602 | test_path_extract_filename_one("../", NULL, -EINVAL); | |
603 | test_path_extract_filename_one(".", NULL, -EINVAL); | |
604 | test_path_extract_filename_one("/.", NULL, -EINVAL); | |
605 | test_path_extract_filename_one("./", NULL, -EINVAL); | |
606 | } | |
607 | ||
63292663 RC |
608 | static void test_filename_is_valid(void) { |
609 | char foo[FILENAME_MAX+2]; | |
610 | int i; | |
611 | ||
771fded3 ZJS |
612 | log_info("/* %s */", __func__); |
613 | ||
63292663 RC |
614 | assert_se(!filename_is_valid("")); |
615 | assert_se(!filename_is_valid("/bar/foo")); | |
616 | assert_se(!filename_is_valid("/")); | |
617 | assert_se(!filename_is_valid(".")); | |
618 | assert_se(!filename_is_valid("..")); | |
1c322571 ZJS |
619 | assert_se(!filename_is_valid("bar/foo")); |
620 | assert_se(!filename_is_valid("bar/foo/")); | |
621 | assert_se(!filename_is_valid("bar//")); | |
63292663 RC |
622 | |
623 | for (i=0; i<FILENAME_MAX+1; i++) | |
624 | foo[i] = 'a'; | |
625 | foo[FILENAME_MAX+1] = '\0'; | |
626 | ||
627 | assert_se(!filename_is_valid(foo)); | |
628 | ||
629 | assert_se(filename_is_valid("foo_bar-333")); | |
630 | assert_se(filename_is_valid("o.o")); | |
631 | } | |
632 | ||
b05b9cde | 633 | static void test_hidden_or_backup_file(void) { |
771fded3 ZJS |
634 | log_info("/* %s */", __func__); |
635 | ||
b05b9cde ZJS |
636 | assert_se(hidden_or_backup_file(".hidden")); |
637 | assert_se(hidden_or_backup_file("..hidden")); | |
638 | assert_se(!hidden_or_backup_file("hidden.")); | |
639 | ||
640 | assert_se(hidden_or_backup_file("backup~")); | |
641 | assert_se(hidden_or_backup_file(".backup~")); | |
642 | ||
643 | assert_se(hidden_or_backup_file("lost+found")); | |
644 | assert_se(hidden_or_backup_file("aquota.user")); | |
645 | assert_se(hidden_or_backup_file("aquota.group")); | |
646 | ||
647 | assert_se(hidden_or_backup_file("test.rpmnew")); | |
648 | assert_se(hidden_or_backup_file("test.dpkg-old")); | |
649 | assert_se(hidden_or_backup_file("test.dpkg-remove")); | |
650 | assert_se(hidden_or_backup_file("test.swp")); | |
651 | ||
652 | assert_se(!hidden_or_backup_file("test.rpmnew.")); | |
653 | assert_se(!hidden_or_backup_file("test.dpkg-old.foo")); | |
654 | } | |
655 | ||
5a46d55f ZJS |
656 | static void test_systemd_installation_has_version(const char *path) { |
657 | int r; | |
a67c318d | 658 | const unsigned versions[] = {0, 231, PROJECT_VERSION, 999}; |
5a46d55f ZJS |
659 | unsigned i; |
660 | ||
771fded3 ZJS |
661 | log_info("/* %s */", __func__); |
662 | ||
5a46d55f ZJS |
663 | for (i = 0; i < ELEMENTSOF(versions); i++) { |
664 | r = systemd_installation_has_version(path, versions[i]); | |
665 | assert_se(r >= 0); | |
666 | log_info("%s has systemd >= %u: %s", | |
667 | path ?: "Current installation", versions[i], yes_no(r)); | |
668 | } | |
669 | } | |
670 | ||
a119ec7c | 671 | static void test_skip_dev_prefix(void) { |
771fded3 | 672 | log_info("/* %s */", __func__); |
a119ec7c LP |
673 | |
674 | assert_se(streq(skip_dev_prefix("/"), "/")); | |
675 | assert_se(streq(skip_dev_prefix("/dev"), "")); | |
676 | assert_se(streq(skip_dev_prefix("/dev/"), "")); | |
677 | assert_se(streq(skip_dev_prefix("/dev/foo"), "foo")); | |
678 | assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar")); | |
679 | assert_se(streq(skip_dev_prefix("//dev"), "")); | |
680 | assert_se(streq(skip_dev_prefix("//dev//"), "")); | |
681 | assert_se(streq(skip_dev_prefix("/dev///foo"), "foo")); | |
682 | assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar")); | |
683 | assert_se(streq(skip_dev_prefix("//foo"), "//foo")); | |
684 | assert_se(streq(skip_dev_prefix("foo"), "foo")); | |
685 | } | |
686 | ||
57ea45e1 | 687 | static void test_empty_or_root(void) { |
771fded3 ZJS |
688 | log_info("/* %s */", __func__); |
689 | ||
57ea45e1 LP |
690 | assert_se(empty_or_root(NULL)); |
691 | assert_se(empty_or_root("")); | |
692 | assert_se(empty_or_root("/")); | |
693 | assert_se(empty_or_root("//")); | |
694 | assert_se(empty_or_root("///")); | |
695 | assert_se(empty_or_root("/////////////////")); | |
696 | assert_se(!empty_or_root("xxx")); | |
697 | assert_se(!empty_or_root("/xxx")); | |
698 | assert_se(!empty_or_root("/xxx/")); | |
699 | assert_se(!empty_or_root("//yy//")); | |
700 | } | |
701 | ||
d898ed65 | 702 | static void test_path_startswith_set(void) { |
771fded3 | 703 | log_info("/* %s */", __func__); |
d898ed65 LP |
704 | |
705 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "")); | |
706 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar")); | |
707 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar")); | |
708 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar")); | |
709 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL)); | |
710 | ||
711 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
712 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2")); | |
713 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2")); | |
714 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2")); | |
715 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL)); | |
716 | ||
717 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
718 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL)); | |
719 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL)); | |
720 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar")); | |
721 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL)); | |
722 | } | |
723 | ||
cc4d7d81 ZJS |
724 | static void test_path_startswith_strv(void) { |
725 | log_info("/* %s */", __func__); | |
726 | ||
727 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), "")); | |
728 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar")); | |
729 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar")); | |
730 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar")); | |
731 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
732 | ||
733 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
734 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2")); | |
735 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2")); | |
736 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2")); | |
737 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
738 | ||
739 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
740 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL)); | |
741 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL)); | |
742 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar")); | |
743 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
744 | } | |
745 | ||
7f076504 | 746 | int main(int argc, char **argv) { |
6d7c4033 | 747 | test_setup_logging(LOG_DEBUG); |
5a46d55f | 748 | |
3602ca6f | 749 | test_print_paths(); |
76877b46 | 750 | test_path(); |
84e72b5e | 751 | test_path_equal_root(); |
92673045 | 752 | test_find_executable_full(); |
f7bc0c32 | 753 | test_find_executable(argv[0]); |
5ca9139a | 754 | test_find_executable_exec(); |
fecffe5d | 755 | test_prefixes(); |
0c6ea3a4 | 756 | test_path_join(); |
eb66db55 | 757 | test_fsck_exists(); |
6b56a651 | 758 | test_make_relative(); |
3e8a78c8 | 759 | test_strv_resolve(); |
5895b62f | 760 | test_path_startswith(); |
1d13f648 | 761 | test_prefix_root(); |
63292663 | 762 | test_file_in_same_dir(); |
b12d25a8 | 763 | test_last_path_component(); |
a60c8eee | 764 | test_path_extract_filename(); |
63292663 | 765 | test_filename_is_valid(); |
b05b9cde | 766 | test_hidden_or_backup_file(); |
a119ec7c | 767 | test_skip_dev_prefix(); |
57ea45e1 | 768 | test_empty_or_root(); |
d898ed65 | 769 | test_path_startswith_set(); |
cc4d7d81 | 770 | test_path_startswith_strv(); |
5895b62f | 771 | |
5a46d55f ZJS |
772 | test_systemd_installation_has_version(argv[1]); /* NULL is OK */ |
773 | ||
76877b46 ZJS |
774 | return 0; |
775 | } |