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