blob: af58a57fca7f955fbdc2fabc2777271489425a04 [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++) {
Marek Vasute73c6112022-12-20 07:26:00 +010036 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo ; echo $?", i));
37 ut_assert_nextline("bar");
38 ut_assert_nextline("%d", i > 0 ? i : 0);
Simon Glassc579bd42024-08-22 07:58:03 -060039 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010040
Marek Vasute73c6112022-12-20 07:26:00 +010041 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo && echo quux ; echo $?", i));
42 ut_assert_nextline("bar");
43 if (i <= 0)
44 ut_assert_nextline("quux");
45 ut_assert_nextline("%d", i > 0 ? i : 0);
Simon Glassc579bd42024-08-22 07:58:03 -060046 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010047
Marek Vasute73c6112022-12-20 07:26:00 +010048 ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo || echo quux ; echo $?", i));
49 ut_assert_nextline("bar");
50 if (i > 0)
51 ut_assert_nextline("quux");
52 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
53 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060054 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010055 }
56
57 /* Validate that 'exit' behaves the same way as 'exit 0' */
Evgeny Bachininb3544032023-03-20 11:23:14 +030058 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010059 ut_assert_nextline("bar");
60 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060061 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010062
Evgeny Bachininb3544032023-03-20 11:23:14 +030063 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010064 ut_assert_nextline("bar");
65 ut_assert_nextline("quux");
66 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060067 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010068
Evgeny Bachininb3544032023-03-20 11:23:14 +030069 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010070 ut_assert_nextline("bar");
71 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
72 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060073 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010074
75 /* Validate that return value still propagates from 'run' command */
Evgeny Bachininb3544032023-03-20 11:23:14 +030076 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010077 ut_assert_nextline("bar");
78 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060079 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010080
Evgeny Bachininb3544032023-03-20 11:23:14 +030081 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010082 ut_assert_nextline("bar");
83 ut_assert_nextline("quux");
84 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060085 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010086
Evgeny Bachininb3544032023-03-20 11:23:14 +030087 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010088 ut_assert_nextline("bar");
89 /* The 'true' returns 0 */
90 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060091 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010092
Evgeny Bachininb3544032023-03-20 11:23:14 +030093 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010094 ut_assert_nextline("bar");
95 ut_assert_nextline("1");
Simon Glassc579bd42024-08-22 07:58:03 -060096 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010097
Evgeny Bachininb3544032023-03-20 11:23:14 +030098 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010099 ut_assert_nextline("bar");
100 ut_assert_nextline("1");
Simon Glassc579bd42024-08-22 07:58:03 -0600101 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +0100102
Evgeny Bachininb3544032023-03-20 11:23:14 +0300103 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +0100104 ut_assert_nextline("bar");
105 ut_assert_nextline("quux");
106 /* The 'echo quux' returns 0 */
107 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -0600108 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +0100109
110 return 0;
111}
Simon Glass11fcfa32024-08-22 07:57:50 -0600112EXIT_TEST(cmd_exit_test, UTF_CONSOLE);
Marek Vasute73c6112022-12-20 07:26:00 +0100113
114int do_ut_exit(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
115{
116 struct unit_test *tests = UNIT_TEST_SUITE_START(exit_test);
117 const int n_ents = UNIT_TEST_SUITE_COUNT(exit_test);
118
119 return cmd_ut_category("cmd_exit", "exit_test_", tests, n_ents,
120 argc, argv);
121}