blob: 5ec93d490ba5f20e9f27a395ba7b30941e261086 [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 Glass674a9fc2024-11-02 13:36:52 -060013#include <test/cmd.h>
14#include <test/ut.h>
Simon Glass10c08ef2012-03-30 21:30:58 +000015
16static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
17 "setenv list ${list}3\0"
18 "setenv list ${list}4";
19
Simon Glass674a9fc2024-11-02 13:36:52 -060020static int command_test(struct unit_test_state *uts)
Simon Glass10c08ef2012-03-30 21:30:58 +000021{
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030022 char long_str[CONFIG_SYS_CBSIZE + 42];
23
Simon Glass10c08ef2012-03-30 21:30:58 +000024 printf("%s: Testing commands\n", __func__);
Stephen Warren2e2a9172014-02-03 13:24:23 -070025 run_command("env default -f -a", 0);
Simon Glass10c08ef2012-03-30 21:30:58 +000026
Simon Glass10c08ef2012-03-30 21:30:58 +000027 /* commands separated by \n */
28 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
Simon Glass24533d32024-11-02 13:36:53 -060029 ut_assert(!strcmp("11", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000030
31 /* command followed by \n and nothing else */
32 run_command_list("setenv list 1${list}\n", -1, 0);
Simon Glass24533d32024-11-02 13:36:53 -060033 ut_assert(!strcmp("111", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000034
Simon Glass10c08ef2012-03-30 21:30:58 +000035 /* 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);
Simon Glass24533d32024-11-02 13:36:53 -060038 ut_assert(!strcmp("123", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000039
40 /*
41 * a command list where we limit execution to only the first command
42 * using the length parameter.
43 */
44 run_command_list("setenv list 1\n setenv list ${list}2; "
45 "setenv list ${list}3", strlen("setenv list 1"), 0);
Simon Glass24533d32024-11-02 13:36:53 -060046 ut_assert(!strcmp("1", env_get("list")));
Simon Glass10c08ef2012-03-30 21:30:58 +000047
Simon Glass24533d32024-11-02 13:36:53 -060048 ut_asserteq(1, run_command("false", 0));
49 ut_assertok(run_command("echo", 0));
50 ut_asserteq(1, run_command_list("false", -1, 0));
51 ut_assertok(run_command_list("echo", -1, 0));
Simon Glass9956b5e2014-05-30 14:41:48 -060052
Masahiro Yamada17a48e42016-06-21 02:11:19 +090053#ifdef CONFIG_HUSH_PARSER
Simon Glass94cb9c22014-10-07 13:59:43 -060054 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0);
55 run_command("run foo", 0);
Simon Glass24533d32024-11-02 13:36:53 -060056 ut_assertnonnull(env_get("black"));
57 ut_asserteq(0, strcmp("1", env_get("black")));
58 ut_assertnonnull(env_get("adder"));
59 ut_asserteq(0, strcmp("2", env_get("adder")));
Stephen Warren65bc0752014-02-03 13:24:24 -070060#endif
61
Simon Glass24533d32024-11-02 13:36:53 -060062 ut_assertok(run_command("", 0));
63 ut_assertok(run_command(" ", 0));
Rabin Vincent401ac652014-10-29 23:21:39 +010064
Simon Glass24533d32024-11-02 13:36:53 -060065 ut_asserteq(1, run_command("'", 0));
Rabin Vincentc3249b82014-10-29 23:21:41 +010066
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030067 /* Variadic function test-cases */
68#pragma GCC diagnostic push
69#pragma GCC diagnostic ignored "-Wformat-zero-length"
Simon Glass24533d32024-11-02 13:36:53 -060070 ut_assertok(run_commandf(""));
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030071#pragma GCC diagnostic pop
Simon Glass24533d32024-11-02 13:36:53 -060072 ut_assertok(run_commandf(" "));
73 ut_asserteq(1, run_commandf("'"));
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030074
Simon Glass24533d32024-11-02 13:36:53 -060075 ut_assertok(run_commandf("env %s %s", "delete -f", "list"));
Simon Glass674a9fc2024-11-02 13:36:52 -060076 /*
77 * Expected: "## Error: "list" not defined"
78 * (disabled to avoid pytest bailing out)
79 *
Simon Glass24533d32024-11-02 13:36:53 -060080 * ut_asserteq(1, run_commandf("printenv list"));
Simon Glass674a9fc2024-11-02 13:36:52 -060081 */
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030082
83 memset(long_str, 'x', sizeof(long_str));
Simon Glass24533d32024-11-02 13:36:53 -060084 ut_asserteq(-ENOSPC, run_commandf("Truncation case: %s", long_str));
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030085
86 if (IS_ENABLED(CONFIG_HUSH_PARSER)) {
Simon Glass24533d32024-11-02 13:36:53 -060087 ut_assertok(run_commandf("env %s %s %s %s", "delete -f",
88 "adder", "black", "foo"));
89 ut_assertok(run_commandf(
90 "setenv foo 'setenv %s 1\nsetenv %s 2'",
91 "black", "adder"));
92 ut_assertok(run_command("run foo", 0));
93 ut_assertnonnull(env_get("black"));
94 ut_asserteq(0, strcmp("1", env_get("black")));
95 ut_assertnonnull(env_get("adder"));
96 ut_asserteq(0, strcmp("2", env_get("adder")));
Evgeny Bachinin0742ce72023-03-20 11:23:12 +030097 }
98
99 /* Clean up before exit */
Simon Glass24533d32024-11-02 13:36:53 -0600100 ut_assertok(run_command("env default -f -a", 0));
Evgeny Bachinin0742ce72023-03-20 11:23:12 +0300101
Simon Glass674a9fc2024-11-02 13:36:52 -0600102 /* put back the FDT environment */
103 ut_assertok(env_set("from_fdt", "yes"));
104
Simon Glass10c08ef2012-03-30 21:30:58 +0000105 printf("%s: Everything went swimmingly\n", __func__);
106 return 0;
107}
Simon Glass674a9fc2024-11-02 13:36:52 -0600108CMD_TEST(command_test, 0);