blob: 62f2828b7c1a5df2c34c9e67a3384c012c87ea52 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa72007d2012-03-30 21:30:58 +00002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glassa72007d2012-03-30 21:30:58 +00004 */
5
6#define DEBUG
7
8#include <common.h>
9
10static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
11 "setenv list ${list}3\0"
12 "setenv list ${list}4";
13
14static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
15{
16 printf("%s: Testing commands\n", __func__);
Stephen Warren67486722014-02-03 13:24:23 -070017 run_command("env default -f -a", 0);
Simon Glassa72007d2012-03-30 21:30:58 +000018
Simon Glassa72007d2012-03-30 21:30:58 +000019 /* commands separated by \n */
20 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
Simon Glass00caae62017-08-03 12:22:12 -060021 assert(!strcmp("11", env_get("list")));
Simon Glassa72007d2012-03-30 21:30:58 +000022
23 /* command followed by \n and nothing else */
24 run_command_list("setenv list 1${list}\n", -1, 0);
Simon Glass00caae62017-08-03 12:22:12 -060025 assert(!strcmp("111", env_get("list")));
Simon Glassa72007d2012-03-30 21:30:58 +000026
Simon Glassa72007d2012-03-30 21:30:58 +000027 /* a command string with \0 in it. Stuff after \0 should be ignored */
28 run_command("setenv list", 0);
29 run_command_list(test_cmd, sizeof(test_cmd), 0);
Simon Glass00caae62017-08-03 12:22:12 -060030 assert(!strcmp("123", env_get("list")));
Simon Glassa72007d2012-03-30 21:30:58 +000031
32 /*
33 * a command list where we limit execution to only the first command
34 * using the length parameter.
35 */
36 run_command_list("setenv list 1\n setenv list ${list}2; "
37 "setenv list ${list}3", strlen("setenv list 1"), 0);
Simon Glass00caae62017-08-03 12:22:12 -060038 assert(!strcmp("1", env_get("list")));
Simon Glassa72007d2012-03-30 21:30:58 +000039
Simon Glass93ce7562014-05-30 14:41:48 -060040 assert(run_command("false", 0) == 1);
41 assert(run_command("echo", 0) == 0);
42 assert(run_command_list("false", -1, 0) == 1);
43 assert(run_command_list("echo", -1, 0) == 0);
44
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090045#ifdef CONFIG_HUSH_PARSER
Simon Glass87b63982014-10-07 13:59:43 -060046 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
47 run_command("run foo", 0);
Simon Glass00caae62017-08-03 12:22:12 -060048 assert(env_get("black") != NULL);
49 assert(!strcmp("1", env_get("black")));
50 assert(env_get("adder") != NULL);
51 assert(!strcmp("2", env_get("adder")));
Stephen Warrenf2afe702014-02-03 13:24:24 -070052#endif
53
Rabin Vincent484408f2014-10-29 23:21:39 +010054 assert(run_command("", 0) == 0);
55 assert(run_command(" ", 0) == 0);
56
Rabin Vincent2302b3a2014-10-29 23:21:41 +010057 assert(run_command("'", 0) == 1);
58
Simon Glassa72007d2012-03-30 21:30:58 +000059 printf("%s: Everything went swimmingly\n", __func__);
60 return 0;
61}
62
63U_BOOT_CMD(
64 ut_cmd, 5, 1, do_ut_cmd,
65 "Very basic test of command parsers",
66 ""
67);