pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__xml_is_name_start_char_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 <glib.h> // gchar, g_ascii_isalpha(), etc.
13#include <libxml/xmlstring.h> // xmlGetUTF8Char()
14
17
18#include "crmcommon_private.h" // pcmk__xml_is_name_start_char()
19
28static void
29assert_name_start_char(int c, bool reference)
30{
31 gchar utf8_buf[6] = { 0, };
32 int len = 4;
33 int ref_len = g_unichar_to_utf8(c, utf8_buf);
34 bool result = pcmk__xml_is_name_start_char(utf8_buf, &len);
35
36 if (reference) {
37 assert_true(result);
38 } else {
39 assert_false(result);
40 }
41
42 if ((c < 0xD800) || (c > 0xDFFF)) {
43 /* Unicode code points in the range D800 to DFFF are UTF-16 surrogate
44 * pair halves. They can be represented in UTF-8, but they shouldn't
45 * appear in valid UTF-8-encoded text. RFC 3629 (Nov 2003) says they
46 * should be treated as invalid:
47 * https://en.wikipedia.org/wiki/UTF-8#Invalid_sequences_and_error_handling.
48 *
49 * GLib treats these characters as valid and returns a length of 3
50 * bytes. So did libxml until v2.12 (commit 845bd99). Since that commit,
51 * libxml treats these characters as invalid and returns a length of 0.
52 * To avoid version-dependent testing behavior, skip the length check
53 * for code points in that range.
54 */
55 assert_int_equal(len, ref_len);
56 }
57}
58
59static void
60null_len(void **state)
61{
62 assert_true(pcmk__xml_is_name_start_char("a", NULL));
63 assert_false(pcmk__xml_is_name_start_char("0", NULL));
64}
65
66static void
67ascii(void **state)
68{
69 for (int c = 0x00; c <= 0x7F; c++) {
70 if (g_ascii_isalpha(c) || c == ':' || c == '_') {
71 assert_name_start_char(c, true);
72 } else {
73 assert_name_start_char(c, false);
74 }
75 }
76}
77
78static void
79unicode_0x80_to_0xBF(void **state)
80{
81 for (int c = 0x80; c <= 0xBF; c++) {
82 assert_name_start_char(c, false);
83 }
84}
85
86static void
87unicode_0xC0_to_0xD6(void **state)
88{
89 for (int c = 0xC0; c <= 0xD6; c++) {
90 assert_name_start_char(c, true);
91 }
92}
93
94static void
95unicode_0xD7(void **state)
96{
97 assert_name_start_char(0xD7, false);
98}
99
100static void
101unicode_0xD8_to_0xF6(void **state)
102{
103 for (int c = 0xD8; c <= 0xF6; c++) {
104 assert_name_start_char(c, true);
105 }
106}
107
108static void
109unicode_0xF7(void **state)
110{
111 assert_name_start_char(0xF7, false);
112}
113
114static void
115unicode_0xF8_to_0x2FF(void **state)
116{
117 for (int c = 0xF8; c <= 0x2FF; c++) {
118 assert_name_start_char(c, true);
119 }
120}
121
122static void
123unicode_0x300_to_0x36F(void **state)
124{
125 for (int c = 0x300; c <= 0x36F; c++) {
126 assert_name_start_char(c, false);
127 }
128}
129
130static void
131unicode_0x370_to_0x37D(void **state)
132{
133 for (int c = 0x370; c <= 0x37D; c++) {
134 assert_name_start_char(c, true);
135 }
136}
137
138static void
139unicode_0x37E(void **state)
140{
141 assert_name_start_char(0x37E, false);
142}
143
144static void
145unicode_0x37F_to_0x1FFF(void **state)
146{
147 for (int c = 0x37F; c <= 0x1FFF; c++) {
148 assert_name_start_char(c, true);
149 }
150}
151
152static void
153unicode_0x2000_to_0x200B(void **state)
154{
155 for (int c = 0x2000; c <= 0x200B; c++) {
156 assert_name_start_char(c, false);
157 }
158}
159
160static void
161unicode_0x200C_to_0x200D(void **state)
162{
163 for (int c = 0x200C; c <= 0x200D; c++) {
164 assert_name_start_char(c, true);
165 }
166}
167
168static void
169unicode_0x200E_to_0x206F(void **state)
170{
171 for (int c = 0x200E; c <= 0x206F; c++) {
172 assert_name_start_char(c, false);
173 }
174}
175
176static void
177unicode_0x2070_to_0x218F(void **state)
178{
179 for (int c = 0x2070; c <= 0x218F; c++) {
180 assert_name_start_char(c, true);
181 }
182}
183
184static void
185unicode_0x2190_to_0x2BFF(void **state)
186{
187 for (int c = 0x2190; c <= 0x2BFF; c++) {
188 assert_name_start_char(c, false);
189 }
190}
191
192static void
193unicode_0x2C00_to_0x2FEF(void **state)
194{
195 for (int c = 0x2C00; c <= 0x2FEF; c++) {
196 assert_name_start_char(c, true);
197 }
198}
199
200static void
201unicode_0x2FF0_to_0x3000(void **state)
202{
203 for (int c = 0x2FF0; c <= 0x3000; c++) {
204 assert_name_start_char(c, false);
205 }
206}
207
208static void
209unicode_0x3001_to_0xD7FF(void **state)
210{
211 for (int c = 0x3001; c <= 0xD7FF; c++) {
212 assert_name_start_char(c, true);
213 }
214}
215
216static void
217unicode_0xD800_to_0xF8FF(void **state)
218{
219 for (int c = 0xD800; c <= 0xF8FF; c++) {
220 assert_name_start_char(c, false);
221 }
222}
223
224static void
225unicode_0xF900_to_0xFDCF(void **state)
226{
227 for (int c = 0xF900; c <= 0xFDCF; c++) {
228 assert_name_start_char(c, true);
229 }
230}
231
232static void
233unicode_0xFDD0_to_0xFDEF(void **state)
234{
235 for (int c = 0xFDD0; c <= 0xFDEF; c++) {
236 assert_name_start_char(c, false);
237 }
238}
239
240static void
241unicode_0xFDF0_to_0xFFFD(void **state)
242{
243 for (int c = 0xFDF0; c <= 0xFFFD; c++) {
244 assert_name_start_char(c, true);
245 }
246}
247
248static void
249unicode_0xFFFE_to_0xFFFF(void **state)
250{
251 for (int c = 0xFFFE; c <= 0xFFFF; c++) {
252 assert_name_start_char(c, false);
253 }
254}
255
256static void
257unicode_0x10000_to_0xEFFFF(void **state)
258{
259 for (int c = 0x10000; c <= 0xEFFFF; c++) {
260 assert_name_start_char(c, true);
261 }
262}
263
264static void
265unicode_0xF0000_to_0x10FFFF(void **state)
266{
267 for (int c = 0xF0000; c <= 0x10FFFF; c++) {
268 assert_name_start_char(c, false);
269 }
270}
271
272PCMK__UNIT_TEST(NULL, NULL,
273 cmocka_unit_test(null_len),
274 cmocka_unit_test(ascii),
275 cmocka_unit_test(unicode_0x80_to_0xBF),
276 cmocka_unit_test(unicode_0xC0_to_0xD6),
277 cmocka_unit_test(unicode_0xD7),
278 cmocka_unit_test(unicode_0xD8_to_0xF6),
279 cmocka_unit_test(unicode_0xF7),
280 cmocka_unit_test(unicode_0xF8_to_0x2FF),
281 cmocka_unit_test(unicode_0x300_to_0x36F),
282 cmocka_unit_test(unicode_0x370_to_0x37D),
283 cmocka_unit_test(unicode_0x37E),
284 cmocka_unit_test(unicode_0x37F_to_0x1FFF),
285 cmocka_unit_test(unicode_0x2000_to_0x200B),
286 cmocka_unit_test(unicode_0x200C_to_0x200D),
287 cmocka_unit_test(unicode_0x200E_to_0x206F),
288 cmocka_unit_test(unicode_0x2070_to_0x218F),
289 cmocka_unit_test(unicode_0x2190_to_0x2BFF),
290 cmocka_unit_test(unicode_0x2C00_to_0x2FEF),
291 cmocka_unit_test(unicode_0x2FF0_to_0x3000),
292 cmocka_unit_test(unicode_0x3001_to_0xD7FF),
293 cmocka_unit_test(unicode_0xD800_to_0xF8FF),
294 cmocka_unit_test(unicode_0xF900_to_0xFDCF),
295 cmocka_unit_test(unicode_0xFDD0_to_0xFDEF),
296 cmocka_unit_test(unicode_0xFDF0_to_0xFFFD),
297 cmocka_unit_test(unicode_0xFFFE_to_0xFFFF),
298 cmocka_unit_test(unicode_0x10000_to_0xEFFFF),
299 cmocka_unit_test(unicode_0xF0000_to_0x10FFFF))
G_GNUC_INTERNAL bool pcmk__xml_is_name_start_char(const char *utf8, int *len)
Definition xml.c:540
pcmk__action_result_t result
Definition pcmk_fence.c:37
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)