diff options
Diffstat (limited to 'lib/common/tests/cmdline')
-rw-r--r-- | lib/common/tests/cmdline/Makefile.am | 5 | ||||
-rw-r--r-- | lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c | 13 | ||||
-rw-r--r-- | lib/common/tests/cmdline/pcmk__new_common_args_test.c | 62 |
3 files changed, 77 insertions, 3 deletions
diff --git a/lib/common/tests/cmdline/Makefile.am b/lib/common/tests/cmdline/Makefile.am index d781ed5..792425b 100644 --- a/lib/common/tests/cmdline/Makefile.am +++ b/lib/common/tests/cmdline/Makefile.am @@ -1,5 +1,5 @@ # -# Copyright 2020-2022 the Pacemaker project contributors +# Copyright 2020-2023 the Pacemaker project contributors # # The version control history for this file may have further details. # @@ -12,6 +12,7 @@ include $(top_srcdir)/mk/unittest.mk # Add "_test" to the end of all test program names to simplify .gitignore. check_PROGRAMS = pcmk__cmdline_preproc_test \ - pcmk__quote_cmdline_test + pcmk__new_common_args_test \ + pcmk__quote_cmdline_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c index 863fbb9..299fec6 100644 --- a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c +++ b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 the Pacemaker project contributors + * Copyright 2020-2023 the Pacemaker project contributors * * The version control history for this file may have further details. * @@ -112,6 +112,16 @@ negative_score_2(void **state) { } static void +negative_score_3(void **state) { + const char *argv[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL }; + const gchar *expected[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "pv"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void string_arg_with_dash(void **state) { const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL }; const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL }; @@ -151,6 +161,7 @@ PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(long_arg), cmocka_unit_test(negative_score), cmocka_unit_test(negative_score_2), + cmocka_unit_test(negative_score_3), cmocka_unit_test(string_arg_with_dash), cmocka_unit_test(string_arg_with_dash_2), cmocka_unit_test(string_arg_with_dash_3)) diff --git a/lib/common/tests/cmdline/pcmk__new_common_args_test.c b/lib/common/tests/cmdline/pcmk__new_common_args_test.c new file mode 100644 index 0000000..6b70465 --- /dev/null +++ b/lib/common/tests/cmdline/pcmk__new_common_args_test.c @@ -0,0 +1,62 @@ +/* + * Copyright 2023 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. + */ + +#include <crm_internal.h> + +#include <crm/common/unittest_internal.h> +#include <crm/common/cmdline_internal.h> + +#include "mock_private.h" + +#include <glib.h> + +static void +calloc_fails(void **state) +{ + pcmk__assert_exits(CRM_EX_OSERR, + { + pcmk__mock_calloc = true; // calloc() will return NULL + expect_value(__wrap_calloc, nmemb, 1); + expect_value(__wrap_calloc, size, sizeof(pcmk__common_args_t)); + pcmk__new_common_args("boring summary"); + pcmk__mock_calloc = false; // Use real calloc() + } + ); +} + +static void +strdup_fails(void **state) +{ + pcmk__assert_exits(CRM_EX_OSERR, + { + pcmk__mock_strdup = true; // strdup() will return NULL + expect_string(__wrap_strdup, s, "boring summary"); + pcmk__new_common_args("boring summary"); + pcmk__mock_strdup = false; // Use the real strdup() + } + ); +} + +static void +success(void **state) +{ + pcmk__common_args_t *args = pcmk__new_common_args("boring summary"); + assert_string_equal(args->summary, "boring summary"); + assert_null(args->output_as_descr); + assert_false(args->version); + assert_false(args->quiet); + assert_int_equal(args->verbosity, 0); + assert_null(args->output_ty); + assert_null(args->output_dest); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(calloc_fails), + cmocka_unit_test(strdup_fails), + cmocka_unit_test(success)) |