pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
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
15static void
16assert_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
25static void
26null_empty(void **state)
27{
28 char *buf = pcmk__str_copy("");
29
32
33 free(buf);
34}
35
36static void
37all_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
64static void
65start_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
80static void
81middle_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
90static void
91end_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
100static void
101all_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__str_copy(str)
#define pcmk__assert_asserts(stmt)
int pcmk__xml_test_teardown_group(void **state)
Definition unittest.c:105
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__xml_test_setup_group(void **state)
Definition unittest.c:87
void pcmk__xml_sanitize_id(char *id)
Definition xml.c:674