test-copy.c revision 3ffd4af22052963e7a29431721ee204e634bea75
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Ronny Chevalier
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenstatic void test_copy_file(void) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(streq(buf, "foo bar bar bar foo\n"));
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic void test_copy_file_fd(void) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering _cleanup_close_ int in_fd = -1, out_fd = -1;
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic void test_copy_tree(void) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char original_dir[] = "/tmp/test-copy_tree/";
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering char copy_dir[] = "/tmp/test-copy_tree-copy/";
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering assert_se(copy_tree(original_dir, copy_dir, true) == 0);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert_se(readlink_and_canonicalize(l, &target) == 0);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering assert_se(copy_tree(original_dir, copy_dir, false) < 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_copy_bytes(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_close_pair_ int pipefd[2] = {-1, -1};
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert_se(lseek(infd, 0, SEEK_SET) == 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering /* test copy_bytes with invalid descriptors */
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = copy_bytes(pipefd[0], pipefd[0], 1, false);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = copy_bytes(pipefd[1], pipefd[1], 1, false);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = copy_bytes(pipefd[1], infd, 1, false);