blob: 9837d10eb5c95d8cb689d819367d2cb36e205f96 [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 Glassed38aef2020-05-10 11:40:03 -060010#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass10c08ef2012-03-30 21:30:58 +000012
13static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
14 "setenv list ${list}3\0"
15 "setenv list ${list}4";
16
Simon Glassed38aef2020-05-10 11:40:03 -060017static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Simon Glass10c08ef2012-03-30 21:30:58 +000019{
20 printf("%s: Testing commands\n", __func__);
Stephen Warren2e2a9172014-02-03 13:24:23 -070021 run_command("env default -f -a", 0);
Simon Glass10c08ef2012-03-30 21:30:58 +000022
Simon Glass10c08ef2012-03-30 21:30:58 +000023 /* commands separated by \n */
24 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
Simon Glass64b723f2017-08-03 12:22:12 -060025 assert(!strcmp("11", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000026
27 /* command followed by \n and nothing else */
28 run_command_list("setenv list 1${list}\n", -1, 0);
Simon Glass64b723f2017-08-03 12:22:12 -060029 assert(!strcmp("111", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000030
Simon Glass10c08ef2012-03-30 21:30:58 +000031 /* a command string with \0 in it. Stuff after \0 should be ignored */
32 run_command("setenv list", 0);
33 run_command_list(test_cmd, sizeof(test_cmd), 0);
Simon Glass64b723f2017-08-03 12:22:12 -060034 assert(!strcmp("123", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000035
36 /*
37 * a command list where we limit execution to only the first command
38 * using the length parameter.
39 */
40 run_command_list("setenv list 1\n setenv list ${list}2; "
41 "setenv list ${list}3", strlen("setenv list 1"), 0);
Simon Glass64b723f2017-08-03 12:22:12 -060042 assert(!strcmp("1", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000043
Simon Glass9956b5e2014-05-30 14:41:48 -060044 assert(run_command("false", 0) == 1);
45 assert(run_command("echo", 0) == 0);
46 assert(run_command_list("false", -1, 0) == 1);
47 assert(run_command_list("echo", -1, 0) == 0);
48
Masahiro Yamada17a48e42016-06-21 02:11:19 +090049#ifdef CONFIG_HUSH_PARSER
Simon Glass94cb9c22014-10-07 13:59:43 -060050 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
51 run_command("run foo", 0);
Simon Glass64b723f2017-08-03 12:22:12 -060052 assert(env_get("black") != NULL);
53 assert(!strcmp("1", env_get("black")));
54 assert(env_get("adder") != NULL);
55 assert(!strcmp("2", env_get("adder")));
Stephen Warren65bc0752014-02-03 13:24:24 -070056#endif
57
Rabin Vincent401ac652014-10-29 23:21:39 +010058 assert(run_command("", 0) == 0);
59 assert(run_command(" ", 0) == 0);
60
Rabin Vincentc3249b82014-10-29 23:21:41 +010061 assert(run_command("'", 0) == 1);
62
Simon Glass10c08ef2012-03-30 21:30:58 +000063 printf("%s: Everything went swimmingly\n", __func__);
64 return 0;
65}
66
67U_BOOT_CMD(
68 ut_cmd, 5, 1, do_ut_cmd,
69 "Very basic test of command parsers",
70 ""
71);