Branch data Line data Source code
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 "mountpoint-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 "tests.h"
16 : : #include "util.h"
17 : :
18 : : #define test_path_compare(a, b, result) { \
19 : : assert_se(path_compare(a, b) == result); \
20 : : assert_se(path_compare(b, a) == -result); \
21 : : assert_se(path_equal(a, b) == !result); \
22 : : assert_se(path_equal(b, a) == !result); \
23 : : }
24 : :
25 : 56 : static void test_path_simplify(const char *in, const char *out, const char *out_dot) {
26 : : char *p;
27 : :
28 [ + - ]: 56 : log_info("/* %s */", __func__);
29 : :
30 : 56 : p = strdupa(in);
31 [ - + ]: 56 : assert_se(streq(path_simplify(p, false), out));
32 : :
33 : 56 : p = strdupa(in);
34 [ - + ]: 56 : assert_se(streq(path_simplify(p, true), out_dot));
35 : 56 : }
36 : :
37 : 4 : static void test_path(void) {
38 : 4 : _cleanup_close_ int fd = -1;
39 : :
40 [ + - ]: 4 : log_info("/* %s */", __func__);
41 : :
42 [ - + - + : 4 : test_path_compare("/goo", "/goo", 0);
- + - + ]
43 [ - + - + : 4 : test_path_compare("/goo", "/goo", 0);
- + - + ]
44 [ - + - + : 4 : test_path_compare("//goo", "/goo", 0);
- + - + ]
45 [ - + - + : 4 : test_path_compare("//goo/////", "/goo", 0);
- + - + ]
46 [ - + - + : 4 : test_path_compare("goo/////", "goo", 0);
- + - + ]
47 : :
48 [ - + - + : 4 : test_path_compare("/goo/boo", "/goo//boo", 0);
- + - + ]
49 [ - + - + : 4 : test_path_compare("//goo/boo", "/goo/boo//", 0);
- + - + ]
50 : :
51 [ - + - + : 4 : test_path_compare("/", "///", 0);
- + - + ]
52 : :
53 [ - + - + : 4 : test_path_compare("/x", "x/", 1);
- + - + ]
54 [ - + - + : 4 : test_path_compare("x/", "/", -1);
- + - + ]
55 : :
56 [ - + - + : 4 : test_path_compare("/x/./y", "x/y", 1);
- + - + ]
57 [ - + - + : 4 : test_path_compare("x/.y", "x/y", -1);
- + - + ]
58 : :
59 [ - + - + : 4 : test_path_compare("foo", "/foo", -1);
- + - + ]
60 [ - + - + : 4 : test_path_compare("/foo", "/foo/bar", -1);
- + - + ]
61 [ - + - + : 4 : test_path_compare("/foo/aaa", "/foo/b", -1);
- + - + ]
62 [ - + - + : 4 : test_path_compare("/foo/aaa", "/foo/b/a", -1);
- + - + ]
63 [ - + - + : 4 : test_path_compare("/foo/a", "/foo/aaa", -1);
- + - + ]
64 [ - + - + : 4 : test_path_compare("/foo/a/b", "/foo/aaa", -1);
- + - + ]
65 : :
66 [ - + ]: 4 : assert_se(path_is_absolute("/"));
67 [ - + ]: 4 : assert_se(!path_is_absolute("./"));
68 : :
69 [ - + ]: 4 : assert_se(is_path("/dir"));
70 [ - + ]: 4 : assert_se(is_path("a/b"));
71 [ - + ]: 4 : assert_se(!is_path("."));
72 : :
73 [ - + ]: 4 : assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
74 [ - + ]: 4 : assert_se(streq(basename("/aa///.file"), ".file"));
75 [ - + ]: 4 : assert_se(streq(basename("/aa///file..."), "file..."));
76 [ - + ]: 4 : assert_se(streq(basename("file.../"), ""));
77 : :
78 : 4 : fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
79 [ - + ]: 4 : assert_se(fd >= 0);
80 [ - + ]: 4 : assert_se(fd_is_mount_point(fd, "/", 0) > 0);
81 : :
82 : 4 : test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
83 : 4 : test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
84 : 4 : test_path_simplify("///", "/", "/");
85 : 4 : test_path_simplify("///.//", "/.", "/");
86 : 4 : test_path_simplify("///.//.///", "/./.", "/");
87 : 4 : test_path_simplify("////.././///../.", "/.././../.", "/../..");
88 : 4 : test_path_simplify(".", ".", ".");
89 : 4 : test_path_simplify("./", ".", ".");
90 : 4 : test_path_simplify(".///.//./.", "./././.", ".");
91 : 4 : test_path_simplify(".///.//././/", "./././.", ".");
92 : 4 : test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
93 : : "/./aaa/././.bbb/../c./d.dd/..eeee/.",
94 : : "/aaa/.bbb/../c./d.dd/..eeee");
95 : 4 : test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
96 : : "/./aaa/././.bbb/../c./d.dd/..eeee/..",
97 : : "/aaa/.bbb/../c./d.dd/..eeee/..");
98 : 4 : test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
99 : : "././aaa/././.bbb/../c./d.dd/..eeee/..",
100 : : "aaa/.bbb/../c./d.dd/..eeee/..");
101 : 4 : test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
102 : : ".././aaa/././.bbb/../c./d.dd/..eeee/..",
103 : : "../aaa/.bbb/../c./d.dd/..eeee/..");
104 : :
105 [ + + + - : 8 : assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
+ - - + ]
106 [ + - + - : 4 : assert_se(PATH_IN_SET("/bin", "/bin"));
+ - - + ]
107 [ + + + - : 8 : assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
+ - - + ]
108 [ + - + - : 4 : assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
+ - - + ]
109 [ - + + - : 12 : assert_se(!PATH_IN_SET("/", "/abc", "/def"));
+ + - + ]
110 : :
111 [ - + ]: 4 : assert_se(path_equal_ptr(NULL, NULL));
112 [ - + ]: 4 : assert_se(path_equal_ptr("/a", "/a"));
113 [ - + ]: 4 : assert_se(!path_equal_ptr("/a", "/b"));
114 [ - + ]: 4 : assert_se(!path_equal_ptr("/a", NULL));
115 [ - + ]: 4 : assert_se(!path_equal_ptr(NULL, "/a"));
116 : 4 : }
117 : :
118 : 4 : static void test_path_equal_root(void) {
119 : : /* Nail down the details of how path_equal("/", ...) works. */
120 : :
121 [ + - ]: 4 : log_info("/* %s */", __func__);
122 : :
123 [ - + ]: 4 : assert_se(path_equal("/", "/"));
124 [ - + ]: 4 : assert_se(path_equal("/", "//"));
125 : :
126 [ - + ]: 4 : assert_se(!path_equal("/", "/./"));
127 [ - + ]: 4 : assert_se(!path_equal("/", "/../"));
128 : :
129 [ - + ]: 4 : assert_se(!path_equal("/", "/.../"));
130 : :
131 : : /* Make sure that files_same works as expected. */
132 : :
133 [ - + ]: 4 : assert_se(files_same("/", "/", 0) > 0);
134 [ - + ]: 4 : assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
135 [ - + ]: 4 : assert_se(files_same("/", "//", 0) > 0);
136 [ - + ]: 4 : assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
137 : :
138 [ - + ]: 4 : assert_se(files_same("/", "/./", 0) > 0);
139 [ - + ]: 4 : assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
140 [ - + ]: 4 : assert_se(files_same("/", "/../", 0) > 0);
141 [ - + ]: 4 : assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
142 : :
143 [ - + ]: 4 : assert_se(files_same("/", "/.../", 0) == -ENOENT);
144 [ - + ]: 4 : assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
145 : :
146 : : /* The same for path_equal_or_files_same. */
147 : :
148 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "/", 0));
149 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
150 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "//", 0));
151 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
152 : :
153 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "/./", 0));
154 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
155 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "/../", 0));
156 [ - + ]: 4 : assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
157 : :
158 [ - + ]: 4 : assert_se(!path_equal_or_files_same("/", "/.../", 0));
159 [ - + ]: 4 : assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
160 : 4 : }
161 : :
162 : 4 : static void test_find_binary(const char *self) {
163 : : char *p;
164 : :
165 [ + - ]: 4 : log_info("/* %s */", __func__);
166 : :
167 [ - + ]: 4 : assert_se(find_binary("/bin/sh", &p) == 0);
168 : 4 : puts(p);
169 [ - + ]: 4 : assert_se(path_equal(p, "/bin/sh"));
170 : 4 : free(p);
171 : :
172 [ - + ]: 4 : assert_se(find_binary(self, &p) == 0);
173 : 4 : puts(p);
174 : : /* libtool might prefix the binary name with "lt-" */
175 [ + - - + ]: 4 : assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
176 [ - + ]: 4 : assert_se(path_is_absolute(p));
177 : 4 : free(p);
178 : :
179 [ - + ]: 4 : assert_se(find_binary("sh", &p) == 0);
180 : 4 : puts(p);
181 [ - + ]: 4 : assert_se(endswith(p, "/sh"));
182 [ - + ]: 4 : assert_se(path_is_absolute(p));
183 : 4 : free(p);
184 : :
185 [ - + ]: 4 : assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
186 [ - + ]: 4 : assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
187 : 4 : }
188 : :
189 : 4 : static void test_prefixes(void) {
190 : : static const char* const values[] = {
191 : : "/a/b/c/d",
192 : : "/a/b/c",
193 : : "/a/b",
194 : : "/a",
195 : : "",
196 : : NULL
197 : : };
198 : : unsigned i;
199 : : char s[PATH_MAX];
200 : : bool b;
201 : :
202 [ + - ]: 4 : log_info("/* %s */", __func__);
203 : :
204 : 4 : i = 0;
205 [ - + + + ]: 24 : PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
206 [ + - ]: 20 : log_error("---%s---", s);
207 [ - + ]: 20 : assert_se(streq(s, values[i++]));
208 : : }
209 [ - + ]: 4 : assert_se(values[i] == NULL);
210 : :
211 : 4 : i = 1;
212 [ + - + + ]: 20 : PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
213 [ + - ]: 16 : log_error("---%s---", s);
214 [ - + ]: 16 : assert_se(streq(s, values[i++]));
215 : : }
216 [ - + ]: 4 : assert_se(values[i] == NULL);
217 : :
218 : 4 : i = 0;
219 [ - + + + ]: 24 : PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
220 [ - + ]: 20 : assert_se(streq(s, values[i++]));
221 [ - + ]: 4 : assert_se(values[i] == NULL);
222 : :
223 : 4 : i = 1;
224 [ + - + + ]: 20 : PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
225 [ - + ]: 16 : assert_se(streq(s, values[i++]));
226 [ - + ]: 4 : assert_se(values[i] == NULL);
227 : :
228 [ - + - + ]: 4 : PATH_FOREACH_PREFIX(s, "////")
229 : 0 : assert_not_reached("Wut?");
230 : :
231 : 4 : b = false;
232 [ + - + + ]: 8 : PATH_FOREACH_PREFIX_MORE(s, "////") {
233 [ - + ]: 4 : assert_se(!b);
234 [ - + ]: 4 : assert_se(streq(s, ""));
235 : 4 : b = true;
236 : : }
237 [ - + ]: 4 : assert_se(b);
238 : :
239 [ + - - + ]: 4 : PATH_FOREACH_PREFIX(s, "")
240 : 0 : assert_not_reached("wut?");
241 : :
242 : 4 : b = false;
243 [ - + + + ]: 8 : PATH_FOREACH_PREFIX_MORE(s, "") {
244 [ - + ]: 4 : assert_se(!b);
245 [ - + ]: 4 : assert_se(streq(s, ""));
246 : 4 : b = true;
247 : : }
248 : 4 : }
249 : :
250 : 4 : static void test_path_join(void) {
251 [ + - ]: 4 : log_info("/* %s */", __func__);
252 : :
253 : : #define test_join(expected, ...) { \
254 : : _cleanup_free_ char *z = NULL; \
255 : : z = path_join(__VA_ARGS__); \
256 : : log_debug("got \"%s\", expected \"%s\"", z, expected); \
257 : : assert_se(streq(z, expected)); \
258 : : }
259 : :
260 [ + - - + ]: 4 : test_join("/root/a/b/c", "/root", "/a/b", "/c");
261 [ + - - + ]: 4 : test_join("/root/a/b/c", "/root", "a/b", "c");
262 [ + - - + ]: 4 : test_join("/root/a/b/c", "/root", "/a/b", "c");
263 [ + - - + ]: 4 : test_join("/root/c", "/root", "/", "c");
264 [ + - - + ]: 4 : test_join("/root/", "/root", "/", NULL);
265 : :
266 [ + - - + ]: 4 : test_join("/a/b/c", "", "/a/b", "/c");
267 [ + - - + ]: 4 : test_join("a/b/c", "", "a/b", "c");
268 [ + - - + ]: 4 : test_join("/a/b/c", "", "/a/b", "c");
269 [ + - - + ]: 4 : test_join("/c", "", "/", "c");
270 [ + - - + ]: 4 : test_join("/", "", "/", NULL);
271 : :
272 [ + - - + ]: 4 : test_join("/a/b/c", NULL, "/a/b", "/c");
273 [ + - - + ]: 4 : test_join("a/b/c", NULL, "a/b", "c");
274 [ + - - + ]: 4 : test_join("/a/b/c", NULL, "/a/b", "c");
275 [ + - - + ]: 4 : test_join("/c", NULL, "/", "c");
276 [ + - - + ]: 4 : test_join("/", NULL, "/", NULL);
277 : :
278 [ + - - + ]: 4 : test_join("", "", NULL);
279 [ + - - + ]: 4 : test_join("", NULL, "");
280 [ + - - + ]: 4 : test_join("", NULL, NULL);
281 : :
282 [ + - - + ]: 4 : test_join("foo/bar", "foo", "bar");
283 [ + - - + ]: 4 : test_join("foo/bar", "", "foo", "bar");
284 [ + - - + ]: 4 : test_join("foo/bar", NULL, "foo", NULL, "bar");
285 [ + - - + ]: 4 : test_join("foo/bar", "", "foo", "", "bar", "");
286 [ + - - + ]: 4 : test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
287 : :
288 [ + - - + ]: 4 : test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
289 [ + - - + ]: 4 : test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
290 [ + - - + ]: 4 : test_join("foo/bar/baz", "foo", "bar", "baz");
291 [ + - - + ]: 4 : test_join("foo/bar/baz", "foo/", "bar", "/baz");
292 [ + - - + ]: 4 : test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
293 [ + - - + ]: 4 : test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
294 : 4 : }
295 : :
296 : 4 : static void test_fsck_exists(void) {
297 [ + - ]: 4 : log_info("/* %s */", __func__);
298 : :
299 : : /* Ensure we use a sane default for PATH. */
300 : 4 : unsetenv("PATH");
301 : :
302 : : /* fsck.minix is provided by util-linux and will probably exist. */
303 [ - + ]: 4 : assert_se(fsck_exists("minix") == 1);
304 : :
305 [ - + ]: 4 : assert_se(fsck_exists("AbCdE") == 0);
306 [ - + ]: 4 : assert_se(fsck_exists("/../bin/") == 0);
307 : 4 : }
308 : :
309 : 4 : static void test_make_relative(void) {
310 : : char *result;
311 : :
312 [ + - ]: 4 : log_info("/* %s */", __func__);
313 : :
314 [ - + ]: 4 : assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
315 [ - + ]: 4 : assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
316 [ - + ]: 4 : assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
317 : :
318 : : #define test(from_dir, to_path, expected) { \
319 : : _cleanup_free_ char *z = NULL; \
320 : : path_make_relative(from_dir, to_path, &z); \
321 : : assert_se(streq(z, expected)); \
322 : : }
323 : :
324 [ - + ]: 4 : test("/", "/", ".");
325 [ - + ]: 4 : test("/", "/some/path", "some/path");
326 [ - + ]: 4 : test("/some/path", "/some/path", ".");
327 [ - + ]: 4 : test("/some/path", "/some/path/in/subdir", "in/subdir");
328 [ - + ]: 4 : test("/some/path", "/", "../..");
329 [ - + ]: 4 : test("/some/path", "/some/other/path", "../other/path");
330 [ - + ]: 4 : test("/some/path/./dot", "/some/further/path", "../../further/path");
331 [ - + ]: 4 : test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
332 : 4 : }
333 : :
334 : 4 : static void test_strv_resolve(void) {
335 : 4 : char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
336 : 4 : _cleanup_strv_free_ char **search_dirs = NULL;
337 : 4 : _cleanup_strv_free_ char **absolute_dirs = NULL;
338 : : char **d;
339 : :
340 [ - + ]: 4 : assert_se(mkdtemp(tmp_dir) != NULL);
341 : :
342 : 4 : search_dirs = strv_new("/dir1", "/dir2", "/dir3");
343 [ - + ]: 4 : assert_se(search_dirs);
344 [ + - + + ]: 16 : STRV_FOREACH(d, search_dirs) {
345 : 12 : char *p = path_join(tmp_dir, *d);
346 [ - + ]: 12 : assert_se(p);
347 [ - + ]: 12 : assert_se(strv_push(&absolute_dirs, p) == 0);
348 : : }
349 : :
350 [ - + ]: 4 : assert_se(mkdir(absolute_dirs[0], 0700) == 0);
351 [ - + ]: 4 : assert_se(mkdir(absolute_dirs[1], 0700) == 0);
352 [ - + ]: 4 : assert_se(symlink("dir2", absolute_dirs[2]) == 0);
353 : :
354 : 4 : path_strv_resolve(search_dirs, tmp_dir);
355 [ - + ]: 4 : assert_se(streq(search_dirs[0], "/dir1"));
356 [ - + ]: 4 : assert_se(streq(search_dirs[1], "/dir2"));
357 [ - + ]: 4 : assert_se(streq(search_dirs[2], "/dir2"));
358 : :
359 [ - + ]: 4 : assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
360 : 4 : }
361 : :
362 : 4 : static void test_path_startswith(void) {
363 : : const char *p;
364 : :
365 [ + - ]: 4 : log_info("/* %s */", __func__);
366 : :
367 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo");
368 [ - + ]: 4 : assert_se(streq_ptr(p, "bar/barfoo/"));
369 : :
370 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo/");
371 [ - + ]: 4 : assert_se(streq_ptr(p, "bar/barfoo/"));
372 : :
373 : 4 : p = path_startswith("/foo/bar/barfoo/", "/");
374 [ - + ]: 4 : assert_se(streq_ptr(p, "foo/bar/barfoo/"));
375 : :
376 : 4 : p = path_startswith("/foo/bar/barfoo/", "////");
377 [ - + ]: 4 : assert_se(streq_ptr(p, "foo/bar/barfoo/"));
378 : :
379 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
380 [ - + ]: 4 : assert_se(streq_ptr(p, ""));
381 : :
382 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
383 [ - + ]: 4 : assert_se(streq_ptr(p, ""));
384 : :
385 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
386 [ - + ]: 4 : assert_se(streq_ptr(p, ""));
387 : :
388 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
389 [ - + ]: 4 : assert_se(streq_ptr(p, ""));
390 : :
391 : 4 : p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
392 [ - + ]: 4 : assert_se(streq_ptr(p, ""));
393 : :
394 : 4 : p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
395 [ - + ]: 4 : assert_se(streq_ptr(p, ""));
396 : :
397 [ - + ]: 4 : assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
398 [ - + ]: 4 : assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
399 [ - + ]: 4 : assert_se(!path_startswith("/foo/bar/barfoo/", ""));
400 [ - + ]: 4 : assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
401 [ - + ]: 4 : assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
402 : 4 : }
403 : :
404 : 60 : static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
405 : 60 : _cleanup_free_ char *s = NULL;
406 : : const char *t;
407 : :
408 [ - + ]: 60 : assert_se(s = path_join(r, p));
409 [ - + ]: 60 : assert_se(path_equal_ptr(s, expected));
410 : :
411 [ + + + + : 136 : t = prefix_roota(r, p);
+ + - + -
+ + + + +
+ + ]
412 [ - + ]: 60 : assert_se(t);
413 [ - + ]: 60 : assert_se(path_equal_ptr(t, expected));
414 : 60 : }
415 : :
416 : 4 : static void test_prefix_root(void) {
417 [ + - ]: 4 : log_info("/* %s */", __func__);
418 : :
419 : 4 : test_prefix_root_one("/", "/foo", "/foo");
420 : 4 : test_prefix_root_one(NULL, "/foo", "/foo");
421 : 4 : test_prefix_root_one("", "/foo", "/foo");
422 : 4 : test_prefix_root_one("///", "/foo", "/foo");
423 : 4 : test_prefix_root_one("/", "////foo", "/foo");
424 : 4 : test_prefix_root_one(NULL, "////foo", "/foo");
425 : 4 : test_prefix_root_one("/", "foo", "/foo");
426 : 4 : test_prefix_root_one("", "foo", "foo");
427 : 4 : test_prefix_root_one(NULL, "foo", "foo");
428 : :
429 : 4 : test_prefix_root_one("/foo", "/bar", "/foo/bar");
430 : 4 : test_prefix_root_one("/foo", "bar", "/foo/bar");
431 : 4 : test_prefix_root_one("foo", "bar", "foo/bar");
432 : 4 : test_prefix_root_one("/foo/", "/bar", "/foo/bar");
433 : 4 : test_prefix_root_one("/foo/", "//bar", "/foo/bar");
434 : 4 : test_prefix_root_one("/foo///", "//bar", "/foo/bar");
435 : 4 : }
436 : :
437 : 4 : static void test_file_in_same_dir(void) {
438 : : char *t;
439 : :
440 [ + - ]: 4 : log_info("/* %s */", __func__);
441 : :
442 : 4 : t = file_in_same_dir("/", "a");
443 [ - + ]: 4 : assert_se(streq(t, "/a"));
444 : 4 : free(t);
445 : :
446 : 4 : t = file_in_same_dir("/", "/a");
447 [ - + ]: 4 : assert_se(streq(t, "/a"));
448 : 4 : free(t);
449 : :
450 : 4 : t = file_in_same_dir("", "a");
451 [ - + ]: 4 : assert_se(streq(t, "a"));
452 : 4 : free(t);
453 : :
454 : 4 : t = file_in_same_dir("a/", "a");
455 [ - + ]: 4 : assert_se(streq(t, "a/a"));
456 : 4 : free(t);
457 : :
458 : 4 : t = file_in_same_dir("bar/foo", "bar");
459 [ - + ]: 4 : assert_se(streq(t, "bar/bar"));
460 : 4 : free(t);
461 : 4 : }
462 : :
463 : 4 : static void test_last_path_component(void) {
464 [ - + ]: 4 : assert_se(last_path_component(NULL) == NULL);
465 [ - + ]: 4 : assert_se(streq(last_path_component("a/b/c"), "c"));
466 [ - + ]: 4 : assert_se(streq(last_path_component("a/b/c/"), "c/"));
467 [ - + ]: 4 : assert_se(streq(last_path_component("/"), "/"));
468 [ - + ]: 4 : assert_se(streq(last_path_component("//"), "/"));
469 [ - + ]: 4 : assert_se(streq(last_path_component("///"), "/"));
470 [ - + ]: 4 : assert_se(streq(last_path_component("."), "."));
471 [ - + ]: 4 : assert_se(streq(last_path_component("./."), "."));
472 [ - + ]: 4 : assert_se(streq(last_path_component("././"), "./"));
473 [ - + ]: 4 : assert_se(streq(last_path_component("././/"), ".//"));
474 [ - + ]: 4 : assert_se(streq(last_path_component("/foo/a"), "a"));
475 [ - + ]: 4 : assert_se(streq(last_path_component("/foo/a/"), "a/"));
476 [ - + ]: 4 : assert_se(streq(last_path_component(""), ""));
477 [ - + ]: 4 : assert_se(streq(last_path_component("a"), "a"));
478 [ - + ]: 4 : assert_se(streq(last_path_component("a/"), "a/"));
479 [ - + ]: 4 : assert_se(streq(last_path_component("/a"), "a"));
480 [ - + ]: 4 : assert_se(streq(last_path_component("/a/"), "a/"));
481 : 4 : }
482 : :
483 : 104 : static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
484 : 104 : _cleanup_free_ char *k = NULL;
485 : : int r;
486 : :
487 : 104 : r = path_extract_filename(input, &k);
488 [ + - ]: 104 : log_info("%s → %s/%s [expected: %s/%s]", strnull(input), strnull(k), strerror_safe(r), strnull(output), strerror_safe(ret));
489 [ - + ]: 104 : assert_se(streq_ptr(k, output));
490 [ - + ]: 104 : assert_se(r == ret);
491 : 104 : }
492 : :
493 : 4 : static void test_path_extract_filename(void) {
494 [ + - ]: 4 : log_info("/* %s */", __func__);
495 : :
496 : 4 : test_path_extract_filename_one(NULL, NULL, -EINVAL);
497 : 4 : test_path_extract_filename_one("a/b/c", "c", 0);
498 : 4 : test_path_extract_filename_one("a/b/c/", "c", 0);
499 : 4 : test_path_extract_filename_one("/", NULL, -EINVAL);
500 : 4 : test_path_extract_filename_one("//", NULL, -EINVAL);
501 : 4 : test_path_extract_filename_one("///", NULL, -EINVAL);
502 : 4 : test_path_extract_filename_one(".", NULL, -EINVAL);
503 : 4 : test_path_extract_filename_one("./.", NULL, -EINVAL);
504 : 4 : test_path_extract_filename_one("././", NULL, -EINVAL);
505 : 4 : test_path_extract_filename_one("././/", NULL, -EINVAL);
506 : 4 : test_path_extract_filename_one("/foo/a", "a", 0);
507 : 4 : test_path_extract_filename_one("/foo/a/", "a", 0);
508 : 4 : test_path_extract_filename_one("", NULL, -EINVAL);
509 : 4 : test_path_extract_filename_one("a", "a", 0);
510 : 4 : test_path_extract_filename_one("a/", "a", 0);
511 : 4 : test_path_extract_filename_one("/a", "a", 0);
512 : 4 : test_path_extract_filename_one("/a/", "a", 0);
513 : 4 : test_path_extract_filename_one("/////////////a/////////////", "a", 0);
514 : 4 : test_path_extract_filename_one("xx/.", NULL, -EINVAL);
515 : 4 : test_path_extract_filename_one("xx/..", NULL, -EINVAL);
516 : 4 : test_path_extract_filename_one("..", NULL, -EINVAL);
517 : 4 : test_path_extract_filename_one("/..", NULL, -EINVAL);
518 : 4 : test_path_extract_filename_one("../", NULL, -EINVAL);
519 : 4 : test_path_extract_filename_one(".", NULL, -EINVAL);
520 : 4 : test_path_extract_filename_one("/.", NULL, -EINVAL);
521 : 4 : test_path_extract_filename_one("./", NULL, -EINVAL);
522 : 4 : }
523 : :
524 : 4 : static void test_filename_is_valid(void) {
525 : : char foo[FILENAME_MAX+2];
526 : : int i;
527 : :
528 [ + - ]: 4 : log_info("/* %s */", __func__);
529 : :
530 [ - + ]: 4 : assert_se(!filename_is_valid(""));
531 [ - + ]: 4 : assert_se(!filename_is_valid("/bar/foo"));
532 [ - + ]: 4 : assert_se(!filename_is_valid("/"));
533 [ - + ]: 4 : assert_se(!filename_is_valid("."));
534 [ - + ]: 4 : assert_se(!filename_is_valid(".."));
535 : :
536 [ + + ]: 16392 : for (i=0; i<FILENAME_MAX+1; i++)
537 : 16388 : foo[i] = 'a';
538 : 4 : foo[FILENAME_MAX+1] = '\0';
539 : :
540 [ - + ]: 4 : assert_se(!filename_is_valid(foo));
541 : :
542 [ - + ]: 4 : assert_se(filename_is_valid("foo_bar-333"));
543 [ - + ]: 4 : assert_se(filename_is_valid("o.o"));
544 : 4 : }
545 : :
546 : 4 : static void test_hidden_or_backup_file(void) {
547 [ + - ]: 4 : log_info("/* %s */", __func__);
548 : :
549 [ - + ]: 4 : assert_se(hidden_or_backup_file(".hidden"));
550 [ - + ]: 4 : assert_se(hidden_or_backup_file("..hidden"));
551 [ - + ]: 4 : assert_se(!hidden_or_backup_file("hidden."));
552 : :
553 [ - + ]: 4 : assert_se(hidden_or_backup_file("backup~"));
554 [ - + ]: 4 : assert_se(hidden_or_backup_file(".backup~"));
555 : :
556 [ - + ]: 4 : assert_se(hidden_or_backup_file("lost+found"));
557 [ - + ]: 4 : assert_se(hidden_or_backup_file("aquota.user"));
558 [ - + ]: 4 : assert_se(hidden_or_backup_file("aquota.group"));
559 : :
560 [ - + ]: 4 : assert_se(hidden_or_backup_file("test.rpmnew"));
561 [ - + ]: 4 : assert_se(hidden_or_backup_file("test.dpkg-old"));
562 [ - + ]: 4 : assert_se(hidden_or_backup_file("test.dpkg-remove"));
563 [ - + ]: 4 : assert_se(hidden_or_backup_file("test.swp"));
564 : :
565 [ - + ]: 4 : assert_se(!hidden_or_backup_file("test.rpmnew."));
566 [ - + ]: 4 : assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
567 : 4 : }
568 : :
569 : 4 : static void test_systemd_installation_has_version(const char *path) {
570 : : int r;
571 : 4 : const unsigned versions[] = {0, 231, PROJECT_VERSION, 999};
572 : : unsigned i;
573 : :
574 [ + - ]: 4 : log_info("/* %s */", __func__);
575 : :
576 [ + + ]: 20 : for (i = 0; i < ELEMENTSOF(versions); i++) {
577 : 16 : r = systemd_installation_has_version(path, versions[i]);
578 [ - + ]: 16 : assert_se(r >= 0);
579 [ + - - + ]: 16 : log_info("%s has systemd >= %u: %s",
580 : : path ?: "Current installation", versions[i], yes_no(r));
581 : : }
582 : 4 : }
583 : :
584 : 4 : static void test_skip_dev_prefix(void) {
585 [ + - ]: 4 : log_info("/* %s */", __func__);
586 : :
587 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("/"), "/"));
588 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("/dev"), ""));
589 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("/dev/"), ""));
590 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
591 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
592 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("//dev"), ""));
593 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("//dev//"), ""));
594 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
595 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
596 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
597 [ - + ]: 4 : assert_se(streq(skip_dev_prefix("foo"), "foo"));
598 : 4 : }
599 : :
600 : 4 : static void test_empty_or_root(void) {
601 [ + - ]: 4 : log_info("/* %s */", __func__);
602 : :
603 [ - + ]: 4 : assert_se(empty_or_root(NULL));
604 [ - + ]: 4 : assert_se(empty_or_root(""));
605 [ - + ]: 4 : assert_se(empty_or_root("/"));
606 [ - + ]: 4 : assert_se(empty_or_root("//"));
607 [ - + ]: 4 : assert_se(empty_or_root("///"));
608 [ - + ]: 4 : assert_se(empty_or_root("/////////////////"));
609 [ - + ]: 4 : assert_se(!empty_or_root("xxx"));
610 [ - + ]: 4 : assert_se(!empty_or_root("/xxx"));
611 [ - + ]: 4 : assert_se(!empty_or_root("/xxx/"));
612 [ - + ]: 4 : assert_se(!empty_or_root("//yy//"));
613 : 4 : }
614 : :
615 : 4 : static void test_path_startswith_set(void) {
616 [ + - ]: 4 : log_info("/* %s */", __func__);
617 : :
618 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
619 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
620 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
621 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
622 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL));
623 : :
624 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL));
625 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
626 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
627 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
628 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL));
629 : :
630 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL));
631 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL));
632 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL));
633 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
634 [ - + ]: 4 : assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL));
635 : 4 : }
636 : :
637 : 4 : static void test_path_startswith_strv(void) {
638 [ + - ]: 4 : log_info("/* %s */", __func__);
639 : :
640 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
641 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
642 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
643 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
644 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
645 : :
646 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL));
647 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
648 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
649 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
650 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
651 : :
652 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL));
653 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL));
654 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL));
655 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
656 [ - + ]: 4 : assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
657 : 4 : }
658 : :
659 : 4 : int main(int argc, char **argv) {
660 : 4 : test_setup_logging(LOG_DEBUG);
661 : :
662 : 4 : test_path();
663 : 4 : test_path_equal_root();
664 : 4 : test_find_binary(argv[0]);
665 : 4 : test_prefixes();
666 : 4 : test_path_join();
667 : 4 : test_fsck_exists();
668 : 4 : test_make_relative();
669 : 4 : test_strv_resolve();
670 : 4 : test_path_startswith();
671 : 4 : test_prefix_root();
672 : 4 : test_file_in_same_dir();
673 : 4 : test_last_path_component();
674 : 4 : test_path_extract_filename();
675 : 4 : test_filename_is_valid();
676 : 4 : test_hidden_or_backup_file();
677 : 4 : test_skip_dev_prefix();
678 : 4 : test_empty_or_root();
679 : 4 : test_path_startswith_set();
680 : 4 : test_path_startswith_strv();
681 : :
682 : 4 : test_systemd_installation_has_version(argv[1]); /* NULL is OK */
683 : :
684 : 4 : return 0;
685 : : }
|