]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-util.c
Merge pull request #8710 from poettering/triviailities-yeah-yeah
[thirdparty/systemd.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 static void test_fsck_exists(void) {
236 /* Ensure we use a sane default for PATH. */
237 unsetenv("PATH");
238
239 /* fsck.minix is provided by util-linux and will probably exist. */
240 assert_se(fsck_exists("minix") == 1);
241
242 assert_se(fsck_exists("AbCdE") == 0);
243 assert_se(fsck_exists("/../bin/") == 0);
244 }
245
246 static void test_make_relative(void) {
247 char *result;
248
249 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
250 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
251 assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
252
253 #define test(from_dir, to_path, expected) { \
254 _cleanup_free_ char *z = NULL; \
255 path_make_relative(from_dir, to_path, &z); \
256 assert_se(streq(z, expected)); \
257 }
258
259 test("/", "/", ".");
260 test("/", "/some/path", "some/path");
261 test("/some/path", "/some/path", ".");
262 test("/some/path", "/some/path/in/subdir", "in/subdir");
263 test("/some/path", "/", "../..");
264 test("/some/path", "/some/other/path", "../other/path");
265 test("/some/path/./dot", "/some/further/path", "../../further/path");
266 test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
267 }
268
269 static void test_strv_resolve(void) {
270 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
271 _cleanup_strv_free_ char **search_dirs = NULL;
272 _cleanup_strv_free_ char **absolute_dirs = NULL;
273 char **d;
274
275 assert_se(mkdtemp(tmp_dir) != NULL);
276
277 search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
278 assert_se(search_dirs);
279 STRV_FOREACH(d, search_dirs) {
280 char *p = strappend(tmp_dir, *d);
281 assert_se(p);
282 assert_se(strv_push(&absolute_dirs, p) == 0);
283 }
284
285 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
286 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
287 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
288
289 path_strv_resolve(search_dirs, tmp_dir);
290 assert_se(streq(search_dirs[0], "/dir1"));
291 assert_se(streq(search_dirs[1], "/dir2"));
292 assert_se(streq(search_dirs[2], "/dir2"));
293
294 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
295 }
296
297 static void test_path_startswith(void) {
298 const char *p;
299
300 p = path_startswith("/foo/bar/barfoo/", "/foo");
301 assert_se(streq_ptr(p, "bar/barfoo/"));
302
303 p = path_startswith("/foo/bar/barfoo/", "/foo/");
304 assert_se(streq_ptr(p, "bar/barfoo/"));
305
306 p = path_startswith("/foo/bar/barfoo/", "/");
307 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
308
309 p = path_startswith("/foo/bar/barfoo/", "////");
310 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
311
312 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
313 assert_se(streq_ptr(p, ""));
314
315 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
316 assert_se(streq_ptr(p, ""));
317
318 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
319 assert_se(streq_ptr(p, ""));
320
321 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
322 assert_se(streq_ptr(p, ""));
323
324 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
325 assert_se(streq_ptr(p, ""));
326
327 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
328 assert_se(streq_ptr(p, ""));
329
330 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
331 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
332 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
333 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
334 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
335 }
336
337 static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
338 _cleanup_free_ char *s = NULL;
339 const char *t;
340
341 assert_se(s = prefix_root(r, p));
342 assert_se(streq_ptr(s, expected));
343
344 t = prefix_roota(r, p);
345 assert_se(t);
346 assert_se(streq_ptr(t, expected));
347 }
348
349 static void test_prefix_root(void) {
350 test_prefix_root_one("/", "/foo", "/foo");
351 test_prefix_root_one(NULL, "/foo", "/foo");
352 test_prefix_root_one("", "/foo", "/foo");
353 test_prefix_root_one("///", "/foo", "/foo");
354 test_prefix_root_one("/", "////foo", "/foo");
355 test_prefix_root_one(NULL, "////foo", "/foo");
356
357 test_prefix_root_one("/foo", "/bar", "/foo/bar");
358 test_prefix_root_one("/foo", "bar", "/foo/bar");
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 }
364
365 static void test_file_in_same_dir(void) {
366 char *t;
367
368 t = file_in_same_dir("/", "a");
369 assert_se(streq(t, "/a"));
370 free(t);
371
372 t = file_in_same_dir("/", "/a");
373 assert_se(streq(t, "/a"));
374 free(t);
375
376 t = file_in_same_dir("", "a");
377 assert_se(streq(t, "a"));
378 free(t);
379
380 t = file_in_same_dir("a/", "a");
381 assert_se(streq(t, "a/a"));
382 free(t);
383
384 t = file_in_same_dir("bar/foo", "bar");
385 assert_se(streq(t, "bar/bar"));
386 free(t);
387 }
388
389 static void test_last_path_component(void) {
390 assert_se(streq(last_path_component("a/b/c"), "c"));
391 assert_se(streq(last_path_component("a/b/c/"), "c/"));
392 assert_se(streq(last_path_component("/"), "/"));
393 assert_se(streq(last_path_component("//"), "/"));
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("/foo/a"), "a"));
400 assert_se(streq(last_path_component("/foo/a/"), "a/"));
401 assert_se(streq(last_path_component(""), ""));
402 assert_se(streq(last_path_component("a"), "a"));
403 assert_se(streq(last_path_component("a/"), "a/"));
404 assert_se(streq(last_path_component("/a"), "a"));
405 assert_se(streq(last_path_component("/a/"), "a/"));
406 }
407
408 static void test_filename_is_valid(void) {
409 char foo[FILENAME_MAX+2];
410 int i;
411
412 assert_se(!filename_is_valid(""));
413 assert_se(!filename_is_valid("/bar/foo"));
414 assert_se(!filename_is_valid("/"));
415 assert_se(!filename_is_valid("."));
416 assert_se(!filename_is_valid(".."));
417
418 for (i=0; i<FILENAME_MAX+1; i++)
419 foo[i] = 'a';
420 foo[FILENAME_MAX+1] = '\0';
421
422 assert_se(!filename_is_valid(foo));
423
424 assert_se(filename_is_valid("foo_bar-333"));
425 assert_se(filename_is_valid("o.o"));
426 }
427
428 static void test_hidden_or_backup_file(void) {
429 assert_se(hidden_or_backup_file(".hidden"));
430 assert_se(hidden_or_backup_file("..hidden"));
431 assert_se(!hidden_or_backup_file("hidden."));
432
433 assert_se(hidden_or_backup_file("backup~"));
434 assert_se(hidden_or_backup_file(".backup~"));
435
436 assert_se(hidden_or_backup_file("lost+found"));
437 assert_se(hidden_or_backup_file("aquota.user"));
438 assert_se(hidden_or_backup_file("aquota.group"));
439
440 assert_se(hidden_or_backup_file("test.rpmnew"));
441 assert_se(hidden_or_backup_file("test.dpkg-old"));
442 assert_se(hidden_or_backup_file("test.dpkg-remove"));
443 assert_se(hidden_or_backup_file("test.swp"));
444
445 assert_se(!hidden_or_backup_file("test.rpmnew."));
446 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
447 }
448
449 static void test_systemd_installation_has_version(const char *path) {
450 int r;
451 const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999};
452 unsigned i;
453
454 for (i = 0; i < ELEMENTSOF(versions); i++) {
455 r = systemd_installation_has_version(path, versions[i]);
456 assert_se(r >= 0);
457 log_info("%s has systemd >= %u: %s",
458 path ?: "Current installation", versions[i], yes_no(r));
459 }
460 }
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 test_fsck_exists();
488 test_make_relative();
489 test_strv_resolve();
490 test_path_startswith();
491 test_prefix_root();
492 test_file_in_same_dir();
493 test_last_path_component();
494 test_filename_is_valid();
495 test_hidden_or_backup_file();
496 test_skip_dev_prefix();
497
498 test_systemd_installation_has_version(argv[1]); /* NULL is OK */
499
500 return 0;
501 }