From aac4f488befe9a18bfaf1cc31d6dd5168b4340f5 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Thu, 1 Jun 2017 16:01:28 +0300 Subject: [PATCH] tests: Add shell tests This adds the following tests: help tests: shell_exec(help): 0 shell_exec(help dummy): 0 shell_exec(help invalid): -22 select tests: shell_exec(select): 0 shell_exec(select dummy): 0 shell_exec(select invalid): -22 module tests: shell_exec(dummy cmd1): 0 shell_exec(dummy cmd1 arg1): -22 shell_exec(dummy cmd2 arg1): 0 shell_exec(dummy cmd2 arg1 arg2): -22 shell_exec(dummy cmd3 arg1 arg2): 0 shell_exec(dummy cmd3 arg1 arg2 arg3): -22 shell_exec(dummy cmd4 arg1 arg2 arg3): -22 shell_exec(cmd1): 0 shell_exec(cmd1 arg1): -22 shell_exec(cmd2 arg1): 0 shell_exec(cmd2 arg1 arg2): -22 shell_exec(cmd3 arg1 arg2): 0 shell_exec(cmd3 arg1 arg2 arg3): -22 shell_exec(cmd4 arg1 arg2 arg3): -22 app handler tests: shell_exec(cmd4 arg1 arg2 arg3): 0 Signed-off-by: Luiz Augusto von Dentz --- tests/shell/Makefile | 4 ++ tests/shell/prj.conf | 2 + tests/shell/src/Makefile | 3 + tests/shell/src/main.c | 123 +++++++++++++++++++++++++++++++++++++++ tests/shell/testcase.ini | 3 + 5 files changed, 135 insertions(+) create mode 100644 tests/shell/Makefile create mode 100644 tests/shell/prj.conf create mode 100644 tests/shell/src/Makefile create mode 100644 tests/shell/src/main.c create mode 100644 tests/shell/testcase.ini diff --git a/tests/shell/Makefile b/tests/shell/Makefile new file mode 100644 index 00000000000000..e70a750a88d25c --- /dev/null +++ b/tests/shell/Makefile @@ -0,0 +1,4 @@ +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.test diff --git a/tests/shell/prj.conf b/tests/shell/prj.conf new file mode 100644 index 00000000000000..1c18407c634b33 --- /dev/null +++ b/tests/shell/prj.conf @@ -0,0 +1,2 @@ +CONFIG_CONSOLE_SHELL=y +CONFIG_ZTEST=y diff --git a/tests/shell/src/Makefile b/tests/shell/src/Makefile new file mode 100644 index 00000000000000..ef2413bf6ccf1f --- /dev/null +++ b/tests/shell/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y = main.o diff --git a/tests/shell/src/main.c b/tests/shell/src/main.c new file mode 100644 index 00000000000000..6b381e88d0064c --- /dev/null +++ b/tests/shell/src/main.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** @file + * @brief Interactive shell test suite + * + */ + +#include +#include + +#include + +static void test_shell_exec(const char *line, int result) +{ + char cmd[80]; + int ret; + + strncpy(cmd, line, sizeof(cmd)); + ret = shell_exec(cmd); + + TC_PRINT("shell_exec(%s): %d\n", line, ret); + + zassert_true(ret == result, line); +} + +static void test_help(void) +{ + test_shell_exec("help", 0); + test_shell_exec("help dummy", 0); + test_shell_exec("help invalid", -EINVAL); +} + +static void test_select(void) +{ + test_shell_exec("select", 0); + test_shell_exec("select dummy", 0); + test_shell_exec("select invalid", -EINVAL); +} + +static void test_exit(void) +{ + test_shell_exec("exit", 0); +} + +static void test_module(void) +{ + test_shell_exec("dummy cmd1", 0); + test_shell_exec("dummy cmd1 arg1", -EINVAL); + + test_shell_exec("dummy cmd2 arg1", 0); + test_shell_exec("dummy cmd2 arg1 arg2", -EINVAL); + + test_shell_exec("dummy cmd3 arg1 arg2", 0); + test_shell_exec("dummy cmd3 arg1 arg2 arg3", -EINVAL); + + test_shell_exec("dummy cmd4 arg1 arg2 arg3", -EINVAL); + + shell_register_default_module("dummy"); + + test_shell_exec("cmd1", 0); + test_shell_exec("cmd1 arg1", -EINVAL); + + test_shell_exec("cmd2 arg1", 0); + test_shell_exec("cmd2 arg1 arg2", -EINVAL); + + test_shell_exec("cmd3 arg1 arg2", 0); + test_shell_exec("cmd3 arg1 arg2 arg3", -EINVAL); + + test_shell_exec("cmd4 arg1 arg2 arg3", -EINVAL); +} + +static int app_cmd_handler(int argc, char *argv[]) +{ + return 0; +} + +static void test_app_handler(void) +{ + shell_register_app_cmd_handler(app_cmd_handler); + + test_shell_exec("cmd4 arg1 arg2 arg3", 0); +} + +static int dummy_cmd(int argc, char *argv[]) +{ + if (!strcmp(argv[0], "cmd1")) { + return argc == 1 ? 0 : -EINVAL; + } + + if (!strcmp(argv[0], "cmd2")) { + return argc == 2 ? 0 : -EINVAL; + } + + if (!strcmp(argv[0], "cmd3")) { + return argc == 3 ? 0 : -EINVAL; + } + + return -EINVAL; +} + +static const struct shell_cmd dummy_cmds[] = { + { "cmd1", dummy_cmd, "" }, + { "cmd2", dummy_cmd, "" }, + { "cmd3", dummy_cmd, " " }, + { NULL, NULL } +}; + +void test_main(void) +{ + SHELL_REGISTER("dummy", dummy_cmds); + + ztest_test_suite(shell_test_suite, + ztest_unit_test(test_help), + ztest_unit_test(test_select), + ztest_unit_test(test_exit), + ztest_unit_test(test_module), + ztest_unit_test(test_app_handler)); + ztest_run_test_suite(shell_test_suite); +} diff --git a/tests/shell/testcase.ini b/tests/shell/testcase.ini new file mode 100644 index 00000000000000..2ddf87c6e93b22 --- /dev/null +++ b/tests/shell/testcase.ini @@ -0,0 +1,3 @@ +[test] +tags = shell +platform_whitelist = qemu_x86