pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__xe_get_flags_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2024 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 General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <stdint.h> // UINT32_MAX
13 #include <libxml/tree.h>
14 
16 
17 #include <crm/common/iso8601.h>
18 #include <crm/common/xml.h>
20 
21 #define DEFAULT_VALUE 0xfff
22 
23 static void
24 assert_flags(const char *value, int expected_rc, unsigned int expected_flags)
25 {
26  int rc = pcmk_rc_ok;
27  uint32_t flags = 0U;
28  xmlNode *xml = pcmk__xe_create(NULL, "element");
29 
30  assert_non_null(xml);
31  crm_xml_add(xml, "attribute", value);
32 
33  // Without output argument
34  assert_int_equal(pcmk__xe_get_flags(xml, "attribute", NULL, DEFAULT_VALUE),
35  expected_rc);
36 
37  // With output argument
38  rc = pcmk__xe_get_flags(xml, "attribute", &flags, DEFAULT_VALUE);
39  assert_int_equal(rc, expected_rc);
40  assert_true(flags == expected_flags);
41 
42  free_xml(xml);
43 }
44 
45 static void
46 null_name_invalid(void **state)
47 {
48  int rc = pcmk_rc_ok;
49  uint32_t flags = 0U;
50  xmlNode *xml = pcmk__xe_create(NULL, "element");
51 
52  assert_non_null(xml);
53 
54  assert_int_equal(pcmk__xe_get_flags(NULL, NULL, NULL, DEFAULT_VALUE),
55  EINVAL);
56 
57  assert_int_equal(pcmk__xe_get_flags(xml, NULL, NULL, DEFAULT_VALUE),
58  EINVAL);
59 
60  rc = pcmk__xe_get_flags(xml, NULL, &flags, DEFAULT_VALUE);
61  assert_int_equal(rc, EINVAL);
62  assert_true(flags == DEFAULT_VALUE);
63 
64  flags = 0U;
65  rc = pcmk__xe_get_flags(NULL, NULL, &flags, DEFAULT_VALUE);
66  assert_int_equal(rc, EINVAL);
67  assert_true(flags == DEFAULT_VALUE);
68 
69  free_xml(xml);
70 }
71 
72 static void
73 null_xml_default(void **state)
74 {
75  int rc = pcmk_rc_ok;
76  uint32_t flags = 0U;
77 
78  assert_int_equal(pcmk__xe_get_flags(NULL, "attribute", NULL, DEFAULT_VALUE),
79  pcmk_rc_ok);
80 
81  rc = pcmk__xe_get_flags(NULL, "attribute", &flags, DEFAULT_VALUE);
82  assert_int_equal(rc, pcmk_rc_ok);
83  assert_true(flags == DEFAULT_VALUE);
84 }
85 
86 static void
87 no_attr_default(void **state)
88 {
89  assert_flags(NULL, pcmk_rc_ok, DEFAULT_VALUE);
90 }
91 
92 static void
93 invalid_attr_default(void **state)
94 {
95  char *too_big = crm_strdup_printf("%lld", UINT32_MAX + 1LL);
96 
97  assert_flags("x", EINVAL, DEFAULT_VALUE);
98  assert_flags("-1", pcmk_rc_bad_input, DEFAULT_VALUE);
99  assert_flags(too_big, pcmk_rc_bad_input, DEFAULT_VALUE);
100  free(too_big);
101 }
102 
103 static void
104 valid_attr(void **state)
105 {
106  assert_flags("0", pcmk_rc_ok, 0x0);
107  assert_flags("15", pcmk_rc_ok, 0x0f);
108  assert_flags("61462", pcmk_rc_ok, 0xf016);
109  assert_flags("4294967295", pcmk_rc_ok, 0xffffffff);
110 }
111 
113  cmocka_unit_test(null_name_invalid),
114  cmocka_unit_test(null_xml_default),
115  cmocka_unit_test(no_attr_default),
116  cmocka_unit_test(invalid_attr_default),
117  cmocka_unit_test(valid_attr))
#define DEFAULT_VALUE
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:313
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
ISO_8601 Date handling.
void free_xml(xmlNode *child)
Definition: xml.c:958
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
int pcmk__xe_get_flags(const xmlNode *xml, const char *name, uint32_t *dest, uint32_t default_value)
Retrieve a flag group from an XML attribute value.
Definition: nvpair.c:535
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml.c:770
uint64_t flags
Definition: remote.c:215