command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / test / command_ut.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012, The Chromium Authors
4  */
5
6 #define DEBUG
7
8 #include <common.h>
9 #include <command.h>
10 #include <env.h>
11
12 static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
13                 "setenv list ${list}3\0"
14                 "setenv list ${list}4";
15
16 static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc,
17                      char *const argv[])
18 {
19         printf("%s: Testing commands\n", __func__);
20         run_command("env default -f -a", 0);
21
22         /* commands separated by \n */
23         run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
24         assert(!strcmp("11", env_get("list")));
25
26         /* command followed by \n and nothing else */
27         run_command_list("setenv list 1${list}\n", -1, 0);
28         assert(!strcmp("111", env_get("list")));
29
30         /* a command string with \0 in it. Stuff after \0 should be ignored */
31         run_command("setenv list", 0);
32         run_command_list(test_cmd, sizeof(test_cmd), 0);
33         assert(!strcmp("123", env_get("list")));
34
35         /*
36          * a command list where we limit execution to only the first command
37          * using the length parameter.
38          */
39         run_command_list("setenv list 1\n setenv list ${list}2; "
40                 "setenv list ${list}3", strlen("setenv list 1"), 0);
41         assert(!strcmp("1", env_get("list")));
42
43         assert(run_command("false", 0) == 1);
44         assert(run_command("echo", 0) == 0);
45         assert(run_command_list("false", -1, 0) == 1);
46         assert(run_command_list("echo", -1, 0) == 0);
47
48 #ifdef CONFIG_HUSH_PARSER
49         run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
50         run_command("run foo", 0);
51         assert(env_get("black") != NULL);
52         assert(!strcmp("1", env_get("black")));
53         assert(env_get("adder") != NULL);
54         assert(!strcmp("2", env_get("adder")));
55 #endif
56
57         assert(run_command("", 0) == 0);
58         assert(run_command(" ", 0) == 0);
59
60         assert(run_command("'", 0) == 1);
61
62         printf("%s: Everything went swimmingly\n", __func__);
63         return 0;
64 }
65
66 U_BOOT_CMD(
67         ut_cmd, 5,      1,      do_ut_cmd,
68         "Very basic test of command parsers",
69         ""
70 );