Loading...
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | /*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
* @brief Interactive shell test suite
*
*/
#include <zephyr.h>
#include <ztest.h>
#include <shell/shell.h>
#include <shell/shell_dummy.h>
#define MAX_CMD_SYNTAX_LEN (11)
static char dynamic_cmd_buffer[][MAX_CMD_SYNTAX_LEN] = {
"dynamic",
"command"
};
static void test_shell_execute_cmd(const char *cmd, int result)
{
int ret;
ret = shell_execute_cmd(NULL, cmd);
TC_PRINT("shell_execute_cmd(%s): %d\n", cmd, ret);
zassert_true(ret == result, "cmd: %s, got:%d, expected:%d",
cmd, ret, result);
}
static void test_cmd_help(void)
{
test_shell_execute_cmd("help", 0);
test_shell_execute_cmd("help -h", 1);
test_shell_execute_cmd("help --help", 1);
test_shell_execute_cmd("help dummy", -EINVAL);
test_shell_execute_cmd("help dummy dummy", -EINVAL);
}
static void test_cmd_clear(void)
{
test_shell_execute_cmd("clear", 0);
test_shell_execute_cmd("clear -h", 1);
test_shell_execute_cmd("clear --help", 1);
test_shell_execute_cmd("clear dummy", -EINVAL);
test_shell_execute_cmd("clear dummy dummy", -EINVAL);
}
static void test_cmd_shell(void)
{
test_shell_execute_cmd("shell -h", 1);
test_shell_execute_cmd("shell --help", 1);
test_shell_execute_cmd("shell dummy", 1);
test_shell_execute_cmd("shell dummy dummy", 1);
/* subcommand: backspace_mode */
test_shell_execute_cmd("shell backspace_mode -h", 1);
test_shell_execute_cmd("shell backspace_mode --help", 1);
test_shell_execute_cmd("shell backspace_mode dummy", 1);
test_shell_execute_cmd("shell backspace_mode backspace", 0);
test_shell_execute_cmd("shell backspace_mode backspace -h", 1);
test_shell_execute_cmd("shell backspace_mode backspace --help", 1);
test_shell_execute_cmd("shell backspace_mode backspace dummy", -EINVAL);
test_shell_execute_cmd("shell backspace_mode backspace dummy dummy",
-EINVAL);
test_shell_execute_cmd("shell backspace_mode delete", 0);
test_shell_execute_cmd("shell backspace_mode delete -h", 1);
test_shell_execute_cmd("shell backspace_mode delete --help", 1);
test_shell_execute_cmd("shell backspace_mode delete dummy", -EINVAL);
test_shell_execute_cmd("shell backspace_mode delete dummy dummy",
-EINVAL);
/* subcommand: colors */
test_shell_execute_cmd("shell colors -h", 1);
test_shell_execute_cmd("shell colors --help", 1);
test_shell_execute_cmd("shell colors dummy", 1);
test_shell_execute_cmd("shell colors dummy dummy", 1);
test_shell_execute_cmd("shell colors off", 0);
test_shell_execute_cmd("shell colors off -h", 1);
test_shell_execute_cmd("shell colors off --help", 1);
test_shell_execute_cmd("shell colors off dummy", -EINVAL);
test_shell_execute_cmd("shell colors off dummy dummy", -EINVAL);
test_shell_execute_cmd("shell colors on", 0);
test_shell_execute_cmd("shell colors on -h", 1);
test_shell_execute_cmd("shell colors on --help", 1);
test_shell_execute_cmd("shell colors on dummy", -EINVAL);
test_shell_execute_cmd("shell colors on dummy dummy", -EINVAL);
/* subcommand: echo */
test_shell_execute_cmd("shell echo", 0);
test_shell_execute_cmd("shell echo -h", 1);
test_shell_execute_cmd("shell echo --help", 1);
test_shell_execute_cmd("shell echo dummy", -EINVAL);
test_shell_execute_cmd("shell echo dummy dummy", -EINVAL);
test_shell_execute_cmd("shell echo off", 0);
test_shell_execute_cmd("shell echo off -h", 1);
test_shell_execute_cmd("shell echo off --help", 1);
test_shell_execute_cmd("shell echo off dummy", -EINVAL);
test_shell_execute_cmd("shell echo off dummy dummy", -EINVAL);
test_shell_execute_cmd("shell echo on", 0);
test_shell_execute_cmd("shell echo on -h", 1);
test_shell_execute_cmd("shell echo on --help", 1);
test_shell_execute_cmd("shell echo on dummy", -EINVAL);
test_shell_execute_cmd("shell echo on dummy dummy", -EINVAL);
/* subcommand: stats */
test_shell_execute_cmd("shell stats", 1);
test_shell_execute_cmd("shell stats -h", 1);
test_shell_execute_cmd("shell stats --help", 1);
test_shell_execute_cmd("shell stats dummy", 1);
test_shell_execute_cmd("shell stats dummy dummy", 1);
test_shell_execute_cmd("shell stats reset", 0);
test_shell_execute_cmd("shell stats reset -h", 1);
test_shell_execute_cmd("shell stats reset --help", 1);
test_shell_execute_cmd("shell stats reset dummy", -EINVAL);
test_shell_execute_cmd("shell stats reset dummy dummy", -EINVAL);
test_shell_execute_cmd("shell stats show", 0);
test_shell_execute_cmd("shell stats show -h", 1);
test_shell_execute_cmd("shell stats show --help", 1);
test_shell_execute_cmd("shell stats show dummy", -EINVAL);
test_shell_execute_cmd("shell stats show dummy dummy", -EINVAL);
}
static void test_cmd_history(void)
{
test_shell_execute_cmd("history", 0);
test_shell_execute_cmd("history -h", 1);
test_shell_execute_cmd("history --help", 1);
test_shell_execute_cmd("history dummy", -EINVAL);
test_shell_execute_cmd("history dummy dummy", -EINVAL);
}
static void test_cmd_resize(void)
{
test_shell_execute_cmd("resize -h", 1);
test_shell_execute_cmd("resize --help", 1);
test_shell_execute_cmd("resize dummy", -EINVAL);
test_shell_execute_cmd("resize dummy dummy", -EINVAL);
/* subcommand: default */
test_shell_execute_cmd("resize default", 0);
test_shell_execute_cmd("resize default -h", 1);
test_shell_execute_cmd("resize default --help", 1);
test_shell_execute_cmd("resize default dummy", -EINVAL);
test_shell_execute_cmd("resize default dummy dummy", -EINVAL);
}
static void test_shell_module(void)
{
test_shell_execute_cmd("test_shell_cmd", 0);
test_shell_execute_cmd("test_shell_cmd -h", 1);
test_shell_execute_cmd("test_shell_cmd --help", 1);
test_shell_execute_cmd("test_shell_cmd dummy", -EINVAL);
test_shell_execute_cmd("test_shell_cmd dummy dummy", -EINVAL);
test_shell_execute_cmd("", -ENOEXEC); /* empty command */
test_shell_execute_cmd("not existing command", -ENOEXEC);
}
/* test wildcard and static subcommands */
static void test_shell_wildcards_static(void)
{
test_shell_execute_cmd("test_wildcard", 0);
test_shell_execute_cmd("test_wildcard argument_1", 1);
test_shell_execute_cmd("test_wildcard argument?1", 1);
test_shell_execute_cmd("test_wildcard argu?ent?1", 1);
test_shell_execute_cmd("test_wildcard a*1", 1);
test_shell_execute_cmd("test_wildcard ar?u*1", 1);
test_shell_execute_cmd("test_wildcard *", 3);
test_shell_execute_cmd("test_wildcard a*", 2);
}
/* test wildcard and dynamic subcommands */
static void test_shell_wildcards_dynamic(void)
{
test_shell_execute_cmd("test_dynamic", 0);
test_shell_execute_cmd("test_dynamic d*", 1);
test_shell_execute_cmd("test_dynamic c*", 1);
test_shell_execute_cmd("test_dynamic d* c*", 2);
}
static int cmd_test_module(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argv);
ARG_UNUSED(argc);
return 0;
}
SHELL_CMD_ARG_REGISTER(test_shell_cmd, NULL, "help", cmd_test_module, 1, 0);
static int cmd_wildcard(const struct shell *shell, size_t argc, char **argv)
{
int valid_arguments = 0;
for (size_t i = 1; i < argc; i++) {
if (!strcmp("argument_1", argv[i])) {
valid_arguments++;
continue;
}
if (!strcmp("argument_2", argv[i])) {
valid_arguments++;
continue;
}
if (!strcmp("dummy", argv[i])) {
valid_arguments++;
}
}
return valid_arguments;
}
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_test_shell_cmdl,
SHELL_CMD(argument_1, NULL, NULL, NULL),
SHELL_CMD(argument_2, NULL, NULL, NULL),
SHELL_CMD(dummy, NULL, NULL, NULL),
SHELL_SUBCMD_SET_END
);
SHELL_CMD_REGISTER(test_wildcard, &m_sub_test_shell_cmdl, NULL, cmd_wildcard);
static int cmd_dynamic(const struct shell *shell, size_t argc, char **argv)
{
int valid_arguments = 0;
for (size_t i = 1; i < argc; i++) {
if (!strcmp(dynamic_cmd_buffer[0], argv[i])) {
valid_arguments++;
continue;
}
if (!strcmp(dynamic_cmd_buffer[1], argv[i])) {
valid_arguments++;
}
}
return valid_arguments;
}
/* dynamic command creation */
static void dynamic_cmd_get(size_t idx, struct shell_static_entry *entry)
{
if (idx < ARRAY_SIZE(dynamic_cmd_buffer)) {
/* m_dynamic_cmd_buffer must be sorted alphabetically to ensure
* correct CLI completion
*/
entry->syntax = dynamic_cmd_buffer[idx];
entry->handler = NULL;
entry->subcmd = NULL;
entry->help = NULL;
} else {
/* if there are no more dynamic commands available syntax
* must be set to NULL.
*/
entry->syntax = NULL;
}
}
SHELL_DYNAMIC_CMD_CREATE(m_sub_test_dynamic, dynamic_cmd_get);
SHELL_CMD_REGISTER(test_dynamic, &m_sub_test_dynamic, NULL, cmd_dynamic);
static void unselect_cmd(void)
{
/* Unselecting command <shell color> */
const struct shell *shell = shell_backend_dummy_get_ptr();
shell->ctx->selected_cmd = NULL;
}
static void test_cmd_select(void)
{
unselect_cmd();
test_shell_execute_cmd("select -h", 1);
test_shell_execute_cmd("select clear", -EINVAL);
test_shell_execute_cmd("off", -ENOEXEC);
test_shell_execute_cmd("on", -ENOEXEC);
test_shell_execute_cmd("select shell colors", 0);
test_shell_execute_cmd("off", 0);
test_shell_execute_cmd("on", 0);
unselect_cmd();
test_shell_execute_cmd("off", -ENOEXEC);
test_shell_execute_cmd("on", -ENOEXEC);
}
static void test_set_root_cmd(void)
{
int err;
test_shell_execute_cmd("shell colors on", 0);
err = shell_set_root_cmd("__shell__");
zassert_equal(err, -EINVAL, "Unexpected error %d", err);
err = shell_set_root_cmd("shell");
zassert_equal(err, 0, "Unexpected error %d", err);
test_shell_execute_cmd("shell colors", 1);
test_shell_execute_cmd("colors on", 0);
err = shell_set_root_cmd(NULL);
zassert_equal(err, 0, "Unexpected error %d", err);
test_shell_execute_cmd("colors", -ENOEXEC);
test_shell_execute_cmd("shell colors on", 0);
}
static void test_shell_fprintf(void)
{
static const char expect[] = "testing 1 2 3";
const struct shell *shell;
const char *buf;
size_t size;
shell = shell_backend_dummy_get_ptr();
zassert_not_null(shell, "Failed to get shell");
/* Clear the output buffer */
shell_backend_dummy_clear_output(shell);
shell_fprintf(shell, SHELL_VT100_COLOR_DEFAULT, "testing %d %s %c",
1, "2", '3');
buf = shell_backend_dummy_get_output(shell, &size);
zassert_true(size >= sizeof(expect), "Expected size > %u, got %d",
sizeof(expect), size);
/*
* There are prompts and various ANSI characters in the output, so just
* check that the string is in there somewhere.
*/
zassert_true(strstr(buf, expect),
"Expected string to contain '%s', got '%s'", expect, buf);
}
#define RAW_ARG "aaa \"\" bbb"
#define CMD_NAME test_cmd_raw_arg
static int cmd_raw_arg(const struct shell *shell, size_t argc, char **argv)
{
if (argc == 2) {
if (strcmp(argv[0], STRINGIFY(CMD_NAME))) {
return -1;
}
if (strcmp(argv[1], RAW_ARG)) {
return -1;
}
} else if (argc > 2) {
return -1;
}
return 0;
}
SHELL_CMD_ARG_REGISTER(CMD_NAME, NULL, NULL, cmd_raw_arg, 1, SHELL_OPT_ARG_RAW);
static void test_raw_arg(void)
{
test_shell_execute_cmd("test_cmd_raw_arg aaa \"\" bbb", 0);
test_shell_execute_cmd("test_cmd_raw_arg", 0);
test_shell_execute_cmd("select test_cmd_raw_arg", 0);
test_shell_execute_cmd("aaa \"\" bbb", 0);
shell_set_root_cmd(NULL);
}
static int cmd_dummy(const struct shell *shell, size_t argc, char **argv)
{
return 0;
}
SHELL_CMD_REGISTER(dummy, NULL, NULL, cmd_dummy);
static void test_max_argc(void)
{
BUILD_ASSERT(CONFIG_SHELL_ARGC_MAX == 12,
"Unexpected test configuration.");
test_shell_execute_cmd("dummy 1 2 3 4 5 6 7 8 9 10 11", 0);
test_shell_execute_cmd("dummy 1 2 3 4 5 6 7 8 9 10 11 12", -ENOEXEC);
}
void test_main(void)
{
ztest_test_suite(shell_test_suite,
ztest_1cpu_unit_test(test_cmd_help),
ztest_unit_test(test_cmd_clear),
ztest_unit_test(test_cmd_shell),
ztest_unit_test(test_cmd_history),
ztest_unit_test(test_cmd_select),
ztest_unit_test(test_cmd_resize),
ztest_unit_test(test_shell_module),
ztest_unit_test(test_shell_wildcards_static),
ztest_unit_test(test_shell_wildcards_dynamic),
ztest_unit_test(test_shell_fprintf),
ztest_unit_test(test_set_root_cmd),
ztest_unit_test(test_raw_arg),
ztest_unit_test(test_max_argc)
);
/* Let the shell backend initialize. */
k_msleep(20);
ztest_run_test_suite(shell_test_suite);
}
|