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