diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index df7859daa1..49532d9a5b 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,38 +1,39 @@ # # 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__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__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__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); +}