Add a test which validates that exit from environment script works as
expected, including return value propagation and clipping to positive
integers.
Signed-off-by: Marek Vasut
---
Cc: Adrian Vovk
Cc: Hector Palacios
Cc: Pantelis Antoniou
Cc: Simon Glass
Cc: Tom Rini
---
include/test/suites.h | 1 +
test/cmd/Makefile | 2 +-
test/cmd/exit.c | 135 ++
test/cmd_ut.c | 1 +
4 files changed, 138 insertions(+), 1 deletion(-)
create mode 100644 test/cmd/exit.c
diff --git a/include/test/suites.h b/include/test/suites.h
index a01000e127b..9ce49cbb031 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -38,6 +38,7 @@ int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int
argc,
char *const argv[]);
int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
+int do_ut_exit(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index bc961df3dce..09e410ec30e 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -8,7 +8,7 @@ endif
ifdef CONFIG_CONSOLE_RECORD
obj-$(CONFIG_CMD_PAUSE) += test_pause.o
endif
-obj-y += mem.o
+obj-y += exit.o mem.o
obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
obj-$(CONFIG_CMD_FDT) += fdt.o
obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
diff --git a/test/cmd/exit.c b/test/cmd/exit.c
new file mode 100644
index 000..ca34abef899
--- /dev/null
+++ b/test/cmd/exit.c
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for exit command
+ *
+ * Copyright 2022 Marek Vasut
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Declare a new exit test */
+#define EXIT_TEST(_name, _flags) UNIT_TEST(_name, _flags, exit_test)
+
+/* 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(console_record_reset_enable());
+ 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_assertok(ut_check_console_end(uts));
+
+ ut_assertok(console_record_reset_enable());
+ 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_assertok(ut_check_console_end(uts));
+
+ ut_assertok(console_record_reset_enable());
+ 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_assertok(ut_check_console_end(uts));
+ }
+
+ /* Validate that 'exit' behaves the same way as 'exit 0' */
+ ut_assertok(console_record_reset_enable());
+ ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run
foo ; echo $?", i));
+ ut_assert_nextline("bar");
+ ut_assert_nextline("0");
+ ut_assertok(ut_check_console_end(uts));
+
+ ut_assertok(console_record_reset_enable());
+ ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run
foo && echo quux ; echo $?", i));
+ ut_assert_nextline("bar");
+ ut_assert_nextline("quux");
+ ut_assert_nextline("0");
+ ut_assertok(ut_check_console_end(uts));
+
+ ut_assertok(console_record_reset_enable());
+ ut_assertok(run_commandf("setenv foo 'echo bar ; exit ; echo baz' ; run
foo || echo quux ; echo $?", i));
+ ut_assert_nextline("bar");
+ /* Either 'exit' returns 0, or 'echo quux' returns 0 */