pacemaker  3.0.0-d8340737c4
Scalable High-Availability cluster resource manager
pcmk__xml_sanitize_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 assert_sanitized(const char *str, const char *reference)
17 {
18  char *buf = pcmk__str_copy(str);
19 
21  assert_string_equal(buf, reference);
22  free(buf);
23 }
24 
25 static void
26 null_empty(void **state)
27 {
28  char *buf = pcmk__str_copy("");
29 
32 
33  free(buf);
34 }
35 
36 static void
37 all_valid(void **state)
38 {
39  const char *str = NULL;
40 
41  // All NameStartChars
42  str = "abc";
43  assert_sanitized(str, str);
44 
45  // '-' NameChar but not NameStartChar
46  str = "b-c";
47  assert_sanitized(str, str);
48  str = "bc-";
49  assert_sanitized(str, str);
50 
51  // #xC2 (NameStartChar), #xB7 (NameChar)
52  str = "a" "\xC2\xB7" "b";
53  assert_sanitized(str, str);
54 
55  // #xEFFFF (NameStartChar)
56  str = "\xF3\xAF\xBF\xBF" "a";
57  assert_sanitized(str, str);
58 
59  // #xEFFFF (NameStartChar), #xB7 (NameChar)
60  str = "\xF3\xAF\xBF\xBF" "\xC2\xB7";
61  assert_sanitized(str, str);
62 }
63 
64 static void
65 start_invalid(void **state)
66 {
67  // '-' NameChar but not NameStartChar
68  assert_sanitized("-ab", "_ab");
69 
70  // '$' neither NameChar nor NameStartChar
71  assert_sanitized("$ab", "_ab");
72 
73  // #xB7 NameChar but not NameStartChar (two-byte character)
74  assert_sanitized("\xC2\xB7" "ab", "_.ab");
75 
76  // #xB8 neither NameChar nor NameStartChar (two-byte character)
77  assert_sanitized("\xC2\xB8" "ab", "_.ab");
78 }
79 
80 static void
81 middle_invalid(void **state)
82 {
83  // '$' not a NameChar
84  assert_sanitized("a$b", "a.b");
85 
86  // #xB8 not a NameChar (two-byte character)
87  assert_sanitized("a" "\xC2\xB8" "b", "a..b");
88 }
89 
90 static void
91 end_invalid(void **state)
92 {
93  // '$' not a NameChar
94  assert_sanitized("ab$", "ab.");
95 
96  // #xB8 not a NameChar (two-byte character)
97  assert_sanitized("ab" "\xC2\xB8", "ab..");
98 }
99 
100 static void
101 all_invalid(void **state)
102 {
103  // None are NameChars (all ASCII)
104  assert_sanitized("$!%", "_..");
105 
106  // None are NameChars (#xB8 two-byte character)
107  assert_sanitized("$!" "\xC2\xB8", "_...");
108 
109  // None are NameChars (all multi-byte characters)
110  assert_sanitized("\xC2\xB7" "\xCD\xBE" "\xF3\xB0\x80\x80",
111  "_." ".." "....");
112 }
113 
115  cmocka_unit_test(null_empty),
116  cmocka_unit_test(all_valid),
117  cmocka_unit_test(start_invalid),
118  cmocka_unit_test(middle_invalid),
119  cmocka_unit_test(end_invalid),
120  cmocka_unit_test(all_invalid))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
#define pcmk__assert_asserts(stmt)
#define pcmk__str_copy(str)
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
void pcmk__xml_sanitize_id(char *id)
Definition: xml.c:654