setexpr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 mem 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. /* The following checks fail at present due to a bug in setexpr */
  218. return 0;
  219. for (i = BUF_SIZE; i < 0x1000; i++) {
  220. ut_assertf(buf[i] == (char)i,
  221. "buf byte at %x should be %02x, got %02x)\n",
  222. i, i & 0xff, (u8)buf[i]);
  223. ut_assertf(nbuf[i] == (char)i,
  224. "nbuf byte at %x should be %02x, got %02x)\n",
  225. i, i & 0xff, (u8)nbuf[i]);
  226. }
  227. unmap_sysmem(buf);
  228. return 0;
  229. }
  230. SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);
  231. /* Test 'setexpr' command with setting strings */
  232. static int setexpr_test_str(struct unit_test_state *uts)
  233. {
  234. ulong start_mem;
  235. char *buf;
  236. buf = map_sysmem(0, BUF_SIZE);
  237. memset(buf, '\xff', BUF_SIZE);
  238. /*
  239. * Set 'fred' to the same length as we expect to get below, to avoid a
  240. * new allocation in 'setexpr'. That way we can check for memory leaks.
  241. */
  242. ut_assertok(env_set("fred", "x"));
  243. start_mem = ut_check_free();
  244. strcpy(buf, "hello");
  245. ut_asserteq(1, run_command("setexpr.s fred 0", 0));
  246. ut_assertok(ut_check_delta(start_mem));
  247. ut_assertok(env_set("fred", "12345"));
  248. start_mem = ut_check_free();
  249. ut_assertok(run_command("setexpr.s fred *0", 0));
  250. ut_asserteq_str("hello", env_get("fred"));
  251. ut_assertok(ut_check_delta(start_mem));
  252. unmap_sysmem(buf);
  253. return 0;
  254. }
  255. SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC);
  256. /* Test 'setexpr' command with concatenating strings */
  257. static int setexpr_test_str_oper(struct unit_test_state *uts)
  258. {
  259. ulong start_mem;
  260. char *buf;
  261. buf = map_sysmem(0, BUF_SIZE);
  262. memset(buf, '\xff', BUF_SIZE);
  263. strcpy(buf, "hello");
  264. strcpy(buf + 0x10, " there");
  265. ut_assertok(console_record_reset_enable());
  266. start_mem = ut_check_free();
  267. ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0));
  268. ut_assertok(ut_check_delta(start_mem));
  269. ut_assert_nextline("invalid op");
  270. ut_assert_console_end();
  271. /*
  272. * Set 'fred' to the same length as we expect to get below, to avoid a
  273. * new allocation in 'setexpr'. That way we can check for memory leaks.
  274. */
  275. ut_assertok(env_set("fred", "12345012345"));
  276. start_mem = ut_check_free();
  277. ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
  278. ut_asserteq_str("hello there", env_get("fred"));
  279. /*
  280. * This check does not work with sandbox_flattree, apparently due to
  281. * memory allocations in env_set().
  282. *
  283. * The truetype console produces lots of memory allocations even though
  284. * the LCD display is not visible. But even without these, it does not
  285. * work.
  286. *
  287. * A better test would be for dlmalloc to record the allocs and frees
  288. * for a particular caller, but that is not supported.
  289. *
  290. * For now, drop this test.
  291. *
  292. * ut_assertok(ut_check_delta(start_mem));
  293. */
  294. unmap_sysmem(buf);
  295. return 0;
  296. }
  297. SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC);
  298. /* Test 'setexpr' command with a string that is too long */
  299. static int setexpr_test_str_long(struct unit_test_state *uts)
  300. {
  301. const int size = 128 << 10; /* setexpr strings are a max of 64KB */
  302. char *buf, *val;
  303. buf = map_sysmem(0, size);
  304. memset(buf, 'a', size);
  305. /* String should be truncated to 64KB */
  306. ut_assertok(run_command("setexpr.s fred *0", 0));
  307. val = env_get("fred");
  308. ut_asserteq(64 << 10, strlen(val));
  309. unmap_sysmem(buf);
  310. return 0;
  311. }
  312. SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
  313. int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  314. {
  315. struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test);
  316. const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test);
  317. return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
  318. argc, argv);
  319. }