]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add test for copying symlink with copy_tree_at()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 15 Feb 2024 10:00:54 +0000 (19:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 15 Feb 2024 15:25:37 +0000 (00:25 +0900)
src/test/test-copy.c

index 61bfbc0c4212d8cda7081956e07fbb92d9333279..41c7884825b4552a9245335349780e1d25e1d372 100644 (file)
@@ -253,6 +253,43 @@ TEST(copy_tree) {
         (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
 }
 
+TEST(copy_tree_at_symlink) {
+        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
+        _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
+        _cleanup_free_ char *p = NULL, *q = NULL;
+        const char *expect = "hgoehogefoobar";
+
+        tfd = mkdtemp_open(NULL, O_PATH, &t);
+        assert_se(tfd >= 0);
+
+        assert_se(symlinkat(expect, tfd, "from") >= 0);
+
+        assert_se(copy_tree_at(tfd, "from", tfd, "to_1", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0);
+        assert_se(readlinkat_malloc(tfd, "to_1", &p) >= 0);
+        assert_se(streq(p, expect));
+        p = mfree(p);
+
+        assert_se(q = path_join(t, "from"));
+        assert_se(copy_tree_at(AT_FDCWD, q, tfd, "to_2", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0);
+        assert_se(readlinkat_malloc(tfd, "to_2", &p) >= 0);
+        assert_se(streq(p, expect));
+        p = mfree(p);
+        q = mfree(q);
+
+        fd = openat(tfd, "from", O_CLOEXEC | O_PATH | O_NOFOLLOW);
+        assert_se(fd >= 0);
+        assert_se(copy_tree_at(fd, NULL, tfd, "to_3", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0);
+        assert_se(readlinkat_malloc(tfd, "to_3", &p) >= 0);
+        assert_se(streq(p, expect));
+        p = mfree(p);
+
+        assert_se(copy_tree_at(fd, "", tfd, "to_4", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0);
+        assert_se(readlinkat_malloc(tfd, "to_4", &p) >= 0);
+        assert_se(streq(p, expect));
+        p = mfree(p);
+        fd = safe_close(fd);
+}
+
 TEST(copy_bytes) {
         _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
         _cleanup_close_ int infd = -EBADF;