]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
76877b46 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2013 Zbigniew Jędrzejewski-Szmek |
76877b46 ZJS |
4 | ***/ |
5 | ||
a696dbef | 6 | #include <stdio.h> |
07630cea | 7 | #include <unistd.h> |
a696dbef | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
3ffd4af2 | 10 | #include "fd-util.h" |
76877b46 | 11 | #include "macro.h" |
4349cd7c | 12 | #include "mount-util.h" |
07630cea | 13 | #include "path-util.h" |
c6878637 | 14 | #include "rm-rf.h" |
84e72b5e | 15 | #include "stat-util.h" |
07630cea LP |
16 | #include "string-util.h" |
17 | #include "strv.h" | |
18 | #include "util.h" | |
76877b46 | 19 | |
2230852b MS |
20 | #define test_path_compare(a, b, result) { \ |
21 | assert_se(path_compare(a, b) == result); \ | |
22 | assert_se(path_compare(b, a) == -result); \ | |
23 | assert_se(path_equal(a, b) == !result); \ | |
24 | assert_se(path_equal(b, a) == !result); \ | |
25 | } | |
76877b46 | 26 | |
858d36c1 YW |
27 | static void test_path_simplify(const char *in, const char *out, const char *out_dot) { |
28 | char *p; | |
29 | ||
30 | p = strdupa(in); | |
31 | assert_se(streq(path_simplify(p, false), out)); | |
32 | ||
33 | p = strdupa(in); | |
34 | assert_se(streq(path_simplify(p, true), out_dot)); | |
35 | } | |
36 | ||
76877b46 | 37 | static void test_path(void) { |
3f72b427 LP |
38 | _cleanup_close_ int fd = -1; |
39 | ||
2230852b MS |
40 | test_path_compare("/goo", "/goo", 0); |
41 | test_path_compare("/goo", "/goo", 0); | |
42 | test_path_compare("//goo", "/goo", 0); | |
43 | test_path_compare("//goo/////", "/goo", 0); | |
44 | test_path_compare("goo/////", "goo", 0); | |
76877b46 | 45 | |
2230852b MS |
46 | test_path_compare("/goo/boo", "/goo//boo", 0); |
47 | test_path_compare("//goo/boo", "/goo/boo//", 0); | |
76877b46 | 48 | |
2230852b | 49 | test_path_compare("/", "///", 0); |
76877b46 | 50 | |
2230852b MS |
51 | test_path_compare("/x", "x/", 1); |
52 | test_path_compare("x/", "/", -1); | |
76877b46 | 53 | |
2230852b MS |
54 | test_path_compare("/x/./y", "x/y", 1); |
55 | test_path_compare("x/.y", "x/y", -1); | |
56 | ||
57 | test_path_compare("foo", "/foo", -1); | |
58 | test_path_compare("/foo", "/foo/bar", -1); | |
59 | test_path_compare("/foo/aaa", "/foo/b", -1); | |
60 | test_path_compare("/foo/aaa", "/foo/b/a", -1); | |
61 | test_path_compare("/foo/a", "/foo/aaa", -1); | |
62 | test_path_compare("/foo/a/b", "/foo/aaa", -1); | |
76877b46 ZJS |
63 | |
64 | assert_se(path_is_absolute("/")); | |
65 | assert_se(!path_is_absolute("./")); | |
66 | ||
67 | assert_se(is_path("/dir")); | |
68 | assert_se(is_path("a/b")); | |
69 | assert_se(!is_path(".")); | |
70 | ||
2b6bf07d ZJS |
71 | assert_se(streq(basename("./aa/bb/../file.da."), "file.da.")); |
72 | assert_se(streq(basename("/aa///.file"), ".file")); | |
73 | assert_se(streq(basename("/aa///file..."), "file...")); | |
74 | assert_se(streq(basename("file.../"), "")); | |
76877b46 | 75 | |
3f72b427 LP |
76 | fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY); |
77 | assert_se(fd >= 0); | |
5d409034 | 78 | assert_se(fd_is_mount_point(fd, "/", 0) > 0); |
76877b46 | 79 | |
858d36c1 YW |
80 | test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc"); |
81 | test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc"); | |
82 | test_path_simplify("///", "/", "/"); | |
83 | test_path_simplify("///.//", "/.", "/"); | |
84 | test_path_simplify("///.//.///", "/./.", "/"); | |
85 | test_path_simplify("////.././///../.", "/.././../.", "/../.."); | |
86 | test_path_simplify(".", ".", ""); | |
87 | test_path_simplify("./", ".", ""); | |
88 | test_path_simplify(".///.//./.", "./././.", ""); | |
89 | test_path_simplify(".///.//././/", "./././.", ""); | |
90 | test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", | |
91 | "/./aaa/././.bbb/../c./d.dd/..eeee/.", | |
92 | "/aaa/.bbb/../c./d.dd/..eeee"); | |
93 | test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
94 | "/./aaa/././.bbb/../c./d.dd/..eeee/..", | |
95 | "/aaa/.bbb/../c./d.dd/..eeee/.."); | |
96 | test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
97 | "././aaa/././.bbb/../c./d.dd/..eeee/..", | |
98 | "aaa/.bbb/../c./d.dd/..eeee/.."); | |
99 | test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
100 | ".././aaa/././.bbb/../c./d.dd/..eeee/..", | |
101 | "../aaa/.bbb/../c./d.dd/..eeee/.."); | |
3ae5990c ZJS |
102 | |
103 | assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo")); | |
104 | assert_se(PATH_IN_SET("/bin", "/bin")); | |
105 | assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin")); | |
106 | assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar")); | |
107 | assert_se(!PATH_IN_SET("/", "/abc", "/def")); | |
24737c29 ZJS |
108 | |
109 | assert_se(path_equal_ptr(NULL, NULL)); | |
110 | assert_se(path_equal_ptr("/a", "/a")); | |
111 | assert_se(!path_equal_ptr("/a", "/b")); | |
112 | assert_se(!path_equal_ptr("/a", NULL)); | |
113 | assert_se(!path_equal_ptr(NULL, "/a")); | |
76877b46 ZJS |
114 | } |
115 | ||
84e72b5e ZJS |
116 | static void test_path_equal_root(void) { |
117 | /* Nail down the details of how path_equal("/", ...) works. */ | |
118 | ||
119 | assert_se(path_equal("/", "/")); | |
120 | assert_se(path_equal("/", "//")); | |
121 | ||
122 | assert_se(!path_equal("/", "/./")); | |
123 | assert_se(!path_equal("/", "/../")); | |
124 | ||
125 | assert_se(!path_equal("/", "/.../")); | |
126 | ||
127 | /* Make sure that files_same works as expected. */ | |
128 | ||
e3f791a2 ZJS |
129 | assert_se(files_same("/", "/", 0) > 0); |
130 | assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0); | |
131 | assert_se(files_same("/", "//", 0) > 0); | |
132 | assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0); | |
84e72b5e | 133 | |
e3f791a2 ZJS |
134 | assert_se(files_same("/", "/./", 0) > 0); |
135 | assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0); | |
136 | assert_se(files_same("/", "/../", 0) > 0); | |
137 | assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0); | |
84e72b5e | 138 | |
e3f791a2 ZJS |
139 | assert_se(files_same("/", "/.../", 0) == -ENOENT); |
140 | assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT); | |
84e72b5e ZJS |
141 | |
142 | /* The same for path_equal_or_files_same. */ | |
143 | ||
e3f791a2 ZJS |
144 | assert_se(path_equal_or_files_same("/", "/", 0)); |
145 | assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW)); | |
146 | assert_se(path_equal_or_files_same("/", "//", 0)); | |
147 | assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e | 148 | |
e3f791a2 ZJS |
149 | assert_se(path_equal_or_files_same("/", "/./", 0)); |
150 | assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW)); | |
151 | assert_se(path_equal_or_files_same("/", "/../", 0)); | |
152 | assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e | 153 | |
e3f791a2 ZJS |
154 | assert_se(!path_equal_or_files_same("/", "/.../", 0)); |
155 | assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW)); | |
84e72b5e ZJS |
156 | } |
157 | ||
85eca92e | 158 | static void test_find_binary(const char *self) { |
c9d954b2 ZJS |
159 | char *p; |
160 | ||
85eca92e | 161 | assert_se(find_binary("/bin/sh", &p) == 0); |
c9d954b2 | 162 | puts(p); |
85eca92e | 163 | assert_se(path_equal(p, "/bin/sh")); |
c9d954b2 ZJS |
164 | free(p); |
165 | ||
85eca92e | 166 | assert_se(find_binary(self, &p) == 0); |
c9d954b2 | 167 | puts(p); |
6d1e2ddd MG |
168 | /* libtool might prefix the binary name with "lt-" */ |
169 | assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util")); | |
8d95631e | 170 | assert_se(path_is_absolute(p)); |
c9d954b2 ZJS |
171 | free(p); |
172 | ||
85eca92e | 173 | assert_se(find_binary("sh", &p) == 0); |
c9d954b2 | 174 | puts(p); |
8d95631e FB |
175 | assert_se(endswith(p, "/sh")); |
176 | assert_se(path_is_absolute(p)); | |
c9d954b2 ZJS |
177 | free(p); |
178 | ||
85eca92e LP |
179 | assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT); |
180 | assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT); | |
c9d954b2 ZJS |
181 | } |
182 | ||
fecffe5d | 183 | static void test_prefixes(void) { |
e203f7c3 LP |
184 | static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL}; |
185 | unsigned i; | |
fecffe5d | 186 | char s[PATH_MAX]; |
e203f7c3 | 187 | bool b; |
fecffe5d | 188 | |
e203f7c3 LP |
189 | i = 0; |
190 | PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { | |
fecffe5d LP |
191 | log_error("---%s---", s); |
192 | assert_se(streq(s, values[i++])); | |
193 | } | |
e203f7c3 | 194 | assert_se(values[i] == NULL); |
fecffe5d | 195 | |
e203f7c3 LP |
196 | i = 1; |
197 | PATH_FOREACH_PREFIX(s, "/a/b/c/d") { | |
198 | log_error("---%s---", s); | |
199 | assert_se(streq(s, values[i++])); | |
200 | } | |
fecffe5d LP |
201 | assert_se(values[i] == NULL); |
202 | ||
203 | i = 0; | |
e203f7c3 | 204 | PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") |
fecffe5d | 205 | assert_se(streq(s, values[i++])); |
e203f7c3 | 206 | assert_se(values[i] == NULL); |
fecffe5d | 207 | |
e203f7c3 LP |
208 | i = 1; |
209 | PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") | |
210 | assert_se(streq(s, values[i++])); | |
fecffe5d LP |
211 | assert_se(values[i] == NULL); |
212 | ||
213 | PATH_FOREACH_PREFIX(s, "////") | |
e203f7c3 LP |
214 | assert_not_reached("Wut?"); |
215 | ||
216 | b = false; | |
217 | PATH_FOREACH_PREFIX_MORE(s, "////") { | |
218 | assert_se(!b); | |
fecffe5d | 219 | assert_se(streq(s, "")); |
e203f7c3 LP |
220 | b = true; |
221 | } | |
222 | assert_se(b); | |
fecffe5d LP |
223 | |
224 | PATH_FOREACH_PREFIX(s, "") | |
225 | assert_not_reached("wut?"); | |
226 | ||
e203f7c3 LP |
227 | b = false; |
228 | PATH_FOREACH_PREFIX_MORE(s, "") { | |
8d95631e FB |
229 | assert_se(!b); |
230 | assert_se(streq(s, "")); | |
e203f7c3 LP |
231 | b = true; |
232 | } | |
fecffe5d LP |
233 | } |
234 | ||
0c6ea3a4 | 235 | static void test_path_join(void) { |
59ae3a95 TA |
236 | |
237 | #define test_join(root, path, rest, expected) { \ | |
238 | _cleanup_free_ char *z = NULL; \ | |
239 | z = path_join(root, path, rest); \ | |
240 | assert_se(streq(z, expected)); \ | |
241 | } | |
242 | ||
243 | test_join("/root", "/a/b", "/c", "/root/a/b/c"); | |
244 | test_join("/root", "a/b", "c", "/root/a/b/c"); | |
245 | test_join("/root", "/a/b", "c", "/root/a/b/c"); | |
bc854dc7 | 246 | test_join("/root", "/", "c", "/root/c"); |
59ae3a95 TA |
247 | test_join("/root", "/", NULL, "/root/"); |
248 | ||
249 | test_join(NULL, "/a/b", "/c", "/a/b/c"); | |
250 | test_join(NULL, "a/b", "c", "a/b/c"); | |
251 | test_join(NULL, "/a/b", "c", "/a/b/c"); | |
bc854dc7 | 252 | test_join(NULL, "/", "c", "/c"); |
59ae3a95 | 253 | test_join(NULL, "/", NULL, "/"); |
0c6ea3a4 ZJS |
254 | } |
255 | ||
eb66db55 MG |
256 | static void test_fsck_exists(void) { |
257 | /* Ensure we use a sane default for PATH. */ | |
258 | unsetenv("PATH"); | |
259 | ||
260 | /* fsck.minix is provided by util-linux and will probably exist. */ | |
85eca92e | 261 | assert_se(fsck_exists("minix") == 1); |
eb66db55 | 262 | |
85eca92e LP |
263 | assert_se(fsck_exists("AbCdE") == 0); |
264 | assert_se(fsck_exists("/../bin/") == 0); | |
eb66db55 MG |
265 | } |
266 | ||
6b56a651 TK |
267 | static void test_make_relative(void) { |
268 | char *result; | |
269 | ||
270 | assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0); | |
271 | assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0); | |
2a5beb66 | 272 | assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0); |
6b56a651 | 273 | |
59ae3a95 TA |
274 | #define test(from_dir, to_path, expected) { \ |
275 | _cleanup_free_ char *z = NULL; \ | |
276 | path_make_relative(from_dir, to_path, &z); \ | |
277 | assert_se(streq(z, expected)); \ | |
6b56a651 TK |
278 | } |
279 | ||
280 | test("/", "/", "."); | |
281 | test("/", "/some/path", "some/path"); | |
282 | test("/some/path", "/some/path", "."); | |
283 | test("/some/path", "/some/path/in/subdir", "in/subdir"); | |
284 | test("/some/path", "/", "../.."); | |
285 | test("/some/path", "/some/other/path", "../other/path"); | |
2a5beb66 | 286 | test("/some/path/./dot", "/some/further/path", "../../further/path"); |
48054262 | 287 | test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); |
6b56a651 TK |
288 | } |
289 | ||
3e8a78c8 MM |
290 | static void test_strv_resolve(void) { |
291 | char tmp_dir[] = "/tmp/test-path-util-XXXXXX"; | |
292 | _cleanup_strv_free_ char **search_dirs = NULL; | |
293 | _cleanup_strv_free_ char **absolute_dirs = NULL; | |
294 | char **d; | |
295 | ||
296 | assert_se(mkdtemp(tmp_dir) != NULL); | |
297 | ||
298 | search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL); | |
299 | assert_se(search_dirs); | |
300 | STRV_FOREACH(d, search_dirs) { | |
301 | char *p = strappend(tmp_dir, *d); | |
302 | assert_se(p); | |
303 | assert_se(strv_push(&absolute_dirs, p) == 0); | |
304 | } | |
305 | ||
306 | assert_se(mkdir(absolute_dirs[0], 0700) == 0); | |
307 | assert_se(mkdir(absolute_dirs[1], 0700) == 0); | |
308 | assert_se(symlink("dir2", absolute_dirs[2]) == 0); | |
309 | ||
310 | path_strv_resolve(search_dirs, tmp_dir); | |
311 | assert_se(streq(search_dirs[0], "/dir1")); | |
312 | assert_se(streq(search_dirs[1], "/dir2")); | |
313 | assert_se(streq(search_dirs[2], "/dir2")); | |
314 | ||
c6878637 | 315 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
3e8a78c8 MM |
316 | } |
317 | ||
5895b62f | 318 | static void test_path_startswith(void) { |
0470289b ZJS |
319 | const char *p; |
320 | ||
321 | p = path_startswith("/foo/bar/barfoo/", "/foo"); | |
322 | assert_se(streq_ptr(p, "bar/barfoo/")); | |
323 | ||
324 | p = path_startswith("/foo/bar/barfoo/", "/foo/"); | |
325 | assert_se(streq_ptr(p, "bar/barfoo/")); | |
326 | ||
327 | p = path_startswith("/foo/bar/barfoo/", "/"); | |
328 | assert_se(streq_ptr(p, "foo/bar/barfoo/")); | |
329 | ||
330 | p = path_startswith("/foo/bar/barfoo/", "////"); | |
331 | assert_se(streq_ptr(p, "foo/bar/barfoo/")); | |
332 | ||
333 | p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///"); | |
334 | assert_se(streq_ptr(p, "")); | |
335 | ||
336 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////"); | |
337 | assert_se(streq_ptr(p, "")); | |
338 | ||
339 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/"); | |
340 | assert_se(streq_ptr(p, "")); | |
341 | ||
342 | p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/"); | |
343 | assert_se(streq_ptr(p, "")); | |
344 | ||
345 | p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/"); | |
346 | assert_se(streq_ptr(p, "")); | |
347 | ||
348 | p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo"); | |
349 | assert_se(streq_ptr(p, "")); | |
5895b62f RC |
350 | |
351 | assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/")); | |
352 | assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa")); | |
353 | assert_se(!path_startswith("/foo/bar/barfoo/", "")); | |
354 | assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo")); | |
355 | assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/")); | |
356 | } | |
357 | ||
1d13f648 LP |
358 | static void test_prefix_root_one(const char *r, const char *p, const char *expected) { |
359 | _cleanup_free_ char *s = NULL; | |
360 | const char *t; | |
361 | ||
362 | assert_se(s = prefix_root(r, p)); | |
363 | assert_se(streq_ptr(s, expected)); | |
364 | ||
365 | t = prefix_roota(r, p); | |
366 | assert_se(t); | |
367 | assert_se(streq_ptr(t, expected)); | |
368 | } | |
369 | ||
370 | static void test_prefix_root(void) { | |
371 | test_prefix_root_one("/", "/foo", "/foo"); | |
372 | test_prefix_root_one(NULL, "/foo", "/foo"); | |
373 | test_prefix_root_one("", "/foo", "/foo"); | |
374 | test_prefix_root_one("///", "/foo", "/foo"); | |
375 | test_prefix_root_one("/", "////foo", "/foo"); | |
376 | test_prefix_root_one(NULL, "////foo", "/foo"); | |
377 | ||
378 | test_prefix_root_one("/foo", "/bar", "/foo/bar"); | |
379 | test_prefix_root_one("/foo", "bar", "/foo/bar"); | |
380 | test_prefix_root_one("foo", "bar", "foo/bar"); | |
381 | test_prefix_root_one("/foo/", "/bar", "/foo/bar"); | |
382 | test_prefix_root_one("/foo/", "//bar", "/foo/bar"); | |
383 | test_prefix_root_one("/foo///", "//bar", "/foo/bar"); | |
384 | } | |
385 | ||
63292663 RC |
386 | static void test_file_in_same_dir(void) { |
387 | char *t; | |
388 | ||
389 | t = file_in_same_dir("/", "a"); | |
390 | assert_se(streq(t, "/a")); | |
391 | free(t); | |
392 | ||
393 | t = file_in_same_dir("/", "/a"); | |
394 | assert_se(streq(t, "/a")); | |
395 | free(t); | |
396 | ||
397 | t = file_in_same_dir("", "a"); | |
398 | assert_se(streq(t, "a")); | |
399 | free(t); | |
400 | ||
401 | t = file_in_same_dir("a/", "a"); | |
402 | assert_se(streq(t, "a/a")); | |
403 | free(t); | |
404 | ||
405 | t = file_in_same_dir("bar/foo", "bar"); | |
406 | assert_se(streq(t, "bar/bar")); | |
407 | free(t); | |
408 | } | |
409 | ||
b12d25a8 ZJS |
410 | static void test_last_path_component(void) { |
411 | assert_se(streq(last_path_component("a/b/c"), "c")); | |
412 | assert_se(streq(last_path_component("a/b/c/"), "c/")); | |
413 | assert_se(streq(last_path_component("/"), "/")); | |
414 | assert_se(streq(last_path_component("//"), "/")); | |
415 | assert_se(streq(last_path_component("///"), "/")); | |
416 | assert_se(streq(last_path_component("."), ".")); | |
417 | assert_se(streq(last_path_component("./."), ".")); | |
418 | assert_se(streq(last_path_component("././"), "./")); | |
419 | assert_se(streq(last_path_component("././/"), ".//")); | |
420 | assert_se(streq(last_path_component("/foo/a"), "a")); | |
421 | assert_se(streq(last_path_component("/foo/a/"), "a/")); | |
69f9ccf1 | 422 | assert_se(streq(last_path_component(""), "")); |
8460289f LP |
423 | assert_se(streq(last_path_component("a"), "a")); |
424 | assert_se(streq(last_path_component("a/"), "a/")); | |
425 | assert_se(streq(last_path_component("/a"), "a")); | |
426 | assert_se(streq(last_path_component("/a/"), "a/")); | |
b12d25a8 ZJS |
427 | } |
428 | ||
63292663 RC |
429 | static void test_filename_is_valid(void) { |
430 | char foo[FILENAME_MAX+2]; | |
431 | int i; | |
432 | ||
433 | assert_se(!filename_is_valid("")); | |
434 | assert_se(!filename_is_valid("/bar/foo")); | |
435 | assert_se(!filename_is_valid("/")); | |
436 | assert_se(!filename_is_valid(".")); | |
437 | assert_se(!filename_is_valid("..")); | |
438 | ||
439 | for (i=0; i<FILENAME_MAX+1; i++) | |
440 | foo[i] = 'a'; | |
441 | foo[FILENAME_MAX+1] = '\0'; | |
442 | ||
443 | assert_se(!filename_is_valid(foo)); | |
444 | ||
445 | assert_se(filename_is_valid("foo_bar-333")); | |
446 | assert_se(filename_is_valid("o.o")); | |
447 | } | |
448 | ||
b05b9cde ZJS |
449 | static void test_hidden_or_backup_file(void) { |
450 | assert_se(hidden_or_backup_file(".hidden")); | |
451 | assert_se(hidden_or_backup_file("..hidden")); | |
452 | assert_se(!hidden_or_backup_file("hidden.")); | |
453 | ||
454 | assert_se(hidden_or_backup_file("backup~")); | |
455 | assert_se(hidden_or_backup_file(".backup~")); | |
456 | ||
457 | assert_se(hidden_or_backup_file("lost+found")); | |
458 | assert_se(hidden_or_backup_file("aquota.user")); | |
459 | assert_se(hidden_or_backup_file("aquota.group")); | |
460 | ||
461 | assert_se(hidden_or_backup_file("test.rpmnew")); | |
462 | assert_se(hidden_or_backup_file("test.dpkg-old")); | |
463 | assert_se(hidden_or_backup_file("test.dpkg-remove")); | |
464 | assert_se(hidden_or_backup_file("test.swp")); | |
465 | ||
466 | assert_se(!hidden_or_backup_file("test.rpmnew.")); | |
467 | assert_se(!hidden_or_backup_file("test.dpkg-old.foo")); | |
468 | } | |
469 | ||
5a46d55f ZJS |
470 | static void test_systemd_installation_has_version(const char *path) { |
471 | int r; | |
472 | const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999}; | |
473 | unsigned i; | |
474 | ||
475 | for (i = 0; i < ELEMENTSOF(versions); i++) { | |
476 | r = systemd_installation_has_version(path, versions[i]); | |
477 | assert_se(r >= 0); | |
478 | log_info("%s has systemd >= %u: %s", | |
479 | path ?: "Current installation", versions[i], yes_no(r)); | |
480 | } | |
481 | } | |
482 | ||
a119ec7c LP |
483 | static void test_skip_dev_prefix(void) { |
484 | ||
485 | assert_se(streq(skip_dev_prefix("/"), "/")); | |
486 | assert_se(streq(skip_dev_prefix("/dev"), "")); | |
487 | assert_se(streq(skip_dev_prefix("/dev/"), "")); | |
488 | assert_se(streq(skip_dev_prefix("/dev/foo"), "foo")); | |
489 | assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar")); | |
490 | assert_se(streq(skip_dev_prefix("//dev"), "")); | |
491 | assert_se(streq(skip_dev_prefix("//dev//"), "")); | |
492 | assert_se(streq(skip_dev_prefix("/dev///foo"), "foo")); | |
493 | assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar")); | |
494 | assert_se(streq(skip_dev_prefix("//foo"), "//foo")); | |
495 | assert_se(streq(skip_dev_prefix("foo"), "foo")); | |
496 | } | |
497 | ||
57ea45e1 LP |
498 | static void test_empty_or_root(void) { |
499 | assert_se(empty_or_root(NULL)); | |
500 | assert_se(empty_or_root("")); | |
501 | assert_se(empty_or_root("/")); | |
502 | assert_se(empty_or_root("//")); | |
503 | assert_se(empty_or_root("///")); | |
504 | assert_se(empty_or_root("/////////////////")); | |
505 | assert_se(!empty_or_root("xxx")); | |
506 | assert_se(!empty_or_root("/xxx")); | |
507 | assert_se(!empty_or_root("/xxx/")); | |
508 | assert_se(!empty_or_root("//yy//")); | |
509 | } | |
510 | ||
7f076504 | 511 | int main(int argc, char **argv) { |
5a46d55f ZJS |
512 | log_set_max_level(LOG_DEBUG); |
513 | log_parse_environment(); | |
514 | log_open(); | |
515 | ||
76877b46 | 516 | test_path(); |
84e72b5e | 517 | test_path_equal_root(); |
85eca92e | 518 | test_find_binary(argv[0]); |
fecffe5d | 519 | test_prefixes(); |
0c6ea3a4 | 520 | test_path_join(); |
eb66db55 | 521 | test_fsck_exists(); |
6b56a651 | 522 | test_make_relative(); |
3e8a78c8 | 523 | test_strv_resolve(); |
5895b62f | 524 | test_path_startswith(); |
1d13f648 | 525 | test_prefix_root(); |
63292663 | 526 | test_file_in_same_dir(); |
b12d25a8 | 527 | test_last_path_component(); |
63292663 | 528 | test_filename_is_valid(); |
b05b9cde | 529 | test_hidden_or_backup_file(); |
a119ec7c | 530 | test_skip_dev_prefix(); |
57ea45e1 | 531 | test_empty_or_root(); |
5895b62f | 532 | |
5a46d55f ZJS |
533 | test_systemd_installation_has_version(argv[1]); /* NULL is OK */ |
534 | ||
76877b46 ZJS |
535 | return 0; |
536 | } |