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