Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, The Chromium Authors |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define DEBUG |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 288b29e | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 12 | |
| 13 | static 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 17 | static int do_ut_cmd(struct cmd_tbl *cmdtp, int flag, int argc, |
| 18 | char *const argv[]) |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 19 | { |
| 20 | printf("%s: Testing commands\n", __func__); |
Stephen Warren | 6748672 | 2014-02-03 13:24:23 -0700 | [diff] [blame] | 21 | run_command("env default -f -a", 0); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 22 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 23 | /* commands separated by \n */ |
| 24 | run_command_list("setenv list 1\n setenv list ${list}1", -1, 0); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 25 | assert(!strcmp("11", env_get("list"))); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 26 | |
| 27 | /* command followed by \n and nothing else */ |
| 28 | run_command_list("setenv list 1${list}\n", -1, 0); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 29 | assert(!strcmp("111", env_get("list"))); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 30 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 31 | /* 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 Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 34 | assert(!strcmp("123", env_get("list"))); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 35 | |
| 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 Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 42 | assert(!strcmp("1", env_get("list"))); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 43 | |
Simon Glass | 93ce756 | 2014-05-30 14:41:48 -0600 | [diff] [blame] | 44 | 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 Yamada | f1f9d4f | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 49 | #ifdef CONFIG_HUSH_PARSER |
Simon Glass | 87b6398 | 2014-10-07 13:59:43 -0600 | [diff] [blame] | 50 | run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0); |
| 51 | run_command("run foo", 0); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 52 | 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 Warren | f2afe70 | 2014-02-03 13:24:24 -0700 | [diff] [blame] | 56 | #endif |
| 57 | |
Rabin Vincent | 484408f | 2014-10-29 23:21:39 +0100 | [diff] [blame] | 58 | assert(run_command("", 0) == 0); |
| 59 | assert(run_command(" ", 0) == 0); |
| 60 | |
Rabin Vincent | 2302b3a | 2014-10-29 23:21:41 +0100 | [diff] [blame] | 61 | assert(run_command("'", 0) == 1); |
| 62 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 63 | printf("%s: Everything went swimmingly\n", __func__); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | U_BOOT_CMD( |
| 68 | ut_cmd, 5, 1, do_ut_cmd, |
| 69 | "Very basic test of command parsers", |
| 70 | "" |
| 71 | ); |