blob: 2b8d28d7ae325d7bd6ec9b001ccb1b372e6a9e2c [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
Simon Glassadaaa482019-11-14 12:57:43 -07008#include <command.h>
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030011#include <string.h>
12#include <linux/errno.h>
Simon Glass10c08ef2012-03-30 21:30:58 +000013
14static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
15 "setenv list ${list}3\0"
16 "setenv list ${list}4";
17
Simon Glassed38aef2020-05-10 11:40:03 -060018static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc,
19 char *const argv[])
Simon Glass10c08ef2012-03-30 21:30:58 +000020{
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030021 char long_str[CONFIG_SYS_CBSIZE + 42];
22
Simon Glass10c08ef2012-03-30 21:30:58 +000023 printf("%s: Testing commands\n", __func__);
Stephen Warren2e2a9172014-02-03 13:24:23 -070024 run_command("env default -f -a", 0);
Simon Glass10c08ef2012-03-30 21:30:58 +000025
Simon Glass10c08ef2012-03-30 21:30:58 +000026 /* commands separated by \n */
27 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
Simon Glass64b723f2017-08-03 12:22:12 -060028 assert(!strcmp("11", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000029
30 /* command followed by \n and nothing else */
31 run_command_list("setenv list 1${list}\n", -1, 0);
Simon Glass64b723f2017-08-03 12:22:12 -060032 assert(!strcmp("111", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000033
Simon Glass10c08ef2012-03-30 21:30:58 +000034 /* a command string with \0 in it. Stuff after \0 should be ignored */
35 run_command("setenv list", 0);
36 run_command_list(test_cmd, sizeof(test_cmd), 0);
Simon Glass64b723f2017-08-03 12:22:12 -060037 assert(!strcmp("123", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000038
39 /*
40 * a command list where we limit execution to only the first command
41 * using the length parameter.
42 */
43 run_command_list("setenv list 1\n setenv list ${list}2; "
44 "setenv list ${list}3", strlen("setenv list 1"), 0);
Simon Glass64b723f2017-08-03 12:22:12 -060045 assert(!strcmp("1", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000046
Simon Glass9956b5e2014-05-30 14:41:48 -060047 assert(run_command("false", 0) == 1);
48 assert(run_command("echo", 0) == 0);
49 assert(run_command_list("false", -1, 0) == 1);
50 assert(run_command_list("echo", -1, 0) == 0);
51
Masahiro Yamada17a48e42016-06-21 02:11:19 +090052#ifdef CONFIG_HUSH_PARSER
Simon Glass94cb9c22014-10-07 13:59:43 -060053 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
54 run_command("run foo", 0);
Simon Glass64b723f2017-08-03 12:22:12 -060055 assert(env_get("black") != NULL);
56 assert(!strcmp("1", env_get("black")));
57 assert(env_get("adder") != NULL);
58 assert(!strcmp("2", env_get("adder")));
Stephen Warren65bc0752014-02-03 13:24:24 -070059#endif
60
Rabin Vincent401ac652014-10-29 23:21:39 +010061 assert(run_command("", 0) == 0);
62 assert(run_command(" ", 0) == 0);
63
Rabin Vincentc3249b82014-10-29 23:21:41 +010064 assert(run_command("'", 0) == 1);
65
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030066 /* Variadic function test-cases */
67#pragma GCC diagnostic push
68#pragma GCC diagnostic ignored "-Wformat-zero-length"
69 assert(run_commandf("") == 0);
70#pragma GCC diagnostic pop
71 assert(run_commandf(" ") == 0);
72 assert(run_commandf("'") == 1);
73
74 assert(run_commandf("env %s %s", "delete -f", "list") == 0);
75 /* Expected: "Error: "list" not defined" */
76 assert(run_commandf("printenv list") == 1);
77
78 memset(long_str, 'x', sizeof(long_str));
79 assert(run_commandf("Truncation case: %s", long_str) == -ENOSPC);
80
81 if (IS_ENABLED(CONFIG_HUSH_PARSER)) {
82 assert(run_commandf("env %s %s %s %s", "delete -f", "adder",
83 "black", "foo") == 0);
84 assert(run_commandf("setenv foo 'setenv %s 1\nsetenv %s 2'",
85 "black", "adder") == 0);
86 run_command("run foo", 0);
87 assert(env_get("black"));
88 assert(!strcmp("1", env_get("black")));
89 assert(env_get("adder"));
90 assert(!strcmp("2", env_get("adder")));
91 }
92
93 /* Clean up before exit */
94 run_command("env default -f -a", 0);
95
Simon Glass10c08ef2012-03-30 21:30:58 +000096 printf("%s: Everything went swimmingly\n", __func__);
97 return 0;
98}
99
100U_BOOT_CMD(
101 ut_cmd, 5, 1, do_ut_cmd,
102 "Very basic test of command parsers",
103 ""
104);