blob: fdde054b92898884f61821e8a6a8ac779667fc89 [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>
Marek Vasute73c6112022-12-20 07:26:00 +010011#include <test/ut.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15/* Declare a new exit test */
Simon Glassb15512c2025-01-20 14:25:32 -070016#define EXIT_TEST(_name, _flags) UNIT_TEST(_name, _flags, exit)
Marek Vasute73c6112022-12-20 07:26:00 +010017
18/* Test 'exit addr' getting/setting address */
19static 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++) {
Marek Vasute73c6112022-12-20 07:26:00 +010035 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);
Simon Glassc579bd42024-08-22 07:58:03 -060038 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010039
Marek Vasute73c6112022-12-20 07:26:00 +010040 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);
Simon Glassc579bd42024-08-22 07:58:03 -060045 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010046
Marek Vasute73c6112022-12-20 07:26:00 +010047 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");
Simon Glassc579bd42024-08-22 07:58:03 -060053 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010054 }
55
56 /* Validate that 'exit' behaves the same way as 'exit 0' */
Evgeny Bachininb3544032023-03-20 11:23:14 +030057 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010058 ut_assert_nextline("bar");
59 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060060 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010061
Evgeny Bachininb3544032023-03-20 11:23:14 +030062 ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010063 ut_assert_nextline("bar");
64 ut_assert_nextline("quux");
65 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060066 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010067
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 /* Either 'exit' returns 0, or 'echo quux' returns 0 */
71 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060072 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010073
74 /* Validate that return value still propagates from 'run' command */
Evgeny Bachininb3544032023-03-20 11:23:14 +030075 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010076 ut_assert_nextline("bar");
77 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060078 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010079
Evgeny Bachininb3544032023-03-20 11:23:14 +030080 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010081 ut_assert_nextline("bar");
82 ut_assert_nextline("quux");
83 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060084 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010085
Evgeny Bachininb3544032023-03-20 11:23:14 +030086 ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010087 ut_assert_nextline("bar");
88 /* The 'true' returns 0 */
89 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -060090 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010091
Evgeny Bachininb3544032023-03-20 11:23:14 +030092 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010093 ut_assert_nextline("bar");
94 ut_assert_nextline("1");
Simon Glassc579bd42024-08-22 07:58:03 -060095 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +010096
Evgeny Bachininb3544032023-03-20 11:23:14 +030097 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +010098 ut_assert_nextline("bar");
99 ut_assert_nextline("1");
Simon Glassc579bd42024-08-22 07:58:03 -0600100 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +0100101
Evgeny Bachininb3544032023-03-20 11:23:14 +0300102 ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
Marek Vasute73c6112022-12-20 07:26:00 +0100103 ut_assert_nextline("bar");
104 ut_assert_nextline("quux");
105 /* The 'echo quux' returns 0 */
106 ut_assert_nextline("0");
Simon Glassc579bd42024-08-22 07:58:03 -0600107 ut_assert_console_end();
Marek Vasute73c6112022-12-20 07:26:00 +0100108
109 return 0;
110}
Simon Glass11fcfa32024-08-22 07:57:50 -0600111EXIT_TEST(cmd_exit_test, UTF_CONSOLE);