blob: ee329e94b85b43f5b13d977d48fb4d03510e0d4a [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
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Simon Glass663a2c12020-11-01 14:15:35 -070010#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
Bin Meng0bcbec72021-02-17 17:04:06 +080018/* Declare a new setexpr test */
Simon Glass663a2c12020-11-01 14:15:35 -070019#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 Glass7c19edf2020-11-01 14:15:37 -070048 ut_asserteq_str("3456789a", env_get("fred"));
Simon Glass663a2c12020-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 Glass7c19edf2020-11-01 14:15:37 -070061 ut_asserteq_str("cdef0123", env_get("fred"));
Simon Glass663a2c12020-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 Glass7c19edf2020-11-01 14:15:37 -070093 ut_asserteq_str("f78ebacf", env_get("fred"));
Simon Glass663a2c12020-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 Glass7c19edf2020-11-01 14:15:37 -0700103 ut_asserteq_str("1407393a6", env_get("fred"));
Simon Glass663a2c12020-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 Glass7c19edf2020-11-01 14:15:37 -0700124 ut_asserteq_str("561234", env_get("fred"));
Simon Glass663a2c12020-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 Glass7c19edf2020-11-01 14:15:37 -0700131 ut_asserteq_str("1200", env_get("fred"));
Simon Glass663a2c12020-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 Glass7c19edf2020-11-01 14:15:37 -0700140 ut_asserteq_str("61ebfa800", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700141
142 ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700143 ut_asserteq_str("4ba", env_get("fred"));
Simon Glass663a2c12020-11-01 14:15:35 -0700144
145 ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
Simon Glass7c19edf2020-11-01 14:15:37 -0700146 ut_asserteq_str("838", env_get("fred"));
Simon Glass663a2c12020-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 Glassbbcdba82020-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));
Simon Glass7a114862020-11-01 14:15:42 -0700169 ut_assertok(run_command("setexpr fred gsub is us", 0));
170 val = env_get("fred");
171 ut_asserteq_str("thus us a test", val);
172
Simon Glassbbcdba82020-11-01 14:15:38 -0700173 /* Global substitution */
174 ut_assertok(run_command("setenv fred 'this is a test'", 0));
175 ut_assertok(run_command("setenv mary 'this is a test'", 0));
176 ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0));
177 val = env_get("fred");
178 ut_asserteq_str("thus us a test", val);
179 val = env_get("mary");
180 ut_asserteq_str("this is a test", val);
181
Massimiliano Minella847f69c2024-02-08 15:58:27 +0100182 /* No match */
183 ut_assertok(run_command("setenv fred 'this is a test'", 0));
184 ut_assertok(run_command("setenv mary ''", 0));
185 ut_assertok(run_command("setexpr fred gsub us is \"${fred}\"", 0));
186 ut_assertok(run_command("setexpr mary gsub us is \"${fred}\"", 0));
187 val = env_get("fred");
188 ut_asserteq_str("this is a test", val);
189 val = env_get("mary");
190 ut_asserteq_str("this is a test", val);
191
Simon Glassbbcdba82020-11-01 14:15:38 -0700192 unmap_sysmem(buf);
193
194 return 0;
195}
196SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC);
197
198/* Test 'setexpr' command with regex replacement that expands the string */
199static int setexpr_test_regex_inc(struct unit_test_state *uts)
200{
201 char *buf, *val;
202
203 buf = map_sysmem(0, BUF_SIZE);
204
205 ut_assertok(run_command("setenv fred 'this is a test'", 0));
Simon Glass7a114862020-11-01 14:15:42 -0700206 ut_assertok(run_command("setexpr fred gsub is much_longer_string", 0));
207 val = env_get("fred");
208 ut_asserteq_str("thmuch_longer_string much_longer_string a test", val);
Simon Glassbbcdba82020-11-01 14:15:38 -0700209 unmap_sysmem(buf);
210
211 return 0;
212}
213SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC);
214
Simon Glass00e44192020-11-01 14:15:40 -0700215/* Test setexpr_regex_sub() directly to check buffer usage */
216static int setexpr_test_sub(struct unit_test_state *uts)
217{
218 char *buf, *nbuf;
219 int i;
220
221 buf = map_sysmem(0, BUF_SIZE);
222 nbuf = map_sysmem(0x1000, BUF_SIZE);
223
224 /* Add a pattern so we can check the buffer limits */
225 memset(buf, '\xff', BUF_SIZE);
226 memset(nbuf, '\xff', BUF_SIZE);
227 for (i = BUF_SIZE; i < 0x1000; i++) {
228 buf[i] = i & 0xff;
229 nbuf[i] = i & 0xff;
230 }
231 strcpy(buf, "this is a test");
232
233 /*
234 * This is a regression test, since a bug was found in the use of
235 * memmove() in setexpr
236 */
237 ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is",
238 "us it is longer", true));
239 ut_asserteq_str("thus it is longer us it is longer a test", buf);
Simon Glass00e44192020-11-01 14:15:40 -0700240 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));
Simon Glass00e44192020-11-01 14:15:40 -0700280 ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test",
281 buf);
282
283 for (i = BUF_SIZE; i < 0x1000; i++) {
284 ut_assertf(buf[i] == (char)i,
285 "buf byte at %x should be %02x, got %02x)\n",
286 i, i & 0xff, (u8)buf[i]);
287 ut_assertf(nbuf[i] == (char)i,
288 "nbuf byte at %x should be %02x, got %02x)\n",
289 i, i & 0xff, (u8)nbuf[i]);
290 }
291
292 unmap_sysmem(buf);
293
294 return 0;
295}
296SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);
297
Simon Glass580a7a52020-11-01 14:15:44 -0700298/* Test 'setexpr' command with setting strings */
299static int setexpr_test_str(struct unit_test_state *uts)
300{
301 ulong start_mem;
302 char *buf;
303
304 buf = map_sysmem(0, BUF_SIZE);
305 memset(buf, '\xff', BUF_SIZE);
306
307 /*
308 * Set 'fred' to the same length as we expect to get below, to avoid a
309 * new allocation in 'setexpr'. That way we can check for memory leaks.
310 */
311 ut_assertok(env_set("fred", "x"));
312 start_mem = ut_check_free();
313 strcpy(buf, "hello");
314 ut_asserteq(1, run_command("setexpr.s fred 0", 0));
315 ut_assertok(ut_check_delta(start_mem));
316
Simon Glass580a7a52020-11-01 14:15:44 -0700317 ut_assertok(env_set("fred", "12345"));
Simon Glass8f05ec42021-03-07 17:34:44 -0700318 start_mem = ut_check_free();
Simon Glass580a7a52020-11-01 14:15:44 -0700319 ut_assertok(run_command("setexpr.s fred *0", 0));
320 ut_asserteq_str("hello", env_get("fred"));
Simon Glassd63c2902022-11-22 15:12:10 -0700321 /*
322 * This fails in CI at present.
323 *
324 * ut_assertok(ut_check_delta(start_mem));
325 */
Simon Glass580a7a52020-11-01 14:15:44 -0700326
327 unmap_sysmem(buf);
328
329 return 0;
330}
331SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC);
332
333
334/* Test 'setexpr' command with concatenating strings */
335static int setexpr_test_str_oper(struct unit_test_state *uts)
336{
337 ulong start_mem;
338 char *buf;
339
340 buf = map_sysmem(0, BUF_SIZE);
341 memset(buf, '\xff', BUF_SIZE);
342 strcpy(buf, "hello");
343 strcpy(buf + 0x10, " there");
344
345 ut_assertok(console_record_reset_enable());
346 start_mem = ut_check_free();
347 ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0));
348 ut_assertok(ut_check_delta(start_mem));
349 ut_assert_nextline("invalid op");
350 ut_assert_console_end();
351
352 /*
353 * Set 'fred' to the same length as we expect to get below, to avoid a
354 * new allocation in 'setexpr'. That way we can check for memory leaks.
355 */
356 ut_assertok(env_set("fred", "12345012345"));
357 start_mem = ut_check_free();
358 ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
359 ut_asserteq_str("hello there", env_get("fred"));
Simon Glass8f05ec42021-03-07 17:34:44 -0700360
361 /*
362 * This check does not work with sandbox_flattree, apparently due to
363 * memory allocations in env_set().
364 *
365 * The truetype console produces lots of memory allocations even though
366 * the LCD display is not visible. But even without these, it does not
367 * work.
368 *
369 * A better test would be for dlmalloc to record the allocs and frees
370 * for a particular caller, but that is not supported.
371 *
372 * For now, drop this test.
373 *
374 * ut_assertok(ut_check_delta(start_mem));
375 */
Simon Glass580a7a52020-11-01 14:15:44 -0700376
377 unmap_sysmem(buf);
378
379 return 0;
380}
381SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC);
382
383/* Test 'setexpr' command with a string that is too long */
384static int setexpr_test_str_long(struct unit_test_state *uts)
385{
386 const int size = 128 << 10; /* setexpr strings are a max of 64KB */
387 char *buf, *val;
388
389 buf = map_sysmem(0, size);
390 memset(buf, 'a', size);
391
392 /* String should be truncated to 64KB */
393 ut_assertok(run_command("setexpr.s fred *0", 0));
394 val = env_get("fred");
395 ut_asserteq(64 << 10, strlen(val));
396
397 unmap_sysmem(buf);
398
399 return 0;
400}
401SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
402
Roland Gaudig63582072021-07-23 12:29:23 +0000403#ifdef CONFIG_CMD_SETEXPR_FMT
404/* Test 'setexpr' command with simply setting integers */
405static int setexpr_test_fmt(struct unit_test_state *uts)
406{
407 u8 *buf;
408
409 buf = map_sysmem(0, BUF_SIZE);
410 memset(buf, '\xff', BUF_SIZE);
411
412 /* Test decimal conversion */
413 ut_assertok(run_command("setexpr fred fmt %d 0xff", 0));
414 ut_asserteq_str("255", env_get("fred"));
415 /* Test hexadecimal conversion with 0x prefix and 4 digits */
416 ut_assertok(run_command("setexpr fred fmt 0x%04x 257", 0));
417 ut_asserteq_str("0x0257", env_get("fred"));
418 /* Test octal conversion with % prefix */
419 ut_assertok(run_command("setexpr fred fmt %%%o 8", 0));
420 ut_asserteq_str("%10", env_get("fred"));
421 /* Test argument surrounded by %% */
422 ut_assertok(run_command("setexpr fred fmt %%%x%% 0xff", 0));
423 ut_asserteq_str("%ff%", env_get("fred"));
424 /* Test escape sequence */
425 ut_assertok(run_command("setexpr fred fmt \"hello\\040world\"", 0));
426 ut_asserteq_str("hello world", env_get("fred"));
427 /* Test %b with string containing octal escape sequence */
428 ut_assertok(run_command("setexpr fred fmt oh%bno \137", 0));
429 ut_asserteq_str("oh_no", env_get("fred"));
430 /* Test %b with string containing \c escape sequence */
431 ut_assertok(run_command("setexpr fred fmt hello%bworld \"\\c\"", 0));
432 ut_asserteq_str("hello", env_get("fred"));
433 /* Test multiple arguments referencing environment varialbes */
434 ut_assertok(run_command("setenv a eff", 0));
435 ut_assertok(run_command("setenv b hello", 0));
436 ut_assertok(run_command("setenv c 0x63", 0));
437 ut_assertok(run_command("setenv d world", 0));
438 ut_assertok(run_command("setexpr fred fmt \"0x%08x-%s-%d-%s\" $a $b $c $d", 0));
439 ut_asserteq_str("0x00000eff-hello-99-world", env_get("fred"));
440 /* Test with two format specifiers, but only one argument */
441 ut_assertok(run_command("setexpr fred fmt %d_%x 100", 0));
442 ut_asserteq_str("256_0", env_get("fred"));
443 /* Test maximum string length */
444 ut_assertok(run_command("setexpr fred fmt \"%0127d\" 7b", 0));
445 ut_asserteq_str("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123", env_get("fred"));
446 /* Test maximum unsigned integer size */
447 ut_assertok(run_command("setexpr fred fmt %u ffffffffffffffff", 0));
448 ut_asserteq_str("18446744073709551615", env_get("fred"));
449 /* Test maximum positive integer size */
450 ut_assertok(run_command("setexpr fred fmt %d 7fffffffffffffff", 0));
451 ut_asserteq_str("9223372036854775807", env_get("fred"));
452 /* Test maximum negative integer size */
453 ut_assertok(run_command("setexpr fred fmt %d 8000000000000000", 0));
454 ut_asserteq_str("-9223372036854775808", env_get("fred"));
455 /* Test minimum negative integer size */
456 ut_assertok(run_command("setexpr fred fmt %d ffffffffffffffff", 0));
457 ut_asserteq_str("-1", env_get("fred"));
458 /* Test signed value with + sign */
459 ut_assertok(run_command("setexpr fred fmt %d +5", 0));
460 ut_asserteq_str("5", env_get("fred"));
461 /* Test signed value with - sign */
462 ut_assertok(run_command("setexpr fred fmt %d -4", 0));
463 ut_asserteq_str("-4", env_get("fred"));
464 /* Test unsigned value with + sign */
465 ut_assertok(run_command("setexpr fred fmt %u +3", 0));
466 ut_asserteq_str("3", env_get("fred"));
467 /* Test unsigned value with - sign */
468 ut_assertok(run_command("setexpr fred fmt %x -2", 0));
469 ut_asserteq_str("fffffffffffffffe", env_get("fred"));
470 /* Error test with missing format specifier */
471 ut_asserteq(1, run_command("setexpr fred fmd hello 0xff", 0));
472 /* Error test with invalid format type */
473 ut_asserteq(1, run_command("setexpr fred fmt %a 0xff", 0));
474 /* Error test with incomplete format specifier */
475 ut_asserteq(1, run_command("setexpr fred fmt hello% bf", 0));
476 /* Error exceeding maximum string length */
477 ut_asserteq(1, run_command("setexpr fred fmt \"%0128d\" 456", 0));
478
479 unmap_sysmem(buf);
480
481 return 0;
482}
483
484SETEXPR_TEST(setexpr_test_fmt, UT_TESTF_CONSOLE_REC);
485#endif
486
Simon Glass663a2c12020-11-01 14:15:35 -0700487int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
488{
Simon Glassb50211f2021-03-07 17:35:10 -0700489 struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test);
490 const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test);
Simon Glass663a2c12020-11-01 14:15:35 -0700491
Simon Glass8f05ec42021-03-07 17:34:44 -0700492 return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
493 argc, argv);
Simon Glass663a2c12020-11-01 14:15:35 -0700494}