This source file includes following definitions.
- bad_input_string
- input_too_long_for_both
- input_too_long_for_pcmk
- valid_inputs_set
- valid_inputs_unset
- disable_compat
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11 #include <crm/common/unittest_internal.h>
12
13 #include "mock_private.h"
14
15 static void
16 bad_input_string(void **state)
17 {
18
19
20
21 pcmk__mock_setenv = true;
22
23 pcmk__set_env_option(NULL, "new_value", true);
24 pcmk__set_env_option("", "new_value", true);
25 pcmk__set_env_option("name=val", "new_value", true);
26
27 pcmk__mock_setenv = false;
28
29
30 pcmk__mock_unsetenv = true;
31
32 pcmk__set_env_option(NULL, NULL, true);
33 pcmk__set_env_option("", NULL, true);
34 pcmk__set_env_option("name=val", NULL, true);
35
36 pcmk__mock_unsetenv = false;
37 }
38
39 static void
40 input_too_long_for_both(void **state)
41 {
42
43
44
45
46
47 char long_opt[NAME_MAX - 2];
48
49 for (int i = 0; i < NAME_MAX - 3; i++) {
50 long_opt[i] = 'a';
51 }
52 long_opt[NAME_MAX - 3] = '\0';
53
54
55 pcmk__mock_setenv = true;
56 pcmk__set_env_option(long_opt, "new_value", true);
57 pcmk__mock_setenv = false;
58
59 pcmk__mock_unsetenv = true;
60 pcmk__set_env_option(long_opt, NULL, true);
61 pcmk__mock_unsetenv = false;
62 }
63
64 static void
65 input_too_long_for_pcmk(void **state)
66 {
67
68
69
70
71
72
73 char long_opt[NAME_MAX - 4];
74 char buf[NAME_MAX];
75
76 for (int i = 0; i < NAME_MAX - 5; i++) {
77 long_opt[i] = 'a';
78 }
79 long_opt[NAME_MAX - 5] = '\0';
80
81 snprintf(buf, NAME_MAX, "HA_%s", long_opt);
82
83
84 pcmk__mock_setenv = true;
85
86 expect_string(__wrap_setenv, name, buf);
87 expect_string(__wrap_setenv, value, "new_value");
88 expect_value(__wrap_setenv, overwrite, 1);
89 will_return(__wrap_setenv, 0);
90 pcmk__set_env_option(long_opt, "new_value", true);
91
92 pcmk__mock_setenv = false;
93
94
95 pcmk__mock_unsetenv = true;
96
97 expect_string(__wrap_unsetenv, name, buf);
98 will_return(__wrap_unsetenv, 0);
99 pcmk__set_env_option(long_opt, NULL, true);
100
101 pcmk__mock_unsetenv = false;
102 }
103
104 static void
105 valid_inputs_set(void **state)
106 {
107
108 pcmk__mock_setenv = true;
109
110 expect_string(__wrap_setenv, name, "PCMK_env_var");
111 expect_string(__wrap_setenv, value, "new_value");
112 expect_value(__wrap_setenv, overwrite, 1);
113 will_return(__wrap_setenv, 0);
114 expect_string(__wrap_setenv, name, "HA_env_var");
115 expect_string(__wrap_setenv, value, "new_value");
116 expect_value(__wrap_setenv, overwrite, 1);
117 will_return(__wrap_setenv, 0);
118 pcmk__set_env_option("env_var", "new_value", true);
119
120
121 expect_string(__wrap_setenv, name, "PCMK_env_var");
122 expect_string(__wrap_setenv, value, "");
123 expect_value(__wrap_setenv, overwrite, 1);
124 will_return(__wrap_setenv, 0);
125 expect_string(__wrap_setenv, name, "HA_env_var");
126 expect_string(__wrap_setenv, value, "");
127 expect_value(__wrap_setenv, overwrite, 1);
128 will_return(__wrap_setenv, 0);
129 pcmk__set_env_option("env_var", "", true);
130
131 pcmk__mock_setenv = false;
132 }
133
134 static void
135 valid_inputs_unset(void **state)
136 {
137
138 pcmk__mock_unsetenv = true;
139
140 expect_string(__wrap_unsetenv, name, "PCMK_env_var");
141 will_return(__wrap_unsetenv, 0);
142 expect_string(__wrap_unsetenv, name, "HA_env_var");
143 will_return(__wrap_unsetenv, 0);
144 pcmk__set_env_option("env_var", NULL, true);
145
146 pcmk__mock_unsetenv = false;
147 }
148
149 static void
150 disable_compat(void **state)
151 {
152
153 pcmk__mock_setenv = true;
154
155 expect_string(__wrap_setenv, name, "PCMK_env_var");
156 expect_string(__wrap_setenv, value, "new_value");
157 expect_value(__wrap_setenv, overwrite, 1);
158 will_return(__wrap_setenv, 0);
159 pcmk__set_env_option("env_var", "new_value", false);
160
161 pcmk__mock_setenv = false;
162
163
164 pcmk__mock_unsetenv = true;
165
166 expect_string(__wrap_unsetenv, name, "PCMK_env_var");
167 will_return(__wrap_unsetenv, 0);
168 expect_string(__wrap_unsetenv, name, "HA_env_var");
169 will_return(__wrap_unsetenv, 0);
170 pcmk__set_env_option("env_var", NULL, false);
171
172 pcmk__mock_unsetenv = false;
173 }
174
175 PCMK__UNIT_TEST(NULL, NULL,
176 cmocka_unit_test(bad_input_string),
177 cmocka_unit_test(input_too_long_for_both),
178 cmocka_unit_test(input_too_long_for_pcmk),
179 cmocka_unit_test(valid_inputs_set),
180 cmocka_unit_test(valid_inputs_unset),
181 cmocka_unit_test(disable_compat))