command_ut.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifdef CONFIG_SANDBOX
  9. #include <os.h>
  10. #endif
  11. static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
  12. "setenv list ${list}3\0"
  13. "setenv list ${list}4";
  14. static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  15. {
  16. printf("%s: Testing commands\n", __func__);
  17. run_command("env default -f -a", 0);
  18. /* run a single command */
  19. run_command("setenv single 1", 0);
  20. assert(!strcmp("1", getenv("single")));
  21. /* make sure that compound statements work */
  22. #ifdef CONFIG_SYS_HUSH_PARSER
  23. run_command("if test -n ${single} ; then setenv check 1; fi", 0);
  24. assert(!strcmp("1", getenv("check")));
  25. run_command("setenv check", 0);
  26. #endif
  27. /* commands separated by ; */
  28. run_command_list("setenv list 1; setenv list ${list}1", -1, 0);
  29. assert(!strcmp("11", getenv("list")));
  30. /* commands separated by \n */
  31. run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
  32. assert(!strcmp("11", getenv("list")));
  33. /* command followed by \n and nothing else */
  34. run_command_list("setenv list 1${list}\n", -1, 0);
  35. assert(!strcmp("111", getenv("list")));
  36. /* three commands in a row */
  37. run_command_list("setenv list 1\n setenv list ${list}2; "
  38. "setenv list ${list}3", -1, 0);
  39. assert(!strcmp("123", getenv("list")));
  40. /* a command string with \0 in it. Stuff after \0 should be ignored */
  41. run_command("setenv list", 0);
  42. run_command_list(test_cmd, sizeof(test_cmd), 0);
  43. assert(!strcmp("123", getenv("list")));
  44. /*
  45. * a command list where we limit execution to only the first command
  46. * using the length parameter.
  47. */
  48. run_command_list("setenv list 1\n setenv list ${list}2; "
  49. "setenv list ${list}3", strlen("setenv list 1"), 0);
  50. assert(!strcmp("1", getenv("list")));
  51. assert(run_command("false", 0) == 1);
  52. assert(run_command("echo", 0) == 0);
  53. assert(run_command_list("false", -1, 0) == 1);
  54. assert(run_command_list("echo", -1, 0) == 0);
  55. run_command("setenv foo 'setenv monty 1; setenv python 2'", 0);
  56. run_command("run foo", 0);
  57. assert(getenv("monty") != NULL);
  58. assert(!strcmp("1", getenv("monty")));
  59. assert(getenv("python") != NULL);
  60. assert(!strcmp("2", getenv("python")));
  61. #ifdef CONFIG_SYS_HUSH_PARSER
  62. run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
  63. run_command("run foo", 0);
  64. assert(getenv("black") != NULL);
  65. assert(!strcmp("1", getenv("black")));
  66. assert(getenv("adder") != NULL);
  67. assert(!strcmp("2", getenv("adder")));
  68. /* Test the 'test' command */
  69. #define HUSH_TEST(name, expr, expected_result) \
  70. run_command("if test " expr " ; then " \
  71. "setenv " #name "_" #expected_result " y; else " \
  72. "setenv " #name "_" #expected_result " n; fi", 0); \
  73. assert(!strcmp(#expected_result, getenv(#name "_" #expected_result))); \
  74. setenv(#name "_" #expected_result, NULL);
  75. /* Basic operators */
  76. HUSH_TEST(streq, "aaa = aaa", y);
  77. HUSH_TEST(streq, "aaa = bbb", n);
  78. HUSH_TEST(strneq, "aaa != bbb", y);
  79. HUSH_TEST(strneq, "aaa != aaa", n);
  80. HUSH_TEST(strlt, "aaa < bbb", y);
  81. HUSH_TEST(strlt, "bbb < aaa", n);
  82. HUSH_TEST(strgt, "bbb > aaa", y);
  83. HUSH_TEST(strgt, "aaa > bbb", n);
  84. HUSH_TEST(eq, "123 -eq 123", y);
  85. HUSH_TEST(eq, "123 -eq 456", n);
  86. HUSH_TEST(ne, "123 -ne 456", y);
  87. HUSH_TEST(ne, "123 -ne 123", n);
  88. HUSH_TEST(lt, "123 -lt 456", y);
  89. HUSH_TEST(lt_eq, "123 -lt 123", n);
  90. HUSH_TEST(lt, "456 -lt 123", n);
  91. HUSH_TEST(le, "123 -le 456", y);
  92. HUSH_TEST(le_eq, "123 -le 123", y);
  93. HUSH_TEST(le, "456 -le 123", n);
  94. HUSH_TEST(gt, "456 -gt 123", y);
  95. HUSH_TEST(gt_eq, "123 -gt 123", n);
  96. HUSH_TEST(gt, "123 -gt 456", n);
  97. HUSH_TEST(ge, "456 -ge 123", y);
  98. HUSH_TEST(ge_eq, "123 -ge 123", y);
  99. HUSH_TEST(ge, "123 -ge 456", n);
  100. HUSH_TEST(z, "-z \"\"", y);
  101. HUSH_TEST(z, "-z \"aaa\"", n);
  102. HUSH_TEST(n, "-n \"aaa\"", y);
  103. HUSH_TEST(n, "-n \"\"", n);
  104. /* Inversion of simple tests */
  105. HUSH_TEST(streq_inv, "! aaa = aaa", n);
  106. HUSH_TEST(streq_inv, "! aaa = bbb", y);
  107. HUSH_TEST(streq_inv_inv, "! ! aaa = aaa", y);
  108. HUSH_TEST(streq_inv_inv, "! ! aaa = bbb", n);
  109. /* Binary operators */
  110. HUSH_TEST(or_0_0, "aaa != aaa -o bbb != bbb", n);
  111. HUSH_TEST(or_0_1, "aaa != aaa -o bbb = bbb", y);
  112. HUSH_TEST(or_1_0, "aaa = aaa -o bbb != bbb", y);
  113. HUSH_TEST(or_1_1, "aaa = aaa -o bbb = bbb", y);
  114. HUSH_TEST(and_0_0, "aaa != aaa -a bbb != bbb", n);
  115. HUSH_TEST(and_0_1, "aaa != aaa -a bbb = bbb", n);
  116. HUSH_TEST(and_1_0, "aaa = aaa -a bbb != bbb", n);
  117. HUSH_TEST(and_1_1, "aaa = aaa -a bbb = bbb", y);
  118. /* Inversion within binary operators */
  119. HUSH_TEST(or_0_0_inv, "! aaa != aaa -o ! bbb != bbb", y);
  120. HUSH_TEST(or_0_1_inv, "! aaa != aaa -o ! bbb = bbb", y);
  121. HUSH_TEST(or_1_0_inv, "! aaa = aaa -o ! bbb != bbb", y);
  122. HUSH_TEST(or_1_1_inv, "! aaa = aaa -o ! bbb = bbb", n);
  123. HUSH_TEST(or_0_0_inv_inv, "! ! aaa != aaa -o ! ! bbb != bbb", n);
  124. HUSH_TEST(or_0_1_inv_inv, "! ! aaa != aaa -o ! ! bbb = bbb", y);
  125. HUSH_TEST(or_1_0_inv_inv, "! ! aaa = aaa -o ! ! bbb != bbb", y);
  126. HUSH_TEST(or_1_1_inv_inv, "! ! aaa = aaa -o ! ! bbb = bbb", y);
  127. setenv("ut_var_nonexistent", NULL);
  128. setenv("ut_var_exists", "1");
  129. HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_nonexistent\"", y);
  130. HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_exists\"", n);
  131. setenv("ut_var_exists", NULL);
  132. run_command("setenv ut_var_space \" \"", 0);
  133. assert(!strcmp(getenv("ut_var_space"), " "));
  134. run_command("setenv ut_var_test $ut_var_space", 0);
  135. assert(!getenv("ut_var_test"));
  136. run_command("setenv ut_var_test \"$ut_var_space\"", 0);
  137. assert(!strcmp(getenv("ut_var_test"), " "));
  138. run_command("setenv ut_var_test \" 1${ut_var_space}${ut_var_space} 2 \"", 0);
  139. assert(!strcmp(getenv("ut_var_test"), " 1 2 "));
  140. setenv("ut_var_space", NULL);
  141. setenv("ut_var_test", NULL);
  142. #ifdef CONFIG_SANDBOX
  143. /* File existence */
  144. HUSH_TEST(e, "-e hostfs - creating_this_file_breaks_uboot_unit_test", n);
  145. run_command("sb save hostfs - creating_this_file_breaks_uboot_unit_test 0 1", 0);
  146. HUSH_TEST(e, "-e hostfs - creating_this_file_breaks_uboot_unit_test", y);
  147. /* Perhaps this could be replaced by an "rm" shell command one day */
  148. assert(!os_unlink("creating_this_file_breaks_uboot_unit_test"));
  149. HUSH_TEST(e, "-e hostfs - creating_this_file_breaks_uboot_unit_test", n);
  150. #endif
  151. #endif
  152. assert(run_command("", 0) == 0);
  153. assert(run_command(" ", 0) == 0);
  154. assert(run_command("'", 0) == 1);
  155. printf("%s: Everything went swimmingly\n", __func__);
  156. return 0;
  157. }
  158. U_BOOT_CMD(
  159. ut_cmd, 5, 1, do_ut_cmd,
  160. "Very basic test of command parsers",
  161. ""
  162. );