]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-copy.c
copy: also copy AF_UNIX sockets
[thirdparty/systemd.git] / src / test / test-copy.c
1 /***
2 This file is part of systemd
3
4 Copyright 2014 Ronny Chevalier
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 "copy.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "fs-util.h"
27 #include "log.h"
28 #include "macro.h"
29 #include "mkdir.h"
30 #include "path-util.h"
31 #include "rm-rf.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "util.h"
35
36 static void test_copy_file(void) {
37 _cleanup_free_ char *buf = NULL;
38 char fn[] = "/tmp/test-copy_file.XXXXXX";
39 char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
40 size_t sz = 0;
41 int fd;
42
43 log_info("%s", __func__);
44
45 fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
46 assert_se(fd >= 0);
47 close(fd);
48
49 fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
50 assert_se(fd >= 0);
51 close(fd);
52
53 assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
54
55 assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
56
57 assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
58 assert_se(streq(buf, "foo bar bar bar foo\n"));
59 assert_se(sz == 20);
60
61 unlink(fn);
62 unlink(fn_copy);
63 }
64
65 static void test_copy_file_fd(void) {
66 char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
67 char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
68 _cleanup_close_ int in_fd = -1, out_fd = -1;
69 char text[] = "boohoo\nfoo\n\tbar\n";
70 char buf[64] = {0};
71
72 log_info("%s", __func__);
73
74 in_fd = mkostemp_safe(in_fn, O_RDWR);
75 assert_se(in_fd >= 0);
76 out_fd = mkostemp_safe(out_fn, O_RDWR);
77 assert_se(out_fd >= 0);
78
79 assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
80 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
81 assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
82 assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
83
84 assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
85 assert_se(streq(buf, text));
86
87 unlink(in_fn);
88 unlink(out_fn);
89 }
90
91 static void test_copy_tree(void) {
92 char original_dir[] = "/tmp/test-copy_tree/";
93 char copy_dir[] = "/tmp/test-copy_tree-copy/";
94 char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
95 char **links = STRV_MAKE("link", "file",
96 "link2", "dir1/file");
97 char **p, **link;
98 const char *unixsockp;
99 struct stat st;
100
101 log_info("%s", __func__);
102
103 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
104 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
105
106 STRV_FOREACH(p, files) {
107 char *f = strjoina(original_dir, *p);
108
109 assert_se(mkdir_parents(f, 0755) >= 0);
110 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
111 }
112
113 STRV_FOREACH_PAIR(link, p, links) {
114 char *f = strjoina(original_dir, *p);
115 char *l = strjoina(original_dir, *link);
116
117 assert_se(mkdir_parents(l, 0755) >= 0);
118 assert_se(symlink(f, l) == 0);
119 }
120
121 unixsockp = strjoina(original_dir, "unixsock");
122 assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
123
124 assert_se(copy_tree(original_dir, copy_dir, true) == 0);
125
126 STRV_FOREACH(p, files) {
127 _cleanup_free_ char *buf = NULL;
128 size_t sz = 0;
129 char *f = strjoina(copy_dir, *p);
130
131 assert_se(access(f, F_OK) == 0);
132 assert_se(read_full_file(f, &buf, &sz) == 0);
133 assert_se(streq(buf, "file\n"));
134 }
135
136 STRV_FOREACH_PAIR(link, p, links) {
137 _cleanup_free_ char *target = NULL;
138 char *f = strjoina(original_dir, *p);
139 char *l = strjoina(copy_dir, *link);
140
141 assert_se(readlink_and_canonicalize(l, &target) == 0);
142 assert_se(path_equal(f, target));
143 }
144
145 unixsockp = strjoina(copy_dir, "unixsock");
146 assert_se(stat(unixsockp, &st) >= 0);
147 assert_se(S_ISSOCK(st.st_mode));
148
149 assert_se(copy_tree(original_dir, copy_dir, false) < 0);
150 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
151
152 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
153 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
154 }
155
156 static void test_copy_bytes(void) {
157 _cleanup_close_pair_ int pipefd[2] = {-1, -1};
158 _cleanup_close_ int infd = -1;
159 int r, r2;
160 char buf[1024], buf2[1024];
161
162 infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
163 if (infd < 0)
164 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
165 assert_se(infd >= 0);
166
167 assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
168
169 r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
170 assert_se(r == 0);
171
172 r = read(pipefd[0], buf, sizeof(buf));
173 assert_se(r >= 0);
174
175 assert_se(lseek(infd, 0, SEEK_SET) == 0);
176 r2 = read(infd, buf2, sizeof(buf2));
177 assert_se(r == r2);
178
179 assert_se(strneq(buf, buf2, r));
180
181 /* test copy_bytes with invalid descriptors */
182 r = copy_bytes(pipefd[0], pipefd[0], 1, false);
183 assert_se(r == -EBADF);
184
185 r = copy_bytes(pipefd[1], pipefd[1], 1, false);
186 assert_se(r == -EBADF);
187
188 r = copy_bytes(pipefd[1], infd, 1, false);
189 assert_se(r == -EBADF);
190 }
191
192 static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint64_t max_bytes) {
193 char fn2[] = "/tmp/test-copy-file-XXXXXX";
194 char fn3[] = "/tmp/test-copy-file-XXXXXX";
195 _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1;
196 int r;
197 struct stat buf, buf2, buf3;
198
199 log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes);
200
201 fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
202 assert_se(fd >= 0);
203
204 fd2 = mkostemp_safe(fn2, O_RDWR);
205 assert_se(fd2 >= 0);
206
207 fd3 = mkostemp_safe(fn3, O_WRONLY);
208 assert_se(fd3 >= 0);
209
210 r = copy_bytes(fd, fd2, max_bytes, try_reflink);
211 if (max_bytes == (uint64_t) -1)
212 assert_se(r == 0);
213 else
214 assert_se(IN_SET(r, 0, 1));
215
216 assert_se(lseek(fd2, 0, SEEK_SET) == 0);
217
218 r = copy_bytes(fd2, fd3, max_bytes, try_reflink);
219 if (max_bytes == (uint64_t) -1)
220 assert_se(r == 0);
221 else
222 /* We cannot distinguish between the input being exactly max_bytes
223 * or longer than max_bytes (without trying to read one more byte,
224 * or calling stat, or FION_READ, etc, and we don't want to do any
225 * of that). So we expect "truncation" since we know that file we
226 * are copying is exactly max_bytes bytes. */
227 assert_se(r == 1);
228
229 assert_se(fstat(fd, &buf) == 0);
230 assert_se(fstat(fd2, &buf2) == 0);
231 assert_se(fstat(fd3, &buf3) == 0);
232
233 assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
234 assert_se(buf3.st_size == buf2.st_size);
235
236 unlink(fn2);
237 unlink(fn3);
238 }
239
240 int main(int argc, char *argv[]) {
241 test_copy_file();
242 test_copy_file_fd();
243 test_copy_tree();
244 test_copy_bytes();
245 test_copy_bytes_regular_file(argv[0], false, (uint64_t) -1);
246 test_copy_bytes_regular_file(argv[0], true, (uint64_t) -1);
247 test_copy_bytes_regular_file(argv[0], false, 1000); /* smaller than copy buffer size */
248 test_copy_bytes_regular_file(argv[0], true, 1000);
249 test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */
250 test_copy_bytes_regular_file(argv[0], true, 32000);
251
252 return 0;
253 }