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