]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fs-util.c
fs-util: add flags parameter to chase_symlinks()
[thirdparty/systemd.git] / src / test / test-fs-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <unistd.h>
21
22 #include "alloc-util.h"
23 #include "fd-util.h"
24 #include "fileio.h"
25 #include "fs-util.h"
26 #include "macro.h"
27 #include "mkdir.h"
28 #include "path-util.h"
29 #include "rm-rf.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "util.h"
33
34 static void test_chase_symlinks(void) {
35 _cleanup_free_ char *result = NULL;
36 char temp[] = "/tmp/test-chase.XXXXXX";
37 const char *top, *p, *q;
38 int r;
39
40 assert_se(mkdtemp(temp));
41
42 top = strjoina(temp, "/top");
43 assert_se(mkdir(top, 0700) >= 0);
44
45 p = strjoina(top, "/dot");
46 assert_se(symlink(".", p) >= 0);
47
48 p = strjoina(top, "/dotdot");
49 assert_se(symlink("..", p) >= 0);
50
51 p = strjoina(top, "/dotdota");
52 assert_se(symlink("../a", p) >= 0);
53
54 p = strjoina(temp, "/a");
55 assert_se(symlink("b", p) >= 0);
56
57 p = strjoina(temp, "/b");
58 assert_se(symlink("/usr", p) >= 0);
59
60 p = strjoina(temp, "/start");
61 assert_se(symlink("top/dot/dotdota", p) >= 0);
62
63 /* Paths that use symlinks underneath the "root" */
64
65 r = chase_symlinks(p, NULL, 0, &result);
66 assert_se(r >= 0);
67 assert_se(path_equal(result, "/usr"));
68
69 result = mfree(result);
70 r = chase_symlinks(p, temp, 0, &result);
71 assert_se(r == -ENOENT);
72
73 q = strjoina(temp, "/usr");
74 assert_se(mkdir(q, 0700) >= 0);
75
76 r = chase_symlinks(p, temp, 0, &result);
77 assert_se(r >= 0);
78 assert_se(path_equal(result, q));
79
80 p = strjoina(temp, "/slash");
81 assert_se(symlink("/", p) >= 0);
82
83 result = mfree(result);
84 r = chase_symlinks(p, NULL, 0, &result);
85 assert_se(r >= 0);
86 assert_se(path_equal(result, "/"));
87
88 result = mfree(result);
89 r = chase_symlinks(p, temp, 0, &result);
90 assert_se(r >= 0);
91 assert_se(path_equal(result, temp));
92
93 /* Paths that would "escape" outside of the "root" */
94
95 p = strjoina(temp, "/6dots");
96 assert_se(symlink("../../..", p) >= 0);
97
98 result = mfree(result);
99 r = chase_symlinks(p, temp, 0, &result);
100 assert_se(r == 0 && path_equal(result, temp));
101
102 p = strjoina(temp, "/6dotsusr");
103 assert_se(symlink("../../../usr", p) >= 0);
104
105 result = mfree(result);
106 r = chase_symlinks(p, temp, 0, &result);
107 assert_se(r == 0 && path_equal(result, q));
108
109 p = strjoina(temp, "/top/8dotsusr");
110 assert_se(symlink("../../../../usr", p) >= 0);
111
112 result = mfree(result);
113 r = chase_symlinks(p, temp, 0, &result);
114 assert_se(r == 0 && path_equal(result, q));
115
116 /* Paths that contain repeated slashes */
117
118 p = strjoina(temp, "/slashslash");
119 assert_se(symlink("///usr///", p) >= 0);
120
121 result = mfree(result);
122 r = chase_symlinks(p, NULL, 0, &result);
123 assert_se(r >= 0);
124 assert_se(path_equal(result, "/usr"));
125
126 result = mfree(result);
127 r = chase_symlinks(p, temp, 0, &result);
128 assert_se(r >= 0);
129 assert_se(path_equal(result, q));
130
131 /* Paths using . */
132
133 result = mfree(result);
134 r = chase_symlinks("/etc/./.././", NULL, 0, &result);
135 assert_se(r >= 0);
136 assert_se(path_equal(result, "/"));
137
138 result = mfree(result);
139 r = chase_symlinks("/etc/./.././", "/etc", 0, &result);
140 assert_se(r == 0 && path_equal(result, "/etc"));
141
142 result = mfree(result);
143 r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
144 assert_se(r == -ENOTDIR);
145
146 /* Path that loops back to self */
147
148 result = mfree(result);
149 p = strjoina(temp, "/recursive-symlink");
150 assert_se(symlink("recursive-symlink", p) >= 0);
151 r = chase_symlinks(p, NULL, 0, &result);
152 assert_se(r == -ELOOP);
153
154 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
155 }
156
157 static void test_unlink_noerrno(void) {
158 char name[] = "/tmp/test-close_nointr.XXXXXX";
159 int fd;
160
161 fd = mkostemp_safe(name);
162 assert_se(fd >= 0);
163 assert_se(close_nointr(fd) >= 0);
164
165 {
166 PROTECT_ERRNO;
167 errno = -42;
168 assert_se(unlink_noerrno(name) >= 0);
169 assert_se(errno == -42);
170 assert_se(unlink_noerrno(name) < 0);
171 assert_se(errno == -42);
172 }
173 }
174
175 static void test_readlink_and_make_absolute(void) {
176 char tempdir[] = "/tmp/test-readlink_and_make_absolute";
177 char name[] = "/tmp/test-readlink_and_make_absolute/original";
178 char name2[] = "test-readlink_and_make_absolute/original";
179 char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
180 char *r = NULL;
181
182 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
183 assert_se(touch(name) >= 0);
184
185 assert_se(symlink(name, name_alias) >= 0);
186 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
187 assert_se(streq(r, name));
188 free(r);
189 assert_se(unlink(name_alias) >= 0);
190
191 assert_se(chdir(tempdir) >= 0);
192 assert_se(symlink(name2, name_alias) >= 0);
193 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
194 assert_se(streq(r, name));
195 free(r);
196 assert_se(unlink(name_alias) >= 0);
197
198 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
199 }
200
201 static void test_get_files_in_directory(void) {
202 _cleanup_strv_free_ char **l = NULL, **t = NULL;
203
204 assert_se(get_files_in_directory("/tmp", &l) >= 0);
205 assert_se(get_files_in_directory(".", &t) >= 0);
206 assert_se(get_files_in_directory(".", NULL) >= 0);
207 }
208
209 static void test_var_tmp(void) {
210 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
211 const char *tmp_dir = NULL, *t;
212
213 t = getenv("TMPDIR");
214 if (t) {
215 tmpdir_backup = strdup(t);
216 assert_se(tmpdir_backup);
217 }
218
219 t = getenv("TEMP");
220 if (t) {
221 temp_backup = strdup(t);
222 assert_se(temp_backup);
223 }
224
225 t = getenv("TMP");
226 if (t) {
227 tmp_backup = strdup(t);
228 assert_se(tmp_backup);
229 }
230
231 assert(unsetenv("TMPDIR") >= 0);
232 assert(unsetenv("TEMP") >= 0);
233 assert(unsetenv("TMP") >= 0);
234
235 assert_se(var_tmp_dir(&tmp_dir) >= 0);
236 assert_se(streq(tmp_dir, "/var/tmp"));
237
238 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
239 assert_se(streq(getenv("TMPDIR"), "/tmp"));
240
241 assert_se(var_tmp_dir(&tmp_dir) >= 0);
242 assert_se(streq(tmp_dir, "/tmp"));
243
244 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
245 assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
246
247 assert_se(var_tmp_dir(&tmp_dir) >= 0);
248 assert_se(streq(tmp_dir, "/var/tmp"));
249
250 if (tmpdir_backup) {
251 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
252 assert_se(streq(getenv("TMPDIR"), tmpdir_backup));
253 }
254
255 if (temp_backup) {
256 assert_se(setenv("TEMP", temp_backup, true) >= 0);
257 assert_se(streq(getenv("TEMP"), temp_backup));
258 }
259
260 if (tmp_backup) {
261 assert_se(setenv("TMP", tmp_backup, true) >= 0);
262 assert_se(streq(getenv("TMP"), tmp_backup));
263 }
264 }
265
266 int main(int argc, char *argv[]) {
267 test_unlink_noerrno();
268 test_readlink_and_make_absolute();
269 test_get_files_in_directory();
270 test_var_tmp();
271 test_chase_symlinks();
272
273 return 0;
274 }