]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-copy.c
util-lib: split out globbing related calls into glob-util.[ch]
[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 "copy.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_copy_file(void) {
35 _cleanup_free_ char *buf = NULL;
36 char fn[] = "/tmp/test-copy_file.XXXXXX";
37 char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
38 size_t sz = 0;
39 int fd;
40
41 fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
42 assert_se(fd >= 0);
43 close(fd);
44
45 fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
46 assert_se(fd >= 0);
47 close(fd);
48
49 assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
50
51 assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
52
53 assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
54 assert_se(streq(buf, "foo bar bar bar foo\n"));
55 assert_se(sz == 20);
56
57 unlink(fn);
58 unlink(fn_copy);
59 }
60
61 static void test_copy_file_fd(void) {
62 char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
63 char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
64 _cleanup_close_ int in_fd = -1, out_fd = -1;
65 char text[] = "boohoo\nfoo\n\tbar\n";
66 char buf[64] = {0};
67
68 in_fd = mkostemp_safe(in_fn, O_RDWR);
69 assert_se(in_fd >= 0);
70 out_fd = mkostemp_safe(out_fn, O_RDWR);
71 assert_se(out_fd >= 0);
72
73 assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
74 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
75 assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
76 assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
77
78 assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
79 assert_se(streq(buf, text));
80
81 unlink(in_fn);
82 unlink(out_fn);
83 }
84
85 static void test_copy_tree(void) {
86 char original_dir[] = "/tmp/test-copy_tree/";
87 char copy_dir[] = "/tmp/test-copy_tree-copy/";
88 char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
89 char **links = STRV_MAKE("link", "file",
90 "link2", "dir1/file");
91 char **p, **link;
92
93 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
94 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
95
96 STRV_FOREACH(p, files) {
97 char *f = strjoina(original_dir, *p);
98
99 assert_se(mkdir_parents(f, 0755) >= 0);
100 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
101 }
102
103 STRV_FOREACH_PAIR(link, p, links) {
104 char *f = strjoina(original_dir, *p);
105 char *l = strjoina(original_dir, *link);
106
107 assert_se(mkdir_parents(l, 0755) >= 0);
108 assert_se(symlink(f, l) == 0);
109 }
110
111 assert_se(copy_tree(original_dir, copy_dir, true) == 0);
112
113 STRV_FOREACH(p, files) {
114 _cleanup_free_ char *buf = NULL;
115 size_t sz = 0;
116 char *f = strjoina(copy_dir, *p);
117
118 assert_se(access(f, F_OK) == 0);
119 assert_se(read_full_file(f, &buf, &sz) == 0);
120 assert_se(streq(buf, "file\n"));
121 }
122
123 STRV_FOREACH_PAIR(link, p, links) {
124 _cleanup_free_ char *target = NULL;
125 char *f = strjoina(original_dir, *p);
126 char *l = strjoina(copy_dir, *link);
127
128 assert_se(readlink_and_canonicalize(l, &target) == 0);
129 assert_se(path_equal(f, target));
130 }
131
132 assert_se(copy_tree(original_dir, copy_dir, false) < 0);
133 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
134
135 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
136 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
137 }
138
139 static void test_copy_bytes(void) {
140 _cleanup_close_pair_ int pipefd[2] = {-1, -1};
141 _cleanup_close_ int infd = -1;
142 int r, r2;
143 char buf[1024], buf2[1024];
144
145 infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
146 if (infd < 0)
147 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
148 assert_se(infd >= 0);
149
150 assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
151
152 r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
153 assert_se(r == 0);
154
155 r = read(pipefd[0], buf, sizeof(buf));
156 assert_se(r >= 0);
157
158 assert_se(lseek(infd, 0, SEEK_SET) == 0);
159 r2 = read(infd, buf2, sizeof(buf2));
160 assert_se(r == r2);
161
162 assert_se(strneq(buf, buf2, r));
163
164 /* test copy_bytes with invalid descriptors */
165 r = copy_bytes(pipefd[0], pipefd[0], 1, false);
166 assert_se(r == -EBADF);
167
168 r = copy_bytes(pipefd[1], pipefd[1], 1, false);
169 assert_se(r == -EBADF);
170
171 r = copy_bytes(pipefd[1], infd, 1, false);
172 assert_se(r == -EBADF);
173 }
174
175 int main(int argc, char *argv[]) {
176 test_copy_file();
177 test_copy_file_fd();
178 test_copy_tree();
179 test_copy_bytes();
180
181 return 0;
182 }