chiark / gitweb /
sd-bus: reuse the KDBUS_CMD_FREE wrapper wherever appropriate
[elogind.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 "path-util.h"
24 #include "fileio.h"
25 #include "mkdir.h"
26 #include "strv.h"
27 #include "macro.h"
28 #include "util.h"
29
30 static void test_copy_file(void) {
31         _cleanup_free_ char *buf = NULL;
32         char fn[] = "/tmp/test-copy_file.XXXXXX";
33         char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
34         size_t sz = 0;
35         int fd;
36
37         fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
38         assert_se(fd >= 0);
39         close(fd);
40
41         fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
42         assert_se(fd >= 0);
43         close(fd);
44
45         assert_se(write_string_file(fn, "foo bar bar bar foo") == 0);
46
47         assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
48
49         assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
50         assert_se(streq(buf, "foo bar bar bar foo\n"));
51         assert_se(sz == 20);
52
53         unlink(fn);
54         unlink(fn_copy);
55 }
56
57 static void test_copy_file_fd(void) {
58         char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
59         char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
60         _cleanup_close_ int in_fd = -1, out_fd = -1;
61         char text[] = "boohoo\nfoo\n\tbar\n";
62         char buf[64] = {0};
63
64         in_fd = mkostemp_safe(in_fn, O_RDWR);
65         assert_se(in_fd >= 0);
66         out_fd = mkostemp_safe(out_fn, O_RDWR);
67         assert_se(out_fd >= 0);
68
69         assert_se(write_string_file(in_fn, text) == 0);
70         assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
71         assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
72         assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
73
74         assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
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 **links = STRV_MAKE("link", "file",
86                                  "link2", "dir1/file");
87         char **p, **link;
88
89         rm_rf_dangerous(copy_dir, false, true, false);
90         rm_rf_dangerous(original_dir, false, true, false);
91
92         STRV_FOREACH(p, files) {
93                 char *f = strappenda(original_dir, *p);
94
95                 assert_se(mkdir_parents(f, 0755) >= 0);
96                 assert_se(write_string_file(f, "file") == 0);
97         }
98
99         STRV_FOREACH_PAIR(link, p, links) {
100                 char *f = strappenda(original_dir, *p);
101                 char *l = strappenda(original_dir, *link);
102
103                 assert_se(mkdir_parents(l, 0755) >= 0);
104                 assert_se(symlink(f, l) == 0);
105         }
106
107         assert_se(copy_tree(original_dir, copy_dir, true) == 0);
108
109         STRV_FOREACH(p, files) {
110                 _cleanup_free_ char *buf = NULL;
111                 size_t sz = 0;
112                 char *f = strappenda(copy_dir, *p);
113
114                 assert_se(access(f, F_OK) == 0);
115                 assert_se(read_full_file(f, &buf, &sz) == 0);
116                 assert_se(streq(buf, "file\n"));
117         }
118
119         STRV_FOREACH_PAIR(link, p, links) {
120                 _cleanup_free_ char *target = NULL;
121                 char *f = strappenda(original_dir, *p);
122                 char *l = strappenda(copy_dir, *link);
123
124                 assert_se(readlink_and_canonicalize(l, &target) == 0);
125                 assert_se(path_equal(f, target));
126         }
127
128         assert_se(copy_tree(original_dir, copy_dir, false) < 0);
129         assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
130
131         rm_rf_dangerous(copy_dir, false, true, false);
132         rm_rf_dangerous(original_dir, false, true, false);
133 }
134
135 int main(int argc, char *argv[]) {
136         test_copy_file();
137         test_copy_file_fd();
138         test_copy_tree();
139
140         return 0;
141 }