From 12ef261794714c5db45e7bbb539e4cc3e1110143 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 14 Mar 2023 14:03:28 +0100 Subject: chase-symlinks: Add more chase_symlinks_at() helpers Copies of the corresponding chase_symlinks() helpers. --- src/test/test-fs-util.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/test') diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 69c43485f1..ce61f28017 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -5,6 +5,7 @@ #include "alloc-util.h" #include "chase-symlinks.h" #include "copy.h" +#include "dirent-util.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" @@ -434,6 +435,9 @@ TEST(chase_symlinks_at) { _cleanup_(rm_rf_physical_and_freep) char *t = NULL; _cleanup_close_ int tfd = -EBADF, fd = -EBADF; _cleanup_free_ char *result = NULL; + _cleanup_closedir_ DIR *dir = NULL; + _cleanup_fclose_ FILE *f = NULL; + struct stat st; const char *p; assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); @@ -550,6 +554,42 @@ TEST(chase_symlinks_at) { assert_se(fd >= 0); assert_se(fd_verify_directory(fd) >= 0); fd = safe_close(fd); + + /* Test chase_symlinks_at_and_opendir() */ + + assert_se(chase_symlinks_at_and_opendir(tfd, "o/p/e/n/d/i", 0, &result, &dir) >= 0); + FOREACH_DIRENT(de, dir, assert_not_reached()) + assert_se(streq(de->d_name, "r")); + assert_se(streq(result, "o/p/e/n/d/i")); + result = mfree(result); + + /* Test chase_symlinks_at_and_stat() */ + + assert_se(chase_symlinks_at_and_stat(tfd, "o/p", 0, &result, &st) >= 0); + assert_se(stat_verify_directory(&st) >= 0); + assert_se(streq(result, "o/p")); + result = mfree(result); + + /* Test chase_symlinks_at_and_access() */ + + assert_se(chase_symlinks_at_and_access(tfd, "o/p/e", 0, F_OK, &result) >= 0); + assert_se(streq(result, "o/p/e")); + result = mfree(result); + + /* Test chase_symlinks_at_and_fopen_unlocked() */ + + assert_se(chase_symlinks_at_and_fopen_unlocked(tfd, "o/p/e/n/f/i/l/e", 0, "re", &result, &f) >= 0); + assert_se(fread(&(char[1]) {}, 1, 1, f) == 0); + assert_se(feof(f)); + f = safe_fclose(f); + assert_se(streq(result, "o/p/e/n/f/i/l/e")); + result = mfree(result); + + /* Test chase_symlinks_at_and_unlink() */ + + assert_se(chase_symlinks_at_and_unlink(tfd, "o/p/e/n/f/i/l/e", 0, 0, &result) >= 0); + assert_se(streq(result, "o/p/e/n/f/i/l/e")); + result = mfree(result); } TEST(readlink_and_make_absolute) { -- cgit v1.2.3