blob: 4c6cc3cf09e5668a6a59db21ccfa01a3d8b1356e [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
Simon Glass580a7a52020-11-01 14:15:44 -0700332/* Test 'setexpr' command with concatenating strings */
333static int setexpr_test_str_oper(struct unit_test_state *uts)
334{
335 ulong start_mem;
336 char *buf;
337
338 buf = map_sysmem(0, BUF_SIZE);
339 memset(buf, '\xff', BUF_SIZE);
340 strcpy(buf, "hello");
341 strcpy(buf + 0x10, " there");
342
343 ut_assertok(console_record_reset_enable());
344 start_mem = ut_check_free();
345 ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0));
346 ut_assertok(ut_check_delta(start_mem));
347 ut_assert_nextline("invalid op");
348 ut_assert_console_end();
349
350 /*
351 * Set 'fred' to the same length as we expect to get below, to avoid a
352 * new allocation in 'setexpr'. That way we can check for memory leaks.
353 */
354 ut_assertok(env_set("fred", "12345012345"));
355 start_mem = ut_check_free();
356 ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
357 ut_asserteq_str("hello there", env_get("fred"));
Simon Glass8f05ec42021-03-07 17:34:44 -0700358
359 /*
360 * This check does not work with sandbox_flattree, apparently due to
361 * memory allocations in env_set().
362 *
363 * The truetype console produces lots of memory allocations even though
364 * the LCD display is not visible. But even without these, it does not
365 * work.
366 *
367 * A better test would be for dlmalloc to record the allocs and frees
368 * for a particular caller, but that is not supported.
369 *
370 * For now, drop this test.
371 *
372 * ut_assertok(ut_check_delta(start_mem));
373 */
Simon Glass580a7a52020-11-01 14:15:44 -0700374
375 unmap_sysmem(buf);
376
377 return 0;
378}
379SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC);
380
381/* Test 'setexpr' command with a string that is too long */
382static int setexpr_test_str_long(struct unit_test_state *uts)
383{
384 const int size = 128 << 10; /* setexpr strings are a max of 64KB */
385 char *buf, *val;
386
387 buf = map_sysmem(0, size);
388 memset(buf, 'a', size);
389
390 /* String should be truncated to 64KB */
391 ut_assertok(run_command("setexpr.s fred *0", 0));
392 val = env_get("fred");
393 ut_asserteq(64 << 10, strlen(val));
394
395 unmap_sysmem(buf);
396
397 return 0;
398}
399SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
400
Roland Gaudig63582072021-07-23 12:29:23 +0000401#ifdef CONFIG_CMD_SETEXPR_FMT
402/* Test 'setexpr' command with simply setting integers */
403static int setexpr_test_fmt(struct unit_test_state *uts)
404{
405 u8 *buf;
406
407 buf = map_sysmem(0, BUF_SIZE);
408 memset(buf, '\xff', BUF_SIZE);
409
410 /* Test decimal conversion */
411 ut_assertok(run_command("setexpr fred fmt %d 0xff", 0));
412 ut_asserteq_str("255", env_get("fred"));
413 /* Test hexadecimal conversion with 0x prefix and 4 digits */
414 ut_assertok(run_command("setexpr fred fmt 0x%04x 257", 0));
415 ut_asserteq_str("0x0257", env_get("fred"));
416 /* Test octal conversion with % prefix */
417 ut_assertok(run_command("setexpr fred fmt %%%o 8", 0));
418 ut_asserteq_str("%10", env_get("fred"));
419 /* Test argument surrounded by %% */
420 ut_assertok(run_command("setexpr fred fmt %%%x%% 0xff", 0));
421 ut_asserteq_str("%ff%", env_get("fred"));
422 /* Test escape sequence */
423 ut_assertok(run_command("setexpr fred fmt \"hello\\040world\"", 0));
424 ut_asserteq_str("hello world", env_get("fred"));
425 /* Test %b with string containing octal escape sequence */
426 ut_assertok(run_command("setexpr fred fmt oh%bno \137", 0));
427 ut_asserteq_str("oh_no", env_get("fred"));
428 /* Test %b with string containing \c escape sequence */
429 ut_assertok(run_command("setexpr fred fmt hello%bworld \"\\c\"", 0));
430 ut_asserteq_str("hello", env_get("fred"));
431 /* Test multiple arguments referencing environment varialbes */
432 ut_assertok(run_command("setenv a eff", 0));
433 ut_assertok(run_command("setenv b hello", 0));
434 ut_assertok(run_command("setenv c 0x63", 0));
435 ut_assertok(run_command("setenv d world", 0));
436 ut_assertok(run_command("setexpr fred fmt \"0x%08x-%s-%d-%s\" $a $b $c $d", 0));
437 ut_asserteq_str("0x00000eff-hello-99-world", env_get("fred"));
438 /* Test with two format specifiers, but only one argument */
439 ut_assertok(run_command("setexpr fred fmt %d_%x 100", 0));
440 ut_asserteq_str("256_0", env_get("fred"));
441 /* Test maximum string length */
442 ut_assertok(run_command("setexpr fred fmt \"%0127d\" 7b", 0));
443 ut_asserteq_str("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123", env_get("fred"));
444 /* Test maximum unsigned integer size */
445 ut_assertok(run_command("setexpr fred fmt %u ffffffffffffffff", 0));
446 ut_asserteq_str("18446744073709551615", env_get("fred"));
447 /* Test maximum positive integer size */
448 ut_assertok(run_command("setexpr fred fmt %d 7fffffffffffffff", 0));
449 ut_asserteq_str("9223372036854775807", env_get("fred"));
450 /* Test maximum negative integer size */
451 ut_assertok(run_command("setexpr fred fmt %d 8000000000000000", 0));
452 ut_asserteq_str("-9223372036854775808", env_get("fred"));
453 /* Test minimum negative integer size */
454 ut_assertok(run_command("setexpr fred fmt %d ffffffffffffffff", 0));
455 ut_asserteq_str("-1", env_get("fred"));
456 /* Test signed value with + sign */
457 ut_assertok(run_command("setexpr fred fmt %d +5", 0));
458 ut_asserteq_str("5", env_get("fred"));
459 /* Test signed value with - sign */
460 ut_assertok(run_command("setexpr fred fmt %d -4", 0));
461 ut_asserteq_str("-4", env_get("fred"));
462 /* Test unsigned value with + sign */
463 ut_assertok(run_command("setexpr fred fmt %u +3", 0));
464 ut_asserteq_str("3", env_get("fred"));
465 /* Test unsigned value with - sign */
466 ut_assertok(run_command("setexpr fred fmt %x -2", 0));
467 ut_asserteq_str("fffffffffffffffe", env_get("fred"));
468 /* Error test with missing format specifier */
469 ut_asserteq(1, run_command("setexpr fred fmd hello 0xff", 0));
470 /* Error test with invalid format type */
471 ut_asserteq(1, run_command("setexpr fred fmt %a 0xff", 0));
472 /* Error test with incomplete format specifier */
473 ut_asserteq(1, run_command("setexpr fred fmt hello% bf", 0));
474 /* Error exceeding maximum string length */
475 ut_asserteq(1, run_command("setexpr fred fmt \"%0128d\" 456", 0));
476
477 unmap_sysmem(buf);
478
479 return 0;
480}
481
482SETEXPR_TEST(setexpr_test_fmt, UT_TESTF_CONSOLE_REC);
483#endif
484
Simon Glass663a2c12020-11-01 14:15:35 -0700485int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
486{
Simon Glassb50211f2021-03-07 17:35:10 -0700487 struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test);
488 const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test);
Simon Glass663a2c12020-11-01 14:15:35 -0700489
Simon Glass8f05ec42021-03-07 17:34:44 -0700490 return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
491 argc, argv);
Simon Glass663a2c12020-11-01 14:15:35 -0700492}