setexpr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // 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. #include <common.h>
  9. #include <console.h>
  10. #include <mapmem.h>
  11. #include <dm/test.h>
  12. #include <test/suites.h>
  13. #include <test/ut.h>
  14. #define BUF_SIZE 0x100
  15. /* Declare a new setexpr test */
  16. #define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test)
  17. /* Test 'setexpr' command with simply setting integers */
  18. static int setexpr_test_int(struct unit_test_state *uts)
  19. {
  20. u8 *buf;
  21. buf = map_sysmem(0, BUF_SIZE);
  22. memset(buf, '\xff', BUF_SIZE);
  23. /* byte */
  24. buf[0x0] = 0x12;
  25. ut_assertok(run_command("setexpr.b fred 0", 0));
  26. ut_asserteq_str("0", env_get("fred"));
  27. ut_assertok(run_command("setexpr.b fred *0", 0));
  28. ut_asserteq_str("12", env_get("fred"));
  29. /* 16-bit */
  30. *(short *)buf = 0x2345;
  31. ut_assertok(run_command("setexpr.w fred 0", 0));
  32. ut_asserteq_str("0", env_get("fred"));
  33. ut_assertok(run_command("setexpr.w fred *0", 0));
  34. ut_asserteq_str("2345", env_get("fred"));
  35. /* 32-bit */
  36. *(u32 *)buf = 0x3456789a;
  37. ut_assertok(run_command("setexpr.l fred 0", 0));
  38. ut_asserteq_str("0", env_get("fred"));
  39. ut_assertok(run_command("setexpr.l fred *0", 0));
  40. ut_asserteq_str("3456789a", env_get("fred"));
  41. /* 64-bit */
  42. *(u64 *)buf = 0x456789abcdef0123;
  43. ut_assertok(run_command("setexpr.q fred 0", 0));
  44. ut_asserteq_str("0", env_get("fred"));
  45. ut_assertok(run_command("setexpr.q fred *0", 0));
  46. ut_asserteq_str("456789abcdef0123", env_get("fred"));
  47. /* default */
  48. ut_assertok(run_command("setexpr fred 0", 0));
  49. ut_asserteq_str("0", env_get("fred"));
  50. ut_assertok(run_command("setexpr fred *0", 0));
  51. ut_asserteq_str("cdef0123", env_get("fred"));
  52. unmap_sysmem(buf);
  53. return 0;
  54. }
  55. SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC);
  56. /* Test 'setexpr' command with + operator */
  57. static int setexpr_test_plus(struct unit_test_state *uts)
  58. {
  59. char *buf;
  60. buf = map_sysmem(0, BUF_SIZE);
  61. memset(buf, '\xff', BUF_SIZE);
  62. /* byte */
  63. buf[0x0] = 0x12;
  64. buf[0x10] = 0x34;
  65. ut_assertok(run_command("setexpr.b fred *0 + *10", 0));
  66. ut_asserteq_str("46", env_get("fred"));
  67. /* 16-bit */
  68. *(short *)buf = 0x2345;
  69. *(short *)(buf + 0x10) = 0xf012;
  70. ut_assertok(run_command("setexpr.w fred *0 + *10", 0));
  71. ut_asserteq_str("11357", env_get("fred"));
  72. /* 32-bit */
  73. *(u32 *)buf = 0x3456789a;
  74. *(u32 *)(buf + 0x10) = 0xc3384235;
  75. ut_assertok(run_command("setexpr.l fred *0 + *10", 0));
  76. ut_asserteq_str("f78ebacf", env_get("fred"));
  77. /* 64-bit */
  78. *(u64 *)buf = 0x456789abcdef0123;
  79. *(u64 *)(buf + 0x10) = 0x4987328372849283;
  80. ut_assertok(run_command("setexpr.q fred *0 + *10", 0));
  81. ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
  82. /* default */
  83. ut_assertok(run_command("setexpr fred *0 + *10", 0));
  84. ut_asserteq_str("1407393a6", env_get("fred"));
  85. unmap_sysmem(buf);
  86. return 0;
  87. }
  88. SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC);
  89. /* Test 'setexpr' command with other operators */
  90. static int setexpr_test_oper(struct unit_test_state *uts)
  91. {
  92. char *buf;
  93. buf = map_sysmem(0, BUF_SIZE);
  94. memset(buf, '\xff', BUF_SIZE);
  95. *(u32 *)buf = 0x1234;
  96. *(u32 *)(buf + 0x10) = 0x560000;
  97. /* Quote | to avoid confusing hush */
  98. ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0));
  99. ut_asserteq_str("561234", env_get("fred"));
  100. *(u32 *)buf = 0x561200;
  101. *(u32 *)(buf + 0x10) = 0x1234;
  102. /* Quote & to avoid confusing hush */
  103. ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0));
  104. ut_asserteq_str("1200", env_get("fred"));
  105. ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0));
  106. ut_asserteq_str("560034", env_get("fred"));
  107. ut_assertok(run_command("setexpr.l fred *0 - *10", 0));
  108. ut_asserteq_str("55ffcc", env_get("fred"));
  109. ut_assertok(run_command("setexpr.l fred *0 * *10", 0));
  110. ut_asserteq_str("61ebfa800", env_get("fred"));
  111. ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
  112. ut_asserteq_str("4ba", env_get("fred"));
  113. ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
  114. ut_asserteq_str("838", env_get("fred"));
  115. unmap_sysmem(buf);
  116. return 0;
  117. }
  118. SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC);
  119. /* Test 'setexpr' command with regex */
  120. static int setexpr_test_regex(struct unit_test_state *uts)
  121. {
  122. char *buf, *val;
  123. buf = map_sysmem(0, BUF_SIZE);
  124. /* Single substitution */
  125. ut_assertok(run_command("setenv fred 'this is a test'", 0));
  126. ut_assertok(run_command("setexpr fred sub is us", 0));
  127. val = env_get("fred");
  128. ut_asserteq_str("thus is a test", val);
  129. /* Global substitution */
  130. ut_assertok(run_command("setenv fred 'this is a test'", 0));
  131. ut_assertok(run_command("setexpr fred gsub is us", 0));
  132. val = env_get("fred");
  133. ut_asserteq_str("thus us a test", val);
  134. /* Global substitution */
  135. ut_assertok(run_command("setenv fred 'this is a test'", 0));
  136. ut_assertok(run_command("setenv mary 'this is a test'", 0));
  137. ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0));
  138. val = env_get("fred");
  139. ut_asserteq_str("thus us a test", val);
  140. val = env_get("mary");
  141. ut_asserteq_str("this is a test", val);
  142. unmap_sysmem(buf);
  143. return 0;
  144. }
  145. SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC);
  146. /* Test 'setexpr' command with regex replacement that expands the string */
  147. static int setexpr_test_regex_inc(struct unit_test_state *uts)
  148. {
  149. char *buf, *val;
  150. buf = map_sysmem(0, BUF_SIZE);
  151. ut_assertok(run_command("setenv fred 'this is a test'", 0));
  152. ut_assertok(run_command("setexpr fred gsub is much_longer_string", 0));
  153. val = env_get("fred");
  154. ut_asserteq_str("thmuch_longer_string much_longer_string a test", val);
  155. unmap_sysmem(buf);
  156. return 0;
  157. }
  158. SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC);
  159. /* Test setexpr_regex_sub() directly to check buffer usage */
  160. static int setexpr_test_sub(struct unit_test_state *uts)
  161. {
  162. char *buf, *nbuf;
  163. int i;
  164. buf = map_sysmem(0, BUF_SIZE);
  165. nbuf = map_sysmem(0x1000, BUF_SIZE);
  166. /* Add a pattern so we can check the buffer limits */
  167. memset(buf, '\xff', BUF_SIZE);
  168. memset(nbuf, '\xff', BUF_SIZE);
  169. for (i = BUF_SIZE; i < 0x1000; i++) {
  170. buf[i] = i & 0xff;
  171. nbuf[i] = i & 0xff;
  172. }
  173. strcpy(buf, "this is a test");
  174. /*
  175. * This is a regression test, since a bug was found in the use of
  176. * memmove() in setexpr
  177. */
  178. ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is",
  179. "us it is longer", true));
  180. ut_asserteq_str("thus it is longer us it is longer a test", buf);
  181. for (i = BUF_SIZE; i < 0x1000; i++) {
  182. ut_assertf(buf[i] == (char)i,
  183. "buf byte at %x should be %02x, got %02x)\n",
  184. i, i & 0xff, (u8)buf[i]);
  185. ut_assertf(nbuf[i] == (char)i,
  186. "nbuf byte at %x should be %02x, got %02x)\n",
  187. i, i & 0xff, (u8)nbuf[i]);
  188. }
  189. unmap_sysmem(buf);
  190. return 0;
  191. }
  192. SETEXPR_TEST(setexpr_test_sub, UT_TESTF_CONSOLE_REC);
  193. /* Test setexpr_regex_sub() with back references */
  194. static int setexpr_test_backref(struct unit_test_state *uts)
  195. {
  196. char *buf, *nbuf;
  197. int i;
  198. buf = map_sysmem(0, BUF_SIZE);
  199. nbuf = map_sysmem(0x1000, BUF_SIZE);
  200. /* Add a pattern so we can check the buffer limits */
  201. memset(buf, '\xff', BUF_SIZE);
  202. memset(nbuf, '\xff', BUF_SIZE);
  203. for (i = BUF_SIZE; i < 0x1000; i++) {
  204. buf[i] = i & 0xff;
  205. nbuf[i] = i & 0xff;
  206. }
  207. strcpy(buf, "this is surely a test is it? yes this is indeed a test");
  208. /*
  209. * This is a regression test, since a bug was found in the use of
  210. * memmove() in setexpr
  211. */
  212. ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE,
  213. "(this) (is) (surely|indeed)",
  214. "us \\1 \\2 \\3!", true));
  215. ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test",
  216. buf);
  217. for (i = BUF_SIZE; i < 0x1000; i++) {
  218. ut_assertf(buf[i] == (char)i,
  219. "buf byte at %x should be %02x, got %02x)\n",
  220. i, i & 0xff, (u8)buf[i]);
  221. ut_assertf(nbuf[i] == (char)i,
  222. "nbuf byte at %x should be %02x, got %02x)\n",
  223. i, i & 0xff, (u8)nbuf[i]);
  224. }
  225. unmap_sysmem(buf);
  226. return 0;
  227. }
  228. SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);
  229. /* Test 'setexpr' command with setting strings */
  230. static int setexpr_test_str(struct unit_test_state *uts)
  231. {
  232. ulong start_mem;
  233. char *buf;
  234. buf = map_sysmem(0, BUF_SIZE);
  235. memset(buf, '\xff', BUF_SIZE);
  236. /*
  237. * Set 'fred' to the same length as we expect to get below, to avoid a
  238. * new allocation in 'setexpr'. That way we can check for memory leaks.
  239. */
  240. ut_assertok(env_set("fred", "x"));
  241. start_mem = ut_check_free();
  242. strcpy(buf, "hello");
  243. ut_asserteq(1, run_command("setexpr.s fred 0", 0));
  244. ut_assertok(ut_check_delta(start_mem));
  245. ut_assertok(env_set("fred", "12345"));
  246. start_mem = ut_check_free();
  247. ut_assertok(run_command("setexpr.s fred *0", 0));
  248. ut_asserteq_str("hello", env_get("fred"));
  249. ut_assertok(ut_check_delta(start_mem));
  250. unmap_sysmem(buf);
  251. return 0;
  252. }
  253. SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC);
  254. /* Test 'setexpr' command with concatenating strings */
  255. static int setexpr_test_str_oper(struct unit_test_state *uts)
  256. {
  257. ulong start_mem;
  258. char *buf;
  259. buf = map_sysmem(0, BUF_SIZE);
  260. memset(buf, '\xff', BUF_SIZE);
  261. strcpy(buf, "hello");
  262. strcpy(buf + 0x10, " there");
  263. ut_assertok(console_record_reset_enable());
  264. start_mem = ut_check_free();
  265. ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0));
  266. ut_assertok(ut_check_delta(start_mem));
  267. ut_assert_nextline("invalid op");
  268. ut_assert_console_end();
  269. /*
  270. * Set 'fred' to the same length as we expect to get below, to avoid a
  271. * new allocation in 'setexpr'. That way we can check for memory leaks.
  272. */
  273. ut_assertok(env_set("fred", "12345012345"));
  274. start_mem = ut_check_free();
  275. ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
  276. ut_asserteq_str("hello there", env_get("fred"));
  277. /*
  278. * This check does not work with sandbox_flattree, apparently due to
  279. * memory allocations in env_set().
  280. *
  281. * The truetype console produces lots of memory allocations even though
  282. * the LCD display is not visible. But even without these, it does not
  283. * work.
  284. *
  285. * A better test would be for dlmalloc to record the allocs and frees
  286. * for a particular caller, but that is not supported.
  287. *
  288. * For now, drop this test.
  289. *
  290. * ut_assertok(ut_check_delta(start_mem));
  291. */
  292. unmap_sysmem(buf);
  293. return 0;
  294. }
  295. SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC);
  296. /* Test 'setexpr' command with a string that is too long */
  297. static int setexpr_test_str_long(struct unit_test_state *uts)
  298. {
  299. const int size = 128 << 10; /* setexpr strings are a max of 64KB */
  300. char *buf, *val;
  301. buf = map_sysmem(0, size);
  302. memset(buf, 'a', size);
  303. /* String should be truncated to 64KB */
  304. ut_assertok(run_command("setexpr.s fred *0", 0));
  305. val = env_get("fred");
  306. ut_asserteq(64 << 10, strlen(val));
  307. unmap_sysmem(buf);
  308. return 0;
  309. }
  310. SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
  311. #ifdef CONFIG_CMD_SETEXPR_FMT
  312. /* Test 'setexpr' command with simply setting integers */
  313. static int setexpr_test_fmt(struct unit_test_state *uts)
  314. {
  315. u8 *buf;
  316. buf = map_sysmem(0, BUF_SIZE);
  317. memset(buf, '\xff', BUF_SIZE);
  318. /* Test decimal conversion */
  319. ut_assertok(run_command("setexpr fred fmt %d 0xff", 0));
  320. ut_asserteq_str("255", env_get("fred"));
  321. /* Test hexadecimal conversion with 0x prefix and 4 digits */
  322. ut_assertok(run_command("setexpr fred fmt 0x%04x 257", 0));
  323. ut_asserteq_str("0x0257", env_get("fred"));
  324. /* Test octal conversion with % prefix */
  325. ut_assertok(run_command("setexpr fred fmt %%%o 8", 0));
  326. ut_asserteq_str("%10", env_get("fred"));
  327. /* Test argument surrounded by %% */
  328. ut_assertok(run_command("setexpr fred fmt %%%x%% 0xff", 0));
  329. ut_asserteq_str("%ff%", env_get("fred"));
  330. /* Test escape sequence */
  331. ut_assertok(run_command("setexpr fred fmt \"hello\\040world\"", 0));
  332. ut_asserteq_str("hello world", env_get("fred"));
  333. /* Test %b with string containing octal escape sequence */
  334. ut_assertok(run_command("setexpr fred fmt oh%bno \137", 0));
  335. ut_asserteq_str("oh_no", env_get("fred"));
  336. /* Test %b with string containing \c escape sequence */
  337. ut_assertok(run_command("setexpr fred fmt hello%bworld \"\\c\"", 0));
  338. ut_asserteq_str("hello", env_get("fred"));
  339. /* Test multiple arguments referencing environment varialbes */
  340. ut_assertok(run_command("setenv a eff", 0));
  341. ut_assertok(run_command("setenv b hello", 0));
  342. ut_assertok(run_command("setenv c 0x63", 0));
  343. ut_assertok(run_command("setenv d world", 0));
  344. ut_assertok(run_command("setexpr fred fmt \"0x%08x-%s-%d-%s\" $a $b $c $d", 0));
  345. ut_asserteq_str("0x00000eff-hello-99-world", env_get("fred"));
  346. /* Test with two format specifiers, but only one argument */
  347. ut_assertok(run_command("setexpr fred fmt %d_%x 100", 0));
  348. ut_asserteq_str("256_0", env_get("fred"));
  349. /* Test maximum string length */
  350. ut_assertok(run_command("setexpr fred fmt \"%0127d\" 7b", 0));
  351. ut_asserteq_str("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123", env_get("fred"));
  352. /* Test maximum unsigned integer size */
  353. ut_assertok(run_command("setexpr fred fmt %u ffffffffffffffff", 0));
  354. ut_asserteq_str("18446744073709551615", env_get("fred"));
  355. /* Test maximum positive integer size */
  356. ut_assertok(run_command("setexpr fred fmt %d 7fffffffffffffff", 0));
  357. ut_asserteq_str("9223372036854775807", env_get("fred"));
  358. /* Test maximum negative integer size */
  359. ut_assertok(run_command("setexpr fred fmt %d 8000000000000000", 0));
  360. ut_asserteq_str("-9223372036854775808", env_get("fred"));
  361. /* Test minimum negative integer size */
  362. ut_assertok(run_command("setexpr fred fmt %d ffffffffffffffff", 0));
  363. ut_asserteq_str("-1", env_get("fred"));
  364. /* Test signed value with + sign */
  365. ut_assertok(run_command("setexpr fred fmt %d +5", 0));
  366. ut_asserteq_str("5", env_get("fred"));
  367. /* Test signed value with - sign */
  368. ut_assertok(run_command("setexpr fred fmt %d -4", 0));
  369. ut_asserteq_str("-4", env_get("fred"));
  370. /* Test unsigned value with + sign */
  371. ut_assertok(run_command("setexpr fred fmt %u +3", 0));
  372. ut_asserteq_str("3", env_get("fred"));
  373. /* Test unsigned value with - sign */
  374. ut_assertok(run_command("setexpr fred fmt %x -2", 0));
  375. ut_asserteq_str("fffffffffffffffe", env_get("fred"));
  376. /* Error test with missing format specifier */
  377. ut_asserteq(1, run_command("setexpr fred fmd hello 0xff", 0));
  378. /* Error test with invalid format type */
  379. ut_asserteq(1, run_command("setexpr fred fmt %a 0xff", 0));
  380. /* Error test with incomplete format specifier */
  381. ut_asserteq(1, run_command("setexpr fred fmt hello% bf", 0));
  382. /* Error exceeding maximum string length */
  383. ut_asserteq(1, run_command("setexpr fred fmt \"%0128d\" 456", 0));
  384. unmap_sysmem(buf);
  385. return 0;
  386. }
  387. SETEXPR_TEST(setexpr_test_fmt, UT_TESTF_CONSOLE_REC);
  388. #endif
  389. int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  390. {
  391. struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test);
  392. const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test);
  393. return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
  394. argc, argv);
  395. }