blob: 8e268e5ee523af975e4aa5836b09b903c2f24e41 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass10c08ef2012-03-30 21:30:58 +00002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glass10c08ef2012-03-30 21:30:58 +00004 */
5
6#define DEBUG
7
8#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -07009#include <command.h>
Simon Glass10c08ef2012-03-30 21:30:58 +000010
11static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
12 "setenv list ${list}3\0"
13 "setenv list ${list}4";
14
15static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
16{
17 printf("%s: Testing commands\n", __func__);
Stephen Warren2e2a9172014-02-03 13:24:23 -070018 run_command("env default -f -a", 0);
Simon Glass10c08ef2012-03-30 21:30:58 +000019
Simon Glass10c08ef2012-03-30 21:30:58 +000020 /* commands separated by \n */
21 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
Simon Glass64b723f2017-08-03 12:22:12 -060022 assert(!strcmp("11", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000023
24 /* command followed by \n and nothing else */
25 run_command_list("setenv list 1${list}\n", -1, 0);
Simon Glass64b723f2017-08-03 12:22:12 -060026 assert(!strcmp("111", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000027
Simon Glass10c08ef2012-03-30 21:30:58 +000028 /* a command string with \0 in it. Stuff after \0 should be ignored */
29 run_command("setenv list", 0);
30 run_command_list(test_cmd, sizeof(test_cmd), 0);
Simon Glass64b723f2017-08-03 12:22:12 -060031 assert(!strcmp("123", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000032
33 /*
34 * a command list where we limit execution to only the first command
35 * using the length parameter.
36 */
37 run_command_list("setenv list 1\n setenv list ${list}2; "
38 "setenv list ${list}3", strlen("setenv list 1"), 0);
Simon Glass64b723f2017-08-03 12:22:12 -060039 assert(!strcmp("1", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000040
Simon Glass9956b5e2014-05-30 14:41:48 -060041 assert(run_command("false", 0) == 1);
42 assert(run_command("echo", 0) == 0);
43 assert(run_command_list("false", -1, 0) == 1);
44 assert(run_command_list("echo", -1, 0) == 0);
45
Masahiro Yamada17a48e42016-06-21 02:11:19 +090046#ifdef CONFIG_HUSH_PARSER
Simon Glass94cb9c22014-10-07 13:59:43 -060047 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
48 run_command("run foo", 0);
Simon Glass64b723f2017-08-03 12:22:12 -060049 assert(env_get("black") != NULL);
50 assert(!strcmp("1", env_get("black")));
51 assert(env_get("adder") != NULL);
52 assert(!strcmp("2", env_get("adder")));
Stephen Warren65bc0752014-02-03 13:24:24 -070053#endif
54
Rabin Vincent401ac652014-10-29 23:21:39 +010055 assert(run_command("", 0) == 0);
56 assert(run_command(" ", 0) == 0);
57
Rabin Vincentc3249b82014-10-29 23:21:41 +010058 assert(run_command("'", 0) == 1);
59
Simon Glass10c08ef2012-03-30 21:30:58 +000060 printf("%s: Everything went swimmingly\n", __func__);
61 return 0;
62}
63
64U_BOOT_CMD(
65 ut_cmd, 5, 1, do_ut_cmd,
66 "Very basic test of command parsers",
67 ""
68);