blob: 093f51b4dfc0fb9c8648a03dc26e1d9c837b6412 [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);
39 ut_assertok(ut_check_console_end(uts));
40
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);
46 ut_assertok(ut_check_console_end(uts));
47
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");
54 ut_assertok(ut_check_console_end(uts));
55 }
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");
61 ut_assertok(ut_check_console_end(uts));
62
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");
67 ut_assertok(ut_check_console_end(uts));
68
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");
73 ut_assertok(ut_check_console_end(uts));
74
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");
79 ut_assertok(ut_check_console_end(uts));
80
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");
85 ut_assertok(ut_check_console_end(uts));
86
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");
91 ut_assertok(ut_check_console_end(uts));
92
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");
96 ut_assertok(ut_check_console_end(uts));
97
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");
101 ut_assertok(ut_check_console_end(uts));
102
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");
108 ut_assertok(ut_check_console_end(uts));
109
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}