pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
crm_str_to_boolean_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2021 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
13 
14 static void
15 bad_input(void **state) {
16  assert_int_equal(crm_str_to_boolean(NULL, NULL), -1);
17  assert_int_equal(crm_str_to_boolean("", NULL), -1);
18  assert_int_equal(crm_str_to_boolean("blahblah", NULL), -1);
19 }
20 
21 static void
22 is_true(void **state) {
23  int ret;
24 
25  assert_int_equal(crm_str_to_boolean("true", &ret), 1);
26  assert_true(ret);
27  assert_int_equal(crm_str_to_boolean("TrUe", &ret), 1);
28  assert_true(ret);
29  assert_int_equal(crm_str_to_boolean("on", &ret), 1);
30  assert_true(ret);
31  assert_int_equal(crm_str_to_boolean("ON", &ret), 1);
32  assert_true(ret);
33  assert_int_equal(crm_str_to_boolean("yes", &ret), 1);
34  assert_true(ret);
35  assert_int_equal(crm_str_to_boolean("yES", &ret), 1);
36  assert_true(ret);
37  assert_int_equal(crm_str_to_boolean("y", &ret), 1);
38  assert_true(ret);
39  assert_int_equal(crm_str_to_boolean("Y", &ret), 1);
40  assert_true(ret);
41  assert_int_equal(crm_str_to_boolean("1", &ret), 1);
42  assert_true(ret);
43 }
44 
45 static void
46 is_not_true(void **state) {
47  assert_int_equal(crm_str_to_boolean("truedat", NULL), -1);
48  assert_int_equal(crm_str_to_boolean("onnn", NULL), -1);
49  assert_int_equal(crm_str_to_boolean("yep", NULL), -1);
50  assert_int_equal(crm_str_to_boolean("Y!", NULL), -1);
51  assert_int_equal(crm_str_to_boolean("100", NULL), -1);
52 }
53 
54 static void
55 is_false(void **state) {
56  int ret;
57 
58  assert_int_equal(crm_str_to_boolean("false", &ret), 1);
59  assert_false(ret);
60  assert_int_equal(crm_str_to_boolean("fAlSe", &ret), 1);
61  assert_false(ret);
62  assert_int_equal(crm_str_to_boolean("off", &ret), 1);
63  assert_false(ret);
64  assert_int_equal(crm_str_to_boolean("OFF", &ret), 1);
65  assert_false(ret);
66  assert_int_equal(crm_str_to_boolean("no", &ret), 1);
67  assert_false(ret);
68  assert_int_equal(crm_str_to_boolean("No", &ret), 1);
69  assert_false(ret);
70  assert_int_equal(crm_str_to_boolean("n", &ret), 1);
71  assert_false(ret);
72  assert_int_equal(crm_str_to_boolean("N", &ret), 1);
73  assert_false(ret);
74  assert_int_equal(crm_str_to_boolean("0", &ret), 1);
75  assert_false(ret);
76 }
77 
78 static void
79 is_not_false(void **state) {
80  assert_int_equal(crm_str_to_boolean("falseee", NULL), -1);
81  assert_int_equal(crm_str_to_boolean("of", NULL), -1);
82  assert_int_equal(crm_str_to_boolean("nope", NULL), -1);
83  assert_int_equal(crm_str_to_boolean("N!", NULL), -1);
84  assert_int_equal(crm_str_to_boolean("000", NULL), -1);
85 }
86 
87 PCMK__UNIT_TEST(NULL, NULL,
88  cmocka_unit_test(bad_input),
89  cmocka_unit_test(is_true),
90  cmocka_unit_test(is_not_true),
91  cmocka_unit_test(is_false),
92  cmocka_unit_test(is_not_false))
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:424