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