summaryrefslogtreecommitdiff
path: root/test/cmd/exit.c
blob: fdde054b92898884f61821e8a6a8ac779667fc89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: GPL-2.0+
/*
 * Tests for exit command
 *
 * Copyright 2022 Marek Vasut <marex@denx.de>
 */

#include <console.h>
#include <mapmem.h>
#include <asm/global_data.h>
#include <test/ut.h>

DECLARE_GLOBAL_DATA_PTR;

/* Declare a new exit test */
#define EXIT_TEST(_name, _flags)	UNIT_TEST(_name, _flags, exit)

/* Test 'exit addr' getting/setting address */
static int cmd_exit_test(struct unit_test_state *uts)
{
	int i;

	/*
	 * Test 'exit' with parameter -3, -2, -1, 0, 1, 2, 3 . Use all those
	 * parameters to cover also the special return value -2 that is used
	 * in HUSH to detect exit command.
	 *
	 * Always test whether 'exit' command:
	 * - exits out of the 'run' command
	 * - return value is propagated out of the 'run' command
	 * - return value can be tested on outside of 'run' command
	 * - return value can be printed outside of 'run' command
	 */
	for (i = -3; i <= 3; i++) {
		ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo ; echo $?", i));
		ut_assert_nextline("bar");
		ut_assert_nextline("%d", i > 0 ? i : 0);
		ut_assert_console_end();

		ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo && echo quux ; echo $?", i));
		ut_assert_nextline("bar");
		if (i <= 0)
			ut_assert_nextline("quux");
		ut_assert_nextline("%d", i > 0 ? i : 0);
		ut_assert_console_end();

		ut_assertok(run_commandf("setenv foo 'echo bar ; exit %d ; echo baz' ; run foo || echo quux ; echo $?", i));
		ut_assert_nextline("bar");
		if (i > 0)
			ut_assert_nextline("quux");
		/* Either 'exit' returns 0, or 'echo quux' returns 0 */
		ut_assert_nextline("0");
		ut_assert_console_end();
	}

	/* Validate that 'exit' behaves the same way as 'exit 0' */
	ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("0");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo && echo quux ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("quux");
	ut_assert_nextline("0");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run foo || echo quux ; echo $?"));
	ut_assert_nextline("bar");
	/* Either 'exit' returns 0, or 'echo quux' returns 0 */
	ut_assert_nextline("0");
	ut_assert_console_end();

	/* Validate that return value still propagates from 'run' command */
	ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("0");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo && echo quux ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("quux");
	ut_assert_nextline("0");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; true' ; run foo || echo quux ; echo $?"));
	ut_assert_nextline("bar");
	/* The 'true' returns 0 */
	ut_assert_nextline("0");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("1");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo && echo quux ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("1");
	ut_assert_console_end();

	ut_assertok(run_commandf("setenv foo 'echo bar ; false' ; run foo || echo quux ; echo $?"));
	ut_assert_nextline("bar");
	ut_assert_nextline("quux");
	/* The 'echo quux' returns 0 */
	ut_assert_nextline("0");
	ut_assert_console_end();

	return 0;
}
EXIT_TEST(cmd_exit_test, UTF_CONSOLE);