diff --git a/lib/common/tests/output/Makefile.am b/lib/common/tests/output/Makefile.am index 1d3563c9a6..99d07239ce 100644 --- a/lib/common/tests/output/Makefile.am +++ b/lib/common/tests/output/Makefile.am @@ -1,29 +1,30 @@ # # Copyright 2021-2022 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # AM_CPPFLAGS = -I$(top_srcdir)/include \ -I$(top_builddir)/include \ -I$(top_srcdir)/lib/common LDADD = $(top_builddir)/lib/common/libcrmcommon_test.la \ -lcmocka AM_CFLAGS = -DPCMK__UNIT_TESTING AM_LDFLAGS = $(LDFLAGS_WRAP) include $(top_srcdir)/mk/tap.mk # Add "_test" to the end of all test program names to simplify .gitignore. check_PROGRAMS = pcmk__call_message_test \ + pcmk__output_free_test \ pcmk__output_new_test \ pcmk__register_format_test \ pcmk__register_formats_test \ pcmk__register_message_test \ pcmk__register_messages_test \ pcmk__unregister_formats_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/output/pcmk__output_free_test.c b/lib/common/tests/output/pcmk__output_free_test.c new file mode 100644 index 0000000000..c9f3702e0e --- /dev/null +++ b/lib/common/tests/output/pcmk__output_free_test.c @@ -0,0 +1,92 @@ +/* + * Copyright 2022 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#include + +#include +#include + +static int +null_message_fn(pcmk__output_t *out, va_list args) { + return pcmk_rc_ok; +} + +static bool +fake_text_init(pcmk__output_t *out) { + return true; +} + +static void +fake_text_free_priv(pcmk__output_t *out) { + function_called(); + /* This function intentionally left blank */ +} + +static pcmk__output_t * +mk_fake_text_output(char **argv) { + pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t)); + + if (retval == NULL) { + return NULL; + } + + retval->fmt_name = "text"; + retval->init = fake_text_init; + retval->free_priv = fake_text_free_priv; + + retval->register_message = pcmk__register_message; + retval->message = pcmk__call_message; + + return retval; +} + +static int +setup(void **state) { + pcmk__register_format(NULL, "text", mk_fake_text_output, NULL); + return 0; +} + +static int +teardown(void **state) { + pcmk__unregister_formats(); + return 0; +} + +static void +no_messages(void **state) { + pcmk__output_t *out = NULL; + + pcmk__output_new(&out, "text", NULL, NULL); + + expect_function_call(fake_text_free_priv); + pcmk__output_free(out); +} + +static void +messages(void **state) { + pcmk__output_t *out = NULL; + + pcmk__output_new(&out, "text", NULL, NULL); + pcmk__register_message(out, "fake", null_message_fn); + + expect_function_call(fake_text_free_priv); + pcmk__output_free(out); +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(no_messages, setup, teardown), + cmocka_unit_test_setup_teardown(messages, setup, teardown), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}