pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__xe_get_bool_attr_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2021 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include "crm/common/results.h"
11 #include <crm_internal.h>
13 
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <setjmp.h>
20 #include <cmocka.h>
21 
22 static void
23 empty_input(void **state)
24 {
25  xmlNode *node = string2xml("<node/>");
26  bool value;
27 
28  assert_int_equal(pcmk__xe_get_bool_attr(NULL, NULL, &value), ENODATA);
29  assert_int_equal(pcmk__xe_get_bool_attr(NULL, "whatever", &value), ENODATA);
30  assert_int_equal(pcmk__xe_get_bool_attr(node, NULL, &value), EINVAL);
31  assert_int_equal(pcmk__xe_get_bool_attr(node, "whatever", NULL), EINVAL);
32 
33  free_xml(node);
34 }
35 
36 static void
37 attr_missing(void **state)
38 {
39  xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
40  bool value;
41 
42  assert_int_equal(pcmk__xe_get_bool_attr(node, "c", &value), ENODATA);
43  free_xml(node);
44 }
45 
46 static void
47 attr_present(void **state)
48 {
49  xmlNode *node = string2xml("<node a=\"true\" b=\"false\" c=\"blah\"/>");
50  bool value;
51 
52  value = false;
53  assert_int_equal(pcmk__xe_get_bool_attr(node, "a", &value), pcmk_rc_ok);
54  assert_true(value);
55  value = true;
56  assert_int_equal(pcmk__xe_get_bool_attr(node, "b", &value), pcmk_rc_ok);
57  assert_false(value);
58  assert_int_equal(pcmk__xe_get_bool_attr(node, "c", &value), pcmk_rc_unknown_format);
59 
60  free_xml(node);
61 }
62 
63 int
64 main(int argc, char **argv)
65 {
66  const struct CMUnitTest tests[] = {
67  cmocka_unit_test(empty_input),
68  cmocka_unit_test(attr_missing),
69  cmocka_unit_test(attr_present),
70  };
71 
72  cmocka_set_message_output(CM_OUTPUT_TAP);
73  return cmocka_run_group_tests(tests, NULL, NULL);
74 }
xmlNode * string2xml(const char *input)
Definition: xml.c:869
int pcmk__xe_get_bool_attr(xmlNodePtr node, const char *name, bool *value)
Definition: nvpair.c:960
int main(int argc, char **argv)
void free_xml(xmlNode *child)
Definition: xml.c:824
Function and executable result codes.
#define ENODATA
Definition: portability.h:145