]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0+ | |
2 | /* | |
3 | * Tests for exit command | |
4 | * | |
5 | * Copyright 2022 Marek Vasut <marex@denx.de> | |
6 | */ | |
7 | ||
8 | #include <console.h> | |
9 | #include <mapmem.h> | |
10 | #include <asm/global_data.h> | |
11 | #include <test/ut.h> | |
12 | ||
13 | DECLARE_GLOBAL_DATA_PTR; | |
14 | ||
15 | /* Declare a new exit test */ | |
16 | #define EXIT_TEST(_name, _flags) UNIT_TEST(_name, _flags, exit) | |
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++) { | |
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(); | |
39 | ||
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); | |
45 | ut_assert_console_end(); | |
46 | ||
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"); | |
53 | ut_assert_console_end(); | |
54 | } | |
55 | ||
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(); | |
61 | ||
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(); | |
67 | ||
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(); | |
73 | ||
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(); | |
79 | ||
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(); | |
85 | ||
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(); | |
91 | ||
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(); | |
96 | ||
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(); | |
101 | ||
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(); | |
108 | ||
109 | return 0; | |
110 | } | |
111 | EXIT_TEST(cmd_exit_test, UTF_CONSOLE); |