diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-09-02 06:41:15 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-09-07 07:08:21 +0200 |
commit | 4e046c5c2fcd743b4af4bb19ce2e7c6d73e898ee (patch) | |
tree | b792050b33b1fd666f176714efceb3beb7e18f9f /src/test/test-fs-util.c | |
parent | mkdir: rewrite mkdir_parents() with path_find_{first,last}_component() (diff) | |
download | systemd-4e046c5c2fcd743b4af4bb19ce2e7c6d73e898ee.tar.xz systemd-4e046c5c2fcd743b4af4bb19ce2e7c6d73e898ee.zip |
fs-util: rewrite rmdir_parents() with path_find_last_component()
Diffstat (limited to 'src/test/test-fs-util.c')
-rw-r--r-- | src/test/test-fs-util.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 08bebcf0e8..ac7ee5d3bf 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -865,6 +865,60 @@ static void test_conservative_rename(void) { assert_se(access(q, F_OK) < 0 && errno == ENOENT); } +static void test_rmdir_parents_one( + const char *prefix, + const char *path, + const char *stop, + int expected, + const char *test_exist, + const char *test_nonexist_subdir) { + + const char *p, *s; + + log_debug("/* %s(%s, %s) */", __func__, path, stop); + + p = strjoina(prefix, path); + s = strjoina(prefix, stop); + + if (expected >= 0) + assert_se(mkdir_parents(p, 0700) >= 0); + + assert_se(rmdir_parents(p, s) == expected); + + if (expected >= 0) { + const char *e, *f; + + e = strjoina(prefix, test_exist); + f = strjoina(e, test_nonexist_subdir); + + assert_se(access(e, F_OK) >= 0); + assert_se(access(f, F_OK) < 0); + } +} + +static void test_rmdir_parents(void) { + char *temp; + + log_info("/* %s */", __func__); + + temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX"); + assert_se(mkdtemp(temp)); + + test_rmdir_parents_one(temp, "/aaa/../hoge/foo", "/hoge/foo", -EINVAL, NULL, NULL); + test_rmdir_parents_one(temp, "/aaa/bbb/ccc", "/hoge/../aaa", -EINVAL, NULL, NULL); + + test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc/ddd", 0, "/aaa/bbb/ccc/ddd", "/eee"); + test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc", 0, "/aaa/bbb/ccc", "/ddd"); + test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb", 0, "/aaa/bbb", "/ccc"); + test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa", 0, "/aaa", "/bbb"); + test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/", 0, "/", "/aaa"); + + test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/hoge/foo", 0, "/aaa", "/bbb"); + test_rmdir_parents_one(temp, "/aaa////bbb/.//ccc//ddd/eee///./.", "///././aaa/.", 0, "/aaa", "/bbb"); + + assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); +} + int main(int argc, char *argv[]) { test_setup_logging(LOG_INFO); @@ -883,6 +937,7 @@ int main(int argc, char *argv[]) { test_rename_noreplace(); test_chmod_and_chown(); test_conservative_rename(); + test_rmdir_parents(); return 0; } |