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