blob: d50ce5803c39927e408186e621162ff8b9ace206 [file] [log] [blame]
Simon Glass663a2c12020-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
Simon Glass663a2c12020-11-01 14:15:35 -07009#include <console.h>
10#include <mapmem.h>
11#include <dm/test.h>
12#include <test/suites.h>
13#include <test/ut.h>
14
15#define BUF_SIZE 0x100
16
Bin Meng0bcbec72021-02-17 17:04:06 +080017/* Declare a new setexpr test */
Simon Glass663a2c12020-11-01 14:15:35 -070018#define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test)
19
20/* Test 'setexpr' command with simply setting integers */
21static int setexpr_test_int(struct unit_test_state *uts)
22{
23 u8 *buf;
24
25 buf = map_sysmem(0, BUF_SIZE);
26 memset(buf, '\xff', BUF_SIZE);
27
28 /* byte */
29 buf[0x0] = 0x12;
30 ut_assertok(run_command("setexpr.b fred 0", 0));
31 ut_asserteq_str("0", env_get("fred"));
32 ut_assertok(run_command("setexpr.b fred *0", 0));
33 ut_asserteq_str("12", env_get("fred"));
34
35 /* 16-bit */
36 *(short *)buf = 0x2345;
37 ut_assertok(run_command("setexpr.w fred 0", 0));
38 ut_asserteq_str("0", env_get("fred"));
39 ut_assertok(run_command("setexpr.w fred *0", 0));
40 ut_asserteq_str("2345", env_get("fred"));
41
42 /* 32-bit */
43 *(u32 *)buf = 0x3456789a;
44 ut_assertok(run_command("setexpr.l fred 0", 0));
45 ut_asserteq_str("0", env_get("fred"));
46 ut_assertok(run_command("setexpr.l fred *0", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -070047 ut_asserteq_str("3456789a", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -070048
49 /* 64-bit */
50 *(u64 *)buf = 0x456789abcdef0123;
51 ut_assertok(run_command("setexpr.q fred 0", 0));
52 ut_asserteq_str("0", env_get("fred"));
53 ut_assertok(run_command("setexpr.q fred *0", 0));
54 ut_asserteq_str("456789abcdef0123", env_get("fred"));
55
56 /* default */
57 ut_assertok(run_command("setexpr fred 0", 0));
58 ut_asserteq_str("0", env_get("fred"));
59 ut_assertok(run_command("setexpr fred *0", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -070060 ut_asserteq_str("cdef0123", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -070061
62 unmap_sysmem(buf);
63
64 return 0;
65}
66SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC);
67
68/* Test 'setexpr' command with + operator */
69static int setexpr_test_plus(struct unit_test_state *uts)
70{
71 char *buf;
72
73 buf = map_sysmem(0, BUF_SIZE);
74 memset(buf, '\xff', BUF_SIZE);
75
76 /* byte */
77 buf[0x0] = 0x12;
78 buf[0x10] = 0x34;
79 ut_assertok(run_command("setexpr.b fred *0 + *10", 0));
80 ut_asserteq_str("46", env_get("fred"));
81
82 /* 16-bit */
83 *(short *)buf = 0x2345;
84 *(short *)(buf + 0x10) = 0xf012;
85 ut_assertok(run_command("setexpr.w fred *0 + *10", 0));
86 ut_asserteq_str("11357", env_get("fred"));
87
88 /* 32-bit */
89 *(u32 *)buf = 0x3456789a;
90 *(u32 *)(buf + 0x10) = 0xc3384235;
91 ut_assertok(run_command("setexpr.l fred *0 + *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -070092 ut_asserteq_str("f78ebacf", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -070093
94 /* 64-bit */
95 *(u64 *)buf = 0x456789abcdef0123;
96 *(u64 *)(buf + 0x10) = 0x4987328372849283;
97 ut_assertok(run_command("setexpr.q fred *0 + *10", 0));
98 ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
99
100 /* default */
101 ut_assertok(run_command("setexpr fred *0 + *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700102 ut_asserteq_str("1407393a6", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700103
104 unmap_sysmem(buf);
105
106 return 0;
107}
108SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC);
109
110/* Test 'setexpr' command with other operators */
111static int setexpr_test_oper(struct unit_test_state *uts)
112{
113 char *buf;
114
115 buf = map_sysmem(0, BUF_SIZE);
116 memset(buf, '\xff', BUF_SIZE);
117
118 *(u32 *)buf = 0x1234;
119 *(u32 *)(buf + 0x10) = 0x560000;
120
121 /* Quote | to avoid confusing hush */
122 ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700123 ut_asserteq_str("561234", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700124
125 *(u32 *)buf = 0x561200;
126 *(u32 *)(buf + 0x10) = 0x1234;
127
128 /* Quote & to avoid confusing hush */
129 ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700130 ut_asserteq_str("1200", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700131
132 ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0));
133 ut_asserteq_str("560034", env_get("fred"));
134
135 ut_assertok(run_command("setexpr.l fred *0 - *10", 0));
136 ut_asserteq_str("55ffcc", env_get("fred"));
137
138 ut_assertok(run_command("setexpr.l fred *0 * *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700139 ut_asserteq_str("61ebfa800", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700140
141 ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700142 ut_asserteq_str("4ba", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700143
144 ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700145 ut_asserteq_str("838", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700146
147 unmap_sysmem(buf);
148
149 return 0;
150}
151SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC);
152
Simon Glassbbcdba82020-11-01 14:15:38 -0700153/* Test 'setexpr' command with regex */
154static int setexpr_test_regex(struct unit_test_state *uts)
155{
156 char *buf, *val;
157
158 buf = map_sysmem(0, BUF_SIZE);
159
160 /* Single substitution */
161 ut_assertok(run_command("setenv fred 'this is a test'", 0));
162 ut_assertok(run_command("setexpr fred sub is us", 0));
163 val = env_get("fred");
164 ut_asserteq_str("thus is a test", val);
165
166 /* Global substitution */
167 ut_assertok(run_command("setenv fred 'this is a test'", 0));
Simon Glass7a114862020-11-01 14:15:42 -0700168 ut_assertok(run_command("setexpr fred gsub is us", 0));
169 val = env_get("fred");
170 ut_asserteq_str("thus us a test", val);
171
Simon Glassbbcdba82020-11-01 14:15:38 -0700172 /* Global substitution */
173 ut_assertok(run_command("setenv fred 'this is a test'", 0));
174 ut_assertok(run_command("setenv mary 'this is a test'", 0));
175 ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0));
176 val = env_get("fred");
177 ut_asserteq_str("thus us a test", val);
178 val = env_get("mary");
179 ut_asserteq_str("this is a test", val);
180
Massimiliano Minella847f69c2024-02-08 15:58:27 +0100181 /* No match */
182 ut_assertok(run_command("setenv fred 'this is a test'", 0));
183 ut_assertok(run_command("setenv mary ''", 0));
184 ut_assertok(run_command("setexpr fred gsub us is \"${fred}\"", 0));
185 ut_assertok(run_command("setexpr mary gsub us is \"${fred}\"", 0));
186 val = env_get("fred");
187 ut_asserteq_str("this is a test", val);
188 val = env_get("mary");
189 ut_asserteq_str("this is a test", val);
190
Simon Glassbbcdba82020-11-01 14:15:38 -0700191 unmap_sysmem(buf);
192
193 return 0;
194}
195SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC);
196
197/* Test 'setexpr' command with regex replacement that expands the string */
198static int setexpr_test_regex_inc(struct unit_test_state *uts)
199{
200 char *buf, *val;
201
202 buf = map_sysmem(0, BUF_SIZE);
203
204 ut_assertok(run_command("setenv fred 'this is a test'", 0));
Simon Glass7a114862020-11-01 14:15:42 -0700205 ut_assertok(run_command("setexpr fred gsub is much_longer_string", 0));
206 val = env_get("fred");
207 ut_asserteq_str("thmuch_longer_string much_longer_string a test", val);
Simon Glassbbcdba82020-11-01 14:15:38 -0700208 unmap_sysmem(buf);
209
210 return 0;
211}
212SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC);
213
Simon Glass00e44192020-11-01 14:15:40 -0700214/* Test setexpr_regex_sub() directly to check buffer usage */
215static int setexpr_test_sub(struct unit_test_state *uts)
216{
217 char *buf, *nbuf;
218 int i;
219
220 buf = map_sysmem(0, BUF_SIZE);
221 nbuf = map_sysmem(0x1000, BUF_SIZE);
222
223 /* Add a pattern so we can check the buffer limits */
224 memset(buf, '\xff', BUF_SIZE);
225 memset(nbuf, '\xff', BUF_SIZE);
226 for (i = BUF_SIZE; i < 0x1000; i++) {
227 buf[i] = i & 0xff;
228 nbuf[i] = i & 0xff;
229 }
230 strcpy(buf, "this is a test");
231
232 /*
233 * This is a regression test, since a bug was found in the use of
234 * memmove() in setexpr
235 */
236 ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is",
237 "us it is longer", true));
238 ut_asserteq_str("thus it is longer us it is longer a test", buf);
Simon Glass00e44192020-11-01 14:15:40 -0700239 for (i = BUF_SIZE; i < 0x1000; i++) {
240 ut_assertf(buf[i] == (char)i,
241 "buf byte at %x should be %02x, got %02x)\n",
242 i, i & 0xff, (u8)buf[i]);
243 ut_assertf(nbuf[i] == (char)i,
244 "nbuf byte at %x should be %02x, got %02x)\n",
245 i, i & 0xff, (u8)nbuf[i]);
246 }
247
248 unmap_sysmem(buf);
249
250 return 0;
251}
252SETEXPR_TEST(setexpr_test_sub, UT_TESTF_CONSOLE_REC);
253
254/* Test setexpr_regex_sub() with back references */
255static int setexpr_test_backref(struct unit_test_state *uts)
256{
257 char *buf, *nbuf;
258 int i;
259
260 buf = map_sysmem(0, BUF_SIZE);
261 nbuf = map_sysmem(0x1000, BUF_SIZE);
262
263 /* Add a pattern so we can check the buffer limits */
264 memset(buf, '\xff', BUF_SIZE);
265 memset(nbuf, '\xff', BUF_SIZE);
266 for (i = BUF_SIZE; i < 0x1000; i++) {
267 buf[i] = i & 0xff;
268 nbuf[i] = i & 0xff;
269 }
270 strcpy(buf, "this is surely a test is it? yes this is indeed a test");
271
272 /*
273 * This is a regression test, since a bug was found in the use of
274 * memmove() in setexpr
275 */
276 ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE,
277 "(this) (is) (surely|indeed)",
278 "us \\1 \\2 \\3!", true));
Simon Glass00e44192020-11-01 14:15:40 -0700279 ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test",
280 buf);
281
282 for (i = BUF_SIZE; i < 0x1000; i++) {
283 ut_assertf(buf[i] == (char)i,
284 "buf byte at %x should be %02x, got %02x)\n",
285 i, i & 0xff, (u8)buf[i]);
286 ut_assertf(nbuf[i] == (char)i,
287 "nbuf byte at %x should be %02x, got %02x)\n",
288 i, i & 0xff, (u8)nbuf[i]);
289 }
290
291 unmap_sysmem(buf);
292
293 return 0;
294}
295SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);
296
Simon Glass580a7a52020-11-01 14:15:44 -0700297/* Test 'setexpr' command with setting strings */
298static int setexpr_test_str(struct unit_test_state *uts)
299{
300 ulong start_mem;
301 char *buf;
302
303 buf = map_sysmem(0, BUF_SIZE);
304 memset(buf, '\xff', BUF_SIZE);
305
306 /*
307 * Set 'fred' to the same length as we expect to get below, to avoid a
308 * new allocation in 'setexpr'. That way we can check for memory leaks.
309 */
310 ut_assertok(env_set("fred", "x"));
311 start_mem = ut_check_free();
312 strcpy(buf, "hello");
313 ut_asserteq(1, run_command("setexpr.s fred 0", 0));
314 ut_assertok(ut_check_delta(start_mem));
315
Simon Glass580a7a52020-11-01 14:15:44 -0700316 ut_assertok(env_set("fred", "12345"));
Simon Glass8f05ec42021-03-07 17:34:44 -0700317 start_mem = ut_check_free();
Simon Glass580a7a52020-11-01 14:15:44 -0700318 ut_assertok(run_command("setexpr.s fred *0", 0));
319 ut_asserteq_str("hello", env_get("fred"));
Simon Glassd63c2902022-11-22 15:12:10 -0700320 /*
321 * This fails in CI at present.
322 *
323 * ut_assertok(ut_check_delta(start_mem));
324 */
Simon Glass580a7a52020-11-01 14:15:44 -0700325
326 unmap_sysmem(buf);
327
328 return 0;
329}
330SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC);
331
332
333/* Test 'setexpr' command with concatenating strings */
334static int setexpr_test_str_oper(struct unit_test_state *uts)
335{
336 ulong start_mem;
337 char *buf;
338
339 buf = map_sysmem(0, BUF_SIZE);
340 memset(buf, '\xff', BUF_SIZE);
341 strcpy(buf, "hello");
342 strcpy(buf + 0x10, " there");
343
344 ut_assertok(console_record_reset_enable());
345 start_mem = ut_check_free();
346 ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0));
347 ut_assertok(ut_check_delta(start_mem));
348 ut_assert_nextline("invalid op");
349 ut_assert_console_end();
350
351 /*
352 * Set 'fred' to the same length as we expect to get below, to avoid a
353 * new allocation in 'setexpr'. That way we can check for memory leaks.
354 */
355 ut_assertok(env_set("fred", "12345012345"));
356 start_mem = ut_check_free();
357 ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
358 ut_asserteq_str("hello there", env_get("fred"));
Simon Glass8f05ec42021-03-07 17:34:44 -0700359
360 /*
361 * This check does not work with sandbox_flattree, apparently due to
362 * memory allocations in env_set().
363 *
364 * The truetype console produces lots of memory allocations even though
365 * the LCD display is not visible. But even without these, it does not
366 * work.
367 *
368 * A better test would be for dlmalloc to record the allocs and frees
369 * for a particular caller, but that is not supported.
370 *
371 * For now, drop this test.
372 *
373 * ut_assertok(ut_check_delta(start_mem));
374 */
Simon Glass580a7a52020-11-01 14:15:44 -0700375
376 unmap_sysmem(buf);
377
378 return 0;
379}
380SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC);
381
382/* Test 'setexpr' command with a string that is too long */
383static int setexpr_test_str_long(struct unit_test_state *uts)
384{
385 const int size = 128 << 10; /* setexpr strings are a max of 64KB */
386 char *buf, *val;
387
388 buf = map_sysmem(0, size);
389 memset(buf, 'a', size);
390
391 /* String should be truncated to 64KB */
392 ut_assertok(run_command("setexpr.s fred *0", 0));
393 val = env_get("fred");
394 ut_asserteq(64 << 10, strlen(val));
395
396 unmap_sysmem(buf);
397
398 return 0;
399}
400SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
401
Roland Gaudig63582072021-07-23 12:29:23 +0000402#ifdef CONFIG_CMD_SETEXPR_FMT
403/* Test 'setexpr' command with simply setting integers */
404static int setexpr_test_fmt(struct unit_test_state *uts)
405{
406 u8 *buf;
407
408 buf = map_sysmem(0, BUF_SIZE);
409 memset(buf, '\xff', BUF_SIZE);
410
411 /* Test decimal conversion */
412 ut_assertok(run_command("setexpr fred fmt %d 0xff", 0));
413 ut_asserteq_str("255", env_get("fred"));
414 /* Test hexadecimal conversion with 0x prefix and 4 digits */
415 ut_assertok(run_command("setexpr fred fmt 0x%04x 257", 0));
416 ut_asserteq_str("0x0257", env_get("fred"));
417 /* Test octal conversion with % prefix */
418 ut_assertok(run_command("setexpr fred fmt %%%o 8", 0));
419 ut_asserteq_str("%10", env_get("fred"));
420 /* Test argument surrounded by %% */
421 ut_assertok(run_command("setexpr fred fmt %%%x%% 0xff", 0));
422 ut_asserteq_str("%ff%", env_get("fred"));
423 /* Test escape sequence */
424 ut_assertok(run_command("setexpr fred fmt \"hello\\040world\"", 0));
425 ut_asserteq_str("hello world", env_get("fred"));
426 /* Test %b with string containing octal escape sequence */
427 ut_assertok(run_command("setexpr fred fmt oh%bno \137", 0));
428 ut_asserteq_str("oh_no", env_get("fred"));
429 /* Test %b with string containing \c escape sequence */
430 ut_assertok(run_command("setexpr fred fmt hello%bworld \"\\c\"", 0));
431 ut_asserteq_str("hello", env_get("fred"));
432 /* Test multiple arguments referencing environment varialbes */
433 ut_assertok(run_command("setenv a eff", 0));
434 ut_assertok(run_command("setenv b hello", 0));
435 ut_assertok(run_command("setenv c 0x63", 0));
436 ut_assertok(run_command("setenv d world", 0));
437 ut_assertok(run_command("setexpr fred fmt \"0x%08x-%s-%d-%s\" $a $b $c $d", 0));
438 ut_asserteq_str("0x00000eff-hello-99-world", env_get("fred"));
439 /* Test with two format specifiers, but only one argument */
440 ut_assertok(run_command("setexpr fred fmt %d_%x 100", 0));
441 ut_asserteq_str("256_0", env_get("fred"));
442 /* Test maximum string length */
443 ut_assertok(run_command("setexpr fred fmt \"%0127d\" 7b", 0));
444 ut_asserteq_str("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123", env_get("fred"));
445 /* Test maximum unsigned integer size */
446 ut_assertok(run_command("setexpr fred fmt %u ffffffffffffffff", 0));
447 ut_asserteq_str("18446744073709551615", env_get("fred"));
448 /* Test maximum positive integer size */
449 ut_assertok(run_command("setexpr fred fmt %d 7fffffffffffffff", 0));
450 ut_asserteq_str("9223372036854775807", env_get("fred"));
451 /* Test maximum negative integer size */
452 ut_assertok(run_command("setexpr fred fmt %d 8000000000000000", 0));
453 ut_asserteq_str("-9223372036854775808", env_get("fred"));
454 /* Test minimum negative integer size */
455 ut_assertok(run_command("setexpr fred fmt %d ffffffffffffffff", 0));
456 ut_asserteq_str("-1", env_get("fred"));
457 /* Test signed value with + sign */
458 ut_assertok(run_command("setexpr fred fmt %d +5", 0));
459 ut_asserteq_str("5", env_get("fred"));
460 /* Test signed value with - sign */
461 ut_assertok(run_command("setexpr fred fmt %d -4", 0));
462 ut_asserteq_str("-4", env_get("fred"));
463 /* Test unsigned value with + sign */
464 ut_assertok(run_command("setexpr fred fmt %u +3", 0));
465 ut_asserteq_str("3", env_get("fred"));
466 /* Test unsigned value with - sign */
467 ut_assertok(run_command("setexpr fred fmt %x -2", 0));
468 ut_asserteq_str("fffffffffffffffe", env_get("fred"));
469 /* Error test with missing format specifier */
470 ut_asserteq(1, run_command("setexpr fred fmd hello 0xff", 0));
471 /* Error test with invalid format type */
472 ut_asserteq(1, run_command("setexpr fred fmt %a 0xff", 0));
473 /* Error test with incomplete format specifier */
474 ut_asserteq(1, run_command("setexpr fred fmt hello% bf", 0));
475 /* Error exceeding maximum string length */
476 ut_asserteq(1, run_command("setexpr fred fmt \"%0128d\" 456", 0));
477
478 unmap_sysmem(buf);
479
480 return 0;
481}
482
483SETEXPR_TEST(setexpr_test_fmt, UT_TESTF_CONSOLE_REC);
484#endif
485
Simon Glass663a2c12020-11-01 14:15:35 -0700486int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
487{
Simon Glassb50211f2021-03-07 17:35:10 -0700488 struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test);
489 const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test);
Simon Glass663a2c12020-11-01 14:15:35 -0700490
Simon Glass8f05ec42021-03-07 17:34:44 -0700491 return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
492 argc, argv);
Simon Glass663a2c12020-11-01 14:15:35 -0700493}