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