xref: /openbmc/u-boot/test/command_ut.c (revision 849fc424)
1 /*
2  * Copyright (c) 2012, The Chromium Authors
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 
23 #define DEBUG
24 
25 #include <common.h>
26 
27 static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
28 		"setenv list ${list}3\0"
29 		"setenv list ${list}4";
30 
31 static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
32 {
33 	printf("%s: Testing commands\n", __func__);
34 	run_command("env default -f", 0);
35 
36 	/* run a single command */
37 	run_command("setenv single 1", 0);
38 	assert(!strcmp("1", getenv("single")));
39 
40 	/* make sure that compound statements work */
41 #ifdef CONFIG_SYS_HUSH_PARSER
42 	run_command("if test -n ${single} ; then setenv check 1; fi", 0);
43 	assert(!strcmp("1", getenv("check")));
44 	run_command("setenv check", 0);
45 #endif
46 
47 	/* commands separated by ; */
48 	run_command_list("setenv list 1; setenv list ${list}1", -1, 0);
49 	assert(!strcmp("11", getenv("list")));
50 
51 	/* commands separated by \n */
52 	run_command_list("setenv list 1\n setenv list ${list}1", -1, 0);
53 	assert(!strcmp("11", getenv("list")));
54 
55 	/* command followed by \n and nothing else */
56 	run_command_list("setenv list 1${list}\n", -1, 0);
57 	assert(!strcmp("111", getenv("list")));
58 
59 	/* three commands in a row */
60 	run_command_list("setenv list 1\n setenv list ${list}2; "
61 		"setenv list ${list}3", -1, 0);
62 	assert(!strcmp("123", getenv("list")));
63 
64 	/* a command string with \0 in it. Stuff after \0 should be ignored */
65 	run_command("setenv list", 0);
66 	run_command_list(test_cmd, sizeof(test_cmd), 0);
67 	assert(!strcmp("123", getenv("list")));
68 
69 	/*
70 	 * a command list where we limit execution to only the first command
71 	 * using the length parameter.
72 	 */
73 	run_command_list("setenv list 1\n setenv list ${list}2; "
74 		"setenv list ${list}3", strlen("setenv list 1"), 0);
75 	assert(!strcmp("1", getenv("list")));
76 
77 	printf("%s: Everything went swimmingly\n", __func__);
78 	return 0;
79 }
80 
81 U_BOOT_CMD(
82 	ut_cmd,	5,	1,	do_ut_cmd,
83 	"Very basic test of command parsers",
84 	""
85 );
86