]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
shared: rename path_strv_canonicalize_absolute functions
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
76877b46
ZJS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
a696dbef
ZJS
22#include <stdio.h>
23
76877b46
ZJS
24#include "path-util.h"
25#include "util.h"
26#include "macro.h"
27
28
29static void test_path(void) {
30 assert_se(path_equal("/goo", "/goo"));
31 assert_se(path_equal("//goo", "/goo"));
32 assert_se(path_equal("//goo/////", "/goo"));
33 assert_se(path_equal("goo/////", "goo"));
34
35 assert_se(path_equal("/goo/boo", "/goo//boo"));
36 assert_se(path_equal("//goo/boo", "/goo/boo//"));
37
38 assert_se(path_equal("/", "///"));
39
40 assert_se(!path_equal("/x", "x/"));
41 assert_se(!path_equal("x/", "/"));
42
43 assert_se(!path_equal("/x/./y", "x/y"));
44 assert_se(!path_equal("x/.y", "x/y"));
45
46 assert_se(path_is_absolute("/"));
47 assert_se(!path_is_absolute("./"));
48
49 assert_se(is_path("/dir"));
50 assert_se(is_path("a/b"));
51 assert_se(!is_path("."));
52
2b6bf07d
ZJS
53 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
54 assert_se(streq(basename("/aa///.file"), ".file"));
55 assert_se(streq(basename("/aa///file..."), "file..."));
56 assert_se(streq(basename("file.../"), ""));
76877b46 57
a696dbef 58#define test_parent(x, y) { \
b463b813 59 char _cleanup_free_ *z = NULL; \
a696dbef
ZJS
60 int r = path_get_parent(x, &z); \
61 printf("expected: %s\n", y ? y : "error"); \
62 printf("actual: %s\n", r<0 ? "error" : z); \
63 assert_se((y==NULL) ^ (r==0)); \
64 assert_se(y==NULL || path_equal(z, y)); \
76877b46
ZJS
65 }
66
67 test_parent("./aa/bb/../file.da.", "./aa/bb/..");
68 test_parent("/aa///.file", "/aa///");
69 test_parent("/aa///file...", "/aa///");
a696dbef 70 test_parent("file.../", NULL);
76877b46
ZJS
71
72 assert_se(path_is_mount_point("/", true));
73 assert_se(path_is_mount_point("/", false));
74
75 {
76 char p1[] = "aaa/bbb////ccc";
77 char p2[] = "//aaa/.////ccc";
78 char p3[] = "/./";
79
80 assert(path_equal(path_kill_slashes(p1), "aaa/bbb/ccc"));
81 assert(path_equal(path_kill_slashes(p2), "/aaa/./ccc"));
82 assert(path_equal(path_kill_slashes(p3), "/./"));
83 }
84}
85
7f076504 86static void test_find_binary(const char *self) {
c9d954b2
ZJS
87 char *p;
88
89 assert(find_binary("/bin/sh", &p) == 0);
90 puts(p);
91 assert(streq(p, "/bin/sh"));
92 free(p);
93
7f076504 94 assert(find_binary(self, &p) == 0);
c9d954b2
ZJS
95 puts(p);
96 assert(endswith(p, "/test-path-util"));
97 assert(path_is_absolute(p));
98 free(p);
99
100 assert(find_binary("sh", &p) == 0);
101 puts(p);
102 assert(endswith(p, "/sh"));
103 assert(path_is_absolute(p));
104 free(p);
105
106 assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
b972115c
ZJS
107
108 assert(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
c9d954b2
ZJS
109}
110
fecffe5d 111static void test_prefixes(void) {
e203f7c3
LP
112 static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
113 unsigned i;
fecffe5d 114 char s[PATH_MAX];
e203f7c3 115 bool b;
fecffe5d 116
e203f7c3
LP
117 i = 0;
118 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
119 log_error("---%s---", s);
120 assert_se(streq(s, values[i++]));
121 }
e203f7c3 122 assert_se(values[i] == NULL);
fecffe5d 123
e203f7c3
LP
124 i = 1;
125 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
126 log_error("---%s---", s);
127 assert_se(streq(s, values[i++]));
128 }
fecffe5d
LP
129 assert_se(values[i] == NULL);
130
131 i = 0;
e203f7c3 132 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 133 assert_se(streq(s, values[i++]));
e203f7c3 134 assert_se(values[i] == NULL);
fecffe5d 135
e203f7c3
LP
136 i = 1;
137 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
138 assert_se(streq(s, values[i++]));
fecffe5d
LP
139 assert_se(values[i] == NULL);
140
141 PATH_FOREACH_PREFIX(s, "////")
e203f7c3
LP
142 assert_not_reached("Wut?");
143
144 b = false;
145 PATH_FOREACH_PREFIX_MORE(s, "////") {
146 assert_se(!b);
fecffe5d 147 assert_se(streq(s, ""));
e203f7c3
LP
148 b = true;
149 }
150 assert_se(b);
fecffe5d
LP
151
152 PATH_FOREACH_PREFIX(s, "")
153 assert_not_reached("wut?");
154
e203f7c3
LP
155 b = false;
156 PATH_FOREACH_PREFIX_MORE(s, "") {
157 assert(!b);
158 assert(streq(s, ""));
159 b = true;
160 }
fecffe5d
LP
161}
162
eb66db55
MG
163static void test_fsck_exists(void) {
164 /* Ensure we use a sane default for PATH. */
165 unsetenv("PATH");
166
167 /* fsck.minix is provided by util-linux and will probably exist. */
168 assert_se(fsck_exists("minix") == 0);
169
170 assert_se(fsck_exists("AbCdE") == -ENOENT);
171}
172
6b56a651
TK
173static void test_make_relative(void) {
174 char *result;
175
176 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
177 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
178
179#define test(from_dir, to_path, expected) { \
180 path_make_relative(from_dir, to_path, &result); \
181 assert_se(streq(result, expected)); \
182 free(result); \
183 }
184
185 test("/", "/", ".");
186 test("/", "/some/path", "some/path");
187 test("/some/path", "/some/path", ".");
188 test("/some/path", "/some/path/in/subdir", "in/subdir");
189 test("/some/path", "/", "../..");
190 test("/some/path", "/some/other/path", "../other/path");
191 test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
192}
193
7f076504 194int main(int argc, char **argv) {
76877b46 195 test_path();
7f076504 196 test_find_binary(argv[0]);
fecffe5d 197 test_prefixes();
eb66db55 198 test_fsck_exists();
6b56a651 199 test_make_relative();
76877b46
ZJS
200 return 0;
201}