]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
76877b46 | 2 | |
a696dbef | 3 | #include <stdio.h> |
07630cea | 4 | #include <unistd.h> |
a696dbef | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
3ffd4af2 | 7 | #include "fd-util.h" |
76877b46 | 8 | #include "macro.h" |
049af8ad | 9 | #include "mountpoint-util.h" |
07630cea | 10 | #include "path-util.h" |
c6878637 | 11 | #include "rm-rf.h" |
84e72b5e | 12 | #include "stat-util.h" |
07630cea LP |
13 | #include "string-util.h" |
14 | #include "strv.h" | |
6d7c4033 | 15 | #include "tests.h" |
07630cea | 16 | #include "util.h" |
76877b46 | 17 | |
2230852b MS |
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 | } | |
76877b46 | 24 | |
858d36c1 YW |
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 | ||
76877b46 | 35 | static void test_path(void) { |
3f72b427 LP |
36 | _cleanup_close_ int fd = -1; |
37 | ||
2230852b MS |
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); | |
76877b46 | 43 | |
2230852b MS |
44 | test_path_compare("/goo/boo", "/goo//boo", 0); |
45 | test_path_compare("//goo/boo", "/goo/boo//", 0); | |
76877b46 | 46 | |
2230852b | 47 | test_path_compare("/", "///", 0); |
76877b46 | 48 | |
2230852b MS |
49 | test_path_compare("/x", "x/", 1); |
50 | test_path_compare("x/", "/", -1); | |
76877b46 | 51 | |
2230852b MS |
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); | |
76877b46 ZJS |
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 | ||
2b6bf07d ZJS |
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.../"), "")); | |
76877b46 | 73 | |
3f72b427 LP |
74 | fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY); |
75 | assert_se(fd >= 0); | |
5d409034 | 76 | assert_se(fd_is_mount_point(fd, "/", 0) > 0); |
76877b46 | 77 | |
858d36c1 YW |
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("////.././///../.", "/.././../.", "/../.."); | |
afbae3e9 TH |
84 | test_path_simplify(".", ".", "."); |
85 | test_path_simplify("./", ".", "."); | |
86 | test_path_simplify(".///.//./.", "./././.", "."); | |
87 | test_path_simplify(".///.//././/", "./././.", "."); | |
858d36c1 YW |
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/.."); | |
3ae5990c ZJS |
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")); | |
24737c29 ZJS |
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")); | |
76877b46 ZJS |
112 | } |
113 | ||
84e72b5e ZJS |
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 | ||
e3f791a2 ZJS |
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); | |
84e72b5e | 131 | |
e3f791a2 ZJS |
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); | |
84e72b5e | 136 | |
e3f791a2 ZJS |
137 | assert_se(files_same("/", "/.../", 0) == -ENOENT); |
138 | assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT); | |
84e72b5e ZJS |
139 | |
140 | /* The same for path_equal_or_files_same. */ | |
141 | ||
e3f791a2 ZJS |
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)); | |
84e72b5e | 146 | |
e3f791a2 ZJS |
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)); | |
84e72b5e | 151 | |
e3f791a2 ZJS |
152 | assert_se(!path_equal_or_files_same("/", "/.../", 0)); |
153 | assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e ZJS |
154 | } |
155 | ||
85eca92e | 156 | static void test_find_binary(const char *self) { |
c9d954b2 ZJS |
157 | char *p; |
158 | ||
85eca92e | 159 | assert_se(find_binary("/bin/sh", &p) == 0); |
c9d954b2 | 160 | puts(p); |
85eca92e | 161 | assert_se(path_equal(p, "/bin/sh")); |
c9d954b2 ZJS |
162 | free(p); |
163 | ||
85eca92e | 164 | assert_se(find_binary(self, &p) == 0); |
c9d954b2 | 165 | puts(p); |
6d1e2ddd MG |
166 | /* libtool might prefix the binary name with "lt-" */ |
167 | assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util")); | |
8d95631e | 168 | assert_se(path_is_absolute(p)); |
c9d954b2 ZJS |
169 | free(p); |
170 | ||
85eca92e | 171 | assert_se(find_binary("sh", &p) == 0); |
c9d954b2 | 172 | puts(p); |
8d95631e FB |
173 | assert_se(endswith(p, "/sh")); |
174 | assert_se(path_is_absolute(p)); | |
c9d954b2 ZJS |
175 | free(p); |
176 | ||
85eca92e LP |
177 | assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT); |
178 | assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT); | |
c9d954b2 ZJS |
179 | } |
180 | ||
fecffe5d | 181 | static void test_prefixes(void) { |
b82f71c7 LP |
182 | static const char* const values[] = { |
183 | "/a/b/c/d", | |
184 | "/a/b/c", | |
185 | "/a/b", | |
186 | "/a", | |
187 | "", | |
188 | NULL | |
189 | }; | |
e203f7c3 | 190 | unsigned i; |
fecffe5d | 191 | char s[PATH_MAX]; |
e203f7c3 | 192 | bool b; |
fecffe5d | 193 | |
e203f7c3 LP |
194 | i = 0; |
195 | PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { | |
fecffe5d LP |
196 | log_error("---%s---", s); |
197 | assert_se(streq(s, values[i++])); | |
198 | } | |
e203f7c3 | 199 | assert_se(values[i] == NULL); |
fecffe5d | 200 | |
e203f7c3 LP |
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 | } | |
fecffe5d LP |
206 | assert_se(values[i] == NULL); |
207 | ||
208 | i = 0; | |
e203f7c3 | 209 | PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") |
fecffe5d | 210 | assert_se(streq(s, values[i++])); |
e203f7c3 | 211 | assert_se(values[i] == NULL); |
fecffe5d | 212 | |
e203f7c3 LP |
213 | i = 1; |
214 | PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") | |
215 | assert_se(streq(s, values[i++])); | |
fecffe5d LP |
216 | assert_se(values[i] == NULL); |
217 | ||
218 | PATH_FOREACH_PREFIX(s, "////") | |
e203f7c3 LP |
219 | assert_not_reached("Wut?"); |
220 | ||
221 | b = false; | |
222 | PATH_FOREACH_PREFIX_MORE(s, "////") { | |
223 | assert_se(!b); | |
fecffe5d | 224 | assert_se(streq(s, "")); |
e203f7c3 LP |
225 | b = true; |
226 | } | |
227 | assert_se(b); | |
fecffe5d LP |
228 | |
229 | PATH_FOREACH_PREFIX(s, "") | |
230 | assert_not_reached("wut?"); | |
231 | ||
e203f7c3 LP |
232 | b = false; |
233 | PATH_FOREACH_PREFIX_MORE(s, "") { | |
8d95631e FB |
234 | assert_se(!b); |
235 | assert_se(streq(s, "")); | |
e203f7c3 LP |
236 | b = true; |
237 | } | |
fecffe5d LP |
238 | } |
239 | ||
0c6ea3a4 | 240 | static void test_path_join(void) { |
59ae3a95 | 241 | |
62a85ee0 | 242 | #define test_join(expected, ...) { \ |
59ae3a95 | 243 | _cleanup_free_ char *z = NULL; \ |
62a85ee0 ZJS |
244 | z = path_join(__VA_ARGS__); \ |
245 | log_debug("got \"%s\", expected \"%s\"", z, expected); \ | |
59ae3a95 TA |
246 | assert_se(streq(z, expected)); \ |
247 | } | |
248 | ||
62a85ee0 ZJS |
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 | ||
652ef298 ZJS |
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 | ||
62a85ee0 | 267 | test_join("", "", NULL); |
652ef298 ZJS |
268 | test_join("", NULL, ""); |
269 | test_join("", NULL, NULL); | |
62a85ee0 ZJS |
270 | |
271 | test_join("foo/bar", "foo", "bar"); | |
272 | test_join("foo/bar", "", "foo", "bar"); | |
652ef298 | 273 | test_join("foo/bar", NULL, "foo", NULL, "bar"); |
62a85ee0 ZJS |
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//"); | |
0c6ea3a4 ZJS |
283 | } |
284 | ||
eb66db55 MG |
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. */ | |
85eca92e | 290 | assert_se(fsck_exists("minix") == 1); |
eb66db55 | 291 | |
85eca92e LP |
292 | assert_se(fsck_exists("AbCdE") == 0); |
293 | assert_se(fsck_exists("/../bin/") == 0); | |
eb66db55 MG |
294 | } |
295 | ||
6b56a651 TK |
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); | |
2a5beb66 | 301 | assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0); |
6b56a651 | 302 | |
59ae3a95 TA |
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)); \ | |
6b56a651 TK |
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"); | |
2a5beb66 | 315 | test("/some/path/./dot", "/some/further/path", "../../further/path"); |
48054262 | 316 | test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); |
6b56a651 TK |
317 | } |
318 | ||
3e8a78c8 MM |
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 | ||
bea1a013 | 327 | search_dirs = strv_new("/dir1", "/dir2", "/dir3"); |
3e8a78c8 MM |
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 | ||
c6878637 | 344 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
3e8a78c8 MM |
345 | } |
346 | ||
5895b62f | 347 | static void test_path_startswith(void) { |
0470289b ZJS |
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, "")); | |
5895b62f RC |
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 | ||
1d13f648 LP |
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 | ||
63292663 RC |
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 | ||
b12d25a8 | 439 | static void test_last_path_component(void) { |
77e0a1b5 | 440 | assert_se(last_path_component(NULL) == NULL); |
b12d25a8 ZJS |
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/")); | |
69f9ccf1 | 452 | assert_se(streq(last_path_component(""), "")); |
8460289f LP |
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/")); | |
b12d25a8 ZJS |
457 | } |
458 | ||
a60c8eee LP |
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 | ||
63292663 RC |
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 | ||
b05b9cde ZJS |
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 | ||
5a46d55f ZJS |
539 | static void test_systemd_installation_has_version(const char *path) { |
540 | int r; | |
a67c318d | 541 | const unsigned versions[] = {0, 231, PROJECT_VERSION, 999}; |
5a46d55f ZJS |
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 | ||
a119ec7c LP |
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 | ||
57ea45e1 LP |
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 | ||
d898ed65 LP |
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 | ||
7f076504 | 601 | int main(int argc, char **argv) { |
6d7c4033 | 602 | test_setup_logging(LOG_DEBUG); |
5a46d55f | 603 | |
76877b46 | 604 | test_path(); |
84e72b5e | 605 | test_path_equal_root(); |
85eca92e | 606 | test_find_binary(argv[0]); |
fecffe5d | 607 | test_prefixes(); |
0c6ea3a4 | 608 | test_path_join(); |
eb66db55 | 609 | test_fsck_exists(); |
6b56a651 | 610 | test_make_relative(); |
3e8a78c8 | 611 | test_strv_resolve(); |
5895b62f | 612 | test_path_startswith(); |
1d13f648 | 613 | test_prefix_root(); |
63292663 | 614 | test_file_in_same_dir(); |
b12d25a8 | 615 | test_last_path_component(); |
a60c8eee | 616 | test_path_extract_filename(); |
63292663 | 617 | test_filename_is_valid(); |
b05b9cde | 618 | test_hidden_or_backup_file(); |
a119ec7c | 619 | test_skip_dev_prefix(); |
57ea45e1 | 620 | test_empty_or_root(); |
d898ed65 | 621 | test_path_startswith_set(); |
5895b62f | 622 | |
5a46d55f ZJS |
623 | test_systemd_installation_has_version(argv[1]); /* NULL is OK */ |
624 | ||
76877b46 ZJS |
625 | return 0; |
626 | } |