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