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