]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/glsdk-u-boot.git/blob - test/command_ut.c
sandbox: Add basic test for command execution
[glsdk/glsdk-u-boot.git] / test / command_ut.c
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  */
23 #define DEBUG
25 #include <common.h>
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";
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);
36         /* run a single command */
37         run_command("setenv single 1", 0);
38         assert(!strcmp("1", getenv("single")));
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
47         /* commands separated by ; */
48         run_command_list("setenv list 1; setenv list ${list}1", -1, 0);
49         assert(!strcmp("11", getenv("list")));
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")));
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")));
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")));
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")));
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")));
77         printf("%s: Everything went swimmingly\n", __func__);
78         return 0;
79 }
81 U_BOOT_CMD(
82         ut_cmd, 5,      1,      do_ut_cmd,
83         "Very basic test of command parsers",
84         ""
85 );