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