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