command_ut.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012, The Chromium Authors
  4. */
  5. #define DEBUG
  6. #include <common.h>
  7. static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
  8. "setenv list ${list}3\0"
  9. "setenv list ${list}4";
  10. static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  11. {
  12. printf("%s: Testing commands\n", __func__);
  13. run_command("env default -f -a", 0);
  14. /* commands separated by \n */
  15. run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
  16. assert(!strcmp("11", env_get("list")));
  17. /* command followed by \n and nothing else */
  18. run_command_list("setenv list 1${list}\n", -1, 0);
  19. assert(!strcmp("111", env_get("list")));
  20. /* a command string with \0 in it. Stuff after \0 should be ignored */
  21. run_command("setenv list", 0);
  22. run_command_list(test_cmd, sizeof(test_cmd), 0);
  23. assert(!strcmp("123", env_get("list")));
  24. /*
  25. * a command list where we limit execution to only the first command
  26. * using the length parameter.
  27. */
  28. run_command_list("setenv list 1\n setenv list ${list}2; "
  29. "setenv list ${list}3", strlen("setenv list 1"), 0);
  30. assert(!strcmp("1", env_get("list")));
  31. assert(run_command("false", 0) == 1);
  32. assert(run_command("echo", 0) == 0);
  33. assert(run_command_list("false", -1, 0) == 1);
  34. assert(run_command_list("echo", -1, 0) == 0);
  35. #ifdef CONFIG_HUSH_PARSER
  36. run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
  37. run_command("run foo", 0);
  38. assert(env_get("black") != NULL);
  39. assert(!strcmp("1", env_get("black")));
  40. assert(env_get("adder") != NULL);
  41. assert(!strcmp("2", env_get("adder")));
  42. #endif
  43. assert(run_command("", 0) == 0);
  44. assert(run_command(" ", 0) == 0);
  45. assert(run_command("'", 0) == 1);
  46. printf("%s: Everything went swimmingly\n", __func__);
  47. return 0;
  48. }
  49. U_BOOT_CMD(
  50. ut_cmd, 5, 1, do_ut_cmd,
  51. "Very basic test of command parsers",
  52. ""
  53. );