chiark / gitweb /
Drop my copyright headers
[elogind.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 "util.h"
16
17 #define test_path_compare(a, b, result) {                 \
18                 assert_se(path_compare(a, b) == result);  \
19                 assert_se(path_compare(b, a) == -result); \
20                 assert_se(path_equal(a, b) == !result);   \
21                 assert_se(path_equal(b, a) == !result);   \
22         }
23
24 static void test_path_simplify(const char *in, const char *out, const char *out_dot) {
25         char *p;
26
27         p = strdupa(in);
28         assert_se(streq(path_simplify(p, false), out));
29
30         p = strdupa(in);
31         assert_se(streq(path_simplify(p, true), out_dot));
32 }
33
34 static void test_path(void) {
35         _cleanup_close_ int fd = -1;
36
37         test_path_compare("/goo", "/goo", 0);
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
43         test_path_compare("/goo/boo", "/goo//boo", 0);
44         test_path_compare("//goo/boo", "/goo/boo//", 0);
45
46         test_path_compare("/", "///", 0);
47
48         test_path_compare("/x", "x/", 1);
49         test_path_compare("x/", "/", -1);
50
51         test_path_compare("/x/./y", "x/y", 1);
52         test_path_compare("x/.y", "x/y", -1);
53
54         test_path_compare("foo", "/foo", -1);
55         test_path_compare("/foo", "/foo/bar", -1);
56         test_path_compare("/foo/aaa", "/foo/b", -1);
57         test_path_compare("/foo/aaa", "/foo/b/a", -1);
58         test_path_compare("/foo/a", "/foo/aaa", -1);
59         test_path_compare("/foo/a/b", "/foo/aaa", -1);
60
61         assert_se(path_is_absolute("/"));
62         assert_se(!path_is_absolute("./"));
63
64         assert_se(is_path("/dir"));
65         assert_se(is_path("a/b"));
66         assert_se(!is_path("."));
67
68         assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
69         assert_se(streq(basename("/aa///.file"), ".file"));
70         assert_se(streq(basename("/aa///file..."), "file..."));
71         assert_se(streq(basename("file.../"), ""));
72
73         fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
74         assert_se(fd >= 0);
75         assert_se(fd_is_mount_point(fd, "/", 0) > 0);
76
77         test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
78         test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
79         test_path_simplify("///", "/", "/");
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("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
88                            "/./aaa/././.bbb/../c./d.dd/..eeee/.",
89                            "/aaa/.bbb/../c./d.dd/..eeee");
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
100         assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
101         assert_se(PATH_IN_SET("/bin", "/bin"));
102         assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
103         assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
104         assert_se(!PATH_IN_SET("/", "/abc", "/def"));
105
106         assert_se(path_equal_ptr(NULL, NULL));
107         assert_se(path_equal_ptr("/a", "/a"));
108         assert_se(!path_equal_ptr("/a", "/b"));
109         assert_se(!path_equal_ptr("/a", NULL));
110         assert_se(!path_equal_ptr(NULL, "/a"));
111 }
112
113 static void test_path_equal_root(void) {
114         /* Nail down the details of how path_equal("/", ...) works. */
115
116         assert_se(path_equal("/", "/"));
117         assert_se(path_equal("/", "//"));
118
119         assert_se(!path_equal("/", "/./"));
120         assert_se(!path_equal("/", "/../"));
121
122         assert_se(!path_equal("/", "/.../"));
123
124         /* Make sure that files_same works as expected. */
125
126         assert_se(files_same("/", "/", 0) > 0);
127         assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
128         assert_se(files_same("/", "//", 0) > 0);
129         assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
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) == -ENOENT);
137         assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
138
139         /* The same for path_equal_or_files_same. */
140
141         assert_se(path_equal_or_files_same("/", "/", 0));
142         assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
143         assert_se(path_equal_or_files_same("/", "//", 0));
144         assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
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 }
154
155 static void test_find_binary(const char *self) {
156         char *p;
157
158         assert_se(find_binary("/bin/sh", &p) == 0);
159         puts(p);
160         assert_se(path_equal(p, "/bin/sh"));
161         free(p);
162
163         assert_se(find_binary(self, &p) == 0);
164         puts(p);
165         /* libtool might prefix the binary name with "lt-" */
166         assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
167         assert_se(path_is_absolute(p));
168         free(p);
169
170         assert_se(find_binary("sh", &p) == 0);
171         puts(p);
172         assert_se(endswith(p, "/sh"));
173         assert_se(path_is_absolute(p));
174         free(p);
175
176         assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
177         assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
178 }
179
180 static void test_prefixes(void) {
181         static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
182         unsigned i;
183         char s[PATH_MAX];
184         bool b;
185
186         i = 0;
187         PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
188                 log_error("---%s---", s);
189                 assert_se(streq(s, values[i++]));
190         }
191         assert_se(values[i] == NULL);
192
193         i = 1;
194         PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
195                 log_error("---%s---", s);
196                 assert_se(streq(s, values[i++]));
197         }
198         assert_se(values[i] == NULL);
199
200         i = 0;
201         PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
202                 assert_se(streq(s, values[i++]));
203         assert_se(values[i] == NULL);
204
205         i = 1;
206         PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
207                 assert_se(streq(s, values[i++]));
208         assert_se(values[i] == NULL);
209
210         PATH_FOREACH_PREFIX(s, "////")
211                 assert_not_reached("Wut?");
212
213         b = false;
214         PATH_FOREACH_PREFIX_MORE(s, "////") {
215                 assert_se(!b);
216                 assert_se(streq(s, ""));
217                 b = true;
218         }
219         assert_se(b);
220
221         PATH_FOREACH_PREFIX(s, "")
222                 assert_not_reached("wut?");
223
224         b = false;
225         PATH_FOREACH_PREFIX_MORE(s, "") {
226                 assert_se(!b);
227                 assert_se(streq(s, ""));
228                 b = true;
229         }
230 }
231
232 static void test_path_join(void) {
233
234 #define test_join(root, path, rest, expected) {  \
235                 _cleanup_free_ char *z = NULL;   \
236                 z = path_join(root, path, rest); \
237                 assert_se(streq(z, expected));   \
238         }
239
240         test_join("/root", "/a/b", "/c", "/root/a/b/c");
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", "/", "c", "/root/c");
244         test_join("/root", "/", NULL, "/root/");
245
246         test_join(NULL, "/a/b", "/c", "/a/b/c");
247         test_join(NULL, "a/b", "c", "a/b/c");
248         test_join(NULL, "/a/b", "c", "/a/b/c");
249         test_join(NULL, "/", "c", "/c");
250         test_join(NULL, "/", NULL, "/");
251 }
252
253 #if 0 /// UNNEEDED by elogind
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 #endif // 0
288
289 static void test_strv_resolve(void) {
290         char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
291         _cleanup_strv_free_ char **search_dirs = NULL;
292         _cleanup_strv_free_ char **absolute_dirs = NULL;
293         char **d;
294
295         assert_se(mkdtemp(tmp_dir) != NULL);
296
297         search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
298         assert_se(search_dirs);
299         STRV_FOREACH(d, search_dirs) {
300                 char *p = strappend(tmp_dir, *d);
301                 assert_se(p);
302                 assert_se(strv_push(&absolute_dirs, p) == 0);
303         }
304
305         assert_se(mkdir(absolute_dirs[0], 0700) == 0);
306         assert_se(mkdir(absolute_dirs[1], 0700) == 0);
307         assert_se(symlink("dir2", absolute_dirs[2]) == 0);
308
309         path_strv_resolve(search_dirs, tmp_dir);
310         assert_se(streq(search_dirs[0], "/dir1"));
311         assert_se(streq(search_dirs[1], "/dir2"));
312         assert_se(streq(search_dirs[2], "/dir2"));
313
314         assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
315 }
316
317 static void test_path_startswith(void) {
318         const char *p;
319
320         p = path_startswith("/foo/bar/barfoo/", "/foo");
321         assert_se(streq_ptr(p, "bar/barfoo/"));
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/", "/");
327         assert_se(streq_ptr(p, "foo/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/", "/foo//bar/////barfoo///");
333         assert_se(streq_ptr(p, ""));
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         assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
351         assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
352         assert_se(!path_startswith("/foo/bar/barfoo/", ""));
353         assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
354         assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
355 }
356
357 static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
358         _cleanup_free_ char *s = NULL;
359         const char *t;
360
361         assert_se(s = prefix_root(r, p));
362         assert_se(streq_ptr(s, expected));
363
364         t = prefix_roota(r, p);
365         assert_se(t);
366         assert_se(streq_ptr(t, expected));
367 }
368
369 static void test_prefix_root(void) {
370         test_prefix_root_one("/", "/foo", "/foo");
371         test_prefix_root_one(NULL, "/foo", "/foo");
372         test_prefix_root_one("", "/foo", "/foo");
373         test_prefix_root_one("///", "/foo", "/foo");
374         test_prefix_root_one("/", "////foo", "/foo");
375         test_prefix_root_one(NULL, "////foo", "/foo");
376
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         test_prefix_root_one("/foo///", "//bar", "/foo/bar");
383 }
384
385 static void test_file_in_same_dir(void) {
386         char *t;
387
388         t = file_in_same_dir("/", "a");
389         assert_se(streq(t, "/a"));
390         free(t);
391
392         t = file_in_same_dir("/", "/a");
393         assert_se(streq(t, "/a"));
394         free(t);
395
396         t = file_in_same_dir("", "a");
397         assert_se(streq(t, "a"));
398         free(t);
399
400         t = file_in_same_dir("a/", "a");
401         assert_se(streq(t, "a/a"));
402         free(t);
403
404         t = file_in_same_dir("bar/foo", "bar");
405         assert_se(streq(t, "bar/bar"));
406         free(t);
407 }
408
409 static void test_last_path_component(void) {
410         assert_se(streq(last_path_component("a/b/c"), "c"));
411         assert_se(streq(last_path_component("a/b/c/"), "c/"));
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("././/"), ".//"));
419         assert_se(streq(last_path_component("/foo/a"), "a"));
420         assert_se(streq(last_path_component("/foo/a/"), "a/"));
421         assert_se(streq(last_path_component(""), ""));
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         assert_se(streq(last_path_component("/a/"), "a/"));
426 }
427
428 static void test_filename_is_valid(void) {
429         char foo[FILENAME_MAX+2];
430         int i;
431
432         assert_se(!filename_is_valid(""));
433         assert_se(!filename_is_valid("/bar/foo"));
434         assert_se(!filename_is_valid("/"));
435         assert_se(!filename_is_valid("."));
436         assert_se(!filename_is_valid(".."));
437
438         for (i=0; i<FILENAME_MAX+1; i++)
439                 foo[i] = 'a';
440         foo[FILENAME_MAX+1] = '\0';
441
442         assert_se(!filename_is_valid(foo));
443
444         assert_se(filename_is_valid("foo_bar-333"));
445         assert_se(filename_is_valid("o.o"));
446 }
447
448 static void test_hidden_or_backup_file(void) {
449         assert_se(hidden_or_backup_file(".hidden"));
450         assert_se(hidden_or_backup_file("..hidden"));
451         assert_se(!hidden_or_backup_file("hidden."));
452
453         assert_se(hidden_or_backup_file("backup~"));
454         assert_se(hidden_or_backup_file(".backup~"));
455
456         assert_se(hidden_or_backup_file("lost+found"));
457         assert_se(hidden_or_backup_file("aquota.user"));
458         assert_se(hidden_or_backup_file("aquota.group"));
459
460         assert_se(hidden_or_backup_file("test.rpmnew"));
461         assert_se(hidden_or_backup_file("test.dpkg-old"));
462         assert_se(hidden_or_backup_file("test.dpkg-remove"));
463         assert_se(hidden_or_backup_file("test.swp"));
464
465         assert_se(!hidden_or_backup_file("test.rpmnew."));
466         assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
467 }
468
469 #if 0 /// UNNEEDED by elogind
470 static void test_systemd_installation_has_version(const char *path) {
471         int r;
472         const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999};
473         unsigned i;
474
475         for (i = 0; i < ELEMENTSOF(versions); i++) {
476                 r = systemd_installation_has_version(path, versions[i]);
477                 assert_se(r >= 0);
478                 log_info("%s has systemd >= %u: %s",
479                          path ?: "Current installation", versions[i], yes_no(r));
480         }
481 }
482 #endif // 0
483
484 static void test_skip_dev_prefix(void) {
485
486         assert_se(streq(skip_dev_prefix("/"), "/"));
487         assert_se(streq(skip_dev_prefix("/dev"), ""));
488         assert_se(streq(skip_dev_prefix("/dev/"), ""));
489         assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
490         assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
491         assert_se(streq(skip_dev_prefix("//dev"), ""));
492         assert_se(streq(skip_dev_prefix("//dev//"), ""));
493         assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
494         assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
495         assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
496         assert_se(streq(skip_dev_prefix("foo"), "foo"));
497 }
498
499 static void test_empty_or_root(void) {
500         assert_se(empty_or_root(NULL));
501         assert_se(empty_or_root(""));
502         assert_se(empty_or_root("/"));
503         assert_se(empty_or_root("//"));
504         assert_se(empty_or_root("///"));
505         assert_se(empty_or_root("/////////////////"));
506         assert_se(!empty_or_root("xxx"));
507         assert_se(!empty_or_root("/xxx"));
508         assert_se(!empty_or_root("/xxx/"));
509         assert_se(!empty_or_root("//yy//"));
510 }
511
512 int main(int argc, char **argv) {
513         log_set_max_level(LOG_DEBUG);
514         log_parse_environment();
515         log_open();
516
517         test_path();
518         test_path_equal_root();
519         test_find_binary(argv[0]);
520         test_prefixes();
521         test_path_join();
522 #if 0 /// UNNEEDED by elogind
523         test_fsck_exists();
524         test_make_relative();
525 #endif // 0
526         test_strv_resolve();
527         test_path_startswith();
528         test_prefix_root();
529         test_file_in_same_dir();
530         test_last_path_component();
531         test_filename_is_valid();
532         test_hidden_or_backup_file();
533         test_skip_dev_prefix();
534         test_empty_or_root();
535
536 #if 0 /// UNNEEDED by elogind
537         test_systemd_installation_has_version(argv[1]); /* NULL is OK */
538 #endif // 0
539
540         return 0;
541 }