exit.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Tests for exit command
  4. *
  5. * Copyright 2022 Marek Vasut <marex@denx.de>
  6. */
  7. #include <common.h>
  8. #include <console.h>
  9. #include <mapmem.h>
  10. #include <asm/global_data.h>
  11. #include <test/suites.h>
  12. #include <test/ut.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* Declare a new exit test */
  15. #define EXIT_TEST(_name, _flags) UNIT_TEST(_name, _flags, exit_test)
  16. /* Test 'exit addr' getting/setting address */
  17. static int cmd_exit_test(struct unit_test_state *uts)
  18. {
  19. int i;
  20. /*
  21. * Test 'exit' with parameter -3, -2, -1, 0, 1, 2, 3 . Use all those
  22. * parameters to cover also the special return value -2 that is used
  23. * in HUSH to detect exit command.
  24. *
  25. * Always test whether 'exit' command:
  26. * - exits out of the 'run' command
  27. * - return value is propagated out of the 'run' command
  28. * - return value can be tested on outside of 'run' command
  29. * - return value can be printed outside of 'run' command
  30. */
  31. for (i = -3; i <= 3; i++) {
  32. ut_assertok(console_record_reset_enable());
  33. ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo ; echo $?", i));
  34. ut_assert_nextline("bar");
  35. ut_assert_nextline("%d", i > 0 ? i : 0);
  36. ut_assertok(ut_check_console_end(uts));
  37. ut_assertok(console_record_reset_enable());
  38. ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo && echo quux ; echo $?", i));
  39. ut_assert_nextline("bar");
  40. if (i <= 0)
  41. ut_assert_nextline("quux");
  42. ut_assert_nextline("%d", i > 0 ? i : 0);
  43. ut_assertok(ut_check_console_end(uts));
  44. ut_assertok(console_record_reset_enable());
  45. ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo || echo quux ; echo $?", i));
  46. ut_assert_nextline("bar");
  47. if (i > 0)
  48. ut_assert_nextline("quux");
  49. /* Either 'exit' returns 0, or 'echo quux' returns 0 */
  50. ut_assert_nextline("0");
  51. ut_assertok(ut_check_console_end(uts));
  52. }
  53. /* Validate that 'exit' behaves the same way as 'exit 0' */
  54. ut_assertok(console_record_reset_enable());
  55. ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
  56. ut_assert_nextline("bar");
  57. ut_assert_nextline("0");
  58. ut_assertok(ut_check_console_end(uts));
  59. ut_assertok(console_record_reset_enable());
  60. ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?"));
  61. ut_assert_nextline("bar");
  62. ut_assert_nextline("quux");
  63. ut_assert_nextline("0");
  64. ut_assertok(ut_check_console_end(uts));
  65. ut_assertok(console_record_reset_enable());
  66. ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?"));
  67. ut_assert_nextline("bar");
  68. /* Either 'exit' returns 0, or 'echo quux' returns 0 */
  69. ut_assert_nextline("0");
  70. ut_assertok(ut_check_console_end(uts));
  71. /* Validate that return value still propagates from 'run' command */
  72. ut_assertok(console_record_reset_enable());
  73. ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
  74. ut_assert_nextline("bar");
  75. ut_assert_nextline("0");
  76. ut_assertok(ut_check_console_end(uts));
  77. ut_assertok(console_record_reset_enable());
  78. ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
  79. ut_assert_nextline("bar");
  80. ut_assert_nextline("quux");
  81. ut_assert_nextline("0");
  82. ut_assertok(ut_check_console_end(uts));
  83. ut_assertok(console_record_reset_enable());
  84. ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
  85. ut_assert_nextline("bar");
  86. /* The 'true' returns 0 */
  87. ut_assert_nextline("0");
  88. ut_assertok(ut_check_console_end(uts));
  89. ut_assertok(console_record_reset_enable());
  90. ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
  91. ut_assert_nextline("bar");
  92. ut_assert_nextline("1");
  93. ut_assertok(ut_check_console_end(uts));
  94. ut_assertok(console_record_reset_enable());
  95. ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
  96. ut_assert_nextline("bar");
  97. ut_assert_nextline("1");
  98. ut_assertok(ut_check_console_end(uts));
  99. ut_assertok(console_record_reset_enable());
  100. ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
  101. ut_assert_nextline("bar");
  102. ut_assert_nextline("quux");
  103. /* The 'echo quux' returns 0 */
  104. ut_assert_nextline("0");
  105. ut_assertok(ut_check_console_end(uts));
  106. return 0;
  107. }
  108. EXIT_TEST(cmd_exit_test, UT_TESTF_CONSOLE_REC);
  109. int do_ut_exit(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  110. {
  111. struct unit_test *tests = UNIT_TEST_SUITE_START(exit_test);
  112. const int n_ents = UNIT_TEST_SUITE_COUNT(exit_test);
  113. return cmd_ut_category("cmd_exit", "exit_test_", tests, n_ents,
  114. argc, argv);
  115. }