aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorSimon Glass2012-03-30 16:30:58 -0500
committerWolfgang Denk2012-08-09 15:06:48 -0500
commita72007d999d3528b6176ffacff1841cb2032a7c1 (patch)
tree16b665f77fc67f2bc2d09158c33b6954617aa071 /test
parent3a8a02beb75d8b3736d0e40ad4d071413869e363 (diff)
downloadu-boot-a72007d999d3528b6176ffacff1841cb2032a7c1.tar.gz
u-boot-a72007d999d3528b6176ffacff1841cb2032a7c1.tar.xz
u-boot-a72007d999d3528b6176ffacff1841cb2032a7c1.zip
sandbox: Add basic test for command execution
Since run_command() and run_command_list() are important and a little confusing, add some basic tests to check that the behaviour is correct. Note: I am not sure that this should be committed, nor where it should go in the source tree. Comments welcome. To run the unit tests use the ut_cmd command available in sandbox: make sandbox_config make ./u-boot -c ut_cmd (To test both hush and built-in parsers, you need to manually change CONFIG_SYS_HUSH_PARSER in include/configs/sandbox.h and build/run again) Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/Makefile45
-rw-r--r--test/command_ut.c85
2 files changed, 130 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000000..83594f3290
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,45 @@
1#
2# (C) Copyright 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
23include $(TOPDIR)/config.mk
24
25LIB = $(obj)libtest.o
26
27COBJS-$(CONFIG_SANDBOX) += command_ut.o
28
29COBJS := $(sort $(COBJS-y))
30SRCS := $(COBJS:.o=.c)
31OBJS := $(addprefix $(obj),$(COBJS))
32
33all: $(LIB) $(XOBJS)
34
35$(LIB): $(obj).depend $(OBJS)
36 $(call cmd_link_o_target, $(OBJS))
37
38#########################################################################
39
40# defines $(obj).depend target
41include $(SRCTREE)/rules.mk
42
43sinclude $(obj).depend
44
45#########################################################################
diff --git a/test/command_ut.c b/test/command_ut.c
new file mode 100644
index 0000000000..dfddc22d99
--- /dev/null
+++ b/test/command_ut.c
@@ -0,0 +1,85 @@
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
27static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
28 "setenv list ${list}3\0"
29 "setenv list ${list}4";
30
31static 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
81U_BOOT_CMD(
82 ut_cmd, 5, 1, do_ut_cmd,
83 "Very basic test of command parsers",
84 ""
85);