command_ut.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <string.h>
  11. #include <linux/errno.h>
  12. static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
  13. "setenv list ${list}3\0"
  14. "setenv list ${list}4";
  15. static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc,
  16. char *const argv[])
  17. {
  18. char long_str[CONFIG_SYS_CBSIZE + 42];
  19. printf("%s: Testing commands\n", __func__);
  20. run_command("env default -f -a", 0);
  21. /* commands separated by \n */
  22. run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
  23. assert(!strcmp("11", env_get("list")));
  24. /* command followed by \n and nothing else */
  25. run_command_list("setenv list 1${list}\n", -1, 0);
  26. assert(!strcmp("111", env_get("list")));
  27. /* a command string with \0 in it. Stuff after \0 should be ignored */
  28. run_command("setenv list", 0);
  29. run_command_list(test_cmd, sizeof(test_cmd), 0);
  30. assert(!strcmp("123", env_get("list")));
  31. /*
  32. * a command list where we limit execution to only the first command
  33. * using the length parameter.
  34. */
  35. run_command_list("setenv list 1\n setenv list ${list}2; "
  36. "setenv list ${list}3", strlen("setenv list 1"), 0);
  37. assert(!strcmp("1", env_get("list")));
  38. assert(run_command("false", 0) == 1);
  39. assert(run_command("echo", 0) == 0);
  40. assert(run_command_list("false", -1, 0) == 1);
  41. assert(run_command_list("echo", -1, 0) == 0);
  42. #ifdef CONFIG_HUSH_PARSER
  43. run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
  44. run_command("run foo", 0);
  45. assert(env_get("black") != NULL);
  46. assert(!strcmp("1", env_get("black")));
  47. assert(env_get("adder") != NULL);
  48. assert(!strcmp("2", env_get("adder")));
  49. #endif
  50. assert(run_command("", 0) == 0);
  51. assert(run_command(" ", 0) == 0);
  52. assert(run_command("'", 0) == 1);
  53. /* Variadic function test-cases */
  54. #pragma GCC diagnostic push
  55. #pragma GCC diagnostic ignored "-Wformat-zero-length"
  56. assert(run_commandf("") == 0);
  57. #pragma GCC diagnostic pop
  58. assert(run_commandf(" ") == 0);
  59. assert(run_commandf("'") == 1);
  60. assert(run_commandf("env %s %s", "delete -f", "list") == 0);
  61. /* Expected: "Error: "list" not defined" */
  62. assert(run_commandf("printenv list") == 1);
  63. memset(long_str, 'x', sizeof(long_str));
  64. assert(run_commandf("Truncation case: %s", long_str) == -ENOSPC);
  65. if (IS_ENABLED(CONFIG_HUSH_PARSER)) {
  66. assert(run_commandf("env %s %s %s %s", "delete -f", "adder",
  67. "black", "foo") == 0);
  68. assert(run_commandf("setenv foo 'setenv %s 1\nsetenv %s 2'",
  69. "black", "adder") == 0);
  70. run_command("run foo", 0);
  71. assert(env_get("black"));
  72. assert(!strcmp("1", env_get("black")));
  73. assert(env_get("adder"));
  74. assert(!strcmp("2", env_get("adder")));
  75. }
  76. /* Clean up before exit */
  77. run_command("env default -f -a", 0);
  78. printf("%s: Everything went swimmingly\n", __func__);
  79. return 0;
  80. }
  81. U_BOOT_CMD(
  82. ut_cmd, 5, 1, do_ut_cmd,
  83. "Very basic test of command parsers",
  84. ""
  85. );