diff --git a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c index d2608511e5..3b5ce52d1c 100644 --- a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c +++ b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c @@ -1,166 +1,166 @@ /* - * Copyright 2020-2021 the Pacemaker project contributors + * 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 Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ #include #include #include #include #include #include #include #include #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[] = { "-a", "-b", "-c", "-d", NULL }; - const gchar *expected[] = { "-a", "-b", "-c", "-d", NULL }; + const char *argv[] = { "-a", "-b", "-c", "-d", "-1", NULL }; + const gchar *expected[] = { "-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[] = { "-", NULL }; const gchar *expected[] = { "-", 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[] = { "-a", "--", "-bc", NULL }; const gchar *expected[] = { "-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[] = { "-aX", "-Fval", NULL }; const gchar *expected[] = { "-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[] = { "-a", NULL }; const gchar *expected[] = { "-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[] = { "--blah=foo", NULL }; const gchar *expected[] = { "--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[] = { "-v", "-1000", NULL }; const gchar *expected[] = { "-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[] = { "-1i3", NULL }; const gchar *expected[] = { "-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[] = { "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL }; const gchar *expected[] = { "-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[] = { "-n", "crm_mon_options", "-v", "-1i3", NULL }; const gchar *expected[] = { "-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[] = { "-abc", "-1i3", NULL }; const gchar *expected[] = { "-a", "-b", "-c", "-1i3", NULL }; gchar **processed = pcmk__cmdline_preproc((char **) argv, "c"); LISTS_EQ(processed, expected); g_strfreev(processed); } int main(int argc, char **argv) { const struct CMUnitTest tests[] = { 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), }; cmocka_set_message_output(CM_OUTPUT_TAP); return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index 13a81beb2d..6f3e100d5e 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,31 +1,40 @@ # -# Copyright 2020-2021 the Pacemaker project contributors +# 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. # AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include LDADD = $(top_builddir)/lib/common/libcrmcommon.la -lcmocka include $(top_srcdir)/mk/tap.mk # Add "_test" to the end of all test program names to simplify .gitignore. check_PROGRAMS = \ crm_get_msec_test \ crm_is_true_test \ crm_str_to_boolean_test \ pcmk__add_word_test \ pcmk__btoa_test \ pcmk__char_in_any_str_test \ + pcmk__compress_test \ pcmk__ends_with_test \ + pcmk__guint_from_hash_test \ + pcmk__numeric_strcasecmp_test \ pcmk__parse_ll_range_test \ pcmk__scan_double_test \ + pcmk__scan_min_int_test \ + pcmk__scan_port_test \ pcmk__starts_with_test \ pcmk__str_any_of_test \ pcmk__str_in_list_test \ - pcmk__strcmp_test + pcmk__str_table_dup_test \ + pcmk__strcmp_test \ + pcmk__strkey_table_test \ + pcmk__strikey_table_test \ + pcmk__trim_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/strings/pcmk__compress_test.c b/lib/common/tests/strings/pcmk__compress_test.c new file mode 100644 index 0000000000..b6e6df74ed --- /dev/null +++ b/lib/common/tests/strings/pcmk__compress_test.c @@ -0,0 +1,51 @@ +/* + * 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 +#include +#include +#include + +#define SIMPLE_DATA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + +const char *SIMPLE_COMPRESSED = "BZh41AY&SYO\x1ai"; + +static void +simple_compress(void **state) +{ + char *result = calloc(1024, sizeof(char)); + unsigned int len; + + assert_int_equal(pcmk__compress(SIMPLE_DATA, 40, 0, &result, &len), pcmk_rc_ok); + assert_memory_equal(result, SIMPLE_COMPRESSED, 13); +} + +static void +max_too_small(void **state) +{ + char *result = calloc(1024, sizeof(char)); + unsigned int len; + + assert_int_equal(pcmk__compress(SIMPLE_DATA, 40, 10, &result, &len), pcmk_rc_error); +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(simple_compress), + cmocka_unit_test(max_too_small), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/strings/pcmk__guint_from_hash_test.c b/lib/common/tests/strings/pcmk__guint_from_hash_test.c new file mode 100644 index 0000000000..0a0653ccbf --- /dev/null +++ b/lib/common/tests/strings/pcmk__guint_from_hash_test.c @@ -0,0 +1,86 @@ +/* + * 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 +#include +#include +#include + +#include + +static void +null_args(void **state) +{ + GHashTable *tbl = pcmk__strkey_table(free, free); + guint result; + + assert_int_equal(pcmk__guint_from_hash(NULL, "abc", 123, &result), EINVAL); + assert_int_equal(pcmk__guint_from_hash(tbl, NULL, 123, &result), EINVAL); + + g_hash_table_destroy(tbl); +} + +static void +missing_key(void **state) +{ + GHashTable *tbl = pcmk__strkey_table(free, free); + guint result; + + assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 123, &result), pcmk_rc_ok); + assert_int_equal(result, 123); + + g_hash_table_destroy(tbl); +} + +static void +standard_usage(void **state) +{ + GHashTable *tbl = pcmk__strkey_table(free, free); + guint result; + + g_hash_table_insert(tbl, strdup("abc"), strdup("123")); + + assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 456, &result), pcmk_rc_ok); + assert_int_equal(result, 123); + + g_hash_table_destroy(tbl); +} + +static void +conversion_errors(void **state) +{ + GHashTable *tbl = pcmk__strkey_table(free, free); + guint result; + + g_hash_table_insert(tbl, strdup("negative"), strdup("-3")); + g_hash_table_insert(tbl, strdup("toobig"), strdup("20000000000000000")); + + assert_int_equal(pcmk__guint_from_hash(tbl, "negative", 456, &result), ERANGE); + assert_int_equal(result, 456); + + assert_int_equal(pcmk__guint_from_hash(tbl, "toobig", 456, &result), ERANGE); + assert_int_equal(result, 456); + + g_hash_table_destroy(tbl); +} + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(null_args), + cmocka_unit_test(missing_key), + cmocka_unit_test(standard_usage), + cmocka_unit_test(conversion_errors), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/strings/pcmk__numeric_strcasecmp_test.c b/lib/common/tests/strings/pcmk__numeric_strcasecmp_test.c new file mode 100644 index 0000000000..4ead552563 --- /dev/null +++ b/lib/common/tests/strings/pcmk__numeric_strcasecmp_test.c @@ -0,0 +1,82 @@ +/* + * 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 +#include +#include +#include + +static void +no_numbers(void **state) +{ + /* All comparisons are done case-insensitively. */ + assert_int_equal(pcmk__numeric_strcasecmp("abcd", "efgh"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("abcd", "abcd"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("efgh", "abcd"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("AbCd", "eFgH"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("ABCD", "abcd"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("EFgh", "ABcd"), 1); +} + +static void +trailing_numbers(void **state) +{ + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node2"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node1"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("node2", "node1"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node10"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node10", "node10"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("node10", "node1"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node10", "remotenode9"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("remotenode9", "node10"), 1); + + /* Longer numbers sort higher than shorter numbers. */ + assert_int_equal(pcmk__numeric_strcasecmp("node001", "node1"), 1); + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node001"), -1); +} + +static void +middle_numbers(void **state) +{ + assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1def"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node1def", "node1abc"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node2abc"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node2abc", "node1abc"), 1); +} + +static void +unequal_lengths(void **state) +{ + assert_int_equal(pcmk__numeric_strcasecmp("node-ab", "node-abc"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node-abc", "node-ab"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node1ab", "node1abc"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1ab"), 1); +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(no_numbers), + cmocka_unit_test(trailing_numbers), + cmocka_unit_test(middle_numbers), + cmocka_unit_test(unequal_lengths), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/strings/pcmk__parse_ll_range_test.c b/lib/common/tests/strings/pcmk__parse_ll_range_test.c index a4f91aef19..e6b8e48959 100644 --- a/lib/common/tests/strings/pcmk__parse_ll_range_test.c +++ b/lib/common/tests/strings/pcmk__parse_ll_range_test.c @@ -1,106 +1,118 @@ /* * Copyright 2020-2021 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 #include #include #include static void empty_input_string(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range(NULL, &start, &end), pcmk_rc_unknown_format); assert_int_equal(pcmk__parse_ll_range("", &start, &end), pcmk_rc_unknown_format); } static void missing_separator(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range("1234", &start, &end), pcmk_rc_ok); assert_int_equal(start, 1234); assert_int_equal(end, 1234); } static void only_separator(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range("-", &start, &end), pcmk_rc_unknown_format); assert_int_equal(start, PCMK__PARSE_INT_DEFAULT); assert_int_equal(end, PCMK__PARSE_INT_DEFAULT); } static void no_range_end(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range("2000-", &start, &end), pcmk_rc_ok); assert_int_equal(start, 2000); assert_int_equal(end, PCMK__PARSE_INT_DEFAULT); } static void no_range_start(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range("-2020", &start, &end), pcmk_rc_ok); assert_int_equal(start, PCMK__PARSE_INT_DEFAULT); assert_int_equal(end, 2020); } static void range_start_and_end(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range("2000-2020", &start, &end), pcmk_rc_ok); assert_int_equal(start, 2000); assert_int_equal(end, 2020); + + assert_int_equal(pcmk__parse_ll_range("2000-2020-2030", &start, &end), pcmk_rc_unknown_format); } static void garbage(void **state) { long long start, end; assert_int_equal(pcmk__parse_ll_range("2000x-", &start, &end), pcmk_rc_unknown_format); assert_int_equal(start, PCMK__PARSE_INT_DEFAULT); assert_int_equal(end, PCMK__PARSE_INT_DEFAULT); assert_int_equal(pcmk__parse_ll_range("-x2000", &start, &end), pcmk_rc_unknown_format); assert_int_equal(start, PCMK__PARSE_INT_DEFAULT); assert_int_equal(end, PCMK__PARSE_INT_DEFAULT); } +static void +strtoll_errors(void **state) +{ + long long start, end; + + assert_int_equal(pcmk__parse_ll_range("20000000000000000000-", &start, &end), pcmk_rc_unknown_format); + assert_int_equal(pcmk__parse_ll_range("100-20000000000000000000", &start, &end), pcmk_rc_unknown_format); +} + int main(int argc, char **argv) { const struct CMUnitTest tests[] = { cmocka_unit_test(empty_input_string), cmocka_unit_test(missing_separator), cmocka_unit_test(only_separator), cmocka_unit_test(no_range_end), cmocka_unit_test(no_range_start), cmocka_unit_test(range_start_and_end), + cmocka_unit_test(strtoll_errors), cmocka_unit_test(garbage), }; cmocka_set_message_output(CM_OUTPUT_TAP); return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/lib/common/tests/strings/pcmk__scan_double_test.c b/lib/common/tests/strings/pcmk__scan_double_test.c index d8befd86c8..26684233f8 100644 --- a/lib/common/tests/strings/pcmk__scan_double_test.c +++ b/lib/common/tests/strings/pcmk__scan_double_test.c @@ -1,164 +1,166 @@ /* - * Copyright 2004-2021 the Pacemaker project contributors + * Copyright 2004-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 #include #include #include #include // DBL_MAX, etc. #include // fabs() // Ensure plenty of characters for %f display #define LOCAL_BUF_SIZE 2 * DBL_MAX_10_EXP /* * assert_float_equal doesn't exist for older versions of cmocka installed on some * of our builders, so define it in terms of regular assert() here in that case. */ #if HAVE_DECL_ASSERT_FLOAT_EQUAL == 0 #define assert_float_equal(a, b, epsilon) assert_true(fabs((a) - (b)) < (epsilon)) #endif static void empty_input_string(void **state) { double result; // Without default_text assert_int_equal(pcmk__scan_double(NULL, &result, NULL, NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); assert_int_equal(pcmk__scan_double("", &result, NULL, NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); // With default_text assert_int_equal(pcmk__scan_double(NULL, &result, "2.0", NULL), pcmk_rc_ok); assert_float_equal(result, 2.0, DBL_EPSILON); assert_int_equal(pcmk__scan_double("", &result, "2.0", NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); } static void bad_input_string(void **state) { double result; // Without default text assert_int_equal(pcmk__scan_double("asdf", &result, NULL, NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); assert_int_equal(pcmk__scan_double("as2.0", &result, NULL, NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); // With default text (not used) assert_int_equal(pcmk__scan_double("asdf", &result, "2.0", NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); assert_int_equal(pcmk__scan_double("as2.0", &result, "2.0", NULL), EINVAL); assert_float_equal(result, PCMK__PARSE_DBL_DEFAULT, DBL_EPSILON); } static void trailing_chars(void **state) { double result; + char *end_text; - assert_int_equal(pcmk__scan_double("2.0asdf", &result, NULL, NULL), pcmk_rc_ok); + assert_int_equal(pcmk__scan_double("2.0asdf", &result, NULL, &end_text), pcmk_rc_ok); assert_float_equal(result, 2.0, DBL_EPSILON); + assert_string_equal(end_text, "asdf"); } static void typical_case(void **state) { char str[LOCAL_BUF_SIZE]; double result; assert_int_equal(pcmk__scan_double("0.0", &result, NULL, NULL), pcmk_rc_ok); assert_float_equal(result, 0.0, DBL_EPSILON); assert_int_equal(pcmk__scan_double("1.0", &result, NULL, NULL), pcmk_rc_ok); assert_float_equal(result, 1.0, DBL_EPSILON); assert_int_equal(pcmk__scan_double("-1.0", &result, NULL, NULL), pcmk_rc_ok); assert_float_equal(result, -1.0, DBL_EPSILON); snprintf(str, LOCAL_BUF_SIZE, "%f", DBL_MAX); assert_int_equal(pcmk__scan_double(str, &result, NULL, NULL), pcmk_rc_ok); assert_float_equal(result, DBL_MAX, DBL_EPSILON); snprintf(str, LOCAL_BUF_SIZE, "%f", -DBL_MAX); assert_int_equal(pcmk__scan_double(str, &result, NULL, NULL), pcmk_rc_ok); assert_float_equal(result, -DBL_MAX, DBL_EPSILON); } static void double_overflow(void **state) { char str[LOCAL_BUF_SIZE]; double result; /* * 1e(DBL_MAX_10_EXP + 1) produces an inf value * Can't use assert_float_equal() because (inf - inf) == NaN */ snprintf(str, LOCAL_BUF_SIZE, "1e%d", DBL_MAX_10_EXP + 1); assert_int_equal(pcmk__scan_double(str, &result, NULL, NULL), EOVERFLOW); assert_true(result > DBL_MAX); snprintf(str, LOCAL_BUF_SIZE, "-1e%d", DBL_MAX_10_EXP + 1); assert_int_equal(pcmk__scan_double(str, &result, NULL, NULL), EOVERFLOW); assert_true(result < -DBL_MAX); } static void double_underflow(void **state) { char str[LOCAL_BUF_SIZE]; double result; /* * 1e(DBL_MIN_10_EXP - 1) produces a denormalized value (between 0 * and DBL_MIN) * * C99/C11: result will be **no greater than** DBL_MIN */ snprintf(str, LOCAL_BUF_SIZE, "1e%d", DBL_MIN_10_EXP - 1); assert_int_equal(pcmk__scan_double(str, &result, NULL, NULL), pcmk_rc_underflow); assert_true(result >= 0.0); assert_true(result <= DBL_MIN); snprintf(str, LOCAL_BUF_SIZE, "-1e%d", DBL_MIN_10_EXP - 1); assert_int_equal(pcmk__scan_double(str, &result, NULL, NULL), pcmk_rc_underflow); assert_true(result <= 0.0); assert_true(result >= -DBL_MIN); } int main(int argc, char **argv) { const struct CMUnitTest tests[] = { // Test for input string issues cmocka_unit_test(empty_input_string), cmocka_unit_test(bad_input_string), cmocka_unit_test(trailing_chars), // Test for numeric issues cmocka_unit_test(typical_case), cmocka_unit_test(double_overflow), cmocka_unit_test(double_underflow), }; cmocka_set_message_output(CM_OUTPUT_TAP); return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/lib/common/tests/strings/pcmk__scan_min_int_test.c b/lib/common/tests/strings/pcmk__scan_min_int_test.c new file mode 100644 index 0000000000..9c5d073ddb --- /dev/null +++ b/lib/common/tests/strings/pcmk__scan_min_int_test.c @@ -0,0 +1,72 @@ +/* + * 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 +#include +#include +#include + +static void +empty_input_string(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_min_int("", &result, 1), EINVAL); + assert_int_equal(result, 1); + + assert_int_equal(pcmk__scan_min_int(NULL, &result, 1), pcmk_rc_ok); + assert_int_equal(result, 1); +} + +static void +input_below_minimum(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_min_int("100", &result, 1024), pcmk_rc_ok); + assert_int_equal(result, 1024); +} + +static void +input_above_maximum(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_min_int("20000000000000000", &result, 100), EOVERFLOW); + assert_int_equal(result, INT_MAX); +} + +static void +input_just_right(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_min_int("1024", &result, 1024), pcmk_rc_ok); + assert_int_equal(result, 1024); + + assert_int_equal(pcmk__scan_min_int("2048", &result, 1024), pcmk_rc_ok); + assert_int_equal(result, 2048); +} + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(empty_input_string), + cmocka_unit_test(input_below_minimum), + cmocka_unit_test(input_above_maximum), + cmocka_unit_test(input_just_right), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} + diff --git a/lib/common/tests/strings/pcmk__scan_port_test.c b/lib/common/tests/strings/pcmk__scan_port_test.c new file mode 100644 index 0000000000..0a6a1a96d2 --- /dev/null +++ b/lib/common/tests/strings/pcmk__scan_port_test.c @@ -0,0 +1,71 @@ +/* + * 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 +#include +#include +#include + +static void +empty_input_string(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_port("", &result), EINVAL); + assert_int_equal(result, -1); +} + +static void +bad_input_string(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_port("abc", &result), EINVAL); + assert_int_equal(result, -1); +} + +static void +out_of_range(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_port("-1", &result), pcmk_rc_before_range); + assert_int_equal(result, -1); + assert_int_equal(pcmk__scan_port("65536", &result), pcmk_rc_after_range); + assert_int_equal(result, -1); +} + +static void +typical_case(void **state) +{ + int result; + + assert_int_equal(pcmk__scan_port("0", &result), pcmk_rc_ok); + assert_int_equal(result, 0); + + assert_int_equal(pcmk__scan_port("80", &result), pcmk_rc_ok); + assert_int_equal(result, 80); +} + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(empty_input_string), + cmocka_unit_test(bad_input_string), + cmocka_unit_test(out_of_range), + cmocka_unit_test(typical_case), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} + diff --git a/lib/common/tests/strings/pcmk__str_table_dup_test.c b/lib/common/tests/strings/pcmk__str_table_dup_test.c new file mode 100644 index 0000000000..de7e41a077 --- /dev/null +++ b/lib/common/tests/strings/pcmk__str_table_dup_test.c @@ -0,0 +1,69 @@ +/* + * 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 +#include +#include +#include + +#include + +static void +null_input_table(void **state) +{ + assert_null(pcmk__str_table_dup(NULL)); +} + +static void +empty_input_table(void **state) +{ + GHashTable *tbl = pcmk__strkey_table(free, free); + GHashTable *copy = NULL; + + copy = pcmk__str_table_dup(tbl); + assert_int_equal(g_hash_table_size(copy), 0); + + g_hash_table_destroy(tbl); + g_hash_table_destroy(copy); +} + +static void +regular_input_table(void **state) +{ + GHashTable *tbl = pcmk__strkey_table(free, free); + GHashTable *copy = NULL; + + g_hash_table_insert(tbl, strdup("abc"), strdup("123")); + g_hash_table_insert(tbl, strdup("def"), strdup("456")); + g_hash_table_insert(tbl, strdup("ghi"), strdup("789")); + + copy = pcmk__str_table_dup(tbl); + assert_int_equal(g_hash_table_size(copy), 3); + + assert_string_equal(g_hash_table_lookup(tbl, "abc"), "123"); + assert_string_equal(g_hash_table_lookup(tbl, "def"), "456"); + assert_string_equal(g_hash_table_lookup(tbl, "ghi"), "789"); + + g_hash_table_destroy(tbl); + g_hash_table_destroy(copy); +} + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(null_input_table), + cmocka_unit_test(empty_input_table), + cmocka_unit_test(regular_input_table), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/strings/pcmk__strikey_table_test.c b/lib/common/tests/strings/pcmk__strikey_table_test.c new file mode 100644 index 0000000000..03d12a7761 --- /dev/null +++ b/lib/common/tests/strings/pcmk__strikey_table_test.c @@ -0,0 +1,50 @@ +/* + * 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 +#include +#include +#include + +#include + +static void +store_strs(void **state) +{ + GHashTable *tbl = NULL; + + tbl = pcmk__strikey_table(free, free); + assert_non_null(tbl); + + assert_true(g_hash_table_insert(tbl, strdup("key-abc"), strdup("val-abc"))); + assert_int_equal(g_hash_table_size(tbl), 1); + assert_string_equal(g_hash_table_lookup(tbl, "key-abc"), "val-abc"); + + assert_false(g_hash_table_insert(tbl, strdup("key-abc"), strdup("val-def"))); + assert_int_equal(g_hash_table_size(tbl), 1); + assert_string_equal(g_hash_table_lookup(tbl, "key-abc"), "val-def"); + + assert_false(g_hash_table_insert(tbl, strdup("key-ABC"), strdup("val-ABC"))); + assert_int_equal(g_hash_table_size(tbl), 1); + assert_string_equal(g_hash_table_lookup(tbl, "key-ABC"), "val-ABC"); + + g_hash_table_destroy(tbl); +} + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(store_strs), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/strings/pcmk__strkey_table_test.c b/lib/common/tests/strings/pcmk__strkey_table_test.c new file mode 100644 index 0000000000..3d4c3b1f03 --- /dev/null +++ b/lib/common/tests/strings/pcmk__strkey_table_test.c @@ -0,0 +1,50 @@ +/* + * 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 +#include +#include +#include + +#include + +static void +store_strs(void **state) +{ + GHashTable *tbl = NULL; + + tbl = pcmk__strkey_table(free, free); + assert_non_null(tbl); + + assert_true(g_hash_table_insert(tbl, strdup("key-abc"), strdup("val-abc"))); + assert_int_equal(g_hash_table_size(tbl), 1); + assert_string_equal(g_hash_table_lookup(tbl, "key-abc"), "val-abc"); + + assert_false(g_hash_table_insert(tbl, strdup("key-abc"), strdup("val-def"))); + assert_int_equal(g_hash_table_size(tbl), 1); + assert_string_equal(g_hash_table_lookup(tbl, "key-abc"), "val-def"); + + assert_true(g_hash_table_insert(tbl, strdup("key-ABC"), strdup("val-abc"))); + assert_int_equal(g_hash_table_size(tbl), 2); + assert_string_equal(g_hash_table_lookup(tbl, "key-ABC"), "val-abc"); + + g_hash_table_destroy(tbl); +} + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(store_strs), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/strings/pcmk__trim_test.c b/lib/common/tests/strings/pcmk__trim_test.c new file mode 100644 index 0000000000..1b9196ac80 --- /dev/null +++ b/lib/common/tests/strings/pcmk__trim_test.c @@ -0,0 +1,82 @@ +/* + * 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 +#include +#include +#include + +#include + +static void +empty_input(void **state) +{ + char *s = strdup(""); + + assert_null(pcmk__trim(NULL)); + assert_string_equal(pcmk__trim(s), ""); + + free(s); +} + +static void +leading_newline(void **state) +{ + char *s = strdup("\nabcd"); + + assert_string_equal(pcmk__trim(s), "\nabcd"); + free(s); +} + +static void +middle_newline(void **state) +{ + char *s = strdup("ab\ncd"); + + assert_string_equal(pcmk__trim(s), "ab\ncd"); + free(s); +} + +static void +trailing_newline(void **state) +{ + char *s = strdup("abcd\n\n"); + + assert_string_equal(pcmk__trim(s), "abcd"); + free(s); + + s = strdup("abcd\n "); + assert_string_equal(pcmk__trim(s), "abcd\n "); + free(s); +} + +static void +other_whitespace(void **state) +{ + char *s = strdup(" ab\t\ncd \t"); + + assert_string_equal(pcmk__trim(s), " ab\t\ncd \t"); + free(s); +} + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(empty_input), + cmocka_unit_test(leading_newline), + cmocka_unit_test(middle_newline), + cmocka_unit_test(trailing_newline), + cmocka_unit_test(other_whitespace), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}