1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012, The Chromium Authors
13 #include <linux/errno.h>
15 static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
16 "setenv list ${list}3\0"
17 "setenv list ${list}4";
19 static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc,
22 char long_str[CONFIG_SYS_CBSIZE + 42];
24 printf("%s: Testing commands\n", __func__);
25 run_command("env default -f -a", 0);
27 /* commands separated by \n */
28 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
29 assert(!strcmp("11", env_get("list")));
31 /* command followed by \n and nothing else */
32 run_command_list("setenv list 1${list}\n", -1, 0);
33 assert(!strcmp("111", env_get("list")));
35 /* a command string with \0 in it. Stuff after \0 should be ignored */
36 run_command("setenv list", 0);
37 run_command_list(test_cmd, sizeof(test_cmd), 0);
38 assert(!strcmp("123", env_get("list")));
41 * a command list where we limit execution to only the first command
42 * using the length parameter.
44 run_command_list("setenv list 1\n setenv list ${list}2; "
45 "setenv list ${list}3", strlen("setenv list 1"), 0);
46 assert(!strcmp("1", env_get("list")));
48 assert(run_command("false", 0) == 1);
49 assert(run_command("echo", 0) == 0);
50 assert(run_command_list("false", -1, 0) == 1);
51 assert(run_command_list("echo", -1, 0) == 0);
53 #ifdef CONFIG_HUSH_PARSER
54 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
55 run_command("run foo", 0);
56 assert(env_get("black") != NULL);
57 assert(!strcmp("1", env_get("black")));
58 assert(env_get("adder") != NULL);
59 assert(!strcmp("2", env_get("adder")));
62 assert(run_command("", 0) == 0);
63 assert(run_command(" ", 0) == 0);
65 assert(run_command("'", 0) == 1);
67 /* Variadic function test-cases */
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wformat-zero-length"
70 assert(run_commandf("") == 0);
71 #pragma GCC diagnostic pop
72 assert(run_commandf(" ") == 0);
73 assert(run_commandf("'") == 1);
75 assert(run_commandf("env %s %s", "delete -f", "list") == 0);
76 /* Expected: "Error: "list" not defined" */
77 assert(run_commandf("printenv list") == 1);
79 memset(long_str, 'x', sizeof(long_str));
80 assert(run_commandf("Truncation case: %s", long_str) == -ENOSPC);
82 if (IS_ENABLED(CONFIG_HUSH_PARSER)) {
83 assert(run_commandf("env %s %s %s %s", "delete -f", "adder",
84 "black", "foo") == 0);
85 assert(run_commandf("setenv foo 'setenv %s 1\nsetenv %s 2'",
86 "black", "adder") == 0);
87 run_command("run foo", 0);
88 assert(env_get("black"));
89 assert(!strcmp("1", env_get("black")));
90 assert(env_get("adder"));
91 assert(!strcmp("2", env_get("adder")));
94 /* Clean up before exit */
95 run_command("env default -f -a", 0);
97 printf("%s: Everything went swimmingly\n", __func__);
102 ut_cmd, 5, 1, do_ut_cmd,
103 "Very basic test of command parsers",