Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, The Chromium Authors |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #define DEBUG |
| 8 | |
| 9 | #include <common.h> |
| 10 | |
| 11 | static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; " |
| 12 | "setenv list ${list}3\0" |
| 13 | "setenv list ${list}4"; |
| 14 | |
| 15 | static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 16 | { |
| 17 | printf("%s: Testing commands\n", __func__); |
Stephen Warren | 6748672 | 2014-02-03 13:24:23 -0700 | [diff] [blame] | 18 | run_command("env default -f -a", 0); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 19 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 20 | /* commands separated by \n */ |
| 21 | run_command_list("setenv list 1\n setenv list ${list}1", -1, 0); |
| 22 | assert(!strcmp("11", getenv("list"))); |
| 23 | |
| 24 | /* command followed by \n and nothing else */ |
| 25 | run_command_list("setenv list 1${list}\n", -1, 0); |
| 26 | assert(!strcmp("111", getenv("list"))); |
| 27 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 28 | /* a command string with \0 in it. Stuff after \0 should be ignored */ |
| 29 | run_command("setenv list", 0); |
| 30 | run_command_list(test_cmd, sizeof(test_cmd), 0); |
| 31 | assert(!strcmp("123", getenv("list"))); |
| 32 | |
| 33 | /* |
| 34 | * a command list where we limit execution to only the first command |
| 35 | * using the length parameter. |
| 36 | */ |
| 37 | run_command_list("setenv list 1\n setenv list ${list}2; " |
| 38 | "setenv list ${list}3", strlen("setenv list 1"), 0); |
| 39 | assert(!strcmp("1", getenv("list"))); |
| 40 | |
Simon Glass | 93ce756 | 2014-05-30 14:41:48 -0600 | [diff] [blame] | 41 | assert(run_command("false", 0) == 1); |
| 42 | assert(run_command("echo", 0) == 0); |
| 43 | assert(run_command_list("false", -1, 0) == 1); |
| 44 | assert(run_command_list("echo", -1, 0) == 0); |
| 45 | |
Stephen Warren | f2afe70 | 2014-02-03 13:24:24 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_SYS_HUSH_PARSER |
Simon Glass | 87b6398 | 2014-10-07 13:59:43 -0600 | [diff] [blame] | 47 | run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0); |
| 48 | run_command("run foo", 0); |
| 49 | assert(getenv("black") != NULL); |
| 50 | assert(!strcmp("1", getenv("black"))); |
| 51 | assert(getenv("adder") != NULL); |
| 52 | assert(!strcmp("2", getenv("adder"))); |
Stephen Warren | f2afe70 | 2014-02-03 13:24:24 -0700 | [diff] [blame] | 53 | #endif |
| 54 | |
Rabin Vincent | 484408f | 2014-10-29 23:21:39 +0100 | [diff] [blame] | 55 | assert(run_command("", 0) == 0); |
| 56 | assert(run_command(" ", 0) == 0); |
| 57 | |
Rabin Vincent | 2302b3a | 2014-10-29 23:21:41 +0100 | [diff] [blame] | 58 | assert(run_command("'", 0) == 1); |
| 59 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 60 | printf("%s: Everything went swimmingly\n", __func__); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | U_BOOT_CMD( |
| 65 | ut_cmd, 5, 1, do_ut_cmd, |
| 66 | "Very basic test of command parsers", |
| 67 | "" |
| 68 | ); |