command_ut.c 1.9 KB

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