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