root/lib/common/tests/xml_idref/pcmk__xe_dereference_children_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. assert_deref
  2. null_for_null
  3. null_for_no_children
  4. without_idref
  5. with_idref
  6. with_broken_idref

   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>   // GList, GHashTable, etc.
  13 
  14 #include <crm/common/unittest_internal.h>
  15 #include <crm/common/xml_internal.h>
  16 
  17 /*!
  18  * \internal
  19  * \brief Test an invocation of pcmk__xe_dereference_children()
  20  *
  21  * \param[in] xml_string    XML to parse, with "test" child to pass to tested
  22  *                          function
  23  * \param[in] element_name  Element name to pass to tested function
  24  * \param[in] ...           NULL-terminated list of child "testattr" values to
  25  *                          expect in tested function's returned list
  26  */
  27 static void
  28 assert_deref(const char *xml_string, const char *element_name, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30     xmlNode *xml = NULL;
  31     GList *list = NULL;
  32     GHashTable *table = NULL;
  33     va_list ap;
  34 
  35     // Parse given XML
  36     if (xml_string != NULL) {
  37         xml = pcmk__xml_parse(xml_string);
  38         assert_non_null(xml);
  39     }
  40 
  41     // Create a hash table with all expected child IDs
  42     va_start(ap, element_name);
  43     for (const char *value = va_arg(ap, const char *);
  44          value != NULL; value = va_arg(ap, const char *)) {
  45         if (table == NULL) {
  46             table = pcmk__strkey_table(NULL, NULL);
  47         }
  48         g_hash_table_add(table, (gpointer) value);
  49     }
  50     va_end(ap);
  51 
  52     // Call tested function on "test" child
  53     list = pcmk__xe_dereference_children(pcmk__xe_first_child(xml, "test",
  54                                                               NULL, NULL),
  55                                          element_name);
  56 
  57     // Ensure returned list has exactly the expected child IDs
  58     if (table == NULL) {
  59         assert_null(list);
  60     } else {
  61         while (list != NULL) {
  62             const char *value = crm_element_value((xmlNode *) list->data,
  63                                                   "testattr");
  64 
  65             assert_true(g_hash_table_remove(table, value));
  66             list = list->next;
  67         }
  68         assert_int_equal(g_hash_table_size(table), 0);
  69     }
  70 
  71     g_list_free(list);
  72     if (table != NULL) {
  73         g_hash_table_destroy(table);
  74     }
  75     pcmk__xml_free(xml);
  76 }
  77 
  78 static void
  79 null_for_null(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81     assert_deref(NULL, NULL, NULL);
  82     assert_deref(NULL, "test", NULL);
  83 }
  84 
  85 #define XML_NO_CHILDREN "<xml><test/></xml>"
  86 #define XML_NO_ELEMENT_CHILDREN "<xml><test><!-- comment -->text</test></xml>"
  87 
  88 static void
  89 null_for_no_children(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  90 {
  91     assert_deref(XML_NO_CHILDREN, NULL, NULL);
  92     assert_deref(XML_NO_CHILDREN, "test", NULL);
  93     assert_deref(XML_NO_ELEMENT_CHILDREN, NULL, NULL);
  94     assert_deref(XML_NO_ELEMENT_CHILDREN, "test", NULL);
  95 }
  96 
  97 #define XML_NO_IDREF                                        \
  98     "<xml>\n"                                               \
  99     "  <test>\n"                                            \
 100     "    <!-- comment -->\n"                                \
 101     "    <other id='other1' testattr='othervalue1' />\n"    \
 102     "    <child id='child1' testattr='childvalue1' />\n"    \
 103     "    <other id='other2' testattr='othervalue2' />\n"    \
 104     "    <child id='child2' testattr='childvalue2' />\n"    \
 105     "    <child id='child3' testattr='childvalue3' />\n"    \
 106     "    <other id='other3' testattr='othervalue3' />\n"    \
 107     "  </test>\n"                                           \
 108     "</xml>\n"
 109 
 110 static void
 111 without_idref(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113     assert_deref(XML_NO_IDREF, NULL,
 114                  "othervalue1", "othervalue2", "othervalue3",
 115                  "childvalue1", "childvalue2", "childvalue3", NULL);
 116 
 117     assert_deref(XML_NO_IDREF, "other",
 118                  "othervalue1", "othervalue2", "othervalue3", NULL);
 119 
 120     assert_deref(XML_NO_IDREF, "child",
 121                  "childvalue1", "childvalue2", "childvalue3", NULL);
 122 
 123     assert_deref(XML_NO_IDREF, "nonexistent", NULL);
 124 }
 125 
 126 #define XML_WITH_IDREF                                      \
 127     "<xml>\n"                                               \
 128     "  <other id='other1' testattr='othervalue1' />\n"      \
 129     "  <child id='child2' testattr='childvalue2' />\n"      \
 130     "  <test>\n"                                            \
 131     "    <!-- comment -->\n"                                \
 132     "    <other id-ref='other1'/>\n"                        \
 133     "    <child id='child1' testattr='childvalue1' />\n"    \
 134     "    <other id='other2' testattr='othervalue2' />\n"    \
 135     "    <child id-ref='child2' />\n"                       \
 136     "    <child id='child3' testattr='childvalue3' />\n"    \
 137     "    <other id='other3' testattr='othervalue3' />\n"    \
 138     "  </test>\n"                                           \
 139     "</xml>\n"
 140 
 141 static void
 142 with_idref(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 143 {
 144     assert_deref(XML_WITH_IDREF, NULL,
 145                  "othervalue1", "othervalue2", "othervalue3",
 146                  "childvalue1", "childvalue2", "childvalue3", NULL);
 147 
 148     assert_deref(XML_WITH_IDREF, "other",
 149                  "othervalue1", "othervalue2", "othervalue3", NULL);
 150 
 151     assert_deref(XML_WITH_IDREF, "child",
 152                  "childvalue1", "childvalue2", "childvalue3", NULL);
 153 
 154     assert_deref(XML_WITH_IDREF, "nonexistent", NULL);
 155 }
 156 
 157 #define XML_WITH_BROKEN_IDREF                               \
 158     "<xml>\n"                                               \
 159     "  <test>\n"                                            \
 160     "    <!-- comment -->\n"                                \
 161     "    <other id-ref='other1'/>\n"                        \
 162     "    <child id='child1' testattr='childvalue1' />\n"    \
 163     "    <other id='other2' testattr='othervalue2' />\n"    \
 164     "    <child id-ref='child2' />\n"                       \
 165     "    <child id='child3' testattr='childvalue3' />\n"    \
 166     "    <other id='other3' testattr='othervalue3' />\n"    \
 167     "  </test>\n"                                           \
 168     "</xml>\n"
 169 
 170 static void
 171 with_broken_idref(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173     assert_deref(XML_WITH_BROKEN_IDREF, NULL,
 174                  "othervalue2", "othervalue3",
 175                  "childvalue1", "childvalue3", NULL);
 176 
 177     assert_deref(XML_WITH_BROKEN_IDREF, "other",
 178                  "othervalue2", "othervalue3", NULL);
 179 
 180     assert_deref(XML_WITH_BROKEN_IDREF, "child",
 181                  "childvalue1", "childvalue3", NULL);
 182 
 183     assert_deref(XML_WITH_BROKEN_IDREF, "nonexistent", NULL);
 184 }
 185 
 186 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 187                 cmocka_unit_test(null_for_null),
 188                 cmocka_unit_test(null_for_no_children),
 189                 cmocka_unit_test(without_idref),
 190                 cmocka_unit_test(with_idref),
 191                 cmocka_unit_test(with_broken_idref))

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