command_ut.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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 -a", 0);
  15. /* commands separated by \n */
  16. run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
  17. assert(!strcmp("11", env_get("list")));
  18. /* command followed by \n and nothing else */
  19. run_command_list("setenv list 1${list}\n", -1, 0);
  20. assert(!strcmp("111", env_get("list")));
  21. /* a command string with \0 in it. Stuff after \0 should be ignored */
  22. run_command("setenv list", 0);
  23. run_command_list(test_cmd, sizeof(test_cmd), 0);
  24. assert(!strcmp("123", env_get("list")));
  25. /*
  26. * a command list where we limit execution to only the first command
  27. * using the length parameter.
  28. */
  29. run_command_list("setenv list 1\n setenv list ${list}2; "
  30. "setenv list ${list}3", strlen("setenv list 1"), 0);
  31. assert(!strcmp("1", env_get("list")));
  32. assert(run_command("false", 0) == 1);
  33. assert(run_command("echo", 0) == 0);
  34. assert(run_command_list("false", -1, 0) == 1);
  35. assert(run_command_list("echo", -1, 0) == 0);
  36. #ifdef CONFIG_HUSH_PARSER
  37. run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
  38. run_command("run foo", 0);
  39. assert(env_get("black") != NULL);
  40. assert(!strcmp("1", env_get("black")));
  41. assert(env_get("adder") != NULL);
  42. assert(!strcmp("2", env_get("adder")));
  43. #endif
  44. assert(run_command("", 0) == 0);
  45. assert(run_command(" ", 0) == 0);
  46. assert(run_command("'", 0) == 1);
  47. printf("%s: Everything went swimmingly\n", __func__);
  48. return 0;
  49. }
  50. U_BOOT_CMD(
  51. ut_cmd, 5, 1, do_ut_cmd,
  52. "Very basic test of command parsers",
  53. ""
  54. );