root/lib/common/tests/output/pcmk__register_messages_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. null_message_fn
  2. null_message_fn_2
  3. fake_text_init
  4. fake_text_free_priv
  5. mk_fake_text_output
  6. setup
  7. teardown
  8. invalid_entries
  9. valid_entries
  10. duplicate_message_ids
  11. duplicate_functions
  12. default_handler
  13. override_default_handler

   1 /*
   2  * Copyright 2022-2023 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/output_internal.h>
  14 
  15 #include "../../crmcommon_private.h"
  16 
  17 static int
  18 null_message_fn(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  19     return pcmk_rc_ok;
  20 }
  21 
  22 static int
  23 null_message_fn_2(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  24     return pcmk_rc_ok;
  25 }
  26 
  27 static bool
  28 fake_text_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  29     return true;
  30 }
  31 
  32 static void
  33 fake_text_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  34     /* This function intentionally left blank */
  35 }
  36 
  37 static pcmk__output_t *
  38 mk_fake_text_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  39     pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
  40 
  41     if (retval == NULL) {
  42         return NULL;
  43     }
  44 
  45     retval->fmt_name = "text";
  46     retval->init = fake_text_init;
  47     retval->free_priv = fake_text_free_priv;
  48 
  49     retval->register_message = pcmk__register_message;
  50     retval->message = pcmk__call_message;
  51 
  52     return retval;
  53 }
  54 
  55 static int
  56 setup(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  57     pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
  58     return 0;
  59 }
  60 
  61 static int
  62 teardown(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  63     pcmk__unregister_formats();
  64     return 0;
  65 }
  66 
  67 static void
  68 invalid_entries(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  69     pcmk__output_t *out = NULL;
  70 
  71     pcmk__message_entry_t entries[] = {
  72         /* We can't test a NULL message_id here because that's the marker for
  73          * the end of the table.
  74          */
  75         { "", "", null_message_fn },
  76         { "", NULL, null_message_fn },
  77         { "", "text", NULL },
  78         { NULL },
  79     };
  80 
  81     pcmk__bare_output_new(&out, "text", NULL, NULL);
  82 
  83     pcmk__assert_asserts(pcmk__register_messages(out, entries));
  84     assert_int_equal(g_hash_table_size(out->messages), 0);
  85 
  86     pcmk__output_free(out);
  87 }
  88 
  89 static void
  90 valid_entries(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  91     pcmk__output_t *out = NULL;
  92 
  93     pcmk__message_entry_t entries[] = {
  94         { "msg1", "text", null_message_fn },
  95         { "msg2", "text", null_message_fn_2 },
  96         { NULL },
  97     };
  98 
  99     pcmk__bare_output_new(&out, "text", NULL, NULL);
 100 
 101     pcmk__register_messages(out, entries);
 102     assert_int_equal(g_hash_table_size(out->messages), 2);
 103     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
 104     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg2"), null_message_fn_2);
 105 
 106     pcmk__output_free(out);
 107 }
 108 
 109 static void
 110 duplicate_message_ids(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 111     pcmk__output_t *out = NULL;
 112 
 113     pcmk__message_entry_t entries[] = {
 114         { "msg1", "text", null_message_fn },
 115         { "msg1", "text", null_message_fn_2 },
 116         { NULL },
 117     };
 118 
 119     pcmk__bare_output_new(&out, "text", NULL, NULL);
 120 
 121     pcmk__register_messages(out, entries);
 122     assert_int_equal(g_hash_table_size(out->messages), 1);
 123     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn_2);
 124 
 125     pcmk__output_free(out);
 126 }
 127 
 128 static void
 129 duplicate_functions(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 130     pcmk__output_t *out = NULL;
 131 
 132     pcmk__message_entry_t entries[] = {
 133         { "msg1", "text", null_message_fn },
 134         { "msg2", "text", null_message_fn },
 135         { NULL },
 136     };
 137 
 138     pcmk__bare_output_new(&out, "text", NULL, NULL);
 139 
 140     pcmk__register_messages(out, entries);
 141     assert_int_equal(g_hash_table_size(out->messages), 2);
 142     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
 143     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg2"), null_message_fn);
 144 
 145     pcmk__output_free(out);
 146 }
 147 
 148 static void
 149 default_handler(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 150     pcmk__output_t *out = NULL;
 151 
 152     pcmk__message_entry_t entries[] = {
 153         { "msg1", "default", null_message_fn },
 154         { NULL },
 155     };
 156 
 157     pcmk__bare_output_new(&out, "text", NULL, NULL);
 158 
 159     pcmk__register_messages(out, entries);
 160     assert_int_equal(g_hash_table_size(out->messages), 1);
 161     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
 162 
 163     pcmk__output_free(out);
 164 }
 165 
 166 static void
 167 override_default_handler(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 168     pcmk__output_t *out = NULL;
 169 
 170     pcmk__message_entry_t entries[] = {
 171         { "msg1", "default", null_message_fn },
 172         { "msg1", "text", null_message_fn_2 },
 173         { NULL },
 174     };
 175 
 176     pcmk__bare_output_new(&out, "text", NULL, NULL);
 177 
 178     pcmk__register_messages(out, entries);
 179     assert_int_equal(g_hash_table_size(out->messages), 1);
 180     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn_2);
 181 
 182     pcmk__output_free(out);
 183 }
 184 
 185 PCMK__UNIT_TEST(NULL, NULL,
 186                 cmocka_unit_test_setup_teardown(invalid_entries, setup, teardown),
 187                 cmocka_unit_test_setup_teardown(valid_entries, setup, teardown),
 188                 cmocka_unit_test_setup_teardown(duplicate_message_ids, setup, teardown),
 189                 cmocka_unit_test_setup_teardown(duplicate_functions, setup, teardown),
 190                 cmocka_unit_test_setup_teardown(default_handler, setup, teardown),
 191                 cmocka_unit_test_setup_teardown(override_default_handler, setup, teardown))

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