command_ut.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2012, The Chromium Authors
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #define DEBUG
  23. #include <common.h>
  24. static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
  25. "setenv list ${list}3\0"
  26. "setenv list ${list}4";
  27. static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  28. {
  29. printf("%s: Testing commands\n", __func__);
  30. run_command("env default -f", 0);
  31. /* run a single command */
  32. run_command("setenv single 1", 0);
  33. assert(!strcmp("1", getenv("single")));
  34. /* make sure that compound statements work */
  35. #ifdef CONFIG_SYS_HUSH_PARSER
  36. run_command("if test -n ${single} ; then setenv check 1; fi", 0);
  37. assert(!strcmp("1", getenv("check")));
  38. run_command("setenv check", 0);
  39. #endif
  40. /* commands separated by ; */
  41. run_command_list("setenv list 1; setenv list ${list}1", -1, 0);
  42. assert(!strcmp("11", getenv("list")));
  43. /* commands separated by \n */
  44. run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
  45. assert(!strcmp("11", getenv("list")));
  46. /* command followed by \n and nothing else */
  47. run_command_list("setenv list 1${list}\n", -1, 0);
  48. assert(!strcmp("111", getenv("list")));
  49. /* three commands in a row */
  50. run_command_list("setenv list 1\n setenv list ${list}2; "
  51. "setenv list ${list}3", -1, 0);
  52. assert(!strcmp("123", getenv("list")));
  53. /* a command string with \0 in it. Stuff after \0 should be ignored */
  54. run_command("setenv list", 0);
  55. run_command_list(test_cmd, sizeof(test_cmd), 0);
  56. assert(!strcmp("123", getenv("list")));
  57. /*
  58. * a command list where we limit execution to only the first command
  59. * using the length parameter.
  60. */
  61. run_command_list("setenv list 1\n setenv list ${list}2; "
  62. "setenv list ${list}3", strlen("setenv list 1"), 0);
  63. assert(!strcmp("1", getenv("list")));
  64. printf("%s: Everything went swimmingly\n", __func__);
  65. return 0;
  66. }
  67. U_BOOT_CMD(
  68. ut_cmd, 5, 1, do_ut_cmd,
  69. "Very basic test of command parsers",
  70. ""
  71. );