]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-copy.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / test / test-copy.c
index d70a0be2a22b798052073756645a18c9d02b5195..ad57cb0202f5cd4ae5d1cc0642708330368a96f2 100644 (file)
 
 #include <unistd.h>
 
+#include "alloc-util.h"
 #include "copy.h"
-#include "path-util.h"
+#include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
+#include "macro.h"
 #include "mkdir.h"
+#include "path-util.h"
+#include "rm-rf.h"
+#include "string-util.h"
 #include "strv.h"
-#include "macro.h"
 #include "util.h"
 
 static void test_copy_file(void) {
@@ -42,9 +47,9 @@ static void test_copy_file(void) {
         assert_se(fd >= 0);
         close(fd);
 
-        assert_se(write_string_file(fn, "foo bar bar bar foo") == 0);
+        assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
 
-        assert_se(copy_file(fn, fn_copy, 0, 0644) == 0);
+        assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
 
         assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
         assert_se(streq(buf, "foo bar bar bar foo\n"));
@@ -66,7 +71,7 @@ static void test_copy_file_fd(void) {
         out_fd = mkostemp_safe(out_fn, O_RDWR);
         assert_se(out_fd >= 0);
 
-        assert_se(write_string_file(in_fn, text) == 0);
+        assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
         assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
         assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
         assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
@@ -86,19 +91,19 @@ static void test_copy_tree(void) {
                                  "link2", "dir1/file");
         char **p, **link;
 
-        rm_rf_dangerous(copy_dir, false, true, false);
-        rm_rf_dangerous(original_dir, false, true, false);
+        (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
+        (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
 
         STRV_FOREACH(p, files) {
-                char *f = strappenda(original_dir, *p);
+                char *f = strjoina(original_dir, *p);
 
                 assert_se(mkdir_parents(f, 0755) >= 0);
-                assert_se(write_string_file(f, "file") == 0);
+                assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
         }
 
         STRV_FOREACH_PAIR(link, p, links) {
-                char *f = strappenda(original_dir, *p);
-                char *l = strappenda(original_dir, *link);
+                char *f = strjoina(original_dir, *p);
+                char *l = strjoina(original_dir, *link);
 
                 assert_se(mkdir_parents(l, 0755) >= 0);
                 assert_se(symlink(f, l) == 0);
@@ -109,7 +114,7 @@ static void test_copy_tree(void) {
         STRV_FOREACH(p, files) {
                 _cleanup_free_ char *buf = NULL;
                 size_t sz = 0;
-                char *f = strappenda(copy_dir, *p);
+                char *f = strjoina(copy_dir, *p);
 
                 assert_se(access(f, F_OK) == 0);
                 assert_se(read_full_file(f, &buf, &sz) == 0);
@@ -118,8 +123,8 @@ static void test_copy_tree(void) {
 
         STRV_FOREACH_PAIR(link, p, links) {
                 _cleanup_free_ char *target = NULL;
-                char *f = strappenda(original_dir, *p);
-                char *l = strappenda(copy_dir, *link);
+                char *f = strjoina(original_dir, *p);
+                char *l = strjoina(copy_dir, *link);
 
                 assert_se(readlink_and_canonicalize(l, &target) == 0);
                 assert_se(path_equal(f, target));
@@ -128,14 +133,51 @@ static void test_copy_tree(void) {
         assert_se(copy_tree(original_dir, copy_dir, false) < 0);
         assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
 
-        rm_rf_dangerous(copy_dir, false, true, false);
-        rm_rf_dangerous(original_dir, false, true, false);
+        (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
+        (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
+}
+
+static void test_copy_bytes(void) {
+        _cleanup_close_pair_ int pipefd[2] = {-1, -1};
+        _cleanup_close_ int infd = -1;
+        int r, r2;
+        char buf[1024], buf2[1024];
+
+        infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
+        if (infd < 0)
+                infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
+        assert_se(infd >= 0);
+
+        assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
+
+        r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
+        assert_se(r == 0);
+
+        r = read(pipefd[0], buf, sizeof(buf));
+        assert_se(r >= 0);
+
+        assert_se(lseek(infd, 0, SEEK_SET) == 0);
+        r2 = read(infd, buf2, sizeof(buf2));
+        assert_se(r == r2);
+
+        assert_se(strneq(buf, buf2, r));
+
+        /* test copy_bytes with invalid descriptors */
+        r = copy_bytes(pipefd[0], pipefd[0], 1, false);
+        assert_se(r == -EBADF);
+
+        r = copy_bytes(pipefd[1], pipefd[1], 1, false);
+        assert_se(r == -EBADF);
+
+        r = copy_bytes(pipefd[1], infd, 1, false);
+        assert_se(r == -EBADF);
 }
 
 int main(int argc, char *argv[]) {
         test_copy_file();
         test_copy_file_fd();
         test_copy_tree();
+        test_copy_bytes();
 
         return 0;
 }