1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * (C) Copyright 2021
4 * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
5 */
6
7 #include <command.h>
8 #include <env.h>
9 #include <env_attr.h>
10 #include <vsprintf.h>
11 #include <test/hush.h>
12 #include <test/ut.h>
13
14 /*
15 * All tests will execute the following:
16 * if condition_to_test; then
17 * true
18 * else
19 * false
20 * fi
21 * If condition is true, command returns 1, 0 otherwise.
22 */
23 const char *if_format = "if %s; then true; else false; fi";
24
hush_test_if_base(struct unit_test_state * uts)25 static int hush_test_if_base(struct unit_test_state *uts)
26 {
27 char if_formatted[128];
28
29 sprintf(if_formatted, if_format, "true");
30 ut_assertok(run_command(if_formatted, 0));
31
32 sprintf(if_formatted, if_format, "false");
33 ut_asserteq(1, run_command(if_formatted, 0));
34
35 return 0;
36 }
37 HUSH_TEST(hush_test_if_base, 0);
38
hush_test_if_basic_operators(struct unit_test_state * uts)39 static int hush_test_if_basic_operators(struct unit_test_state *uts)
40 {
41 char if_formatted[128];
42
43 sprintf(if_formatted, if_format, "test aaa = aaa");
44 ut_assertok(run_command(if_formatted, 0));
45
46 sprintf(if_formatted, if_format, "test aaa = bbb");
47 ut_asserteq(1, run_command(if_formatted, 0));
48
49 sprintf(if_formatted, if_format, "test aaa != bbb");
50 ut_assertok(run_command(if_formatted, 0));
51
52 sprintf(if_formatted, if_format, "test aaa != aaa");
53 ut_asserteq(1, run_command(if_formatted, 0));
54
55 sprintf(if_formatted, if_format, "test aaa < bbb");
56 ut_assertok(run_command(if_formatted, 0));
57
58 sprintf(if_formatted, if_format, "test bbb < aaa");
59 ut_asserteq(1, run_command(if_formatted, 0));
60
61 sprintf(if_formatted, if_format, "test bbb > aaa");
62 ut_assertok(run_command(if_formatted, 0));
63
64 sprintf(if_formatted, if_format, "test aaa > bbb");
65 ut_asserteq(1, run_command(if_formatted, 0));
66
67 sprintf(if_formatted, if_format, "test 123 -eq 123");
68 ut_assertok(run_command(if_formatted, 0));
69
70 sprintf(if_formatted, if_format, "test 123 -eq 456");
71 ut_asserteq(1, run_command(if_formatted, 0));
72
73 sprintf(if_formatted, if_format, "test 123 -ne 456");
74 ut_assertok(run_command(if_formatted, 0));
75
76 sprintf(if_formatted, if_format, "test 123 -ne 123");
77 ut_asserteq(1, run_command(if_formatted, 0));
78
79 sprintf(if_formatted, if_format, "test 123 -lt 456");
80 ut_assertok(run_command(if_formatted, 0));
81
82 sprintf(if_formatted, if_format, "test 123 -lt 123");
83 ut_asserteq(1, run_command(if_formatted, 0));
84
85 sprintf(if_formatted, if_format, "test 456 -lt 123");
86 ut_asserteq(1, run_command(if_formatted, 0));
87
88 sprintf(if_formatted, if_format, "test 123 -le 456");
89 ut_assertok(run_command(if_formatted, 0));
90
91 sprintf(if_formatted, if_format, "test 123 -le 123");
92 ut_assertok(run_command(if_formatted, 0));
93
94 sprintf(if_formatted, if_format, "test 456 -le 123");
95 ut_asserteq(1, run_command(if_formatted, 0));
96
97 sprintf(if_formatted, if_format, "test 456 -gt 123");
98 ut_assertok(run_command(if_formatted, 0));
99
100 sprintf(if_formatted, if_format, "test 123 -gt 123");
101 ut_asserteq(1, run_command(if_formatted, 0));
102
103 sprintf(if_formatted, if_format, "test 123 -gt 456");
104 ut_asserteq(1, run_command(if_formatted, 0));
105
106 sprintf(if_formatted, if_format, "test 456 -ge 123");
107 ut_assertok(run_command(if_formatted, 0));
108
109 sprintf(if_formatted, if_format, "test 123 -ge 123");
110 ut_assertok(run_command(if_formatted, 0));
111
112 sprintf(if_formatted, if_format, "test 123 -ge 456");
113 ut_asserteq(1, run_command(if_formatted, 0));
114
115 return 0;
116 }
117 HUSH_TEST(hush_test_if_basic_operators, 0);
118
hush_test_if_octal(struct unit_test_state * uts)119 static int hush_test_if_octal(struct unit_test_state *uts)
120 {
121 char if_formatted[128];
122
123 sprintf(if_formatted, if_format, "test 010 -eq 010");
124 ut_assertok(run_command(if_formatted, 0));
125
126 sprintf(if_formatted, if_format, "test 010 -eq 011");
127 ut_asserteq(1, run_command(if_formatted, 0));
128
129 sprintf(if_formatted, if_format, "test 010 -ne 011");
130 ut_assertok(run_command(if_formatted, 0));
131
132 sprintf(if_formatted, if_format, "test 010 -ne 010");
133 ut_asserteq(1, run_command(if_formatted, 0));
134
135 return 0;
136 }
137 HUSH_TEST(hush_test_if_octal, 0);
138
hush_test_if_hexadecimal(struct unit_test_state * uts)139 static int hush_test_if_hexadecimal(struct unit_test_state *uts)
140 {
141 char if_formatted[128];
142
143 sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000001");
144 ut_asserteq(1, run_command(if_formatted, 0));
145
146 sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000000");
147 ut_asserteq(1, run_command(if_formatted, 0));
148
149 sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x1ffffff");
150 ut_assertok(run_command(if_formatted, 0));
151
152 return 0;
153 }
154 HUSH_TEST(hush_test_if_hexadecimal, 0);
155
hush_test_if_mixed(struct unit_test_state * uts)156 static int hush_test_if_mixed(struct unit_test_state *uts)
157 {
158 char if_formatted[128];
159
160 sprintf(if_formatted, if_format, "test 010 -eq 10");
161 ut_asserteq(1, run_command(if_formatted, 0));
162
163 sprintf(if_formatted, if_format, "test 010 -ne 10");
164 ut_assertok(run_command(if_formatted, 0));
165
166 sprintf(if_formatted, if_format, "test 0xa -eq 10");
167 ut_assertok(run_command(if_formatted, 0));
168
169 sprintf(if_formatted, if_format, "test 0xa -eq 012");
170 ut_assertok(run_command(if_formatted, 0));
171
172 sprintf(if_formatted, if_format, "test 2000000 -gt 0x1ffffff");
173 ut_asserteq(1, run_command(if_formatted, 0));
174
175 sprintf(if_formatted, if_format, "test 0x2000000 -gt 1ffffff");
176 ut_assertok(run_command(if_formatted, 0));
177
178 sprintf(if_formatted, if_format, "test 0x2000000 -lt 1ffffff");
179 ut_asserteq(1, run_command(if_formatted, 0));
180
181 sprintf(if_formatted, if_format, "test 0x2000000 -eq 2000000");
182 ut_asserteq(1, run_command(if_formatted, 0));
183
184 sprintf(if_formatted, if_format, "test 0x2000000 -ne 2000000");
185 ut_assertok(run_command(if_formatted, 0));
186
187 sprintf(if_formatted, if_format, "test -z \"\"");
188 ut_assertok(run_command(if_formatted, 0));
189
190 sprintf(if_formatted, if_format, "test -z \"aaa\"");
191 ut_asserteq(1, run_command(if_formatted, 0));
192
193 sprintf(if_formatted, if_format, "test -n \"aaa\"");
194 ut_assertok(run_command(if_formatted, 0));
195
196 sprintf(if_formatted, if_format, "test -n \"\"");
197 ut_asserteq(1, run_command(if_formatted, 0));
198
199 return 0;
200 }
201 HUSH_TEST(hush_test_if_mixed, 0);
202
hush_test_if_inverted(struct unit_test_state * uts)203 static int hush_test_if_inverted(struct unit_test_state *uts)
204 {
205 char if_formatted[128];
206
207 sprintf(if_formatted, if_format, "test ! aaa = aaa");
208 ut_asserteq(1, run_command(if_formatted, 0));
209
210 sprintf(if_formatted, if_format, "test ! aaa = bbb");
211 ut_assertok(run_command(if_formatted, 0));
212
213 sprintf(if_formatted, if_format, "test ! ! aaa = aaa");
214 ut_assertok(run_command(if_formatted, 0));
215
216 sprintf(if_formatted, if_format, "test ! ! aaa = bbb");
217 ut_asserteq(1, run_command(if_formatted, 0));
218
219 return 0;
220 }
221 HUSH_TEST(hush_test_if_inverted, 0);
222
hush_test_if_binary(struct unit_test_state * uts)223 static int hush_test_if_binary(struct unit_test_state *uts)
224 {
225 char if_formatted[128];
226
227 sprintf(if_formatted, if_format, "test aaa != aaa -o bbb != bbb");
228 ut_asserteq(1, run_command(if_formatted, 0));
229
230 sprintf(if_formatted, if_format, "test aaa != aaa -o bbb = bbb");
231 ut_assertok(run_command(if_formatted, 0));
232
233 sprintf(if_formatted, if_format, "test aaa = aaa -o bbb != bbb");
234 ut_assertok(run_command(if_formatted, 0));
235
236 sprintf(if_formatted, if_format, "test aaa = aaa -o bbb = bbb");
237 ut_assertok(run_command(if_formatted, 0));
238
239 sprintf(if_formatted, if_format, "test aaa != aaa -a bbb != bbb");
240 ut_asserteq(1, run_command(if_formatted, 0));
241
242 sprintf(if_formatted, if_format, "test aaa != aaa -a bbb = bbb");
243 ut_asserteq(1, run_command(if_formatted, 0));
244
245 sprintf(if_formatted, if_format, "test aaa = aaa -a bbb != bbb");
246 ut_asserteq(1, run_command(if_formatted, 0));
247
248 sprintf(if_formatted, if_format, "test aaa = aaa -a bbb = bbb");
249 ut_assertok(run_command(if_formatted, 0));
250
251 return 0;
252 }
253 HUSH_TEST(hush_test_if_binary, 0);
254
hush_test_if_inverted_binary(struct unit_test_state * uts)255 static int hush_test_if_inverted_binary(struct unit_test_state *uts)
256 {
257 char if_formatted[128];
258
259 sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb != bbb");
260 ut_assertok(run_command(if_formatted, 0));
261
262 sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb = bbb");
263 ut_assertok(run_command(if_formatted, 0));
264
265 sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb != bbb");
266 ut_assertok(run_command(if_formatted, 0));
267
268 sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb = bbb");
269 ut_asserteq(1, run_command(if_formatted, 0));
270
271 sprintf(if_formatted, if_format,
272 "test ! ! aaa != aaa -o ! ! bbb != bbb");
273 ut_asserteq(1, run_command(if_formatted, 0));
274
275 sprintf(if_formatted, if_format,
276 "test ! ! aaa != aaa -o ! ! bbb = bbb");
277 ut_assertok(run_command(if_formatted, 0));
278
279 sprintf(if_formatted, if_format,
280 "test ! ! aaa = aaa -o ! ! bbb != bbb");
281 ut_assertok(run_command(if_formatted, 0));
282
283 sprintf(if_formatted, if_format, "test ! ! aaa = aaa -o ! ! bbb = bbb");
284 ut_assertok(run_command(if_formatted, 0));
285
286 return 0;
287 }
288 HUSH_TEST(hush_test_if_inverted_binary, 0);
289
hush_test_if_z_operator(struct unit_test_state * uts)290 static int hush_test_if_z_operator(struct unit_test_state *uts)
291 {
292 char if_formatted[128];
293
294 /* Deal with environment variable used during test. */
295 env_set("ut_var_nonexistent", NULL);
296 env_set("ut_var_exists", "1");
297 env_set("ut_var_unset", "1");
298
299 sprintf(if_formatted, if_format, "test -z \"$ut_var_nonexistent\"");
300 ut_assertok(run_command(if_formatted, 0));
301
302 sprintf(if_formatted, if_format, "test -z \"$ut_var_exists\"");
303 ut_asserteq(1, run_command(if_formatted, 0));
304
305 sprintf(if_formatted, if_format, "test -z \"$ut_var_unset\"");
306 ut_asserteq(1, run_command(if_formatted, 0));
307
308 env_set("ut_var_unset", NULL);
309 sprintf(if_formatted, if_format, "test -z \"$ut_var_unset\"");
310 ut_assertok(run_command(if_formatted, 0));
311
312 /* Clear the set environment variable. */
313 env_set("ut_var_exists", NULL);
314
315 return 0;
316 }
317 HUSH_TEST(hush_test_if_z_operator, 0);
318