summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-03-03 22:58:34 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2023-03-03 22:58:49 +0900
commit31c35de1a43a2fe4a5d3efea7d6258bec1a32a15 (patch)
treef925e20a6a9a07516ce99a6b3bc36e8940b43176 /src/test
parentc1b1492a94b43ca636eb383c3b058feff27ff7b1 (diff)
downloadsystemd-31c35de1a43a2fe4a5d3efea7d6258bec1a32a15.tar.gz
test: add tests for error code propagation from mkdir_xyz()
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-mkdir.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/test/test-mkdir.c b/src/test/test-mkdir.c
index 2ea7257609..24933a3464 100644
--- a/src/test/test-mkdir.c
+++ b/src/test/test-mkdir.c
@@ -2,9 +2,11 @@
#include <unistd.h>
+#include "capability-util.h"
#include "fs-util.h"
#include "mkdir.h"
#include "path-util.h"
+#include "process-util.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "tests.h"
@@ -14,6 +16,7 @@
TEST(mkdir_p_safe) {
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
_cleanup_free_ char *p = NULL, *q = NULL;
+ int r;
assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);
@@ -35,6 +38,12 @@ TEST(mkdir_p_safe) {
assert_se(is_dir(p, false) == 0);
assert_se(is_dir(p, true) > 0);
+ assert_se(mkdir_safe(p, 0755, UID_INVALID, GID_INVALID, 0) == -ENOTDIR);
+ assert_se(mkdir_safe(p, 0755, UID_INVALID, GID_INVALID, MKDIR_IGNORE_EXISTING) >= 0);
+ assert_se(mkdir_safe(p, 0755, UID_INVALID, GID_INVALID, MKDIR_FOLLOW_SYMLINK) >= 0);
+ assert_se(is_dir(p, false) == 0);
+ assert_se(is_dir(p, true) > 0);
+
p = mfree(p);
assert_se(p = path_join(tmp, "var/run/hoge/foo/baz"));
assert_se(mkdir_p_safe(tmp, p, 0755, UID_INVALID, GID_INVALID, 0) >= 0);
@@ -67,6 +76,17 @@ TEST(mkdir_p_safe) {
assert_se(is_dir(q, true) > 0);
assert_se(mkdir_p_safe(tmp, "/tmp/test-mkdir-outside", 0755, UID_INVALID, GID_INVALID, 0) == -ENOTDIR);
+
+ p = mfree(p);
+ assert_se(p = path_join(tmp, "zero-mode/should-fail-to-create-child"));
+ assert_se(mkdir_parents_safe(tmp, p, 0000, UID_INVALID, GID_INVALID, 0) >= 0);
+ r = safe_fork("(test-mkdir-no-cap)", FORK_DEATHSIG | FORK_WAIT | FORK_LOG, NULL);
+ if (r == 0) {
+ (void) capability_bounding_set_drop(0, /* right_now = */ true);
+ assert_se(mkdir_p_safe(tmp, p, 0000, UID_INVALID, GID_INVALID, 0) == -EACCES);
+ _exit(EXIT_SUCCESS);
+ }
+ assert_se(r >= 0);
}
TEST(mkdir_p_root) {