root/lib/common/tests/xml/pcmk__xml_is_name_char_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. assert_name_char
  2. null_len
  3. ascii
  4. unicode_0x80_to_0xB6
  5. unicode_0xB7
  6. unicode_0xB8_to_0xBF
  7. unicode_0xC0_to_0xD6
  8. unicode_0xD7
  9. unicode_0xD8_to_0xF6
  10. unicode_0xF7
  11. unicode_0xF8_to_0x2FF
  12. unicode_0x300_to_0x36F
  13. unicode_0x370_to_0x37D
  14. unicode_0x37E
  15. unicode_0x37F_to_0x1FFF
  16. unicode_0x2000_to_0x200B
  17. unicode_0x200C_to_0x200D
  18. unicode_0x200E_to_0x203E
  19. unicode_0x203F_to_0x2040
  20. unicode_0x2041_to_0x206F
  21. unicode_0x2070_to_0x218F
  22. unicode_0x2190_to_0x2BFF
  23. unicode_0x2C00_to_0x2FEF
  24. unicode_0x2FF0_to_0x3000
  25. unicode_0x3001_to_0xD7FF
  26. unicode_0xD800_to_0xF8FF
  27. unicode_0xF900_to_0xFDCF
  28. unicode_0xFDD0_to_0xFDEF
  29. unicode_0xFDF0_to_0xFFFD
  30. unicode_0xFFFE_to_0xFFFF
  31. unicode_0x10000_to_0xEFFFF
  32. unicode_0xF0000_to_0x10FFFF

   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_isalnum(), etc.
  13 
  14 #include <crm/common/unittest_internal.h>
  15 #include <crm/common/xml_internal.h>
  16 
  17 #include "crmcommon_private.h"              // pcmk__xml_is_name_char()
  18 
  19 /*!
  20  * \internal
  21  * \brief Assert that a Unicode character is (not) a valid XML \c NameChar
  22  *
  23  * \param[in] c          Unicode code point of character to check
  24  * \param[in] reference  If \c true, \p c should be considered a valid
  25  *                       \c NameChar; if \c false, it should not
  26  */
  27 static void
  28 assert_name_char(int c, bool reference)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30     gchar utf8_buf[6] = { 0, };
  31     int len = 4;
  32     int ref_len = g_unichar_to_utf8(c, utf8_buf);
  33     bool result = pcmk__xml_is_name_char(utf8_buf, &len);
  34 
  35     if (reference) {
  36         assert_true(result);
  37     } else {
  38         assert_false(result);
  39     }
  40 
  41     if ((c < 0xD800) || (c > 0xDFFF)) {
  42         /* Unicode code points in the range D800 to DFFF are UTF-16 surrogate
  43          * pair halves. They can be represented in UTF-8, but they shouldn't
  44          * appear in valid UTF-8-encoded text. RFC 3629 (Nov 2003) says they
  45          * should be treated as invalid:
  46          * https://en.wikipedia.org/wiki/UTF-8#Invalid_sequences_and_error_handling.
  47          *
  48          * GLib treats these characters as valid and returns a length of 3
  49          * bytes. So did libxml until v2.12 (commit 845bd99). Since that commit,
  50          * libxml treats these characters as invalid and returns a length of 0.
  51          * To avoid version-dependent testing behavior, skip the length check
  52          * for code points in that range.
  53          */
  54         assert_int_equal(len, ref_len);
  55     }
  56 }
  57 
  58 static void
  59 null_len(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  60 {
  61     assert_true(pcmk__xml_is_name_char("a", NULL));
  62     assert_false(pcmk__xml_is_name_char("@", NULL));
  63 }
  64 
  65 static void
  66 ascii(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {
  68     for (int c = 0x00; c <= 0x7F; c++) {
  69         if (g_ascii_isalnum(c)
  70             || c == ':' || c == '_' || c == '-' || c == '.') {
  71 
  72             assert_name_char(c, true);
  73         } else {
  74             assert_name_char(c, false);
  75         }
  76     }
  77 }
  78 
  79 static void
  80 unicode_0x80_to_0xB6(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82     for (int c = 0x80; c <= 0xB6; c++) {
  83         assert_name_char(c, false);
  84     }
  85 }
  86 
  87 static void
  88 unicode_0xB7(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90     assert_name_char(0xB7, true);
  91 }
  92 
  93 static void
  94 unicode_0xB8_to_0xBF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96     for (int c = 0xB8; c <= 0xBF; c++) {
  97         assert_name_char(c, false);
  98     }
  99 }
 100 
 101 static void
 102 unicode_0xC0_to_0xD6(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104     for (int c = 0xC0; c <= 0xD6; c++) {
 105         assert_name_char(c, true);
 106     }
 107 }
 108 
 109 static void
 110 unicode_0xD7(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112     assert_name_char(0xD7, false);
 113 }
 114 
 115 static void
 116 unicode_0xD8_to_0xF6(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118     for (int c = 0xD8; c <= 0xF6; c++) {
 119         assert_name_char(c, true);
 120     }
 121 }
 122 
 123 static void
 124 unicode_0xF7(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126     assert_name_char(0xF7, false);
 127 }
 128 
 129 static void
 130 unicode_0xF8_to_0x2FF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132     for (int c = 0xF8; c <= 0x2FF; c++) {
 133         assert_name_char(c, true);
 134     }
 135 }
 136 
 137 static void
 138 unicode_0x300_to_0x36F(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140     for (int c = 0x300; c <= 0x36F; c++) {
 141         assert_name_char(c, true);
 142     }
 143 }
 144 
 145 static void
 146 unicode_0x370_to_0x37D(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     for (int c = 0x370; c <= 0x37D; c++) {
 149         assert_name_char(c, true);
 150     }
 151 }
 152 
 153 static void
 154 unicode_0x37E(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 155 {
 156     assert_name_char(0x37E, false);
 157 }
 158 
 159 static void
 160 unicode_0x37F_to_0x1FFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 161 {
 162     for (int c = 0x37F; c <= 0x1FFF; c++) {
 163         assert_name_char(c, true);
 164     }
 165 }
 166 
 167 static void
 168 unicode_0x2000_to_0x200B(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170     for (int c = 0x2000; c <= 0x200B; c++) {
 171         assert_name_char(c, false);
 172     }
 173 }
 174 
 175 static void
 176 unicode_0x200C_to_0x200D(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178     for (int c = 0x200C; c <= 0x200D; c++) {
 179         assert_name_char(c, true);
 180     }
 181 }
 182 
 183 static void
 184 unicode_0x200E_to_0x203E(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186     for (int c = 0x200E; c <= 0x203E; c++) {
 187         assert_name_char(c, false);
 188     }
 189 }
 190 
 191 static void
 192 unicode_0x203F_to_0x2040(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 193 {
 194     for (int c = 0x203F; c <= 0x2040; c++) {
 195         assert_name_char(c, true);
 196     }
 197 }
 198 
 199 static void
 200 unicode_0x2041_to_0x206F(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 201 {
 202     for (int c = 0x2041; c <= 0x206F; c++) {
 203         assert_name_char(c, false);
 204     }
 205 }
 206 
 207 static void
 208 unicode_0x2070_to_0x218F(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210     for (int c = 0x2070; c <= 0x218F; c++) {
 211         assert_name_char(c, true);
 212     }
 213 }
 214 
 215 static void
 216 unicode_0x2190_to_0x2BFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218     for (int c = 0x2190; c <= 0x2BFF; c++) {
 219         assert_name_char(c, false);
 220     }
 221 }
 222 
 223 static void
 224 unicode_0x2C00_to_0x2FEF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226     for (int c = 0x2C00; c <= 0x2FEF; c++) {
 227         assert_name_char(c, true);
 228     }
 229 }
 230 
 231 static void
 232 unicode_0x2FF0_to_0x3000(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234     for (int c = 0x2FF0; c <= 0x3000; c++) {
 235         assert_name_char(c, false);
 236     }
 237 }
 238 
 239 static void
 240 unicode_0x3001_to_0xD7FF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 241 {
 242     for (int c = 0x3001; c <= 0xD7FF; c++) {
 243         assert_name_char(c, true);
 244     }
 245 }
 246 
 247 static void
 248 unicode_0xD800_to_0xF8FF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 249 {
 250     for (int c = 0xD800; c <= 0xF8FF; c++) {
 251         assert_name_char(c, false);
 252     }
 253 }
 254 
 255 static void
 256 unicode_0xF900_to_0xFDCF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258     for (int c = 0xF900; c <= 0xFDCF; c++) {
 259         assert_name_char(c, true);
 260     }
 261 }
 262 
 263 static void
 264 unicode_0xFDD0_to_0xFDEF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266     for (int c = 0xFDD0; c <= 0xFDEF; c++) {
 267         assert_name_char(c, false);
 268     }
 269 }
 270 
 271 static void
 272 unicode_0xFDF0_to_0xFFFD(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274     for (int c = 0xFDF0; c <= 0xFFFD; c++) {
 275         assert_name_char(c, true);
 276     }
 277 }
 278 
 279 static void
 280 unicode_0xFFFE_to_0xFFFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 281 {
 282     for (int c = 0xFFFE; c <= 0xFFFF; c++) {
 283         assert_name_char(c, false);
 284     }
 285 }
 286 
 287 static void
 288 unicode_0x10000_to_0xEFFFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 289 {
 290     for (int c = 0x10000; c <= 0xEFFFF; c++) {
 291         assert_name_char(c, true);
 292     }
 293 }
 294 
 295 static void
 296 unicode_0xF0000_to_0x10FFFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 297 {
 298     for (int c = 0xF0000; c <= 0x10FFFF; c++) {
 299         assert_name_char(c, false);
 300     }
 301 }
 302 
 303 PCMK__UNIT_TEST(NULL, NULL,
 304                 cmocka_unit_test(null_len),
 305                 cmocka_unit_test(ascii),
 306                 cmocka_unit_test(unicode_0x80_to_0xB6),
 307                 cmocka_unit_test(unicode_0xB7),
 308                 cmocka_unit_test(unicode_0xB8_to_0xBF),
 309                 cmocka_unit_test(unicode_0xC0_to_0xD6),
 310                 cmocka_unit_test(unicode_0xD7),
 311                 cmocka_unit_test(unicode_0xD8_to_0xF6),
 312                 cmocka_unit_test(unicode_0xF7),
 313                 cmocka_unit_test(unicode_0xF8_to_0x2FF),
 314                 cmocka_unit_test(unicode_0x300_to_0x36F),
 315                 cmocka_unit_test(unicode_0x370_to_0x37D),
 316                 cmocka_unit_test(unicode_0x37E),
 317                 cmocka_unit_test(unicode_0x37F_to_0x1FFF),
 318                 cmocka_unit_test(unicode_0x2000_to_0x200B),
 319                 cmocka_unit_test(unicode_0x200C_to_0x200D),
 320                 cmocka_unit_test(unicode_0x200E_to_0x203E),
 321                 cmocka_unit_test(unicode_0x203F_to_0x2040),
 322                 cmocka_unit_test(unicode_0x2041_to_0x206F),
 323                 cmocka_unit_test(unicode_0x2070_to_0x218F),
 324                 cmocka_unit_test(unicode_0x2190_to_0x2BFF),
 325                 cmocka_unit_test(unicode_0x2C00_to_0x2FEF),
 326                 cmocka_unit_test(unicode_0x2FF0_to_0x3000),
 327                 cmocka_unit_test(unicode_0x3001_to_0xD7FF),
 328                 cmocka_unit_test(unicode_0xD800_to_0xF8FF),
 329                 cmocka_unit_test(unicode_0xF900_to_0xFDCF),
 330                 cmocka_unit_test(unicode_0xFDD0_to_0xFDEF),
 331                 cmocka_unit_test(unicode_0xFDF0_to_0xFFFD),
 332                 cmocka_unit_test(unicode_0xFFFE_to_0xFFFF),
 333                 cmocka_unit_test(unicode_0x10000_to_0xEFFFF),
 334                 cmocka_unit_test(unicode_0xF0000_to_0x10FFFF))

/* [previous][next][first][last][top][bottom][index][help] */