]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-copy.c
basic: spit out chase_symlinks() from fs-util.[ch] → chase-symlinks.[ch]
[thirdparty/systemd.git] / src / test / test-copy.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/xattr.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "chase-symlinks.h"
8 #include "copy.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "hexdecoct.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "mkdir.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "tests.h"
21 #include "tmpfile-util.h"
22 #include "user-util.h"
23 #include "util.h"
24 #include "xattr-util.h"
25
26 static void test_copy_file(void) {
27 _cleanup_free_ char *buf = NULL;
28 char fn[] = "/tmp/test-copy_file.XXXXXX";
29 char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
30 size_t sz = 0;
31 int fd;
32
33 log_info("%s", __func__);
34
35 fd = mkostemp_safe(fn);
36 assert_se(fd >= 0);
37 close(fd);
38
39 fd = mkostemp_safe(fn_copy);
40 assert_se(fd >= 0);
41 close(fd);
42
43 assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
44
45 assert_se(copy_file(fn, fn_copy, 0, 0644, 0, 0, COPY_REFLINK) == 0);
46
47 assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
48 assert_se(streq(buf, "foo bar bar bar foo\n"));
49 assert_se(sz == 20);
50
51 unlink(fn);
52 unlink(fn_copy);
53 }
54
55 static void test_copy_file_fd(void) {
56 char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
57 char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
58 _cleanup_close_ int in_fd = -1, out_fd = -1;
59 const char *text = "boohoo\nfoo\n\tbar\n";
60 char buf[64] = {};
61
62 log_info("%s", __func__);
63
64 in_fd = mkostemp_safe(in_fn);
65 assert_se(in_fd >= 0);
66 out_fd = mkostemp_safe(out_fn);
67 assert_se(out_fd >= 0);
68
69 assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
70 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0);
71 assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
72 assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
73
74 assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text));
75 assert_se(streq(buf, text));
76
77 unlink(in_fn);
78 unlink(out_fn);
79 }
80
81 static void test_copy_tree(void) {
82 char original_dir[] = "/tmp/test-copy_tree/";
83 char copy_dir[] = "/tmp/test-copy_tree-copy/";
84 char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
85 char **symlinks = STRV_MAKE("link", "file",
86 "link2", "dir1/file");
87 char **hardlinks = STRV_MAKE("hlink", "file",
88 "hlink2", "dir1/file");
89 const char *unixsockp;
90 char **p, **ll;
91 struct stat st;
92 int xattr_worked = -1; /* xattr support is optional in temporary directories, hence use it if we can,
93 * but don't fail if we can't */
94
95 log_info("%s", __func__);
96
97 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
98 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
99
100 STRV_FOREACH(p, files) {
101 _cleanup_free_ char *f, *c;
102 int k;
103
104 assert_se(f = path_join(original_dir, *p));
105
106 assert_se(mkdir_parents(f, 0755) >= 0);
107 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
108
109 assert_se(base64mem(*p, strlen(*p), &c) >= 0);
110
111 k = setxattr(f, "user.testxattr", c, strlen(c), 0);
112 assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
113 xattr_worked = k >= 0;
114 }
115
116 STRV_FOREACH_PAIR(ll, p, symlinks) {
117 _cleanup_free_ char *f, *l;
118
119 assert_se(f = path_join(original_dir, *p));
120 assert_se(l = path_join(original_dir, *ll));
121
122 assert_se(mkdir_parents(l, 0755) >= 0);
123 assert_se(symlink(f, l) == 0);
124 }
125
126 STRV_FOREACH_PAIR(ll, p, hardlinks) {
127 _cleanup_free_ char *f, *l;
128
129 assert_se(f = path_join(original_dir, *p));
130 assert_se(l = path_join(original_dir, *ll));
131
132 assert_se(mkdir_parents(l, 0755) >= 0);
133 assert_se(link(f, l) == 0);
134 }
135
136 unixsockp = strjoina(original_dir, "unixsock");
137 assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
138
139 assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS) == 0);
140
141 STRV_FOREACH(p, files) {
142 _cleanup_free_ char *buf, *f, *c = NULL;
143 size_t sz;
144 int k;
145
146 assert_se(f = path_join(copy_dir, *p));
147
148 assert_se(access(f, F_OK) == 0);
149 assert_se(read_full_file(f, &buf, &sz) == 0);
150 assert_se(streq(buf, "file\n"));
151
152 k = getxattr_malloc(f, "user.testxattr", &c, false);
153 assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
154
155 if (k >= 0) {
156 _cleanup_free_ char *d = NULL;
157
158 assert_se(base64mem(*p, strlen(*p), &d) >= 0);
159 assert_se(streq(d, c));
160 }
161 }
162
163 STRV_FOREACH_PAIR(ll, p, symlinks) {
164 _cleanup_free_ char *target, *f, *l;
165
166 assert_se(f = strjoin(original_dir, *p));
167 assert_se(l = strjoin(copy_dir, *ll));
168
169 assert_se(chase_symlinks(l, NULL, 0, &target, NULL) == 1);
170 assert_se(path_equal(f, target));
171 }
172
173 STRV_FOREACH_PAIR(ll, p, hardlinks) {
174 _cleanup_free_ char *f, *l;
175 struct stat a, b;
176
177 assert_se(f = strjoin(copy_dir, *p));
178 assert_se(l = strjoin(copy_dir, *ll));
179
180 assert_se(lstat(f, &a) >= 0);
181 assert_se(lstat(l, &b) >= 0);
182
183 assert_se(a.st_ino == b.st_ino);
184 assert_se(a.st_dev == b.st_dev);
185 }
186
187 unixsockp = strjoina(copy_dir, "unixsock");
188 assert_se(stat(unixsockp, &st) >= 0);
189 assert_se(S_ISSOCK(st.st_mode));
190
191 assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
192 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
193
194 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
195 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
196 }
197
198 static void test_copy_bytes(void) {
199 _cleanup_close_pair_ int pipefd[2] = {-1, -1};
200 _cleanup_close_ int infd = -1;
201 int r, r2;
202 char buf[1024], buf2[1024];
203
204 infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
205 if (infd < 0)
206 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
207 assert_se(infd >= 0);
208
209 assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
210
211 r = copy_bytes(infd, pipefd[1], UINT64_MAX, 0);
212 assert_se(r == 0);
213
214 r = read(pipefd[0], buf, sizeof(buf));
215 assert_se(r >= 0);
216
217 assert_se(lseek(infd, 0, SEEK_SET) == 0);
218 r2 = read(infd, buf2, sizeof(buf2));
219 assert_se(r == r2);
220
221 assert_se(strneq(buf, buf2, r));
222
223 /* test copy_bytes with invalid descriptors */
224 r = copy_bytes(pipefd[0], pipefd[0], 1, 0);
225 assert_se(r == -EBADF);
226
227 r = copy_bytes(pipefd[1], pipefd[1], 1, 0);
228 assert_se(r == -EBADF);
229
230 r = copy_bytes(pipefd[1], infd, 1, 0);
231 assert_se(r == -EBADF);
232 }
233
234 static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint64_t max_bytes) {
235 char fn2[] = "/tmp/test-copy-file-XXXXXX";
236 char fn3[] = "/tmp/test-copy-file-XXXXXX";
237 _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1;
238 int r;
239 struct stat buf, buf2, buf3;
240
241 log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes);
242
243 fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
244 assert_se(fd >= 0);
245
246 fd2 = mkostemp_safe(fn2);
247 assert_se(fd2 >= 0);
248
249 fd3 = mkostemp_safe(fn3);
250 assert_se(fd3 >= 0);
251
252 r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0);
253 if (max_bytes == UINT64_MAX)
254 assert_se(r == 0);
255 else
256 assert_se(IN_SET(r, 0, 1));
257
258 assert_se(fstat(fd, &buf) == 0);
259 assert_se(fstat(fd2, &buf2) == 0);
260 assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
261
262 if (max_bytes < UINT64_MAX)
263 /* Make sure the file is now higher than max_bytes */
264 assert_se(ftruncate(fd2, max_bytes + 1) == 0);
265
266 assert_se(lseek(fd2, 0, SEEK_SET) == 0);
267
268 r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0);
269 if (max_bytes == UINT64_MAX)
270 assert_se(r == 0);
271 else
272 /* We cannot distinguish between the input being exactly max_bytes
273 * or longer than max_bytes (without trying to read one more byte,
274 * or calling stat, or FION_READ, etc, and we don't want to do any
275 * of that). So we expect "truncation" since we know that file we
276 * are copying is exactly max_bytes bytes. */
277 assert_se(r == 1);
278
279 assert_se(fstat(fd3, &buf3) == 0);
280
281 if (max_bytes == UINT64_MAX)
282 assert_se(buf3.st_size == buf2.st_size);
283 else
284 assert_se((uint64_t) buf3.st_size == max_bytes);
285
286 unlink(fn2);
287 unlink(fn3);
288 }
289
290 static void test_copy_atomic(void) {
291 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
292 const char *q;
293 int r;
294
295 assert_se(mkdtemp_malloc(NULL, &p) >= 0);
296
297 q = strjoina(p, "/fstab");
298
299 r = copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK);
300 if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
301 return;
302
303 assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK) == -EEXIST);
304
305 assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0);
306 }
307
308 static void test_copy_proc(void) {
309 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
310 _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL;
311
312 /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */
313
314 assert_se(mkdtemp_malloc(NULL, &p) >= 0);
315 assert_se(f = path_join(p, "version"));
316 assert_se(copy_file("/proc/version", f, 0, MODE_INVALID, 0, 0, 0) >= 0);
317
318 assert_se(read_one_line_file("/proc/version", &a) >= 0);
319 assert_se(read_one_line_file(f, &b) >= 0);
320 assert_se(streq(a, b));
321 assert_se(!isempty(a));
322 }
323
324 int main(int argc, char *argv[]) {
325 test_setup_logging(LOG_DEBUG);
326
327 test_copy_file();
328 test_copy_file_fd();
329 test_copy_tree();
330 test_copy_bytes();
331 test_copy_bytes_regular_file(argv[0], false, UINT64_MAX);
332 test_copy_bytes_regular_file(argv[0], true, UINT64_MAX);
333 test_copy_bytes_regular_file(argv[0], false, 1000); /* smaller than copy buffer size */
334 test_copy_bytes_regular_file(argv[0], true, 1000);
335 test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */
336 test_copy_bytes_regular_file(argv[0], true, 32000);
337 test_copy_atomic();
338 test_copy_proc();
339
340 return 0;
341 }