This source file includes following definitions.
- default_message_fn
- failed_message_fn
- message_fn_1
- message_fn_2
- fake_text_init
- fake_text_free_priv
- mk_fake_text_output
- setup
- teardown
- no_such_message
- message_return_value
- wrong_format
- default_called
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 default_message_fn(pcmk__output_t *out, va_list args) {
17 function_called();
18 return pcmk_rc_ok;
19 }
20
21 static int
22 failed_message_fn(pcmk__output_t *out, va_list args) {
23 function_called();
24 return pcmk_rc_no_output;
25 }
26
27 static int
28 message_fn_1(pcmk__output_t *out, va_list args) {
29 function_called();
30 return pcmk_rc_ok;
31 }
32
33 static int
34 message_fn_2(pcmk__output_t *out, va_list args) {
35 function_called();
36 return pcmk_rc_ok;
37 }
38
39 static bool
40 fake_text_init(pcmk__output_t *out) {
41 return true;
42 }
43
44 static void
45 fake_text_free_priv(pcmk__output_t *out) {
46
47 }
48
49 static pcmk__output_t *
50 mk_fake_text_output(char **argv) {
51 pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
52
53 if (retval == NULL) {
54 return NULL;
55 }
56
57 retval->fmt_name = "text";
58 retval->init = fake_text_init;
59 retval->free_priv = fake_text_free_priv;
60
61 retval->register_message = pcmk__register_message;
62 retval->message = pcmk__call_message;
63
64 return retval;
65 }
66
67 static int
68 setup(void **state) {
69 pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
70 return 0;
71 }
72
73 static int
74 teardown(void **state) {
75 pcmk__unregister_formats();
76 return 0;
77 }
78
79 static void
80 no_such_message(void **state) {
81 pcmk__output_t *out = NULL;
82
83 pcmk__output_new(&out, "text", NULL, NULL);
84
85 assert_int_equal(out->message(out, "fake"), EINVAL);
86 pcmk__assert_asserts(out->message(out, ""));
87 pcmk__assert_asserts(out->message(out, NULL));
88
89 pcmk__output_free(out);
90 }
91
92 static void
93 message_return_value(void **state) {
94 pcmk__output_t *out = NULL;
95
96 pcmk__message_entry_t entries[] = {
97 { "msg1", "text", message_fn_1 },
98 { "msg2", "text", message_fn_2 },
99 { "fail", "text", failed_message_fn },
100 { NULL },
101 };
102
103 pcmk__output_new(&out, "text", NULL, NULL);
104 pcmk__register_messages(out, entries);
105
106 expect_function_call(message_fn_1);
107 assert_int_equal(out->message(out, "msg1"), pcmk_rc_ok);
108 expect_function_call(message_fn_2);
109 assert_int_equal(out->message(out, "msg2"), pcmk_rc_ok);
110 expect_function_call(failed_message_fn);
111 assert_int_equal(out->message(out, "fail"), pcmk_rc_no_output);
112
113 pcmk__output_free(out);
114 }
115
116 static void
117 wrong_format(void **state) {
118 pcmk__output_t *out = NULL;
119
120 pcmk__message_entry_t entries[] = {
121 { "msg1", "xml", message_fn_1 },
122 { NULL },
123 };
124
125 pcmk__output_new(&out, "text", NULL, NULL);
126 pcmk__register_messages(out, entries);
127
128 assert_int_equal(out->message(out, "msg1"), EINVAL);
129
130 pcmk__output_free(out);
131 }
132
133 static void
134 default_called(void **state) {
135 pcmk__output_t *out = NULL;
136
137 pcmk__message_entry_t entries[] = {
138 { "msg1", "default", default_message_fn },
139 { "msg1", "xml", message_fn_1 },
140 { NULL },
141 };
142
143 pcmk__output_new(&out, "text", NULL, NULL);
144 pcmk__register_messages(out, entries);
145
146 expect_function_call(default_message_fn);
147 assert_int_equal(out->message(out, "msg1"), pcmk_rc_ok);
148
149 pcmk__output_free(out);
150 }
151
152 PCMK__UNIT_TEST(NULL, NULL,
153 cmocka_unit_test_setup_teardown(no_such_message, setup, teardown),
154 cmocka_unit_test_setup_teardown(message_return_value, setup, teardown),
155 cmocka_unit_test_setup_teardown(wrong_format, setup, teardown),
156 cmocka_unit_test_setup_teardown(default_called, setup, teardown))