]>
Commit | Line | Data |
---|---|---|
f08dcd9f MV |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Tests for exit command | |
4 | * | |
5 | * Copyright 2022 Marek Vasut <marex@denx.de> | |
6 | */ | |
7 | ||
f08dcd9f MV |
8 | #include <console.h> |
9 | #include <mapmem.h> | |
10 | #include <asm/global_data.h> | |
f08dcd9f MV |
11 | #include <test/ut.h> |
12 | ||
13 | DECLARE_GLOBAL_DATA_PTR; | |
14 | ||
15 | /* Declare a new exit test */ | |
4ba3ab49 | 16 | #define EXIT_TEST(_name, _flags) UNIT_TEST(_name, _flags, exit) |
f08dcd9f MV |
17 | |
18 | /* Test 'exit addr' getting/setting address */ | |
19 | static int cmd_exit_test(struct unit_test_state *uts) | |
20 | { | |
21 | int i; | |
22 | ||
23 | /* | |
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. | |
27 | * | |
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 | |
33 | */ | |
34 | for (i = -3; i <= 3; i++) { | |
f08dcd9f MV |
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); | |
e6f498e7 | 38 | ut_assert_console_end(); |
f08dcd9f | 39 | |
f08dcd9f MV |
40 | ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo && echo quux ; echo $?", i)); |
41 | ut_assert_nextline("bar"); | |
42 | if (i <= 0) | |
43 | ut_assert_nextline("quux"); | |
44 | ut_assert_nextline("%d", i > 0 ? i : 0); | |
e6f498e7 | 45 | ut_assert_console_end(); |
f08dcd9f | 46 | |
f08dcd9f MV |
47 | ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo || echo quux ; echo $?", i)); |
48 | ut_assert_nextline("bar"); | |
49 | if (i > 0) | |
50 | ut_assert_nextline("quux"); | |
51 | /* Either 'exit' returns 0, or 'echo quux' returns 0 */ | |
52 | ut_assert_nextline("0"); | |
e6f498e7 | 53 | ut_assert_console_end(); |
f08dcd9f MV |
54 | } |
55 | ||
56 | /* Validate that 'exit' behaves the same way as 'exit 0' */ | |
49b7d69f | 57 | ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?")); |
f08dcd9f MV |
58 | ut_assert_nextline("bar"); |
59 | ut_assert_nextline("0"); | |
e6f498e7 | 60 | ut_assert_console_end(); |
f08dcd9f | 61 | |
49b7d69f | 62 | ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?")); |
f08dcd9f MV |
63 | ut_assert_nextline("bar"); |
64 | ut_assert_nextline("quux"); | |
65 | ut_assert_nextline("0"); | |
e6f498e7 | 66 | ut_assert_console_end(); |
f08dcd9f | 67 | |
49b7d69f | 68 | ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?")); |
f08dcd9f MV |
69 | ut_assert_nextline("bar"); |
70 | /* Either 'exit' returns 0, or 'echo quux' returns 0 */ | |
71 | ut_assert_nextline("0"); | |
e6f498e7 | 72 | ut_assert_console_end(); |
f08dcd9f MV |
73 | |
74 | /* Validate that return value still propagates from 'run' command */ | |
49b7d69f | 75 | ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?")); |
f08dcd9f MV |
76 | ut_assert_nextline("bar"); |
77 | ut_assert_nextline("0"); | |
e6f498e7 | 78 | ut_assert_console_end(); |
f08dcd9f | 79 | |
49b7d69f | 80 | ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?")); |
f08dcd9f MV |
81 | ut_assert_nextline("bar"); |
82 | ut_assert_nextline("quux"); | |
83 | ut_assert_nextline("0"); | |
e6f498e7 | 84 | ut_assert_console_end(); |
f08dcd9f | 85 | |
49b7d69f | 86 | ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?")); |
f08dcd9f MV |
87 | ut_assert_nextline("bar"); |
88 | /* The 'true' returns 0 */ | |
89 | ut_assert_nextline("0"); | |
e6f498e7 | 90 | ut_assert_console_end(); |
f08dcd9f | 91 | |
49b7d69f | 92 | ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?")); |
f08dcd9f MV |
93 | ut_assert_nextline("bar"); |
94 | ut_assert_nextline("1"); | |
e6f498e7 | 95 | ut_assert_console_end(); |
f08dcd9f | 96 | |
49b7d69f | 97 | ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?")); |
f08dcd9f MV |
98 | ut_assert_nextline("bar"); |
99 | ut_assert_nextline("1"); | |
e6f498e7 | 100 | ut_assert_console_end(); |
f08dcd9f | 101 | |
49b7d69f | 102 | ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?")); |
f08dcd9f MV |
103 | ut_assert_nextline("bar"); |
104 | ut_assert_nextline("quux"); | |
105 | /* The 'echo quux' returns 0 */ | |
106 | ut_assert_nextline("0"); | |
e6f498e7 | 107 | ut_assert_console_end(); |
f08dcd9f MV |
108 | |
109 | return 0; | |
110 | } | |
9b99762e | 111 | EXIT_TEST(cmd_exit_test, UTF_CONSOLE); |