Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, The Chromium Authors |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define DEBUG |
| 7 | |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 9 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <linux/errno.h> |
Simon Glass | 674a9fc | 2024-11-02 13:36:52 -0600 | [diff] [blame] | 13 | #include <test/cmd.h> |
| 14 | #include <test/ut.h> |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 15 | |
| 16 | static 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 Glass | 674a9fc | 2024-11-02 13:36:52 -0600 | [diff] [blame] | 20 | static int command_test(struct unit_test_state *uts) |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 21 | { |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 22 | char long_str[CONFIG_SYS_CBSIZE + 42]; |
| 23 | |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 24 | printf("%s: Testing commands\n", __func__); |
Stephen Warren | 2e2a917 | 2014-02-03 13:24:23 -0700 | [diff] [blame] | 25 | run_command("env default -f -a", 0); |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 26 | |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 27 | /* commands separated by \n */ |
| 28 | run_command_list("setenv list 1\n setenv list ${list}1", -1, 0); |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 29 | ut_assert(!strcmp("11", env_get("list"))); |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 30 | |
| 31 | /* command followed by \n and nothing else */ |
| 32 | run_command_list("setenv list 1${list}\n", -1, 0); |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 33 | ut_assert(!strcmp("111", env_get("list"))); |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 34 | |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 35 | /* 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 Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 38 | ut_assert(!strcmp("123", env_get("list"))); |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 39 | |
| 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 Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 46 | ut_assert(!strcmp("1", env_get("list"))); |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 47 | |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 48 | 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 Glass | 9956b5e | 2014-05-30 14:41:48 -0600 | [diff] [blame] | 52 | |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 53 | #ifdef CONFIG_HUSH_PARSER |
Simon Glass | 94cb9c2 | 2014-10-07 13:59:43 -0600 | [diff] [blame] | 54 | run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0); |
| 55 | run_command("run foo", 0); |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 56 | 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 Warren | 65bc075 | 2014-02-03 13:24:24 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 62 | ut_assertok(run_command("", 0)); |
| 63 | ut_assertok(run_command(" ", 0)); |
Rabin Vincent | 401ac65 | 2014-10-29 23:21:39 +0100 | [diff] [blame] | 64 | |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 65 | ut_asserteq(1, run_command("'", 0)); |
Rabin Vincent | c3249b8 | 2014-10-29 23:21:41 +0100 | [diff] [blame] | 66 | |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 67 | /* Variadic function test-cases */ |
| 68 | #pragma GCC diagnostic push |
| 69 | #pragma GCC diagnostic ignored "-Wformat-zero-length" |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 70 | ut_assertok(run_commandf("")); |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 71 | #pragma GCC diagnostic pop |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 72 | ut_assertok(run_commandf(" ")); |
| 73 | ut_asserteq(1, run_commandf("'")); |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 74 | |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 75 | ut_assertok(run_commandf("env %s %s", "delete -f", "list")); |
Simon Glass | 674a9fc | 2024-11-02 13:36:52 -0600 | [diff] [blame] | 76 | /* |
| 77 | * Expected: "## Error: "list" not defined" |
| 78 | * (disabled to avoid pytest bailing out) |
| 79 | * |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 80 | * ut_asserteq(1, run_commandf("printenv list")); |
Simon Glass | 674a9fc | 2024-11-02 13:36:52 -0600 | [diff] [blame] | 81 | */ |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 82 | |
| 83 | memset(long_str, 'x', sizeof(long_str)); |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 84 | ut_asserteq(-ENOSPC, run_commandf("Truncation case: %s", long_str)); |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 85 | |
| 86 | if (IS_ENABLED(CONFIG_HUSH_PARSER)) { |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 87 | 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 Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Clean up before exit */ |
Simon Glass | 24533d3 | 2024-11-02 13:36:53 -0600 | [diff] [blame] | 100 | ut_assertok(run_command("env default -f -a", 0)); |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 101 | |
Simon Glass | 674a9fc | 2024-11-02 13:36:52 -0600 | [diff] [blame] | 102 | /* put back the FDT environment */ |
| 103 | ut_assertok(env_set("from_fdt", "yes")); |
| 104 | |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 105 | printf("%s: Everything went swimmingly\n", __func__); |
| 106 | return 0; |
| 107 | } |
Simon Glass | 674a9fc | 2024-11-02 13:36:52 -0600 | [diff] [blame] | 108 | CMD_TEST(command_test, 0); |