diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:53:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:53:20 +0000 |
commit | e5a812082ae033afb1eed82c0f2df3d0f6bdc93f (patch) | |
tree | a6716c9275b4b413f6c9194798b34b91affb3cc7 /lib/common/tests/cmdline | |
parent | Initial commit. (diff) | |
download | pacemaker-e5a812082ae033afb1eed82c0f2df3d0f6bdc93f.tar.xz pacemaker-e5a812082ae033afb1eed82c0f2df3d0f6bdc93f.zip |
Adding upstream version 2.1.6.upstream/2.1.6
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/common/tests/cmdline')
-rw-r--r-- | lib/common/tests/cmdline/Makefile.am | 17 | ||||
-rw-r--r-- | lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c | 156 | ||||
-rw-r--r-- | lib/common/tests/cmdline/pcmk__quote_cmdline_test.c | 56 |
3 files changed, 229 insertions, 0 deletions
diff --git a/lib/common/tests/cmdline/Makefile.am b/lib/common/tests/cmdline/Makefile.am new file mode 100644 index 0000000..d781ed5 --- /dev/null +++ b/lib/common/tests/cmdline/Makefile.am @@ -0,0 +1,17 @@ +# +# Copyright 2020-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. +# + +include $(top_srcdir)/mk/tap.mk +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 + +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 new file mode 100644 index 0000000..863fbb9 --- /dev/null +++ b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c @@ -0,0 +1,156 @@ +/* + * Copyright 2020-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. + */ + +#include <crm_internal.h> + +#include <crm/common/unittest_internal.h> +#include <crm/common/cmdline_internal.h> + +#include <glib.h> +#include <stdint.h> + +#define LISTS_EQ(a, b) { \ + assert_int_equal(g_strv_length((gchar **) (a)), g_strv_length((gchar **) (b))); \ + for (int i = 0; i < g_strv_length((a)); i++) { \ + assert_string_equal((a)[i], (b)[i]); \ + } \ +} + +static void +empty_input(void **state) { + assert_null(pcmk__cmdline_preproc(NULL, "")); +} + +static void +no_specials(void **state) { + const char *argv[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL }; + const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL); + LISTS_EQ(processed, expected); + g_strfreev(processed); + + processed = pcmk__cmdline_preproc((char **) argv, ""); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +single_dash(void **state) { + const char *argv[] = { "crm_mon", "-", NULL }; + const gchar *expected[] = { "crm_mon", "-", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +double_dash(void **state) { + const char *argv[] = { "crm_mon", "-a", "--", "-bc", NULL }; + const gchar *expected[] = { "crm_mon", "-a", "--", "-bc", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +special_args(void **state) { + const char *argv[] = { "crm_mon", "-aX", "-Fval", NULL }; + const gchar *expected[] = { "crm_mon", "-a", "X", "-F", "val", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +special_arg_at_end(void **state) { + const char *argv[] = { "crm_mon", "-a", NULL }; + const gchar *expected[] = { "crm_mon", "-a", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "a"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +long_arg(void **state) { + const char *argv[] = { "crm_mon", "--blah=foo", NULL }; + const gchar *expected[] = { "crm_mon", "--blah=foo", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +negative_score(void **state) { + const char *argv[] = { "crm_mon", "-v", "-1000", NULL }; + const gchar *expected[] = { "crm_mon", "-v", "-1000", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "v"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +negative_score_2(void **state) { + const char *argv[] = { "crm_mon", "-1i3", NULL }; + const gchar *expected[] = { "crm_mon", "-1", "-i", "-3", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL); + 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 }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "v"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +string_arg_with_dash_2(void **state) { + const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL }; + const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "v"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +static void +string_arg_with_dash_3(void **state) { + const char *argv[] = { "crm_mon", "-abc", "-1i3", NULL }; + const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-1i3", NULL }; + + gchar **processed = pcmk__cmdline_preproc((char **) argv, "c"); + LISTS_EQ(processed, expected); + g_strfreev(processed); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(empty_input), + cmocka_unit_test(no_specials), + cmocka_unit_test(single_dash), + cmocka_unit_test(double_dash), + cmocka_unit_test(special_args), + cmocka_unit_test(special_arg_at_end), + cmocka_unit_test(long_arg), + cmocka_unit_test(negative_score), + cmocka_unit_test(negative_score_2), + 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__quote_cmdline_test.c b/lib/common/tests/cmdline/pcmk__quote_cmdline_test.c new file mode 100644 index 0000000..42bd8ca --- /dev/null +++ b/lib/common/tests/cmdline/pcmk__quote_cmdline_test.c @@ -0,0 +1,56 @@ +/* + * 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 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 <glib.h> + +static void +empty_input(void **state) { + assert_null(pcmk__quote_cmdline(NULL)); +} + +static void +no_spaces(void **state) { + const char *argv[] = { "crm_resource", "-r", "rsc1", "--meta", "-p", "comment", "-v", "hello", "--output-as=xml", NULL }; + const gchar *expected = "crm_resource -r rsc1 --meta -p comment -v hello --output-as=xml"; + + gchar *processed = pcmk__quote_cmdline((gchar **) argv); + assert_string_equal(processed, expected); + g_free(processed); +} + +static void +spaces_no_quote(void **state) { + const char *argv[] = { "crm_resource", "-r", "rsc1", "--meta", "-p", "comment", "-v", "hello world", "--output-as=xml", NULL }; + const gchar *expected = "crm_resource -r rsc1 --meta -p comment -v 'hello world' --output-as=xml"; + + gchar *processed = pcmk__quote_cmdline((gchar **) argv); + assert_string_equal(processed, expected); + g_free(processed); +} + +static void +spaces_with_quote(void **state) { + const char *argv[] = { "crm_resource", "-r", "rsc1", "--meta", "-p", "comment", "-v", "here's johnny", "--output-as=xml", NULL }; + const gchar *expected = "crm_resource -r rsc1 --meta -p comment -v 'here\\\'s johnny' --output-as=xml"; + + gchar *processed = pcmk__quote_cmdline((gchar **) argv); + assert_string_equal(processed, expected); + g_free(processed); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(empty_input), + cmocka_unit_test(no_spaces), + cmocka_unit_test(spaces_no_quote), + cmocka_unit_test(spaces_with_quote)) |