summaryrefslogtreecommitdiffstats
path: root/lib/common/tests/nvpair
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common/tests/nvpair')
-rw-r--r--lib/common/tests/nvpair/Makefile.am18
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c50
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c59
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c31
4 files changed, 158 insertions, 0 deletions
diff --git a/lib/common/tests/nvpair/Makefile.am b/lib/common/tests/nvpair/Makefile.am
new file mode 100644
index 0000000..7acaba3
--- /dev/null
+++ b/lib/common/tests/nvpair/Makefile.am
@@ -0,0 +1,18 @@
+#
+# 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 $(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__xe_attr_is_true_test \
+ pcmk__xe_get_bool_attr_test \
+ pcmk__xe_set_bool_attr_test
+
+TESTS = $(check_PROGRAMS)
diff --git a/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c b/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c
new file mode 100644
index 0000000..3723707
--- /dev/null
+++ b/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2021 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 <crm/common/xml_internal.h>
+
+static void
+empty_input(void **state)
+{
+ xmlNode *node = string2xml("<node/>");
+
+ assert_false(pcmk__xe_attr_is_true(NULL, NULL));
+ assert_false(pcmk__xe_attr_is_true(NULL, "whatever"));
+ assert_false(pcmk__xe_attr_is_true(node, NULL));
+
+ free_xml(node);
+}
+
+static void
+attr_missing(void **state)
+{
+ xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
+
+ assert_false(pcmk__xe_attr_is_true(node, "c"));
+ free_xml(node);
+}
+
+static void
+attr_present(void **state)
+{
+ xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
+
+ assert_true(pcmk__xe_attr_is_true(node, "a"));
+ assert_false(pcmk__xe_attr_is_true(node, "b"));
+
+ free_xml(node);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(empty_input),
+ cmocka_unit_test(attr_missing),
+ cmocka_unit_test(attr_present))
diff --git a/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c b/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c
new file mode 100644
index 0000000..500d8a6
--- /dev/null
+++ b/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2021-2023 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 <crm/common/xml_internal.h>
+
+static void
+empty_input(void **state)
+{
+ xmlNode *node = string2xml("<node/>");
+ bool value;
+
+ assert_int_equal(pcmk__xe_get_bool_attr(NULL, NULL, &value), ENODATA);
+ assert_int_equal(pcmk__xe_get_bool_attr(NULL, "whatever", &value), ENODATA);
+ assert_int_equal(pcmk__xe_get_bool_attr(node, NULL, &value), EINVAL);
+ assert_int_equal(pcmk__xe_get_bool_attr(node, "whatever", NULL), EINVAL);
+
+ free_xml(node);
+}
+
+static void
+attr_missing(void **state)
+{
+ xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
+ bool value;
+
+ assert_int_equal(pcmk__xe_get_bool_attr(node, "c", &value), ENODATA);
+ free_xml(node);
+}
+
+static void
+attr_present(void **state)
+{
+ xmlNode *node = string2xml("<node a=\"true\" b=\"false\" c=\"blah\"/>");
+ bool value;
+
+ value = false;
+ assert_int_equal(pcmk__xe_get_bool_attr(node, "a", &value), pcmk_rc_ok);
+ assert_true(value);
+ value = true;
+ assert_int_equal(pcmk__xe_get_bool_attr(node, "b", &value), pcmk_rc_ok);
+ assert_false(value);
+ assert_int_equal(pcmk__xe_get_bool_attr(node, "c", &value), pcmk_rc_bad_input);
+
+ free_xml(node);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(empty_input),
+ cmocka_unit_test(attr_missing),
+ cmocka_unit_test(attr_present))
diff --git a/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c b/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c
new file mode 100644
index 0000000..e1fb9c4
--- /dev/null
+++ b/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2021 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 <crm/common/xml_internal.h>
+#include <crm/msg_xml.h>
+
+static void
+set_attr(void **state)
+{
+ xmlNode *node = string2xml("<node/>");
+
+ pcmk__xe_set_bool_attr(node, "a", true);
+ pcmk__xe_set_bool_attr(node, "b", false);
+
+ assert_string_equal(crm_element_value(node, "a"), XML_BOOLEAN_TRUE);
+ assert_string_equal(crm_element_value(node, "b"), XML_BOOLEAN_FALSE);
+
+ free_xml(node);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(set_attr))