diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index fd1e794217..33f8b98d12 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,37 +1,38 @@ # # 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 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 include $(top_srcdir)/mk/glib-tap.mk # Add each test program here. Each test should be written as a little standalone # program using the glib unit testing functions. See the documentation for more # information. # # https://developer.gnome.org/glib/unstable/glib-Testing.html # # Add "_test" to the end of all test program names to simplify .gitignore. -test_programs = pcmk__add_word_test \ +test_programs = crm_str_to_boolean_test \ + pcmk__add_word_test \ pcmk__btoa_test \ pcmk__char_in_any_str_test \ pcmk__parse_ll_range_test \ pcmk__scan_double_test \ pcmk__str_any_of_test \ pcmk__str_in_list_test \ pcmk__strcmp_test # If any extra data needs to be added to the source distribution, add it to the # following list. dist_test_data = # If any extra data needs to be used by tests but should not be added to the # source distribution, add it to the following list. test_data = diff --git a/lib/common/tests/strings/crm_str_to_boolean_test.c b/lib/common/tests/strings/crm_str_to_boolean_test.c new file mode 100644 index 0000000000..03b799f9d9 --- /dev/null +++ b/lib/common/tests/strings/crm_str_to_boolean_test.c @@ -0,0 +1,96 @@ +/* + * Copyright 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 + +static void +bad_input(void) { + g_assert_cmpint(crm_str_to_boolean(NULL, NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("blahblah", NULL), ==, -1); +} + +static void +is_true(void) { + int ret; + + g_assert_cmpint(crm_str_to_boolean("true", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("TrUe", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("on", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("ON", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("yes", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("yES", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("y", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("Y", &ret), ==, 1); + g_assert_true(ret); + g_assert_cmpint(crm_str_to_boolean("1", &ret), ==, 1); + g_assert_true(ret); +} + +static void +is_not_true(void) { + g_assert_cmpint(crm_str_to_boolean("truedat", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("onnn", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("yep", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("Y!", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("100", NULL), ==, -1); +} + +static void +is_false(void) { + int ret; + + g_assert_cmpint(crm_str_to_boolean("false", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("fAlSe", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("off", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("OFF", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("no", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("No", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("n", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("N", &ret), ==, 1); + g_assert_false(ret); + g_assert_cmpint(crm_str_to_boolean("0", &ret), ==, 1); + g_assert_false(ret); +} + +static void +is_not_false(void) { + g_assert_cmpint(crm_str_to_boolean("falseee", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("of", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("nope", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("N!", NULL), ==, -1); + g_assert_cmpint(crm_str_to_boolean("000", NULL), ==, -1); +} + +int +main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/common/strings/crm_str_to_boolean/bad_input", bad_input); + g_test_add_func("/common/strings/crm_str_to_boolean/is_true", is_true); + g_test_add_func("/common/strings/crm_str_to_boolean/is_not_true", is_not_true); + g_test_add_func("/common/strings/crm_str_to_boolean/is_false", is_false); + g_test_add_func("/common/strings/crm_str_to_boolean/is_not_false", is_not_false); + return g_test_run(); +}