]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fs-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
22 #include "alloc-util.h"
28 #include "path-util.h"
30 #include "string-util.h"
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
;
40 assert_se(mkdtemp(temp
));
42 top
= strjoina(temp
, "/top");
43 assert_se(mkdir(top
, 0700) >= 0);
45 p
= strjoina(top
, "/dot");
46 assert_se(symlink(".", p
) >= 0);
48 p
= strjoina(top
, "/dotdot");
49 assert_se(symlink("..", p
) >= 0);
51 p
= strjoina(top
, "/dotdota");
52 assert_se(symlink("../a", p
) >= 0);
54 p
= strjoina(temp
, "/a");
55 assert_se(symlink("b", p
) >= 0);
57 p
= strjoina(temp
, "/b");
58 assert_se(symlink("/usr", p
) >= 0);
60 p
= strjoina(temp
, "/start");
61 assert_se(symlink("top/dot/dotdota", p
) >= 0);
63 /* Paths that use symlinks underneath the "root" */
65 r
= chase_symlinks(p
, NULL
, 0, &result
);
67 assert_se(path_equal(result
, "/usr"));
69 result
= mfree(result
);
70 r
= chase_symlinks(p
, temp
, 0, &result
);
71 assert_se(r
== -ENOENT
);
73 q
= strjoina(temp
, "/usr");
74 assert_se(mkdir(q
, 0700) >= 0);
76 r
= chase_symlinks(p
, temp
, 0, &result
);
78 assert_se(path_equal(result
, q
));
80 p
= strjoina(temp
, "/slash");
81 assert_se(symlink("/", p
) >= 0);
83 result
= mfree(result
);
84 r
= chase_symlinks(p
, NULL
, 0, &result
);
86 assert_se(path_equal(result
, "/"));
88 result
= mfree(result
);
89 r
= chase_symlinks(p
, temp
, 0, &result
);
91 assert_se(path_equal(result
, temp
));
93 /* Paths that would "escape" outside of the "root" */
95 p
= strjoina(temp
, "/6dots");
96 assert_se(symlink("../../..", p
) >= 0);
98 result
= mfree(result
);
99 r
= chase_symlinks(p
, temp
, 0, &result
);
100 assert_se(r
== 0 && path_equal(result
, temp
));
102 p
= strjoina(temp
, "/6dotsusr");
103 assert_se(symlink("../../../usr", p
) >= 0);
105 result
= mfree(result
);
106 r
= chase_symlinks(p
, temp
, 0, &result
);
107 assert_se(r
== 0 && path_equal(result
, q
));
109 p
= strjoina(temp
, "/top/8dotsusr");
110 assert_se(symlink("../../../../usr", p
) >= 0);
112 result
= mfree(result
);
113 r
= chase_symlinks(p
, temp
, 0, &result
);
114 assert_se(r
== 0 && path_equal(result
, q
));
116 /* Paths that contain repeated slashes */
118 p
= strjoina(temp
, "/slashslash");
119 assert_se(symlink("///usr///", p
) >= 0);
121 result
= mfree(result
);
122 r
= chase_symlinks(p
, NULL
, 0, &result
);
124 assert_se(path_equal(result
, "/usr"));
126 result
= mfree(result
);
127 r
= chase_symlinks(p
, temp
, 0, &result
);
129 assert_se(path_equal(result
, q
));
133 result
= mfree(result
);
134 r
= chase_symlinks("/etc/./.././", NULL
, 0, &result
);
136 assert_se(path_equal(result
, "/"));
138 result
= mfree(result
);
139 r
= chase_symlinks("/etc/./.././", "/etc", 0, &result
);
140 assert_se(r
== 0 && path_equal(result
, "/etc"));
142 result
= mfree(result
);
143 r
= chase_symlinks("/etc/machine-id/foo", NULL
, 0, &result
);
144 assert_se(r
== -ENOTDIR
);
146 /* Path that loops back to self */
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
);
154 assert_se(rm_rf(temp
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
157 static void test_unlink_noerrno(void) {
158 char name
[] = "/tmp/test-close_nointr.XXXXXX";
161 fd
= mkostemp_safe(name
);
163 assert_se(close_nointr(fd
) >= 0);
168 assert_se(unlink_noerrno(name
) >= 0);
169 assert_se(errno
== -42);
170 assert_se(unlink_noerrno(name
) < 0);
171 assert_se(errno
== -42);
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";
182 assert_se(mkdir_safe(tempdir
, 0755, getuid(), getgid()) >= 0);
183 assert_se(touch(name
) >= 0);
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
));
189 assert_se(unlink(name_alias
) >= 0);
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
));
196 assert_se(unlink(name_alias
) >= 0);
198 assert_se(rm_rf(tempdir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
201 static void test_get_files_in_directory(void) {
202 _cleanup_strv_free_
char **l
= NULL
, **t
= NULL
;
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);
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
;
213 t
= getenv("TMPDIR");
215 tmpdir_backup
= strdup(t
);
216 assert_se(tmpdir_backup
);
221 temp_backup
= strdup(t
);
222 assert_se(temp_backup
);
227 tmp_backup
= strdup(t
);
228 assert_se(tmp_backup
);
231 assert(unsetenv("TMPDIR") >= 0);
232 assert(unsetenv("TEMP") >= 0);
233 assert(unsetenv("TMP") >= 0);
235 assert_se(var_tmp_dir(&tmp_dir
) >= 0);
236 assert_se(streq(tmp_dir
, "/var/tmp"));
238 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
239 assert_se(streq(getenv("TMPDIR"), "/tmp"));
241 assert_se(var_tmp_dir(&tmp_dir
) >= 0);
242 assert_se(streq(tmp_dir
, "/tmp"));
244 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
245 assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
247 assert_se(var_tmp_dir(&tmp_dir
) >= 0);
248 assert_se(streq(tmp_dir
, "/var/tmp"));
251 assert_se(setenv("TMPDIR", tmpdir_backup
, true) >= 0);
252 assert_se(streq(getenv("TMPDIR"), tmpdir_backup
));
256 assert_se(setenv("TEMP", temp_backup
, true) >= 0);
257 assert_se(streq(getenv("TEMP"), temp_backup
));
261 assert_se(setenv("TMP", tmp_backup
, true) >= 0);
262 assert_se(streq(getenv("TMP"), tmp_backup
));
266 int main(int argc
, char *argv
[]) {
267 test_unlink_noerrno();
268 test_readlink_and_make_absolute();
269 test_get_files_in_directory();
271 test_chase_symlinks();