]> git.ipfire.org Git - people/ms/u-boot.git/blob - test/command_ut.c
test/py: add test of setenv/printenv/echo
[people/ms/u-boot.git] / test / command_ut.c
1 /*
2 * Copyright (c) 2012, The Chromium Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #define DEBUG
8
9 #include <common.h>
10 #ifdef CONFIG_SANDBOX
11 #include <os.h>
12 #endif
13
14 static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
15 "setenv list ${list}3\0"
16 "setenv list ${list}4";
17
18 static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
19 {
20 printf("%s: Testing commands\n", __func__);
21 run_command("env default -f -a", 0);
22
23 /* make sure that compound statements work */
24 #ifdef CONFIG_SYS_HUSH_PARSER
25 run_command("if test -n ${single} ; then setenv check 1; fi", 0);
26 assert(!strcmp("1", getenv("check")));
27 run_command("setenv check", 0);
28 #endif
29
30 /* commands separated by ; */
31 run_command_list("setenv list 1; setenv list ${list}1", -1, 0);
32 assert(!strcmp("11", getenv("list")));
33
34 /* commands separated by \n */
35 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
36 assert(!strcmp("11", getenv("list")));
37
38 /* command followed by \n and nothing else */
39 run_command_list("setenv list 1${list}\n", -1, 0);
40 assert(!strcmp("111", getenv("list")));
41
42 /* three commands in a row */
43 run_command_list("setenv list 1\n setenv list ${list}2; "
44 "setenv list ${list}3", -1, 0);
45 assert(!strcmp("123", getenv("list")));
46
47 /* a command string with \0 in it. Stuff after \0 should be ignored */
48 run_command("setenv list", 0);
49 run_command_list(test_cmd, sizeof(test_cmd), 0);
50 assert(!strcmp("123", getenv("list")));
51
52 /*
53 * a command list where we limit execution to only the first command
54 * using the length parameter.
55 */
56 run_command_list("setenv list 1\n setenv list ${list}2; "
57 "setenv list ${list}3", strlen("setenv list 1"), 0);
58 assert(!strcmp("1", getenv("list")));
59
60 assert(run_command("false", 0) == 1);
61 assert(run_command("echo", 0) == 0);
62 assert(run_command_list("false", -1, 0) == 1);
63 assert(run_command_list("echo", -1, 0) == 0);
64
65 run_command("setenv foo 'setenv monty 1; setenv python 2'", 0);
66 run_command("run foo", 0);
67 assert(getenv("monty") != NULL);
68 assert(!strcmp("1", getenv("monty")));
69 assert(getenv("python") != NULL);
70 assert(!strcmp("2", getenv("python")));
71
72 #ifdef CONFIG_SYS_HUSH_PARSER
73 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
74 run_command("run foo", 0);
75 assert(getenv("black") != NULL);
76 assert(!strcmp("1", getenv("black")));
77 assert(getenv("adder") != NULL);
78 assert(!strcmp("2", getenv("adder")));
79
80 /* Test the 'test' command */
81
82 #define HUSH_TEST(name, expr, expected_result) \
83 run_command("if test " expr " ; then " \
84 "setenv " #name "_" #expected_result " y; else " \
85 "setenv " #name "_" #expected_result " n; fi", 0); \
86 assert(!strcmp(#expected_result, getenv(#name "_" #expected_result))); \
87 setenv(#name "_" #expected_result, NULL);
88
89 /* Basic operators */
90 HUSH_TEST(streq, "aaa = aaa", y);
91 HUSH_TEST(streq, "aaa = bbb", n);
92
93 HUSH_TEST(strneq, "aaa != bbb", y);
94 HUSH_TEST(strneq, "aaa != aaa", n);
95
96 HUSH_TEST(strlt, "aaa < bbb", y);
97 HUSH_TEST(strlt, "bbb < aaa", n);
98
99 HUSH_TEST(strgt, "bbb > aaa", y);
100 HUSH_TEST(strgt, "aaa > bbb", n);
101
102 HUSH_TEST(eq, "123 -eq 123", y);
103 HUSH_TEST(eq, "123 -eq 456", n);
104
105 HUSH_TEST(ne, "123 -ne 456", y);
106 HUSH_TEST(ne, "123 -ne 123", n);
107
108 HUSH_TEST(lt, "123 -lt 456", y);
109 HUSH_TEST(lt_eq, "123 -lt 123", n);
110 HUSH_TEST(lt, "456 -lt 123", n);
111
112 HUSH_TEST(le, "123 -le 456", y);
113 HUSH_TEST(le_eq, "123 -le 123", y);
114 HUSH_TEST(le, "456 -le 123", n);
115
116 HUSH_TEST(gt, "456 -gt 123", y);
117 HUSH_TEST(gt_eq, "123 -gt 123", n);
118 HUSH_TEST(gt, "123 -gt 456", n);
119
120 HUSH_TEST(ge, "456 -ge 123", y);
121 HUSH_TEST(ge_eq, "123 -ge 123", y);
122 HUSH_TEST(ge, "123 -ge 456", n);
123
124 HUSH_TEST(z, "-z \"\"", y);
125 HUSH_TEST(z, "-z \"aaa\"", n);
126
127 HUSH_TEST(n, "-n \"aaa\"", y);
128 HUSH_TEST(n, "-n \"\"", n);
129
130 /* Inversion of simple tests */
131 HUSH_TEST(streq_inv, "! aaa = aaa", n);
132 HUSH_TEST(streq_inv, "! aaa = bbb", y);
133
134 HUSH_TEST(streq_inv_inv, "! ! aaa = aaa", y);
135 HUSH_TEST(streq_inv_inv, "! ! aaa = bbb", n);
136
137 /* Binary operators */
138 HUSH_TEST(or_0_0, "aaa != aaa -o bbb != bbb", n);
139 HUSH_TEST(or_0_1, "aaa != aaa -o bbb = bbb", y);
140 HUSH_TEST(or_1_0, "aaa = aaa -o bbb != bbb", y);
141 HUSH_TEST(or_1_1, "aaa = aaa -o bbb = bbb", y);
142
143 HUSH_TEST(and_0_0, "aaa != aaa -a bbb != bbb", n);
144 HUSH_TEST(and_0_1, "aaa != aaa -a bbb = bbb", n);
145 HUSH_TEST(and_1_0, "aaa = aaa -a bbb != bbb", n);
146 HUSH_TEST(and_1_1, "aaa = aaa -a bbb = bbb", y);
147
148 /* Inversion within binary operators */
149 HUSH_TEST(or_0_0_inv, "! aaa != aaa -o ! bbb != bbb", y);
150 HUSH_TEST(or_0_1_inv, "! aaa != aaa -o ! bbb = bbb", y);
151 HUSH_TEST(or_1_0_inv, "! aaa = aaa -o ! bbb != bbb", y);
152 HUSH_TEST(or_1_1_inv, "! aaa = aaa -o ! bbb = bbb", n);
153
154 HUSH_TEST(or_0_0_inv_inv, "! ! aaa != aaa -o ! ! bbb != bbb", n);
155 HUSH_TEST(or_0_1_inv_inv, "! ! aaa != aaa -o ! ! bbb = bbb", y);
156 HUSH_TEST(or_1_0_inv_inv, "! ! aaa = aaa -o ! ! bbb != bbb", y);
157 HUSH_TEST(or_1_1_inv_inv, "! ! aaa = aaa -o ! ! bbb = bbb", y);
158
159 setenv("ut_var_nonexistent", NULL);
160 setenv("ut_var_exists", "1");
161 HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_nonexistent\"", y);
162 HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_exists\"", n);
163 setenv("ut_var_exists", NULL);
164
165 run_command("setenv ut_var_space \" \"", 0);
166 assert(!strcmp(getenv("ut_var_space"), " "));
167 run_command("setenv ut_var_test $ut_var_space", 0);
168 assert(!getenv("ut_var_test"));
169 run_command("setenv ut_var_test \"$ut_var_space\"", 0);
170 assert(!strcmp(getenv("ut_var_test"), " "));
171 run_command("setenv ut_var_test \" 1${ut_var_space}${ut_var_space} 2 \"", 0);
172 assert(!strcmp(getenv("ut_var_test"), " 1 2 "));
173 setenv("ut_var_space", NULL);
174 setenv("ut_var_test", NULL);
175
176 #ifdef CONFIG_SANDBOX
177 /* File existence */
178 HUSH_TEST(e, "-e hostfs - creating_this_file_breaks_uboot_unit_test", n);
179 run_command("sb save hostfs - creating_this_file_breaks_uboot_unit_test 0 1", 0);
180 HUSH_TEST(e, "-e hostfs - creating_this_file_breaks_uboot_unit_test", y);
181 /* Perhaps this could be replaced by an "rm" shell command one day */
182 assert(!os_unlink("creating_this_file_breaks_uboot_unit_test"));
183 HUSH_TEST(e, "-e hostfs - creating_this_file_breaks_uboot_unit_test", n);
184 #endif
185 #endif
186
187 assert(run_command("", 0) == 0);
188 assert(run_command(" ", 0) == 0);
189
190 assert(run_command("'", 0) == 1);
191
192 printf("%s: Everything went swimmingly\n", __func__);
193 return 0;
194 }
195
196 U_BOOT_CMD(
197 ut_cmd, 5, 1, do_ut_cmd,
198 "Very basic test of command parsers",
199 ""
200 );