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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_name_start_char
  2. null_len
  3. ascii
  4. unicode_0x80_to_0xBF
  5. unicode_0xC0_to_0xD6
  6. unicode_0xD7
  7. unicode_0xD8_to_0xF6
  8. unicode_0xF7
  9. unicode_0xF8_to_0x2FF
  10. unicode_0x300_to_0x36F
  11. unicode_0x370_to_0x37D
  12. unicode_0x37E
  13. unicode_0x37F_to_0x1FFF
  14. unicode_0x2000_to_0x200B
  15. unicode_0x200C_to_0x200D
  16. unicode_0x200E_to_0x206F
  17. unicode_0x2070_to_0x218F
  18. unicode_0x2190_to_0x2BFF
  19. unicode_0x2C00_to_0x2FEF
  20. unicode_0x2FF0_to_0x3000
  21. unicode_0x3001_to_0xD7FF
  22. unicode_0xD800_to_0xF8FF
  23. unicode_0xF900_to_0xFDCF
  24. unicode_0xFDD0_to_0xFDEF
  25. unicode_0xFDF0_to_0xFFFD
  26. unicode_0xFFFE_to_0xFFFF
  27. unicode_0x10000_to_0xEFFFF
  28. 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_isalpha(), etc.
  13 #include <libxml/xmlstring.h>               // xmlGetUTF8Char()
  14 
  15 #include <crm/common/unittest_internal.h>
  16 #include <crm/common/xml_internal.h>
  17 
  18 #include "crmcommon_private.h"              // pcmk__xml_is_name_start_char()
  19 
  20 /*!
  21  * \internal
  22  * \brief Assert that a Unicode character is (not) a valid XML \c NameStartChar
  23  *
  24  * \param[in] c          Unicode code point of character to check
  25  * \param[in] reference  If \c true, \p c should be considered a valid
  26  *                       \c NameStartChar; if \c false, it should not
  27  */
  28 static void
  29 assert_name_start_char(int c, bool reference)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  59 static void
  60 null_len(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  66 static void
  67 ascii(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  78 static void
  79 unicode_0x80_to_0xBF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81     for (int c = 0x80; c <= 0xBF; c++) {
  82         assert_name_start_char(c, false);
  83     }
  84 }
  85 
  86 static void
  87 unicode_0xC0_to_0xD6(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     for (int c = 0xC0; c <= 0xD6; c++) {
  90         assert_name_start_char(c, true);
  91     }
  92 }
  93 
  94 static void
  95 unicode_0xD7(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  96 {
  97     assert_name_start_char(0xD7, false);
  98 }
  99 
 100 static void
 101 unicode_0xD8_to_0xF6(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103     for (int c = 0xD8; c <= 0xF6; c++) {
 104         assert_name_start_char(c, true);
 105     }
 106 }
 107 
 108 static void
 109 unicode_0xF7(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 110 {
 111     assert_name_start_char(0xF7, false);
 112 }
 113 
 114 static void
 115 unicode_0xF8_to_0x2FF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 116 {
 117     for (int c = 0xF8; c <= 0x2FF; c++) {
 118         assert_name_start_char(c, true);
 119     }
 120 }
 121 
 122 static void
 123 unicode_0x300_to_0x36F(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125     for (int c = 0x300; c <= 0x36F; c++) {
 126         assert_name_start_char(c, false);
 127     }
 128 }
 129 
 130 static void
 131 unicode_0x370_to_0x37D(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133     for (int c = 0x370; c <= 0x37D; c++) {
 134         assert_name_start_char(c, true);
 135     }
 136 }
 137 
 138 static void
 139 unicode_0x37E(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141     assert_name_start_char(0x37E, false);
 142 }
 143 
 144 static void
 145 unicode_0x37F_to_0x1FFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147     for (int c = 0x37F; c <= 0x1FFF; c++) {
 148         assert_name_start_char(c, true);
 149     }
 150 }
 151 
 152 static void
 153 unicode_0x2000_to_0x200B(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 154 {
 155     for (int c = 0x2000; c <= 0x200B; c++) {
 156         assert_name_start_char(c, false);
 157     }
 158 }
 159 
 160 static void
 161 unicode_0x200C_to_0x200D(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163     for (int c = 0x200C; c <= 0x200D; c++) {
 164         assert_name_start_char(c, true);
 165     }
 166 }
 167 
 168 static void
 169 unicode_0x200E_to_0x206F(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171     for (int c = 0x200E; c <= 0x206F; c++) {
 172         assert_name_start_char(c, false);
 173     }
 174 }
 175 
 176 static void
 177 unicode_0x2070_to_0x218F(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179     for (int c = 0x2070; c <= 0x218F; c++) {
 180         assert_name_start_char(c, true);
 181     }
 182 }
 183 
 184 static void
 185 unicode_0x2190_to_0x2BFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187     for (int c = 0x2190; c <= 0x2BFF; c++) {
 188         assert_name_start_char(c, false);
 189     }
 190 }
 191 
 192 static void
 193 unicode_0x2C00_to_0x2FEF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195     for (int c = 0x2C00; c <= 0x2FEF; c++) {
 196         assert_name_start_char(c, true);
 197     }
 198 }
 199 
 200 static void
 201 unicode_0x2FF0_to_0x3000(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203     for (int c = 0x2FF0; c <= 0x3000; c++) {
 204         assert_name_start_char(c, false);
 205     }
 206 }
 207 
 208 static void
 209 unicode_0x3001_to_0xD7FF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211     for (int c = 0x3001; c <= 0xD7FF; c++) {
 212         assert_name_start_char(c, true);
 213     }
 214 }
 215 
 216 static void
 217 unicode_0xD800_to_0xF8FF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219     for (int c = 0xD800; c <= 0xF8FF; c++) {
 220         assert_name_start_char(c, false);
 221     }
 222 }
 223 
 224 static void
 225 unicode_0xF900_to_0xFDCF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 226 {
 227     for (int c = 0xF900; c <= 0xFDCF; c++) {
 228         assert_name_start_char(c, true);
 229     }
 230 }
 231 
 232 static void
 233 unicode_0xFDD0_to_0xFDEF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235     for (int c = 0xFDD0; c <= 0xFDEF; c++) {
 236         assert_name_start_char(c, false);
 237     }
 238 }
 239 
 240 static void
 241 unicode_0xFDF0_to_0xFFFD(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 242 {
 243     for (int c = 0xFDF0; c <= 0xFFFD; c++) {
 244         assert_name_start_char(c, true);
 245     }
 246 }
 247 
 248 static void
 249 unicode_0xFFFE_to_0xFFFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251     for (int c = 0xFFFE; c <= 0xFFFF; c++) {
 252         assert_name_start_char(c, false);
 253     }
 254 }
 255 
 256 static void
 257 unicode_0x10000_to_0xEFFFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 258 {
 259     for (int c = 0x10000; c <= 0xEFFFF; c++) {
 260         assert_name_start_char(c, true);
 261     }
 262 }
 263 
 264 static void
 265 unicode_0xF0000_to_0x10FFFF(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 266 {
 267     for (int c = 0xF0000; c <= 0x10FFFF; c++) {
 268         assert_name_start_char(c, false);
 269     }
 270 }
 271 
 272 PCMK__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))

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