pacemaker  3.0.0-d8340737c4
Scalable High-Availability cluster resource manager
pcmk__xe_set_id_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 
14 
15 static void
16 null_node(void **state)
17 {
18  pcmk__xe_set_id(NULL, "test_id");
19 }
20 
21 static void
22 null_format(void **state)
23 {
24  xmlNode *test_xml = pcmk__xe_create(NULL, "test");
25 
27  pcmk__assert_asserts(pcmk__xe_set_id(test_xml, NULL));
28 
29 #ifdef HAVE_FORMAT_NONLITERAL
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wformat-zero-length"
32 #endif // HAVE_FORMAT_NONLITERAL
34 #ifdef HAVE_FORMAT_NONLITERAL
35 #pragma GCC diagnostic pop
36 #endif // HAVE_FORMAT_NONLITERAL
37 
38  pcmk__xml_free(test_xml);
39 }
40 
41 static void
42 valid_id(void **state)
43 {
44  // IDs that are already valid XML names
45  xmlNode *test_xml = pcmk__xe_create(NULL, "test");
46  const char *id = NULL;
47 
48  // No ID set initially
49  pcmk__xe_set_id(test_xml, "test_id");
50  assert_string_equal(pcmk__xe_id(test_xml), "test_id");
51 
52  // ID already set: test overwrite
53  // #xEFFFF (NameStartChar), #xB7 (NameChar)
54  id = "\xF3\xAF\xBF\xBF" "\xC2\xB7";
55  pcmk__xe_set_id(test_xml, "%s", id);
56  assert_string_equal(pcmk__xe_id(test_xml), id);
57 
58  pcmk__xml_free(test_xml);
59 }
60 
61 static void
62 invalid_id(void **state)
63 {
64  // IDs that need sanitization
65  xmlNode *test_xml = pcmk__xe_create(NULL, "test");
66 
67  // No ID set initially
68  pcmk__xe_set_id(test_xml, "-ab");
69  assert_string_equal(pcmk__xe_id(test_xml), "_ab");
70 
71  // ID already set: test overwrite
72  pcmk__xe_set_id(test_xml, "a$b");
73  assert_string_equal(pcmk__xe_id(test_xml), "a.b");
74 
75  pcmk__xe_set_id(test_xml, "ab$");
76  assert_string_equal(pcmk__xe_id(test_xml), "ab.");
77 
78  pcmk__xe_set_id(test_xml, "$$$");
79  assert_string_equal(pcmk__xe_id(test_xml), "_..");
80 
81  pcmk__xml_free(test_xml);
82 }
83 
84 static void
85 format_args(void **state)
86 {
87  xmlNode *test_xml = pcmk__xe_create(NULL, "test");
88  const char *str = "test";
89  int num = 42;
90 
91  pcmk__xe_set_id(test_xml, "rsc-%s-%d", str, num);
92  assert_string_equal(pcmk__xe_id(test_xml), "rsc-test-42");
93 
94  pcmk__xml_free(test_xml);
95 }
96 
98  cmocka_unit_test(null_node),
99  cmocka_unit_test(null_format),
100  cmocka_unit_test(valid_id),
101  cmocka_unit_test(invalid_id),
102  cmocka_unit_test(format_args))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml_element.c:407
void pcmk__xml_free(xmlNode *xml)
Definition: xml.c:789
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
#define pcmk__assert_asserts(stmt)
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
void pcmk__xe_set_id(xmlNode *xml, const char *format,...) G_GNUC_PRINTF(2