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