]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-util.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-path-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "fd-util.h"
8 #include "macro.h"
9 #include "mount-util.h"
10 #include "path-util.h"
11 #include "rm-rf.h"
12 #include "stat-util.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "tests.h"
16 #include "util.h"
17
18 #define test_path_compare(a, b, result) { \
19 assert_se(path_compare(a, b) == result); \
20 assert_se(path_compare(b, a) == -result); \
21 assert_se(path_equal(a, b) == !result); \
22 assert_se(path_equal(b, a) == !result); \
23 }
24
25 static void test_path_simplify(const char *in, const char *out, const char *out_dot) {
26 char *p;
27
28 p = strdupa(in);
29 assert_se(streq(path_simplify(p, false), out));
30
31 p = strdupa(in);
32 assert_se(streq(path_simplify(p, true), out_dot));
33 }
34
35 static void test_path(void) {
36 _cleanup_close_ int fd = -1;
37
38 test_path_compare("/goo", "/goo", 0);
39 test_path_compare("/goo", "/goo", 0);
40 test_path_compare("//goo", "/goo", 0);
41 test_path_compare("//goo/////", "/goo", 0);
42 test_path_compare("goo/////", "goo", 0);
43
44 test_path_compare("/goo/boo", "/goo//boo", 0);
45 test_path_compare("//goo/boo", "/goo/boo//", 0);
46
47 test_path_compare("/", "///", 0);
48
49 test_path_compare("/x", "x/", 1);
50 test_path_compare("x/", "/", -1);
51
52 test_path_compare("/x/./y", "x/y", 1);
53 test_path_compare("x/.y", "x/y", -1);
54
55 test_path_compare("foo", "/foo", -1);
56 test_path_compare("/foo", "/foo/bar", -1);
57 test_path_compare("/foo/aaa", "/foo/b", -1);
58 test_path_compare("/foo/aaa", "/foo/b/a", -1);
59 test_path_compare("/foo/a", "/foo/aaa", -1);
60 test_path_compare("/foo/a/b", "/foo/aaa", -1);
61
62 assert_se(path_is_absolute("/"));
63 assert_se(!path_is_absolute("./"));
64
65 assert_se(is_path("/dir"));
66 assert_se(is_path("a/b"));
67 assert_se(!is_path("."));
68
69 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
70 assert_se(streq(basename("/aa///.file"), ".file"));
71 assert_se(streq(basename("/aa///file..."), "file..."));
72 assert_se(streq(basename("file.../"), ""));
73
74 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
75 assert_se(fd >= 0);
76 assert_se(fd_is_mount_point(fd, "/", 0) > 0);
77
78 test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
79 test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
80 test_path_simplify("///", "/", "/");
81 test_path_simplify("///.//", "/.", "/");
82 test_path_simplify("///.//.///", "/./.", "/");
83 test_path_simplify("////.././///../.", "/.././../.", "/../..");
84 test_path_simplify(".", ".", ".");
85 test_path_simplify("./", ".", ".");
86 test_path_simplify(".///.//./.", "./././.", ".");
87 test_path_simplify(".///.//././/", "./././.", ".");
88 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
89 "/./aaa/././.bbb/../c./d.dd/..eeee/.",
90 "/aaa/.bbb/../c./d.dd/..eeee");
91 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
92 "/./aaa/././.bbb/../c./d.dd/..eeee/..",
93 "/aaa/.bbb/../c./d.dd/..eeee/..");
94 test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
95 "././aaa/././.bbb/../c./d.dd/..eeee/..",
96 "aaa/.bbb/../c./d.dd/..eeee/..");
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
101 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
102 assert_se(PATH_IN_SET("/bin", "/bin"));
103 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
104 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
105 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
106
107 assert_se(path_equal_ptr(NULL, NULL));
108 assert_se(path_equal_ptr("/a", "/a"));
109 assert_se(!path_equal_ptr("/a", "/b"));
110 assert_se(!path_equal_ptr("/a", NULL));
111 assert_se(!path_equal_ptr(NULL, "/a"));
112 }
113
114 static void test_path_equal_root(void) {
115 /* Nail down the details of how path_equal("/", ...) works. */
116
117 assert_se(path_equal("/", "/"));
118 assert_se(path_equal("/", "//"));
119
120 assert_se(!path_equal("/", "/./"));
121 assert_se(!path_equal("/", "/../"));
122
123 assert_se(!path_equal("/", "/.../"));
124
125 /* Make sure that files_same works as expected. */
126
127 assert_se(files_same("/", "/", 0) > 0);
128 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
129 assert_se(files_same("/", "//", 0) > 0);
130 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
131
132 assert_se(files_same("/", "/./", 0) > 0);
133 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
134 assert_se(files_same("/", "/../", 0) > 0);
135 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
136
137 assert_se(files_same("/", "/.../", 0) == -ENOENT);
138 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
139
140 /* The same for path_equal_or_files_same. */
141
142 assert_se(path_equal_or_files_same("/", "/", 0));
143 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
144 assert_se(path_equal_or_files_same("/", "//", 0));
145 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
146
147 assert_se(path_equal_or_files_same("/", "/./", 0));
148 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
149 assert_se(path_equal_or_files_same("/", "/../", 0));
150 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
151
152 assert_se(!path_equal_or_files_same("/", "/.../", 0));
153 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
154 }
155
156 static void test_find_binary(const char *self) {
157 char *p;
158
159 assert_se(find_binary("/bin/sh", &p) == 0);
160 puts(p);
161 assert_se(path_equal(p, "/bin/sh"));
162 free(p);
163
164 assert_se(find_binary(self, &p) == 0);
165 puts(p);
166 /* libtool might prefix the binary name with "lt-" */
167 assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
168 assert_se(path_is_absolute(p));
169 free(p);
170
171 assert_se(find_binary("sh", &p) == 0);
172 puts(p);
173 assert_se(endswith(p, "/sh"));
174 assert_se(path_is_absolute(p));
175 free(p);
176
177 assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
178 assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
179 }
180
181 static void test_prefixes(void) {
182 static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
183 unsigned i;
184 char s[PATH_MAX];
185 bool b;
186
187 i = 0;
188 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
189 log_error("---%s---", s);
190 assert_se(streq(s, values[i++]));
191 }
192 assert_se(values[i] == NULL);
193
194 i = 1;
195 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
196 log_error("---%s---", s);
197 assert_se(streq(s, values[i++]));
198 }
199 assert_se(values[i] == NULL);
200
201 i = 0;
202 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
203 assert_se(streq(s, values[i++]));
204 assert_se(values[i] == NULL);
205
206 i = 1;
207 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
208 assert_se(streq(s, values[i++]));
209 assert_se(values[i] == NULL);
210
211 PATH_FOREACH_PREFIX(s, "////")
212 assert_not_reached("Wut?");
213
214 b = false;
215 PATH_FOREACH_PREFIX_MORE(s, "////") {
216 assert_se(!b);
217 assert_se(streq(s, ""));
218 b = true;
219 }
220 assert_se(b);
221
222 PATH_FOREACH_PREFIX(s, "")
223 assert_not_reached("wut?");
224
225 b = false;
226 PATH_FOREACH_PREFIX_MORE(s, "") {
227 assert_se(!b);
228 assert_se(streq(s, ""));
229 b = true;
230 }
231 }
232
233 static void test_path_join(void) {
234
235 #define test_join(root, path, rest, expected) { \
236 _cleanup_free_ char *z = NULL; \
237 z = path_join(root, path, rest); \
238 assert_se(streq(z, expected)); \
239 }
240
241 test_join("/root", "/a/b", "/c", "/root/a/b/c");
242 test_join("/root", "a/b", "c", "/root/a/b/c");
243 test_join("/root", "/a/b", "c", "/root/a/b/c");
244 test_join("/root", "/", "c", "/root/c");
245 test_join("/root", "/", NULL, "/root/");
246
247 test_join(NULL, "/a/b", "/c", "/a/b/c");
248 test_join(NULL, "a/b", "c", "a/b/c");
249 test_join(NULL, "/a/b", "c", "/a/b/c");
250 test_join(NULL, "/", "c", "/c");
251 test_join(NULL, "/", NULL, "/");
252 }
253
254 static void test_fsck_exists(void) {
255 /* Ensure we use a sane default for PATH. */
256 unsetenv("PATH");
257
258 /* fsck.minix is provided by util-linux and will probably exist. */
259 assert_se(fsck_exists("minix") == 1);
260
261 assert_se(fsck_exists("AbCdE") == 0);
262 assert_se(fsck_exists("/../bin/") == 0);
263 }
264
265 static void test_make_relative(void) {
266 char *result;
267
268 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
269 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
270 assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
271
272 #define test(from_dir, to_path, expected) { \
273 _cleanup_free_ char *z = NULL; \
274 path_make_relative(from_dir, to_path, &z); \
275 assert_se(streq(z, expected)); \
276 }
277
278 test("/", "/", ".");
279 test("/", "/some/path", "some/path");
280 test("/some/path", "/some/path", ".");
281 test("/some/path", "/some/path/in/subdir", "in/subdir");
282 test("/some/path", "/", "../..");
283 test("/some/path", "/some/other/path", "../other/path");
284 test("/some/path/./dot", "/some/further/path", "../../further/path");
285 test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
286 }
287
288 static void test_strv_resolve(void) {
289 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
290 _cleanup_strv_free_ char **search_dirs = NULL;
291 _cleanup_strv_free_ char **absolute_dirs = NULL;
292 char **d;
293
294 assert_se(mkdtemp(tmp_dir) != NULL);
295
296 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
297 assert_se(search_dirs);
298 STRV_FOREACH(d, search_dirs) {
299 char *p = strappend(tmp_dir, *d);
300 assert_se(p);
301 assert_se(strv_push(&absolute_dirs, p) == 0);
302 }
303
304 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
305 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
306 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
307
308 path_strv_resolve(search_dirs, tmp_dir);
309 assert_se(streq(search_dirs[0], "/dir1"));
310 assert_se(streq(search_dirs[1], "/dir2"));
311 assert_se(streq(search_dirs[2], "/dir2"));
312
313 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
314 }
315
316 static void test_path_startswith(void) {
317 const char *p;
318
319 p = path_startswith("/foo/bar/barfoo/", "/foo");
320 assert_se(streq_ptr(p, "bar/barfoo/"));
321
322 p = path_startswith("/foo/bar/barfoo/", "/foo/");
323 assert_se(streq_ptr(p, "bar/barfoo/"));
324
325 p = path_startswith("/foo/bar/barfoo/", "/");
326 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
327
328 p = path_startswith("/foo/bar/barfoo/", "////");
329 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
330
331 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
332 assert_se(streq_ptr(p, ""));
333
334 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
335 assert_se(streq_ptr(p, ""));
336
337 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
338 assert_se(streq_ptr(p, ""));
339
340 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
341 assert_se(streq_ptr(p, ""));
342
343 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
344 assert_se(streq_ptr(p, ""));
345
346 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
347 assert_se(streq_ptr(p, ""));
348
349 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
350 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
351 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
352 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
353 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
354 }
355
356 static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
357 _cleanup_free_ char *s = NULL;
358 const char *t;
359
360 assert_se(s = prefix_root(r, p));
361 assert_se(streq_ptr(s, expected));
362
363 t = prefix_roota(r, p);
364 assert_se(t);
365 assert_se(streq_ptr(t, expected));
366 }
367
368 static void test_prefix_root(void) {
369 test_prefix_root_one("/", "/foo", "/foo");
370 test_prefix_root_one(NULL, "/foo", "/foo");
371 test_prefix_root_one("", "/foo", "/foo");
372 test_prefix_root_one("///", "/foo", "/foo");
373 test_prefix_root_one("/", "////foo", "/foo");
374 test_prefix_root_one(NULL, "////foo", "/foo");
375
376 test_prefix_root_one("/foo", "/bar", "/foo/bar");
377 test_prefix_root_one("/foo", "bar", "/foo/bar");
378 test_prefix_root_one("foo", "bar", "foo/bar");
379 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
380 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
381 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
382 }
383
384 static void test_file_in_same_dir(void) {
385 char *t;
386
387 t = file_in_same_dir("/", "a");
388 assert_se(streq(t, "/a"));
389 free(t);
390
391 t = file_in_same_dir("/", "/a");
392 assert_se(streq(t, "/a"));
393 free(t);
394
395 t = file_in_same_dir("", "a");
396 assert_se(streq(t, "a"));
397 free(t);
398
399 t = file_in_same_dir("a/", "a");
400 assert_se(streq(t, "a/a"));
401 free(t);
402
403 t = file_in_same_dir("bar/foo", "bar");
404 assert_se(streq(t, "bar/bar"));
405 free(t);
406 }
407
408 static void test_last_path_component(void) {
409 assert_se(streq(last_path_component("a/b/c"), "c"));
410 assert_se(streq(last_path_component("a/b/c/"), "c/"));
411 assert_se(streq(last_path_component("/"), "/"));
412 assert_se(streq(last_path_component("//"), "/"));
413 assert_se(streq(last_path_component("///"), "/"));
414 assert_se(streq(last_path_component("."), "."));
415 assert_se(streq(last_path_component("./."), "."));
416 assert_se(streq(last_path_component("././"), "./"));
417 assert_se(streq(last_path_component("././/"), ".//"));
418 assert_se(streq(last_path_component("/foo/a"), "a"));
419 assert_se(streq(last_path_component("/foo/a/"), "a/"));
420 assert_se(streq(last_path_component(""), ""));
421 assert_se(streq(last_path_component("a"), "a"));
422 assert_se(streq(last_path_component("a/"), "a/"));
423 assert_se(streq(last_path_component("/a"), "a"));
424 assert_se(streq(last_path_component("/a/"), "a/"));
425 }
426
427 static void test_filename_is_valid(void) {
428 char foo[FILENAME_MAX+2];
429 int i;
430
431 assert_se(!filename_is_valid(""));
432 assert_se(!filename_is_valid("/bar/foo"));
433 assert_se(!filename_is_valid("/"));
434 assert_se(!filename_is_valid("."));
435 assert_se(!filename_is_valid(".."));
436
437 for (i=0; i<FILENAME_MAX+1; i++)
438 foo[i] = 'a';
439 foo[FILENAME_MAX+1] = '\0';
440
441 assert_se(!filename_is_valid(foo));
442
443 assert_se(filename_is_valid("foo_bar-333"));
444 assert_se(filename_is_valid("o.o"));
445 }
446
447 static void test_hidden_or_backup_file(void) {
448 assert_se(hidden_or_backup_file(".hidden"));
449 assert_se(hidden_or_backup_file("..hidden"));
450 assert_se(!hidden_or_backup_file("hidden."));
451
452 assert_se(hidden_or_backup_file("backup~"));
453 assert_se(hidden_or_backup_file(".backup~"));
454
455 assert_se(hidden_or_backup_file("lost+found"));
456 assert_se(hidden_or_backup_file("aquota.user"));
457 assert_se(hidden_or_backup_file("aquota.group"));
458
459 assert_se(hidden_or_backup_file("test.rpmnew"));
460 assert_se(hidden_or_backup_file("test.dpkg-old"));
461 assert_se(hidden_or_backup_file("test.dpkg-remove"));
462 assert_se(hidden_or_backup_file("test.swp"));
463
464 assert_se(!hidden_or_backup_file("test.rpmnew."));
465 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
466 }
467
468 static void test_systemd_installation_has_version(const char *path) {
469 int r;
470 const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999};
471 unsigned i;
472
473 for (i = 0; i < ELEMENTSOF(versions); i++) {
474 r = systemd_installation_has_version(path, versions[i]);
475 assert_se(r >= 0);
476 log_info("%s has systemd >= %u: %s",
477 path ?: "Current installation", versions[i], yes_no(r));
478 }
479 }
480
481 static void test_skip_dev_prefix(void) {
482
483 assert_se(streq(skip_dev_prefix("/"), "/"));
484 assert_se(streq(skip_dev_prefix("/dev"), ""));
485 assert_se(streq(skip_dev_prefix("/dev/"), ""));
486 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
487 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
488 assert_se(streq(skip_dev_prefix("//dev"), ""));
489 assert_se(streq(skip_dev_prefix("//dev//"), ""));
490 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
491 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
492 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
493 assert_se(streq(skip_dev_prefix("foo"), "foo"));
494 }
495
496 static void test_empty_or_root(void) {
497 assert_se(empty_or_root(NULL));
498 assert_se(empty_or_root(""));
499 assert_se(empty_or_root("/"));
500 assert_se(empty_or_root("//"));
501 assert_se(empty_or_root("///"));
502 assert_se(empty_or_root("/////////////////"));
503 assert_se(!empty_or_root("xxx"));
504 assert_se(!empty_or_root("/xxx"));
505 assert_se(!empty_or_root("/xxx/"));
506 assert_se(!empty_or_root("//yy//"));
507 }
508
509 int main(int argc, char **argv) {
510 test_setup_logging(LOG_DEBUG);
511
512 test_path();
513 test_path_equal_root();
514 test_find_binary(argv[0]);
515 test_prefixes();
516 test_path_join();
517 test_fsck_exists();
518 test_make_relative();
519 test_strv_resolve();
520 test_path_startswith();
521 test_prefix_root();
522 test_file_in_same_dir();
523 test_last_path_component();
524 test_filename_is_valid();
525 test_hidden_or_backup_file();
526 test_skip_dev_prefix();
527 test_empty_or_root();
528
529 test_systemd_installation_has_version(argv[1]); /* NULL is OK */
530
531 return 0;
532 }