]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - test/cmd/exit.c
fdde054b92898884f61821e8a6a8ac779667fc89
1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for exit command
5 * Copyright 2022 Marek Vasut <marex@denx.de>
10 #include <asm/global_data.h>
13 DECLARE_GLOBAL_DATA_PTR
;
15 /* Declare a new exit test */
16 #define EXIT_TEST(_name, _flags) UNIT_TEST(_name, _flags, exit)
18 /* Test 'exit addr' getting/setting address */
19 static int cmd_exit_test(struct unit_test_state
*uts
)
24 * Test 'exit' with parameter -3, -2, -1, 0, 1, 2, 3 . Use all those
25 * parameters to cover also the special return value -2 that is used
26 * in HUSH to detect exit command.
28 * Always test whether 'exit' command:
29 * - exits out of the 'run' command
30 * - return value is propagated out of the 'run' command
31 * - return value can be tested on outside of 'run' command
32 * - return value can be printed outside of 'run' command
34 for (i
= -3; i
<= 3; i
++) {
35 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo ; echo $?", i
));
36 ut_assert_nextline("bar");
37 ut_assert_nextline("%d", i
> 0 ? i
: 0);
38 ut_assert_console_end();
40 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo && echo quux ; echo $?", i
));
41 ut_assert_nextline("bar");
43 ut_assert_nextline("quux");
44 ut_assert_nextline("%d", i
> 0 ? i
: 0);
45 ut_assert_console_end();
47 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo || echo quux ; echo $?", i
));
48 ut_assert_nextline("bar");
50 ut_assert_nextline("quux");
51 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
52 ut_assert_nextline("0");
53 ut_assert_console_end();
56 /* Validate that 'exit' behaves the same way as 'exit 0' */
57 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
58 ut_assert_nextline("bar");
59 ut_assert_nextline("0");
60 ut_assert_console_end();
62 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?"));
63 ut_assert_nextline("bar");
64 ut_assert_nextline("quux");
65 ut_assert_nextline("0");
66 ut_assert_console_end();
68 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?"));
69 ut_assert_nextline("bar");
70 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
71 ut_assert_nextline("0");
72 ut_assert_console_end();
74 /* Validate that return value still propagates from 'run' command */
75 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
76 ut_assert_nextline("bar");
77 ut_assert_nextline("0");
78 ut_assert_console_end();
80 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
81 ut_assert_nextline("bar");
82 ut_assert_nextline("quux");
83 ut_assert_nextline("0");
84 ut_assert_console_end();
86 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
87 ut_assert_nextline("bar");
88 /* The 'true' returns 0 */
89 ut_assert_nextline("0");
90 ut_assert_console_end();
92 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
93 ut_assert_nextline("bar");
94 ut_assert_nextline("1");
95 ut_assert_console_end();
97 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
98 ut_assert_nextline("bar");
99 ut_assert_nextline("1");
100 ut_assert_console_end();
102 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
103 ut_assert_nextline("bar");
104 ut_assert_nextline("quux");
105 /* The 'echo quux' returns 0 */
106 ut_assert_nextline("0");
107 ut_assert_console_end();
111 EXIT_TEST(cmd_exit_test
, UTF_CONSOLE
);