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