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 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame^] | 8 | #include <common.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Evgeny Bachinin | 0742ce7 | 2023-03-20 11:23:12 +0300 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <linux/errno.h> |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 14 | |
| 15 | static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; " |
| 16 | "setenv list ${list}3\0" |
| 17 | "setenv list ${list}4"; |
| 18 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 19 | static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc, |
| 20 | char *const argv[]) |
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 | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 29 | 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 | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 33 | 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 | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 38 | 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 | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 46 | assert(!strcmp("1", env_get("list"))); |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 47 | |
Simon Glass | 9956b5e | 2014-05-30 14:41:48 -0600 | [diff] [blame] | 48 | assert(run_command("false", 0) == 1); |
| 49 | assert(run_command("echo", 0) == 0); |
| 50 | assert(run_command_list("false", -1, 0) == 1); |
| 51 | assert(run_command_list("echo", -1, 0) == 0); |
| 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 | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 56 | assert(env_get("black") != NULL); |
| 57 | assert(!strcmp("1", env_get("black"))); |
| 58 | assert(env_get("adder") != NULL); |
| 59 | assert(!strcmp("2", env_get("adder"))); |
Stephen Warren | 65bc075 | 2014-02-03 13:24:24 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
Rabin Vincent | 401ac65 | 2014-10-29 23:21:39 +0100 | [diff] [blame] | 62 | assert(run_command("", 0) == 0); |
| 63 | assert(run_command(" ", 0) == 0); |
| 64 | |
Rabin Vincent | c3249b8 | 2014-10-29 23:21:41 +0100 | [diff] [blame] | 65 | assert(run_command("'", 0) == 1); |
| 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" |
| 70 | assert(run_commandf("") == 0); |
| 71 | #pragma GCC diagnostic pop |
| 72 | assert(run_commandf(" ") == 0); |
| 73 | assert(run_commandf("'") == 1); |
| 74 | |
| 75 | assert(run_commandf("env %s %s", "delete -f", "list") == 0); |
| 76 | /* Expected: "Error: "list" not defined" */ |
| 77 | assert(run_commandf("printenv list") == 1); |
| 78 | |
| 79 | memset(long_str, 'x', sizeof(long_str)); |
| 80 | assert(run_commandf("Truncation case: %s", long_str) == -ENOSPC); |
| 81 | |
| 82 | if (IS_ENABLED(CONFIG_HUSH_PARSER)) { |
| 83 | assert(run_commandf("env %s %s %s %s", "delete -f", "adder", |
| 84 | "black", "foo") == 0); |
| 85 | assert(run_commandf("setenv foo 'setenv %s 1\nsetenv %s 2'", |
| 86 | "black", "adder") == 0); |
| 87 | run_command("run foo", 0); |
| 88 | assert(env_get("black")); |
| 89 | assert(!strcmp("1", env_get("black"))); |
| 90 | assert(env_get("adder")); |
| 91 | assert(!strcmp("2", env_get("adder"))); |
| 92 | } |
| 93 | |
| 94 | /* Clean up before exit */ |
| 95 | run_command("env default -f -a", 0); |
| 96 | |
Simon Glass | 10c08ef | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 97 | printf("%s: Everything went swimmingly\n", __func__); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | U_BOOT_CMD( |
| 102 | ut_cmd, 5, 1, do_ut_cmd, |
| 103 | "Very basic test of command parsers", |
| 104 | "" |
| 105 | ); |