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