]>
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 | ||
85eca92e | 167 | static void test_find_binary(const char *self) { |
c9d954b2 ZJS |
168 | char *p; |
169 | ||
771fded3 ZJS |
170 | log_info("/* %s */", __func__); |
171 | ||
85eca92e | 172 | assert_se(find_binary("/bin/sh", &p) == 0); |
c9d954b2 | 173 | puts(p); |
85eca92e | 174 | assert_se(path_equal(p, "/bin/sh")); |
c9d954b2 ZJS |
175 | free(p); |
176 | ||
85eca92e | 177 | assert_se(find_binary(self, &p) == 0); |
c9d954b2 | 178 | puts(p); |
6d1e2ddd MG |
179 | /* libtool might prefix the binary name with "lt-" */ |
180 | assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util")); | |
8d95631e | 181 | assert_se(path_is_absolute(p)); |
c9d954b2 ZJS |
182 | free(p); |
183 | ||
85eca92e | 184 | assert_se(find_binary("sh", &p) == 0); |
c9d954b2 | 185 | puts(p); |
8d95631e FB |
186 | assert_se(endswith(p, "/sh")); |
187 | assert_se(path_is_absolute(p)); | |
c9d954b2 ZJS |
188 | free(p); |
189 | ||
85eca92e LP |
190 | assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT); |
191 | assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT); | |
c9d954b2 ZJS |
192 | } |
193 | ||
fecffe5d | 194 | static void test_prefixes(void) { |
b82f71c7 LP |
195 | static const char* const values[] = { |
196 | "/a/b/c/d", | |
197 | "/a/b/c", | |
198 | "/a/b", | |
199 | "/a", | |
200 | "", | |
201 | NULL | |
202 | }; | |
e203f7c3 | 203 | unsigned i; |
fecffe5d | 204 | char s[PATH_MAX]; |
e203f7c3 | 205 | bool b; |
fecffe5d | 206 | |
771fded3 ZJS |
207 | log_info("/* %s */", __func__); |
208 | ||
e203f7c3 LP |
209 | i = 0; |
210 | PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { | |
fecffe5d LP |
211 | log_error("---%s---", s); |
212 | assert_se(streq(s, values[i++])); | |
213 | } | |
e203f7c3 | 214 | assert_se(values[i] == NULL); |
fecffe5d | 215 | |
e203f7c3 LP |
216 | i = 1; |
217 | PATH_FOREACH_PREFIX(s, "/a/b/c/d") { | |
218 | log_error("---%s---", s); | |
219 | assert_se(streq(s, values[i++])); | |
220 | } | |
fecffe5d LP |
221 | assert_se(values[i] == NULL); |
222 | ||
223 | i = 0; | |
e203f7c3 | 224 | PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") |
fecffe5d | 225 | assert_se(streq(s, values[i++])); |
e203f7c3 | 226 | assert_se(values[i] == NULL); |
fecffe5d | 227 | |
e203f7c3 LP |
228 | i = 1; |
229 | PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") | |
230 | assert_se(streq(s, values[i++])); | |
fecffe5d LP |
231 | assert_se(values[i] == NULL); |
232 | ||
233 | PATH_FOREACH_PREFIX(s, "////") | |
e203f7c3 LP |
234 | assert_not_reached("Wut?"); |
235 | ||
236 | b = false; | |
237 | PATH_FOREACH_PREFIX_MORE(s, "////") { | |
238 | assert_se(!b); | |
fecffe5d | 239 | assert_se(streq(s, "")); |
e203f7c3 LP |
240 | b = true; |
241 | } | |
242 | assert_se(b); | |
fecffe5d LP |
243 | |
244 | PATH_FOREACH_PREFIX(s, "") | |
245 | assert_not_reached("wut?"); | |
246 | ||
e203f7c3 LP |
247 | b = false; |
248 | PATH_FOREACH_PREFIX_MORE(s, "") { | |
8d95631e FB |
249 | assert_se(!b); |
250 | assert_se(streq(s, "")); | |
e203f7c3 LP |
251 | b = true; |
252 | } | |
fecffe5d LP |
253 | } |
254 | ||
0c6ea3a4 | 255 | static void test_path_join(void) { |
771fded3 | 256 | log_info("/* %s */", __func__); |
59ae3a95 | 257 | |
62a85ee0 | 258 | #define test_join(expected, ...) { \ |
59ae3a95 | 259 | _cleanup_free_ char *z = NULL; \ |
62a85ee0 ZJS |
260 | z = path_join(__VA_ARGS__); \ |
261 | log_debug("got \"%s\", expected \"%s\"", z, expected); \ | |
59ae3a95 TA |
262 | assert_se(streq(z, expected)); \ |
263 | } | |
264 | ||
62a85ee0 ZJS |
265 | test_join("/root/a/b/c", "/root", "/a/b", "/c"); |
266 | test_join("/root/a/b/c", "/root", "a/b", "c"); | |
267 | test_join("/root/a/b/c", "/root", "/a/b", "c"); | |
268 | test_join("/root/c", "/root", "/", "c"); | |
269 | test_join("/root/", "/root", "/", NULL); | |
270 | ||
271 | test_join("/a/b/c", "", "/a/b", "/c"); | |
272 | test_join("a/b/c", "", "a/b", "c"); | |
273 | test_join("/a/b/c", "", "/a/b", "c"); | |
274 | test_join("/c", "", "/", "c"); | |
275 | test_join("/", "", "/", NULL); | |
276 | ||
652ef298 ZJS |
277 | test_join("/a/b/c", NULL, "/a/b", "/c"); |
278 | test_join("a/b/c", NULL, "a/b", "c"); | |
279 | test_join("/a/b/c", NULL, "/a/b", "c"); | |
280 | test_join("/c", NULL, "/", "c"); | |
281 | test_join("/", NULL, "/", NULL); | |
282 | ||
62a85ee0 | 283 | test_join("", "", NULL); |
652ef298 ZJS |
284 | test_join("", NULL, ""); |
285 | test_join("", NULL, NULL); | |
62a85ee0 ZJS |
286 | |
287 | test_join("foo/bar", "foo", "bar"); | |
288 | test_join("foo/bar", "", "foo", "bar"); | |
652ef298 | 289 | test_join("foo/bar", NULL, "foo", NULL, "bar"); |
62a85ee0 ZJS |
290 | test_join("foo/bar", "", "foo", "", "bar", ""); |
291 | test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", ""); | |
292 | ||
293 | test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", ""); | |
294 | test_join("/foo/bar/", "/", "foo", "/", "bar", "/"); | |
295 | test_join("foo/bar/baz", "foo", "bar", "baz"); | |
296 | test_join("foo/bar/baz", "foo/", "bar", "/baz"); | |
297 | test_join("foo//bar//baz", "foo/", "/bar/", "/baz"); | |
298 | test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//"); | |
0c6ea3a4 ZJS |
299 | } |
300 | ||
eb66db55 | 301 | static void test_fsck_exists(void) { |
771fded3 ZJS |
302 | log_info("/* %s */", __func__); |
303 | ||
eb66db55 MG |
304 | /* Ensure we use a sane default for PATH. */ |
305 | unsetenv("PATH"); | |
306 | ||
307 | /* fsck.minix is provided by util-linux and will probably exist. */ | |
85eca92e | 308 | assert_se(fsck_exists("minix") == 1); |
eb66db55 | 309 | |
85eca92e LP |
310 | assert_se(fsck_exists("AbCdE") == 0); |
311 | assert_se(fsck_exists("/../bin/") == 0); | |
eb66db55 MG |
312 | } |
313 | ||
6b56a651 TK |
314 | static void test_make_relative(void) { |
315 | char *result; | |
316 | ||
771fded3 ZJS |
317 | log_info("/* %s */", __func__); |
318 | ||
6b56a651 TK |
319 | assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0); |
320 | assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0); | |
2a5beb66 | 321 | assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0); |
6b56a651 | 322 | |
59ae3a95 TA |
323 | #define test(from_dir, to_path, expected) { \ |
324 | _cleanup_free_ char *z = NULL; \ | |
325 | path_make_relative(from_dir, to_path, &z); \ | |
326 | assert_se(streq(z, expected)); \ | |
6b56a651 TK |
327 | } |
328 | ||
329 | test("/", "/", "."); | |
330 | test("/", "/some/path", "some/path"); | |
331 | test("/some/path", "/some/path", "."); | |
332 | test("/some/path", "/some/path/in/subdir", "in/subdir"); | |
333 | test("/some/path", "/", "../.."); | |
334 | test("/some/path", "/some/other/path", "../other/path"); | |
2a5beb66 | 335 | test("/some/path/./dot", "/some/further/path", "../../further/path"); |
48054262 | 336 | test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); |
6b56a651 TK |
337 | } |
338 | ||
3e8a78c8 MM |
339 | static void test_strv_resolve(void) { |
340 | char tmp_dir[] = "/tmp/test-path-util-XXXXXX"; | |
341 | _cleanup_strv_free_ char **search_dirs = NULL; | |
342 | _cleanup_strv_free_ char **absolute_dirs = NULL; | |
343 | char **d; | |
344 | ||
345 | assert_se(mkdtemp(tmp_dir) != NULL); | |
346 | ||
bea1a013 | 347 | search_dirs = strv_new("/dir1", "/dir2", "/dir3"); |
3e8a78c8 MM |
348 | assert_se(search_dirs); |
349 | STRV_FOREACH(d, search_dirs) { | |
b910cc72 | 350 | char *p = path_join(tmp_dir, *d); |
3e8a78c8 MM |
351 | assert_se(p); |
352 | assert_se(strv_push(&absolute_dirs, p) == 0); | |
353 | } | |
354 | ||
355 | assert_se(mkdir(absolute_dirs[0], 0700) == 0); | |
356 | assert_se(mkdir(absolute_dirs[1], 0700) == 0); | |
357 | assert_se(symlink("dir2", absolute_dirs[2]) == 0); | |
358 | ||
359 | path_strv_resolve(search_dirs, tmp_dir); | |
360 | assert_se(streq(search_dirs[0], "/dir1")); | |
361 | assert_se(streq(search_dirs[1], "/dir2")); | |
362 | assert_se(streq(search_dirs[2], "/dir2")); | |
363 | ||
c6878637 | 364 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
3e8a78c8 MM |
365 | } |
366 | ||
5895b62f | 367 | static void test_path_startswith(void) { |
0470289b ZJS |
368 | const char *p; |
369 | ||
771fded3 ZJS |
370 | log_info("/* %s */", __func__); |
371 | ||
0470289b ZJS |
372 | p = path_startswith("/foo/bar/barfoo/", "/foo"); |
373 | assert_se(streq_ptr(p, "bar/barfoo/")); | |
374 | ||
375 | p = path_startswith("/foo/bar/barfoo/", "/foo/"); | |
376 | assert_se(streq_ptr(p, "bar/barfoo/")); | |
377 | ||
378 | p = path_startswith("/foo/bar/barfoo/", "/"); | |
379 | assert_se(streq_ptr(p, "foo/bar/barfoo/")); | |
380 | ||
381 | p = path_startswith("/foo/bar/barfoo/", "////"); | |
382 | assert_se(streq_ptr(p, "foo/bar/barfoo/")); | |
383 | ||
384 | p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///"); | |
385 | assert_se(streq_ptr(p, "")); | |
386 | ||
387 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////"); | |
388 | assert_se(streq_ptr(p, "")); | |
389 | ||
390 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/"); | |
391 | assert_se(streq_ptr(p, "")); | |
392 | ||
393 | p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/"); | |
394 | assert_se(streq_ptr(p, "")); | |
395 | ||
396 | p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/"); | |
397 | assert_se(streq_ptr(p, "")); | |
398 | ||
399 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo"); | |
400 | assert_se(streq_ptr(p, "")); | |
5895b62f RC |
401 | |
402 | assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/")); | |
403 | assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa")); | |
404 | assert_se(!path_startswith("/foo/bar/barfoo/", "")); | |
405 | assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo")); | |
406 | assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/")); | |
407 | } | |
408 | ||
1d13f648 LP |
409 | static void test_prefix_root_one(const char *r, const char *p, const char *expected) { |
410 | _cleanup_free_ char *s = NULL; | |
411 | const char *t; | |
412 | ||
c6134d3e LP |
413 | assert_se(s = path_join(r, p)); |
414 | assert_se(path_equal_ptr(s, expected)); | |
1d13f648 LP |
415 | |
416 | t = prefix_roota(r, p); | |
417 | assert_se(t); | |
c6134d3e | 418 | assert_se(path_equal_ptr(t, expected)); |
1d13f648 LP |
419 | } |
420 | ||
421 | static void test_prefix_root(void) { | |
771fded3 ZJS |
422 | log_info("/* %s */", __func__); |
423 | ||
1d13f648 LP |
424 | test_prefix_root_one("/", "/foo", "/foo"); |
425 | test_prefix_root_one(NULL, "/foo", "/foo"); | |
426 | test_prefix_root_one("", "/foo", "/foo"); | |
427 | test_prefix_root_one("///", "/foo", "/foo"); | |
428 | test_prefix_root_one("/", "////foo", "/foo"); | |
429 | test_prefix_root_one(NULL, "////foo", "/foo"); | |
f9421dd8 YW |
430 | test_prefix_root_one("/", "foo", "/foo"); |
431 | test_prefix_root_one("", "foo", "foo"); | |
432 | test_prefix_root_one(NULL, "foo", "foo"); | |
1d13f648 LP |
433 | |
434 | test_prefix_root_one("/foo", "/bar", "/foo/bar"); | |
435 | test_prefix_root_one("/foo", "bar", "/foo/bar"); | |
436 | test_prefix_root_one("foo", "bar", "foo/bar"); | |
437 | test_prefix_root_one("/foo/", "/bar", "/foo/bar"); | |
438 | test_prefix_root_one("/foo/", "//bar", "/foo/bar"); | |
439 | test_prefix_root_one("/foo///", "//bar", "/foo/bar"); | |
440 | } | |
441 | ||
63292663 RC |
442 | static void test_file_in_same_dir(void) { |
443 | char *t; | |
444 | ||
771fded3 ZJS |
445 | log_info("/* %s */", __func__); |
446 | ||
63292663 RC |
447 | t = file_in_same_dir("/", "a"); |
448 | assert_se(streq(t, "/a")); | |
449 | free(t); | |
450 | ||
451 | t = file_in_same_dir("/", "/a"); | |
452 | assert_se(streq(t, "/a")); | |
453 | free(t); | |
454 | ||
455 | t = file_in_same_dir("", "a"); | |
456 | assert_se(streq(t, "a")); | |
457 | free(t); | |
458 | ||
459 | t = file_in_same_dir("a/", "a"); | |
460 | assert_se(streq(t, "a/a")); | |
461 | free(t); | |
462 | ||
463 | t = file_in_same_dir("bar/foo", "bar"); | |
464 | assert_se(streq(t, "bar/bar")); | |
465 | free(t); | |
466 | } | |
467 | ||
b12d25a8 | 468 | static void test_last_path_component(void) { |
77e0a1b5 | 469 | assert_se(last_path_component(NULL) == NULL); |
b12d25a8 ZJS |
470 | assert_se(streq(last_path_component("a/b/c"), "c")); |
471 | assert_se(streq(last_path_component("a/b/c/"), "c/")); | |
472 | assert_se(streq(last_path_component("/"), "/")); | |
473 | assert_se(streq(last_path_component("//"), "/")); | |
474 | assert_se(streq(last_path_component("///"), "/")); | |
475 | assert_se(streq(last_path_component("."), ".")); | |
476 | assert_se(streq(last_path_component("./."), ".")); | |
477 | assert_se(streq(last_path_component("././"), "./")); | |
478 | assert_se(streq(last_path_component("././/"), ".//")); | |
479 | assert_se(streq(last_path_component("/foo/a"), "a")); | |
480 | assert_se(streq(last_path_component("/foo/a/"), "a/")); | |
69f9ccf1 | 481 | assert_se(streq(last_path_component(""), "")); |
8460289f LP |
482 | assert_se(streq(last_path_component("a"), "a")); |
483 | assert_se(streq(last_path_component("a/"), "a/")); | |
484 | assert_se(streq(last_path_component("/a"), "a")); | |
485 | assert_se(streq(last_path_component("/a/"), "a/")); | |
b12d25a8 ZJS |
486 | } |
487 | ||
a60c8eee LP |
488 | static void test_path_extract_filename_one(const char *input, const char *output, int ret) { |
489 | _cleanup_free_ char *k = NULL; | |
490 | int r; | |
491 | ||
492 | r = path_extract_filename(input, &k); | |
4bbccb02 | 493 | log_info("%s → %s/%s [expected: %s/%s]", strnull(input), strnull(k), strerror_safe(r), strnull(output), strerror_safe(ret)); |
a60c8eee LP |
494 | assert_se(streq_ptr(k, output)); |
495 | assert_se(r == ret); | |
496 | } | |
497 | ||
498 | static void test_path_extract_filename(void) { | |
771fded3 ZJS |
499 | log_info("/* %s */", __func__); |
500 | ||
a60c8eee LP |
501 | test_path_extract_filename_one(NULL, NULL, -EINVAL); |
502 | test_path_extract_filename_one("a/b/c", "c", 0); | |
503 | test_path_extract_filename_one("a/b/c/", "c", 0); | |
504 | test_path_extract_filename_one("/", NULL, -EINVAL); | |
505 | test_path_extract_filename_one("//", NULL, -EINVAL); | |
506 | test_path_extract_filename_one("///", NULL, -EINVAL); | |
507 | test_path_extract_filename_one(".", NULL, -EINVAL); | |
508 | test_path_extract_filename_one("./.", NULL, -EINVAL); | |
509 | test_path_extract_filename_one("././", NULL, -EINVAL); | |
510 | test_path_extract_filename_one("././/", NULL, -EINVAL); | |
511 | test_path_extract_filename_one("/foo/a", "a", 0); | |
512 | test_path_extract_filename_one("/foo/a/", "a", 0); | |
513 | test_path_extract_filename_one("", NULL, -EINVAL); | |
514 | test_path_extract_filename_one("a", "a", 0); | |
515 | test_path_extract_filename_one("a/", "a", 0); | |
516 | test_path_extract_filename_one("/a", "a", 0); | |
517 | test_path_extract_filename_one("/a/", "a", 0); | |
518 | test_path_extract_filename_one("/////////////a/////////////", "a", 0); | |
519 | test_path_extract_filename_one("xx/.", NULL, -EINVAL); | |
520 | test_path_extract_filename_one("xx/..", NULL, -EINVAL); | |
521 | test_path_extract_filename_one("..", NULL, -EINVAL); | |
522 | test_path_extract_filename_one("/..", NULL, -EINVAL); | |
523 | test_path_extract_filename_one("../", NULL, -EINVAL); | |
524 | test_path_extract_filename_one(".", NULL, -EINVAL); | |
525 | test_path_extract_filename_one("/.", NULL, -EINVAL); | |
526 | test_path_extract_filename_one("./", NULL, -EINVAL); | |
527 | } | |
528 | ||
63292663 RC |
529 | static void test_filename_is_valid(void) { |
530 | char foo[FILENAME_MAX+2]; | |
531 | int i; | |
532 | ||
771fded3 ZJS |
533 | log_info("/* %s */", __func__); |
534 | ||
63292663 RC |
535 | assert_se(!filename_is_valid("")); |
536 | assert_se(!filename_is_valid("/bar/foo")); | |
537 | assert_se(!filename_is_valid("/")); | |
538 | assert_se(!filename_is_valid(".")); | |
539 | assert_se(!filename_is_valid("..")); | |
540 | ||
541 | for (i=0; i<FILENAME_MAX+1; i++) | |
542 | foo[i] = 'a'; | |
543 | foo[FILENAME_MAX+1] = '\0'; | |
544 | ||
545 | assert_se(!filename_is_valid(foo)); | |
546 | ||
547 | assert_se(filename_is_valid("foo_bar-333")); | |
548 | assert_se(filename_is_valid("o.o")); | |
549 | } | |
550 | ||
b05b9cde | 551 | static void test_hidden_or_backup_file(void) { |
771fded3 ZJS |
552 | log_info("/* %s */", __func__); |
553 | ||
b05b9cde ZJS |
554 | assert_se(hidden_or_backup_file(".hidden")); |
555 | assert_se(hidden_or_backup_file("..hidden")); | |
556 | assert_se(!hidden_or_backup_file("hidden.")); | |
557 | ||
558 | assert_se(hidden_or_backup_file("backup~")); | |
559 | assert_se(hidden_or_backup_file(".backup~")); | |
560 | ||
561 | assert_se(hidden_or_backup_file("lost+found")); | |
562 | assert_se(hidden_or_backup_file("aquota.user")); | |
563 | assert_se(hidden_or_backup_file("aquota.group")); | |
564 | ||
565 | assert_se(hidden_or_backup_file("test.rpmnew")); | |
566 | assert_se(hidden_or_backup_file("test.dpkg-old")); | |
567 | assert_se(hidden_or_backup_file("test.dpkg-remove")); | |
568 | assert_se(hidden_or_backup_file("test.swp")); | |
569 | ||
570 | assert_se(!hidden_or_backup_file("test.rpmnew.")); | |
571 | assert_se(!hidden_or_backup_file("test.dpkg-old.foo")); | |
572 | } | |
573 | ||
5a46d55f ZJS |
574 | static void test_systemd_installation_has_version(const char *path) { |
575 | int r; | |
a67c318d | 576 | const unsigned versions[] = {0, 231, PROJECT_VERSION, 999}; |
5a46d55f ZJS |
577 | unsigned i; |
578 | ||
771fded3 ZJS |
579 | log_info("/* %s */", __func__); |
580 | ||
5a46d55f ZJS |
581 | for (i = 0; i < ELEMENTSOF(versions); i++) { |
582 | r = systemd_installation_has_version(path, versions[i]); | |
583 | assert_se(r >= 0); | |
584 | log_info("%s has systemd >= %u: %s", | |
585 | path ?: "Current installation", versions[i], yes_no(r)); | |
586 | } | |
587 | } | |
588 | ||
a119ec7c | 589 | static void test_skip_dev_prefix(void) { |
771fded3 | 590 | log_info("/* %s */", __func__); |
a119ec7c LP |
591 | |
592 | assert_se(streq(skip_dev_prefix("/"), "/")); | |
593 | assert_se(streq(skip_dev_prefix("/dev"), "")); | |
594 | assert_se(streq(skip_dev_prefix("/dev/"), "")); | |
595 | assert_se(streq(skip_dev_prefix("/dev/foo"), "foo")); | |
596 | assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar")); | |
597 | assert_se(streq(skip_dev_prefix("//dev"), "")); | |
598 | assert_se(streq(skip_dev_prefix("//dev//"), "")); | |
599 | assert_se(streq(skip_dev_prefix("/dev///foo"), "foo")); | |
600 | assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar")); | |
601 | assert_se(streq(skip_dev_prefix("//foo"), "//foo")); | |
602 | assert_se(streq(skip_dev_prefix("foo"), "foo")); | |
603 | } | |
604 | ||
57ea45e1 | 605 | static void test_empty_or_root(void) { |
771fded3 ZJS |
606 | log_info("/* %s */", __func__); |
607 | ||
57ea45e1 LP |
608 | assert_se(empty_or_root(NULL)); |
609 | assert_se(empty_or_root("")); | |
610 | assert_se(empty_or_root("/")); | |
611 | assert_se(empty_or_root("//")); | |
612 | assert_se(empty_or_root("///")); | |
613 | assert_se(empty_or_root("/////////////////")); | |
614 | assert_se(!empty_or_root("xxx")); | |
615 | assert_se(!empty_or_root("/xxx")); | |
616 | assert_se(!empty_or_root("/xxx/")); | |
617 | assert_se(!empty_or_root("//yy//")); | |
618 | } | |
619 | ||
d898ed65 | 620 | static void test_path_startswith_set(void) { |
771fded3 | 621 | log_info("/* %s */", __func__); |
d898ed65 LP |
622 | |
623 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "")); | |
624 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar")); | |
625 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar")); | |
626 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar")); | |
627 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL)); | |
628 | ||
629 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
630 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2")); | |
631 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2")); | |
632 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2")); | |
633 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL)); | |
634 | ||
635 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
636 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL)); | |
637 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL)); | |
638 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar")); | |
639 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL)); | |
640 | } | |
641 | ||
cc4d7d81 ZJS |
642 | static void test_path_startswith_strv(void) { |
643 | log_info("/* %s */", __func__); | |
644 | ||
645 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), "")); | |
646 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar")); | |
647 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar")); | |
648 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar")); | |
649 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
650 | ||
651 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
652 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2")); | |
653 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2")); | |
654 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2")); | |
655 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
656 | ||
657 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
658 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL)); | |
659 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL)); | |
660 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar")); | |
661 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
662 | } | |
663 | ||
7f076504 | 664 | int main(int argc, char **argv) { |
6d7c4033 | 665 | test_setup_logging(LOG_DEBUG); |
5a46d55f | 666 | |
3602ca6f | 667 | test_print_paths(); |
76877b46 | 668 | test_path(); |
84e72b5e | 669 | test_path_equal_root(); |
85eca92e | 670 | test_find_binary(argv[0]); |
fecffe5d | 671 | test_prefixes(); |
0c6ea3a4 | 672 | test_path_join(); |
eb66db55 | 673 | test_fsck_exists(); |
6b56a651 | 674 | test_make_relative(); |
3e8a78c8 | 675 | test_strv_resolve(); |
5895b62f | 676 | test_path_startswith(); |
1d13f648 | 677 | test_prefix_root(); |
63292663 | 678 | test_file_in_same_dir(); |
b12d25a8 | 679 | test_last_path_component(); |
a60c8eee | 680 | test_path_extract_filename(); |
63292663 | 681 | test_filename_is_valid(); |
b05b9cde | 682 | test_hidden_or_backup_file(); |
a119ec7c | 683 | test_skip_dev_prefix(); |
57ea45e1 | 684 | test_empty_or_root(); |
d898ed65 | 685 | test_path_startswith_set(); |
cc4d7d81 | 686 | test_path_startswith_strv(); |
5895b62f | 687 | |
5a46d55f ZJS |
688 | test_systemd_installation_has_version(argv[1]); /* NULL is OK */ |
689 | ||
76877b46 ZJS |
690 | return 0; |
691 | } |