blob: a6940fd82dd6e79e06b0c4a2dc8ec21c02a83158 [file] [log] [blame]
Simon Glass90a99012020-11-01 14:15:35 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for setexpr command
4 *
5 * Copyright 2020 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
10#include <console.h>
11#include <mapmem.h>
12#include <dm/test.h>
13#include <test/suites.h>
14#include <test/ut.h>
15
16#define BUF_SIZE 0x100
17
18/* Declare a new mem test */
19#define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test)
20
21/* Test 'setexpr' command with simply setting integers */
22static int setexpr_test_int(struct unit_test_state *uts)
23{
24 u8 *buf;
25
26 buf = map_sysmem(0, BUF_SIZE);
27 memset(buf, '\xff', BUF_SIZE);
28
29 /* byte */
30 buf[0x0] = 0x12;
31 ut_assertok(run_command("setexpr.b fred 0", 0));
32 ut_asserteq_str("0", env_get("fred"));
33 ut_assertok(run_command("setexpr.b fred *0", 0));
34 ut_asserteq_str("12", env_get("fred"));
35
36 /* 16-bit */
37 *(short *)buf = 0x2345;
38 ut_assertok(run_command("setexpr.w fred 0", 0));
39 ut_asserteq_str("0", env_get("fred"));
40 ut_assertok(run_command("setexpr.w fred *0", 0));
41 ut_asserteq_str("2345", env_get("fred"));
42
43 /* 32-bit */
44 *(u32 *)buf = 0x3456789a;
45 ut_assertok(run_command("setexpr.l fred 0", 0));
46 ut_asserteq_str("0", env_get("fred"));
47 ut_assertok(run_command("setexpr.l fred *0", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -070048 ut_asserteq_str("3456789a", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -070049
50 /* 64-bit */
51 *(u64 *)buf = 0x456789abcdef0123;
52 ut_assertok(run_command("setexpr.q fred 0", 0));
53 ut_asserteq_str("0", env_get("fred"));
54 ut_assertok(run_command("setexpr.q fred *0", 0));
55 ut_asserteq_str("456789abcdef0123", env_get("fred"));
56
57 /* default */
58 ut_assertok(run_command("setexpr fred 0", 0));
59 ut_asserteq_str("0", env_get("fred"));
60 ut_assertok(run_command("setexpr fred *0", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -070061 ut_asserteq_str("cdef0123", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -070062
63 unmap_sysmem(buf);
64
65 return 0;
66}
67SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC);
68
69/* Test 'setexpr' command with + operator */
70static int setexpr_test_plus(struct unit_test_state *uts)
71{
72 char *buf;
73
74 buf = map_sysmem(0, BUF_SIZE);
75 memset(buf, '\xff', BUF_SIZE);
76
77 /* byte */
78 buf[0x0] = 0x12;
79 buf[0x10] = 0x34;
80 ut_assertok(run_command("setexpr.b fred *0 + *10", 0));
81 ut_asserteq_str("46", env_get("fred"));
82
83 /* 16-bit */
84 *(short *)buf = 0x2345;
85 *(short *)(buf + 0x10) = 0xf012;
86 ut_assertok(run_command("setexpr.w fred *0 + *10", 0));
87 ut_asserteq_str("11357", env_get("fred"));
88
89 /* 32-bit */
90 *(u32 *)buf = 0x3456789a;
91 *(u32 *)(buf + 0x10) = 0xc3384235;
92 ut_assertok(run_command("setexpr.l fred *0 + *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -070093 ut_asserteq_str("f78ebacf", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -070094
95 /* 64-bit */
96 *(u64 *)buf = 0x456789abcdef0123;
97 *(u64 *)(buf + 0x10) = 0x4987328372849283;
98 ut_assertok(run_command("setexpr.q fred *0 + *10", 0));
99 ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
100
101 /* default */
102 ut_assertok(run_command("setexpr fred *0 + *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -0700103 ut_asserteq_str("1407393a6", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -0700104
105 unmap_sysmem(buf);
106
107 return 0;
108}
109SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC);
110
111/* Test 'setexpr' command with other operators */
112static int setexpr_test_oper(struct unit_test_state *uts)
113{
114 char *buf;
115
116 buf = map_sysmem(0, BUF_SIZE);
117 memset(buf, '\xff', BUF_SIZE);
118
119 *(u32 *)buf = 0x1234;
120 *(u32 *)(buf + 0x10) = 0x560000;
121
122 /* Quote | to avoid confusing hush */
123 ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -0700124 ut_asserteq_str("561234", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -0700125
126 *(u32 *)buf = 0x561200;
127 *(u32 *)(buf + 0x10) = 0x1234;
128
129 /* Quote & to avoid confusing hush */
130 ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -0700131 ut_asserteq_str("1200", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -0700132
133 ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0));
134 ut_asserteq_str("560034", env_get("fred"));
135
136 ut_assertok(run_command("setexpr.l fred *0 - *10", 0));
137 ut_asserteq_str("55ffcc", env_get("fred"));
138
139 ut_assertok(run_command("setexpr.l fred *0 * *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -0700140 ut_asserteq_str("61ebfa800", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -0700141
142 ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -0700143 ut_asserteq_str("4ba", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -0700144
145 ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
Simon Glass25a43ac2020-11-01 14:15:37 -0700146 ut_asserteq_str("838", env_get("fred"));
Simon Glass90a99012020-11-01 14:15:35 -0700147
148 unmap_sysmem(buf);
149
150 return 0;
151}
152SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC);
153
Simon Glasse7131242020-11-01 14:15:38 -0700154/* Test 'setexpr' command with regex */
155static int setexpr_test_regex(struct unit_test_state *uts)
156{
157 char *buf, *val;
158
159 buf = map_sysmem(0, BUF_SIZE);
160
161 /* Single substitution */
162 ut_assertok(run_command("setenv fred 'this is a test'", 0));
163 ut_assertok(run_command("setexpr fred sub is us", 0));
164 val = env_get("fred");
165 ut_asserteq_str("thus is a test", val);
166
167 /* Global substitution */
168 ut_assertok(run_command("setenv fred 'this is a test'", 0));
169 if (0) {
170 /* Causes a crash at present due to a bug in setexpr */
171 ut_assertok(run_command("setexpr fred gsub is us", 0));
172 val = env_get("fred");
173 ut_asserteq_str("thus us a test", val);
174 }
175 /* Global substitution */
176 ut_assertok(run_command("setenv fred 'this is a test'", 0));
177 ut_assertok(run_command("setenv mary 'this is a test'", 0));
178 ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0));
179 val = env_get("fred");
180 ut_asserteq_str("thus us a test", val);
181 val = env_get("mary");
182 ut_asserteq_str("this is a test", val);
183
184 unmap_sysmem(buf);
185
186 return 0;
187}
188SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC);
189
190/* Test 'setexpr' command with regex replacement that expands the string */
191static int setexpr_test_regex_inc(struct unit_test_state *uts)
192{
193 char *buf, *val;
194
195 buf = map_sysmem(0, BUF_SIZE);
196
197 ut_assertok(run_command("setenv fred 'this is a test'", 0));
198 if (0) {
199 /* Causes a crash at present due to a bug in setexpr */
200 ut_assertok(run_command("setexpr fred gsub is much_longer_string",
201 0));
202 val = env_get("fred");
203 ut_asserteq_str("thmuch_longer_string much_longer_string a test",
204 val);
205 }
206 unmap_sysmem(buf);
207
208 return 0;
209}
210SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC);
211
Simon Glassd422c772020-11-01 14:15:40 -0700212/* Test setexpr_regex_sub() directly to check buffer usage */
213static int setexpr_test_sub(struct unit_test_state *uts)
214{
215 char *buf, *nbuf;
216 int i;
217
218 buf = map_sysmem(0, BUF_SIZE);
219 nbuf = map_sysmem(0x1000, BUF_SIZE);
220
221 /* Add a pattern so we can check the buffer limits */
222 memset(buf, '\xff', BUF_SIZE);
223 memset(nbuf, '\xff', BUF_SIZE);
224 for (i = BUF_SIZE; i < 0x1000; i++) {
225 buf[i] = i & 0xff;
226 nbuf[i] = i & 0xff;
227 }
228 strcpy(buf, "this is a test");
229
230 /*
231 * This is a regression test, since a bug was found in the use of
232 * memmove() in setexpr
233 */
234 ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is",
235 "us it is longer", true));
236 ut_asserteq_str("thus it is longer us it is longer a test", buf);
237
238 /* The following checks fail at present due to a bug in setexpr */
239 return 0;
240 for (i = BUF_SIZE; i < 0x1000; i++) {
241 ut_assertf(buf[i] == (char)i,
242 "buf byte at %x should be %02x, got %02x)\n",
243 i, i & 0xff, (u8)buf[i]);
244 ut_assertf(nbuf[i] == (char)i,
245 "nbuf byte at %x should be %02x, got %02x)\n",
246 i, i & 0xff, (u8)nbuf[i]);
247 }
248
249 unmap_sysmem(buf);
250
251 return 0;
252}
253SETEXPR_TEST(setexpr_test_sub, UT_TESTF_CONSOLE_REC);
254
255/* Test setexpr_regex_sub() with back references */
256static int setexpr_test_backref(struct unit_test_state *uts)
257{
258 char *buf, *nbuf;
259 int i;
260
261 buf = map_sysmem(0, BUF_SIZE);
262 nbuf = map_sysmem(0x1000, BUF_SIZE);
263
264 /* Add a pattern so we can check the buffer limits */
265 memset(buf, '\xff', BUF_SIZE);
266 memset(nbuf, '\xff', BUF_SIZE);
267 for (i = BUF_SIZE; i < 0x1000; i++) {
268 buf[i] = i & 0xff;
269 nbuf[i] = i & 0xff;
270 }
271 strcpy(buf, "this is surely a test is it? yes this is indeed a test");
272
273 /*
274 * This is a regression test, since a bug was found in the use of
275 * memmove() in setexpr
276 */
277 ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE,
278 "(this) (is) (surely|indeed)",
279 "us \\1 \\2 \\3!", true));
280
281 /* The following checks fail at present due to bugs in setexpr */
282 return 0;
283 ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test",
284 buf);
285
286 for (i = BUF_SIZE; i < 0x1000; i++) {
287 ut_assertf(buf[i] == (char)i,
288 "buf byte at %x should be %02x, got %02x)\n",
289 i, i & 0xff, (u8)buf[i]);
290 ut_assertf(nbuf[i] == (char)i,
291 "nbuf byte at %x should be %02x, got %02x)\n",
292 i, i & 0xff, (u8)nbuf[i]);
293 }
294
295 unmap_sysmem(buf);
296
297 return 0;
298}
299SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);
300
Simon Glass90a99012020-11-01 14:15:35 -0700301int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
302{
303 struct unit_test *tests = ll_entry_start(struct unit_test,
304 setexpr_test);
305 const int n_ents = ll_entry_count(struct unit_test, setexpr_test);
306
307 return cmd_ut_category("cmd_setexpr", "cmd_mem_", tests, n_ents, argc,
308 argv);
309}