command_ut.c 1.9 KB

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