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
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");
24 pcmk__set_env_option("", "new_value");
25 pcmk__set_env_option("name=val", "new_value");
26
27 pcmk__mock_setenv = false;
28
29
30 pcmk__mock_unsetenv = true;
31
32 pcmk__set_env_option(NULL, NULL);
33 pcmk__set_env_option("", NULL);
34 pcmk__set_env_option("name=val", NULL);
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");
57 pcmk__mock_setenv = false;
58
59 pcmk__mock_unsetenv = true;
60 pcmk__set_env_option(long_opt, NULL);
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");
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);
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");
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", "");
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);
145
146 pcmk__mock_unsetenv = false;
147 }
148
149 PCMK__UNIT_TEST(NULL, NULL,
150 cmocka_unit_test(bad_input_string),
151 cmocka_unit_test(input_too_long_for_both),
152 cmocka_unit_test(input_too_long_for_pcmk),
153 cmocka_unit_test(valid_inputs_set),
154 cmocka_unit_test(valid_inputs_unset))