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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_sanitized
  2. null_empty
  3. all_valid
  4. start_invalid
  5. middle_invalid
  6. end_invalid
  7. all_invalid

   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 <crm/common/unittest_internal.h>
  13 #include <crm/common/xml_internal.h>
  14 
  15 static void
  16 assert_sanitized(const char *str, const char *reference)
     /* [previous][next][first][last][top][bottom][index][help] */
  17 {
  18     char *buf = pcmk__str_copy(str);
  19 
  20     pcmk__xml_sanitize_id(buf);
  21     assert_string_equal(buf, reference);
  22     free(buf);
  23 }
  24 
  25 static void
  26 null_empty(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28     char *buf = pcmk__str_copy("");
  29 
  30     pcmk__assert_asserts(pcmk__xml_sanitize_id(NULL));
  31     pcmk__assert_asserts(pcmk__xml_sanitize_id(buf));
  32 
  33     free(buf);
  34 }
  35 
  36 static void
  37 all_valid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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 
 114 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 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))

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