xref: /openbmc/u-boot/test/command_ut.c (revision 3765b3e7)
1 /*
2  * Copyright (c) 2012, The Chromium Authors
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
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__);
18 	run_command("env default -f", 0);
19 
20 	/* run a single command */
21 	run_command("setenv single 1", 0);
22 	assert(!strcmp("1", getenv("single")));
23 
24 	/* make sure that compound statements work */
25 #ifdef CONFIG_SYS_HUSH_PARSER
26 	run_command("if test -n ${single} ; then setenv check 1; fi", 0);
27 	assert(!strcmp("1", getenv("check")));
28 	run_command("setenv check", 0);
29 #endif
30 
31 	/* commands separated by ; */
32 	run_command_list("setenv list 1; setenv list ${list}1", -1, 0);
33 	assert(!strcmp("11", getenv("list")));
34 
35 	/* commands separated by \n */
36 	run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
37 	assert(!strcmp("11", getenv("list")));
38 
39 	/* command followed by \n and nothing else */
40 	run_command_list("setenv list 1${list}\n", -1, 0);
41 	assert(!strcmp("111", getenv("list")));
42 
43 	/* three commands in a row */
44 	run_command_list("setenv list 1\n setenv list ${list}2; "
45 		"setenv list ${list}3", -1, 0);
46 	assert(!strcmp("123", getenv("list")));
47 
48 	/* a command string with \0 in it. Stuff after \0 should be ignored */
49 	run_command("setenv list", 0);
50 	run_command_list(test_cmd, sizeof(test_cmd), 0);
51 	assert(!strcmp("123", getenv("list")));
52 
53 	/*
54 	 * a command list where we limit execution to only the first command
55 	 * using the length parameter.
56 	 */
57 	run_command_list("setenv list 1\n setenv list ${list}2; "
58 		"setenv list ${list}3", strlen("setenv list 1"), 0);
59 	assert(!strcmp("1", getenv("list")));
60 
61 	printf("%s: Everything went swimmingly\n", __func__);
62 	return 0;
63 }
64 
65 U_BOOT_CMD(
66 	ut_cmd,	5,	1,	do_ut_cmd,
67 	"Very basic test of command parsers",
68 	""
69 );
70