blob: d310ec8531be1269e198088e84cf78e7d0e32b41 [file] [log] [blame]
Marek Vasute73c6112022-12-20 07:26:00 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for exit command
4 *
5 * Copyright 2022 Marek Vasut <marex@denx.de>
6 */
7
Marek Vasute73c6112022-12-20 07:26:00 +01008#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());
Evgeny Bachininb3544032023-03-20 11:23:14 +030062 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010063 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +030068 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010069 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +030075 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010076 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +030083 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010084 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +030089 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010090 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +030096 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010097 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +0300103 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +0100104 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +0300109 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +0100110 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());
Evgeny Bachininb3544032023-03-20 11:23:14 +0300115 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +0100116 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}