8d419f
From 010c5dc6b7f18eba37f3df015b3e36b4f28bd559 Mon Sep 17 00:00:00 2001
8d419f
From: Yu Watanabe <watanabe.yu+github@gmail.com>
8d419f
Date: Wed, 2 Feb 2022 15:08:18 +0900
8d419f
Subject: [PATCH] test: add a test for mkdir_p()
8d419f
8d419f
(cherry picked from commit 6f6b017b9bc69df3f3e308c36c95597002ce6e29)
8d419f
8d419f
Related: #2017035
8d419f
---
8d419f
 src/test/meson.build  |  2 ++
8d419f
 src/test/test-mkdir.c | 30 ++++++++++++++++++++++++++++++
8d419f
 2 files changed, 32 insertions(+)
8d419f
 create mode 100644 src/test/test-mkdir.c
8d419f
8d419f
diff --git a/src/test/meson.build b/src/test/meson.build
8d419f
index 75f78e2e1a..14b7939b1f 100644
8d419f
--- a/src/test/meson.build
8d419f
+++ b/src/test/meson.build
8d419f
@@ -193,6 +193,8 @@ tests += [
8d419f
 
8d419f
         [files('test-macro.c')],
8d419f
 
8d419f
+        [files('test-mkdir.c')],
8d419f
+
8d419f
         [files('test-json.c')],
8d419f
 
8d419f
         [files('test-modhex.c')],
8d419f
diff --git a/src/test/test-mkdir.c b/src/test/test-mkdir.c
8d419f
new file mode 100644
8d419f
index 0000000000..c715d5f096
8d419f
--- /dev/null
8d419f
+++ b/src/test/test-mkdir.c
8d419f
@@ -0,0 +1,30 @@
8d419f
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
8d419f
+
8d419f
+#include <unistd.h>
8d419f
+
8d419f
+#include "mkdir.h"
8d419f
+#include "path-util.h"
8d419f
+#include "rm-rf.h"
8d419f
+#include "tests.h"
8d419f
+#include "tmpfile-util.h"
8d419f
+
8d419f
+TEST(mkdir_p) {
8d419f
+        _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
8d419f
+        _cleanup_free_ char *p = NULL;
8d419f
+
8d419f
+        assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);
8d419f
+
8d419f
+        assert_se(p = path_join(tmp, "run"));
8d419f
+        assert_se(mkdir_p(p, 0755) >= 0);
8d419f
+
8d419f
+        p = mfree(p);
8d419f
+        assert_se(p = path_join(tmp, "var/run"));
8d419f
+        assert_se(mkdir_parents(p, 0755) >= 0);
8d419f
+        assert_se(symlink("../run", p) >= 0);
8d419f
+
8d419f
+        p = mfree(p);
8d419f
+        assert_se(p = path_join(tmp, "var/run/hoge/foo/baz"));
8d419f
+        assert_se(mkdir_p(p, 0755) >= 0);
8d419f
+}
8d419f
+
8d419f
+DEFINE_TEST_MAIN(LOG_DEBUG);