From: Daan De Meyer Date: Mon, 26 Sep 2022 20:33:23 +0000 (+0200) Subject: test-copy: Rework test_copy_tree_replace_dirs() to use new openat() helpers X-Git-Tag: v253-rc1~561^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dcb3d0934c39af35b5577cdd23244c15a89795de;p=thirdparty%2Fsystemd.git test-copy: Rework test_copy_tree_replace_dirs() to use new openat() helpers --- diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 4091b425d16..02709a4d070 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -50,11 +50,11 @@ TEST(copy_file) { unlink(fn_copy); } -static bool read_file_and_streq(const char* filepath, const char* expected_contents) { +static bool read_file_at_and_streq(int dir_fd, const char *path, const char *expected) { _cleanup_free_ char *buf = NULL; - assert_se(read_full_file(filepath, &buf, NULL) == 0); - return streq(buf, expected_contents); + assert_se(read_full_file_at(dir_fd, path, &buf, NULL) == 0); + return streq(buf, expected); } TEST(copy_tree_replace_file) { @@ -70,53 +70,41 @@ TEST(copy_tree_replace_file) { assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST); - assert_se(read_file_and_streq(dst, "foo foo foo\n")); + assert_se(read_file_at_and_streq(AT_FDCWD, dst, "foo foo foo\n")); assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE) == 0); - assert_se(read_file_and_streq(dst, "bar bar\n")); + assert_se(read_file_at_and_streq(AT_FDCWD, dst, "bar bar\n")); } TEST(copy_tree_replace_dirs) { - _cleanup_free_ char *src_path1 = NULL, *src_path2 = NULL, *dst_path1 = NULL, *dst_path2 = NULL; - _cleanup_(rm_rf_physical_and_freep) char *src_directory = NULL, *dst_directory = NULL; - const char *file1 = "foo_file", *file2 = "bar_file"; + _cleanup_(rm_rf_physical_and_closep) int src = -1, dst = -1; /* Create the random source/destination directories */ - assert_se(mkdtemp_malloc("/tmp/dirXXXXXX", &src_directory) >= 0); - assert_se(mkdtemp_malloc("/tmp/dirXXXXXX", &dst_directory) >= 0); - - /* Construct the source/destination filepaths (should have different dir name, but same file names within) */ - assert_se(src_path1 = path_join(src_directory, file1)); - assert_se(src_path2 = path_join(src_directory, file2)); - assert_se(dst_path1 = path_join(dst_directory, file1)); - assert_se(dst_path2 = path_join(dst_directory, file2)); + assert_se((src = mkdtemp_open(NULL, 0, NULL)) >= 0); + assert_se((dst = mkdtemp_open(NULL, 0, NULL)) >= 0); /* Populate some data to differentiate the files. */ - assert_se(write_string_file(src_path1, "src file 1", WRITE_STRING_FILE_CREATE) == 0); - assert_se(write_string_file(src_path2, "src file 2", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file_at(src, "foo", "src file 1", WRITE_STRING_FILE_CREATE) >= 0); + assert_se(write_string_file_at(src, "bar", "src file 2", WRITE_STRING_FILE_CREATE) == 0); - assert_se(write_string_file(dst_path1, "dest file 1", WRITE_STRING_FILE_CREATE) == 0); - assert_se(write_string_file(dst_path2, "dest file 2", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file_at(dst, "foo", "dest file 1", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file_at(dst, "bar", "dest file 2", WRITE_STRING_FILE_CREATE) == 0); /* Copying without COPY_REPLACE should fail because the destination file already exists. */ - assert_se(copy_tree(src_directory, dst_directory, UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST); + assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST); - { - assert_se(read_file_and_streq(src_path1, "src file 1\n")); - assert_se(read_file_and_streq(src_path2, "src file 2\n")); - assert_se(read_file_and_streq(dst_path1, "dest file 1\n")); - assert_se(read_file_and_streq(dst_path2, "dest file 2\n")); - } + assert_se(read_file_at_and_streq(src, "foo", "src file 1\n")); + assert_se(read_file_at_and_streq(src, "bar", "src file 2\n")); + assert_se(read_file_at_and_streq(dst, "foo", "dest file 1\n")); + assert_se(read_file_at_and_streq(dst, "bar", "dest file 2\n")); - assert_se(copy_tree(src_directory, dst_directory, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE) == 0); + assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE) == 0); - { - assert_se(read_file_and_streq(src_path1, "src file 1\n")); - assert_se(read_file_and_streq(src_path2, "src file 2\n")); - assert_se(read_file_and_streq(dst_path1, "src file 1\n")); - assert_se(read_file_and_streq(dst_path2, "src file 2\n")); - } + assert_se(read_file_at_and_streq(src, "foo", "src file 1\n")); + assert_se(read_file_at_and_streq(src, "bar", "src file 2\n")); + assert_se(read_file_at_and_streq(dst, "foo", "src file 1\n")); + assert_se(read_file_at_and_streq(dst, "bar", "src file 2\n")); } TEST(copy_file_fd) {