]> git.ipfire.org Git - thirdparty/u-boot.git/blame - test/cmd/exit.c
test: Remove <common.h> and add needed includes
[thirdparty/u-boot.git] / test / cmd / exit.c
CommitLineData
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>
11#include <test/suites.h>
12#include <test/ut.h>
13
14DECLARE_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 */
20static 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++) {
36 ut_assertok(console_record_reset_enable());
37 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo ; echo $?", i));
38 ut_assert_nextline("bar");
39 ut_assert_nextline("%d", i > 0 ? i : 0);
40 ut_assertok(ut_check_console_end(uts));
41
42 ut_assertok(console_record_reset_enable());
43 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo && echo quux ; echo $?", i));
44 ut_assert_nextline("bar");
45 if (i <= 0)
46 ut_assert_nextline("quux");
47 ut_assert_nextline("%d", i > 0 ? i : 0);
48 ut_assertok(ut_check_console_end(uts));
49
50 ut_assertok(console_record_reset_enable());
51 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo || echo quux ; echo $?", i));
52 ut_assert_nextline("bar");
53 if (i > 0)
54 ut_assert_nextline("quux");
55 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
56 ut_assert_nextline("0");
57 ut_assertok(ut_check_console_end(uts));
58 }
59
60 /* Validate that 'exit' behaves the same way as 'exit 0' */
61 ut_assertok(console_record_reset_enable());
49b7d69f 62 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
f08dcd9f
MV
63 ut_assert_nextline("bar");
64 ut_assert_nextline("0");
65 ut_assertok(ut_check_console_end(uts));
66
67 ut_assertok(console_record_reset_enable());
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 ut_assert_nextline("quux");
71 ut_assert_nextline("0");
72 ut_assertok(ut_check_console_end(uts));
73
74 ut_assertok(console_record_reset_enable());
49b7d69f 75 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?"));
f08dcd9f
MV
76 ut_assert_nextline("bar");
77 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
78 ut_assert_nextline("0");
79 ut_assertok(ut_check_console_end(uts));
80
81 /* Validate that return value still propagates from 'run' command */
82 ut_assertok(console_record_reset_enable());
49b7d69f 83 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
f08dcd9f
MV
84 ut_assert_nextline("bar");
85 ut_assert_nextline("0");
86 ut_assertok(ut_check_console_end(uts));
87
88 ut_assertok(console_record_reset_enable());
49b7d69f 89 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
f08dcd9f
MV
90 ut_assert_nextline("bar");
91 ut_assert_nextline("quux");
92 ut_assert_nextline("0");
93 ut_assertok(ut_check_console_end(uts));
94
95 ut_assertok(console_record_reset_enable());
49b7d69f 96 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
f08dcd9f
MV
97 ut_assert_nextline("bar");
98 /* The 'true' returns 0 */
99 ut_assert_nextline("0");
100 ut_assertok(ut_check_console_end(uts));
101
102 ut_assertok(console_record_reset_enable());
49b7d69f 103 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
f08dcd9f
MV
104 ut_assert_nextline("bar");
105 ut_assert_nextline("1");
106 ut_assertok(ut_check_console_end(uts));
107
108 ut_assertok(console_record_reset_enable());
49b7d69f 109 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
f08dcd9f
MV
110 ut_assert_nextline("bar");
111 ut_assert_nextline("1");
112 ut_assertok(ut_check_console_end(uts));
113
114 ut_assertok(console_record_reset_enable());
49b7d69f 115 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
f08dcd9f
MV
116 ut_assert_nextline("bar");
117 ut_assert_nextline("quux");
118 /* The 'echo quux' returns 0 */
119 ut_assert_nextline("0");
120 ut_assertok(ut_check_console_end(uts));
121
122 return 0;
123}
124
125EXIT_TEST(cmd_exit_test, UT_TESTF_CONSOLE_REC);
126
127int do_ut_exit(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
128{
129 struct unit_test *tests = UNIT_TEST_SUITE_START(exit_test);
130 const int n_ents = UNIT_TEST_SUITE_COUNT(exit_test);
131
132 return cmd_ut_category("cmd_exit", "exit_test_", tests, n_ents,
133 argc, argv);
134}