summaryrefslogtreecommitdiffstats
path: root/lib/common/tests/io
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common/tests/io')
-rw-r--r--lib/common/tests/io/Makefile.am18
-rw-r--r--lib/common/tests/io/pcmk__full_path_test.c52
-rw-r--r--lib/common/tests/io/pcmk__get_tmpdir_test.c68
3 files changed, 138 insertions, 0 deletions
diff --git a/lib/common/tests/io/Makefile.am b/lib/common/tests/io/Makefile.am
new file mode 100644
index 0000000..c26482c
--- /dev/null
+++ b/lib/common/tests/io/Makefile.am
@@ -0,0 +1,18 @@
+#
+# Copyright 2020-2022 the Pacemaker project contributors
+#
+# The version control history for this file may have further details.
+#
+# This source code is licensed under the GNU General Public License version 2
+# or later (GPLv2+) WITHOUT ANY WARRANTY.
+#
+
+include $(top_srcdir)/mk/tap.mk
+include $(top_srcdir)/mk/unittest.mk
+
+# Add "_test" to the end of all test program names to simplify .gitignore.
+check_PROGRAMS = \
+ pcmk__full_path_test \
+ pcmk__get_tmpdir_test
+
+TESTS = $(check_PROGRAMS)
diff --git a/lib/common/tests/io/pcmk__full_path_test.c b/lib/common/tests/io/pcmk__full_path_test.c
new file mode 100644
index 0000000..dbbd71b
--- /dev/null
+++ b/lib/common/tests/io/pcmk__full_path_test.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2020-2022 the Pacemaker project contributors
+ *
+ * The version control history for this file may have further details.
+ *
+ * This source code is licensed under the GNU General Public License version 2
+ * or later (GPLv2+) WITHOUT ANY WARRANTY.
+ */
+
+#include <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+
+#include "mock_private.h"
+
+static void
+function_asserts(void **state)
+{
+ pcmk__assert_asserts(pcmk__full_path(NULL, "/dir"));
+ pcmk__assert_asserts(pcmk__full_path("file", NULL));
+
+ pcmk__assert_asserts(
+ {
+ pcmk__mock_strdup = true; // strdup() will return NULL
+ expect_string(__wrap_strdup, s, "/full/path");
+ pcmk__full_path("/full/path", "/dir");
+ pcmk__mock_strdup = false; // Use real strdup()
+ }
+ );
+}
+
+static void
+full_path(void **state)
+{
+ char *path = NULL;
+
+ path = pcmk__full_path("file", "/dir");
+ assert_int_equal(strcmp(path, "/dir/file"), 0);
+ free(path);
+
+ path = pcmk__full_path("/full/path", "/dir");
+ assert_int_equal(strcmp(path, "/full/path"), 0);
+ free(path);
+
+ path = pcmk__full_path("../relative/path", "/dir");
+ assert_int_equal(strcmp(path, "/dir/../relative/path"), 0);
+ free(path);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(function_asserts),
+ cmocka_unit_test(full_path))
diff --git a/lib/common/tests/io/pcmk__get_tmpdir_test.c b/lib/common/tests/io/pcmk__get_tmpdir_test.c
new file mode 100644
index 0000000..bc17513
--- /dev/null
+++ b/lib/common/tests/io/pcmk__get_tmpdir_test.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2021-2022 the Pacemaker project contributors
+ *
+ * The version control history for this file may have further details.
+ *
+ * This source code is licensed under the GNU General Public License version 2
+ * or later (GPLv2+) WITHOUT ANY WARRANTY.
+ */
+
+#include <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+
+#include "mock_private.h"
+
+static void
+getenv_returns_invalid(void **state)
+{
+ const char *result;
+
+ pcmk__mock_getenv = true;
+
+ expect_string(__wrap_getenv, name, "TMPDIR");
+ will_return(__wrap_getenv, NULL); // getenv("TMPDIR") return value
+ result = pcmk__get_tmpdir();
+ assert_string_equal(result, "/tmp");
+
+ expect_string(__wrap_getenv, name, "TMPDIR");
+ will_return(__wrap_getenv, ""); // getenv("TMPDIR") return value
+ result = pcmk__get_tmpdir();
+ assert_string_equal(result, "/tmp");
+
+ expect_string(__wrap_getenv, name, "TMPDIR");
+ will_return(__wrap_getenv, "subpath"); // getenv("TMPDIR") return value
+ result = pcmk__get_tmpdir();
+ assert_string_equal(result, "/tmp");
+
+ pcmk__mock_getenv = false;
+}
+
+static void
+getenv_returns_valid(void **state)
+{
+ const char *result;
+
+ pcmk__mock_getenv = true;
+
+ expect_string(__wrap_getenv, name, "TMPDIR");
+ will_return(__wrap_getenv, "/var/tmp"); // getenv("TMPDIR") return value
+ result = pcmk__get_tmpdir();
+ assert_string_equal(result, "/var/tmp");
+
+ expect_string(__wrap_getenv, name, "TMPDIR");
+ will_return(__wrap_getenv, "/"); // getenv("TMPDIR") return value
+ result = pcmk__get_tmpdir();
+ assert_string_equal(result, "/");
+
+ expect_string(__wrap_getenv, name, "TMPDIR");
+ will_return(__wrap_getenv, "/tmp/abcd.1234"); // getenv("TMPDIR") return value
+ result = pcmk__get_tmpdir();
+ assert_string_equal(result, "/tmp/abcd.1234");
+
+ pcmk__mock_getenv = false;
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(getenv_returns_invalid),
+ cmocka_unit_test(getenv_returns_valid))