[RFC PATCH 0/6] Add C tests for hush

Hi everyone.
First, I hope you are fine and the same for your relatives.
This series introduces C tests for hush. The main goal of these tests is to act as regression tests in case of hush being modified. During their writings some bugs were found, unfortunately I did not write patches to fix them. First, I think these bugs were present in Busybox when hush was taken from. Also, as the whole U-Boot board scripts seem to work with these bugs maybe correcting them can lead to breaking things somewhere... This is why I marked this series as RFC as I would like the hear the opinion of the community on this problem.
Regarding the series, first commit introduces a new subcommand to ut: ut hush. Then, others add tests for the following: * if test condition: All these tests were taken from test_hush_if_test.py which was then removed. * variable expansion: These tests ensures variable expansion is correct. While writing them a bug was found in hush where the following: foo='bar "quux' echo $foo prints: bar quux instead of: bar "quux * commands lists: These tests test several command separated by ';', "&&" and "||". Here also, some bugs were found, indeed the following: false && false || true returns 0 in actual (2021) hush's Busybox, while it returns 1 in U-Boot. * loops: This test is quite simple as we do not have math support in U-Boot, this can be tricky to heavily test while and until loops.
To conclude, here is this series diffstat: Francis Laniel (6): test: Add framework to test hush behavior. test: hush: Test hush if/else. test/py: hush_if_test: Remove the test file. test: hush: Test hush variable expansion. test: hush: Test hush commands list. test: hush: Test hush loops.
include/test/hush.h | 15 ++ include/test/suites.h | 1 + test/Makefile | 3 + test/cmd_ut.c | 6 + test/hush/Makefile | 10 + test/hush/cmd_ut_hush.c | 20 ++ test/hush/dollar.c | 188 ++++++++++++++++++ test/hush/if.c | 308 +++++++++++++++++++++++++++++ test/hush/list.c | 79 ++++++++ test/hush/loop.c | 64 ++++++ test/py/tests/test_hush_if_test.py | 192 ------------------ 11 files changed, 694 insertions(+), 192 deletions(-) create mode 100644 include/test/hush.h create mode 100644 test/hush/Makefile create mode 100644 test/hush/cmd_ut_hush.c create mode 100644 test/hush/dollar.c create mode 100644 test/hush/if.c create mode 100644 test/hush/list.c create mode 100644 test/hush/loop.c delete mode 100644 test/py/tests/test_hush_if_test.py
Best regards. -- 2.25.1

This commit introduces a new subcommand to ut: ut hush. For the moment, this command does nothing, future commits will add tests which will be run on command call.
Note that CONFIG_HUSH_PARSER must be defined to compile this new subcommand.
Signed-off-by: Francis Laniel francis.laniel@amarulasolutions.com --- include/test/hush.h | 15 +++++++++++++++ include/test/suites.h | 1 + test/Makefile | 3 +++ test/cmd_ut.c | 6 ++++++ test/hush/Makefile | 6 ++++++ test/hush/cmd_ut_hush.c | 20 ++++++++++++++++++++ 6 files changed, 51 insertions(+) create mode 100644 include/test/hush.h create mode 100644 test/hush/Makefile create mode 100644 test/hush/cmd_ut_hush.c
diff --git a/include/test/hush.h b/include/test/hush.h new file mode 100644 index 0000000000..cca66544a0 --- /dev/null +++ b/include/test/hush.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#ifndef __TEST_HUSH_H__ +#define __TEST_HUSH_H__ + +#include <test/test.h> + +/* Declare a new environment test */ +#define HUSH_TEST(_name, _flags) UNIT_TEST(_name, _flags, hush_test) + +#endif /* __TEST_HUSH_H__ */ diff --git a/include/test/suites.h b/include/test/suites.h index d35cd83a4e..35a8429e18 100644 --- a/include/test/suites.h +++ b/include/test/suites.h @@ -36,6 +36,7 @@ int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); +int do_ut_hush(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]); int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); diff --git a/test/Makefile b/test/Makefile index b3b2902e2e..c471125982 100644 --- a/test/Makefile +++ b/test/Makefile @@ -16,6 +16,9 @@ obj-$(CONFIG_$(SPL_)CMDLINE) += cmd_ut.o obj-$(CONFIG_$(SPL_)CMDLINE) += command_ut.o obj-$(CONFIG_$(SPL_)UT_COMPRESSION) += compression.o obj-y += dm/ +ifneq ($(CONFIG_HUSH_PARSER),) +obj-$(CONFIG_CMDLINE) += hush/ +endif obj-$(CONFIG_$(SPL_)CMDLINE) += print_ut.o obj-$(CONFIG_$(SPL_)CMDLINE) += str_ut.o obj-$(CONFIG_UT_TIME) += time_ut.o diff --git a/test/cmd_ut.c b/test/cmd_ut.c index 90b260f72d..714b7e7417 100644 --- a/test/cmd_ut.c +++ b/test/cmd_ut.c @@ -70,6 +70,9 @@ static struct cmd_tbl cmd_ut_sub[] = { #ifdef CONFIG_CMD_ADDRMAP U_BOOT_CMD_MKENT(addrmap, CONFIG_SYS_MAXARGS, 1, do_ut_addrmap, "", ""), #endif +#if CONFIG_IS_ENABLED(HUSH_PARSER) + U_BOOT_CMD_MKENT(hush, CONFIG_SYS_MAXARGS, 1, do_ut_hush, "", ""), +#endif };
static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc, @@ -148,6 +151,9 @@ static char ut_help_text[] = #endif #ifdef CONFIG_CMD_ADDRMAP "ut addrmap - Very basic test of addrmap command\n" +#endif +#if CONFIG_IS_ENABLED(HUSH_PARSER) + "ut hush [test-name] - Test hush behavior\n" #endif ; #endif /* CONFIG_SYS_LONGHELP */ diff --git a/test/hush/Makefile b/test/hush/Makefile new file mode 100644 index 0000000000..dfa2a92615 --- /dev/null +++ b/test/hush/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# (C) Copyright 2021 +# Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + +obj-y += cmd_ut_hush.o diff --git a/test/hush/cmd_ut_hush.c b/test/hush/cmd_ut_hush.c new file mode 100644 index 0000000000..48a1adbf28 --- /dev/null +++ b/test/hush/cmd_ut_hush.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include <common.h> +#include <command.h> +#include <test/hush.h> +#include <test/suites.h> +#include <test/ut.h> + +int do_ut_hush(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + struct unit_test *tests = UNIT_TEST_SUITE_START(hush_test); + const int n_ents = UNIT_TEST_SUITE_COUNT(hush_test); + + return cmd_ut_category("hush", "hush_test_", + tests, n_ents, argc, argv); +}

As asked in commit 9c6bf1715f6a ("test/py: hush_if_test: Add tests to cover octal/hex values"), this commit translates test_hush_if_test.py to a C test.
Signed-off-by: Francis Laniel francis.laniel@amarulasolutions.com --- test/hush/Makefile | 1 + test/hush/if.c | 308 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 309 insertions(+) create mode 100644 test/hush/if.c
diff --git a/test/hush/Makefile b/test/hush/Makefile index dfa2a92615..a3c9ae5106 100644 --- a/test/hush/Makefile +++ b/test/hush/Makefile @@ -4,3 +4,4 @@ # Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
obj-y += cmd_ut_hush.o +obj-y += if.o diff --git a/test/hush/if.c b/test/hush/if.c new file mode 100644 index 0000000000..a57d88850f --- /dev/null +++ b/test/hush/if.c @@ -0,0 +1,308 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include <common.h> +#include <command.h> +#include <env_attr.h> +#include <test/hush.h> +#include <test/ut.h> + +/* + * All tests will execute the following: + * if condition_to_test; then + * true + * else + * false + * fi + * If condition is true, command returns 1, 0 otherwise. + */ +const char *if_format = "if %s; then true; else false; fi"; + +static int hush_test_if_base(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "true"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "false"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_base, 0); + +static int hush_test_if_basic_operators(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test aaa = aaa"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa < bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test bbb < aaa"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test bbb > aaa"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa > bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -eq 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -eq 456"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ne 456"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ne 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -lt 456"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -lt 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -lt 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -le 456"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -le 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -le 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -gt 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -gt 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -gt 456"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -ge 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ge 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ge 456"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_basic_operators, 0); + +static int hush_test_if_octal(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test 010 -eq 010"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -eq 011"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -ne 011"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -ne 010"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_octal, 0); + +static int hush_test_if_hexadecimal(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000001"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000000"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x1ffffff"); + ut_assertok(run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_hexadecimal, 0); + +static int hush_test_if_mixed(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test 010 -eq 10"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -ne 10"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0xa -eq 10"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0xa -eq 012"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 2000000 -gt 0x1ffffff"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 1ffffff"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -lt 1ffffff"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -eq 2000000"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -ne 2000000"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -z """); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -z "aaa""); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -n "aaa""); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -n """); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_mixed, 0); + +static int hush_test_if_inverted(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test ! aaa = aaa"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! ! aaa = aaa"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! ! aaa = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_inverted, 0); + +static int hush_test_if_binary(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test aaa != aaa -o bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa -o bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -o bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -o bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa -a bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa -a bbb = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -a bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -a bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_binary, 0); + +static int hush_test_if_inverted_binary(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, + "test ! ! aaa != aaa -o ! ! bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, + "test ! ! aaa != aaa -o ! ! bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, + "test ! ! aaa = aaa -o ! ! bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! ! aaa = aaa -o ! ! bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_inverted_binary, 0); + +static int hush_test_if_z_operator(struct unit_test_state *uts) +{ + char if_formatted[128]; + + /* Deal with environment variable used during test. */ + env_set("ut_var_nonexistent", NULL); + env_set("ut_var_exists", "1"); + + sprintf(if_formatted, if_format, "test -z "$ut_var_nonexistent""); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -z "$ut_var_exists""); + ut_asserteq(1, run_command(if_formatted, 0)); + + /* Clear the set environment variable. */ + env_set("ut_var_exists", NULL); + + return 0; +} +HUSH_TEST(hush_test_if_z_operator, 0);

Commit 9087ab2cc4 ("test/py: hush_if_test: Remove the test file.") translated this test to a C test, so this python file is no more needed.
Signed-off-by: Francis Laniel francis.laniel@amarulasolutions.com --- test/py/tests/test_hush_if_test.py | 192 ----------------------------- 1 file changed, 192 deletions(-) delete mode 100644 test/py/tests/test_hush_if_test.py
diff --git a/test/py/tests/test_hush_if_test.py b/test/py/tests/test_hush_if_test.py deleted file mode 100644 index d117921a6a..0000000000 --- a/test/py/tests/test_hush_if_test.py +++ /dev/null @@ -1,192 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0 -# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. - -# Test operation of the "if" shell command. - -import os -import os.path -import pytest - -# TODO: These tests should be converted to a C test. -# For more information please take a look at the thread -# https://lists.denx.de/pipermail/u-boot/2019-October/388732.html - -pytestmark = pytest.mark.buildconfigspec('hush_parser') - -# The list of "if test" conditions to test. -subtests = ( - # Base if functionality. - - ('true', True), - ('false', False), - - # Basic operators. - - ('test aaa = aaa', True), - ('test aaa = bbb', False), - - ('test aaa != bbb', True), - ('test aaa != aaa', False), - - ('test aaa < bbb', True), - ('test bbb < aaa', False), - - ('test bbb > aaa', True), - ('test aaa > bbb', False), - - ('test 123 -eq 123', True), - ('test 123 -eq 456', False), - - ('test 123 -ne 456', True), - ('test 123 -ne 123', False), - - ('test 123 -lt 456', True), - ('test 123 -lt 123', False), - ('test 456 -lt 123', False), - - ('test 123 -le 456', True), - ('test 123 -le 123', True), - ('test 456 -le 123', False), - - ('test 456 -gt 123', True), - ('test 123 -gt 123', False), - ('test 123 -gt 456', False), - - ('test 456 -ge 123', True), - ('test 123 -ge 123', True), - ('test 123 -ge 456', False), - - # Octal tests - - ('test 010 -eq 010', True), - ('test 010 -eq 011', False), - - ('test 010 -ne 011', True), - ('test 010 -ne 010', False), - - # Hexadecimal tests - - ('test 0x2000000 -gt 0x2000001', False), - ('test 0x2000000 -gt 0x2000000', False), - ('test 0x2000000 -gt 0x1ffffff', True), - - # Mixed tests - - ('test 010 -eq 10', False), - ('test 010 -ne 10', True), - ('test 0xa -eq 10', True), - ('test 0xa -eq 012', True), - - ('test 2000000 -gt 0x1ffffff', False), - ('test 0x2000000 -gt 1ffffff', True), - ('test 0x2000000 -lt 1ffffff', False), - ('test 0x2000000 -eq 2000000', False), - ('test 0x2000000 -ne 2000000', True), - - ('test -z ""', True), - ('test -z "aaa"', False), - - ('test -n "aaa"', True), - ('test -n ""', False), - - # Inversion of simple tests. - - ('test ! aaa = aaa', False), - ('test ! aaa = bbb', True), - ('test ! ! aaa = aaa', True), - ('test ! ! aaa = bbb', False), - - # Binary operators. - - ('test aaa != aaa -o bbb != bbb', False), - ('test aaa != aaa -o bbb = bbb', True), - ('test aaa = aaa -o bbb != bbb', True), - ('test aaa = aaa -o bbb = bbb', True), - - ('test aaa != aaa -a bbb != bbb', False), - ('test aaa != aaa -a bbb = bbb', False), - ('test aaa = aaa -a bbb != bbb', False), - ('test aaa = aaa -a bbb = bbb', True), - - # Inversion within binary operators. - - ('test ! aaa != aaa -o ! bbb != bbb', True), - ('test ! aaa != aaa -o ! bbb = bbb', True), - ('test ! aaa = aaa -o ! bbb != bbb', True), - ('test ! aaa = aaa -o ! bbb = bbb', False), - - ('test ! ! aaa != aaa -o ! ! bbb != bbb', False), - ('test ! ! aaa != aaa -o ! ! bbb = bbb', True), - ('test ! ! aaa = aaa -o ! ! bbb != bbb', True), - ('test ! ! aaa = aaa -o ! ! bbb = bbb', True), - - # -z operator. - - ('test -z "$ut_var_nonexistent"', True), - ('test -z "$ut_var_exists"', False), -) - -def exec_hush_if(u_boot_console, expr, result): - """Execute a shell "if" command, and validate its result.""" - - config = u_boot_console.config.buildconfig - maxargs = int(config.get('config_sys_maxargs', '0')) - args = len(expr.split(' ')) - 1 - if args > maxargs: - u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' + - str(args)) - pytest.skip() - - cmd = 'if ' + expr + '; then echo true; else echo false; fi' - response = u_boot_console.run_command(cmd) - assert response.strip() == str(result).lower() - -def test_hush_if_test_setup(u_boot_console): - """Set up environment variables used during the "if" tests.""" - - u_boot_console.run_command('setenv ut_var_nonexistent') - u_boot_console.run_command('setenv ut_var_exists 1') - -@pytest.mark.buildconfigspec('cmd_echo') -@pytest.mark.parametrize('expr,result', subtests) -def test_hush_if_test(u_boot_console, expr, result): - """Test a single "if test" condition.""" - - exec_hush_if(u_boot_console, expr, result) - -def test_hush_if_test_teardown(u_boot_console): - """Clean up environment variables used during the "if" tests.""" - - u_boot_console.run_command('setenv ut_var_exists') - -# We might test this on real filesystems via UMS, DFU, 'save', etc. -# Of those, only UMS currently allows file removal though. -@pytest.mark.buildconfigspec('cmd_echo') -@pytest.mark.boardspec('sandbox') -def test_hush_if_test_host_file_exists(u_boot_console): - """Test the "if test -e" shell command.""" - - test_file = u_boot_console.config.result_dir + \ - '/creating_this_file_breaks_u_boot_tests' - - try: - os.unlink(test_file) - except: - pass - assert not os.path.exists(test_file) - - expr = 'test -e hostfs - ' + test_file - exec_hush_if(u_boot_console, expr, False) - - try: - with open(test_file, 'wb'): - pass - assert os.path.exists(test_file) - - expr = 'test -e hostfs - ' + test_file - exec_hush_if(u_boot_console, expr, True) - finally: - os.unlink(test_file) - - expr = 'test -e hostfs - ' + test_file - exec_hush_if(u_boot_console, expr, False)

This commit ensures shell variables are replaced by their values.
Signed-off-by: Francis Laniel francis.laniel@amarulasolutions.com --- test/hush/Makefile | 1 + test/hush/dollar.c | 188 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 test/hush/dollar.c
diff --git a/test/hush/Makefile b/test/hush/Makefile index a3c9ae5106..feb4f71956 100644 --- a/test/hush/Makefile +++ b/test/hush/Makefile @@ -5,3 +5,4 @@
obj-y += cmd_ut_hush.o obj-y += if.o +obj-y += dollar.o diff --git a/test/hush/dollar.c b/test/hush/dollar.c new file mode 100644 index 0000000000..4f153958ae --- /dev/null +++ b/test/hush/dollar.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include <common.h> +#include <command.h> +#include <env_attr.h> +#include <test/hush.h> +#include <test/ut.h> + +static int hush_test_simple_dollar(struct unit_test_state *uts) +{ + console_record_reset_enable(); + ut_assertok(run_command("echo $dollar_foo", 0)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-zero-length" + /* + * Next line is empty as $dollar_foo was not set before, but compiler + * complains about the format being empty, so we disable this warning + * only for this line. + */ + ut_assert_nextline(""); +#pragma GCC diagnostic pop + ut_assert_console_end(); + + ut_assertok(run_command("echo ${dollar_foo}", 0)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-zero-length" + /* + * Next line is empty as $dollar_foo was not set before, but compiler + * complains about the format being empty, so we disable this warning + * only for this line. + */ + ut_assert_nextline(""); +#pragma GCC diagnostic pop + ut_assert_console_end(); + + ut_assertok(run_command("dollar_foo=bar", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("echo ${dollar_foo}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + /* + * R is way of giving raw string literals in C++. + * It means a "\n" is a R string literal will not be interpreted as line + * feed but printed as "\n". + * GCC provides it for C as an extension. + */ + ut_assertok(run_command(R"(dollar_foo=$bar)", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("$bar"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_foo='$bar'", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("$bar"); + ut_assert_console_end(); + + ut_asserteq(1, run_command("dollar_foo=bar quux", 0)); + /* Next line contains error message. */ + ut_assert_skipline(); + ut_assert_console_end(); + + ut_asserteq(1, run_command("dollar_foo='bar quux", 0)); + /* Next line contains error message. */ + ut_assert_skipline(); + ut_assert_console_end(); + + ut_asserteq(1, run_command(R"(dollar_foo=bar quux")", 0)); + /* Two next lines contain error message. */ + ut_assert_skipline(); + ut_assert_skipline(); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_foo='bar "quux')", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + /* + * This one is buggy. + * ut_assert_nextline(R"(bar "quux)"); + * ut_assert_console_end(); + */ + + ut_asserteq(1, run_command(R"(dollar_foo="bar 'quux")", 0)); + /* Next line contains error message. */ + ut_assert_skipline(); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_foo='bar quux'", 0)); + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("bar quux"); + ut_assert_console_end(); + + puts("Beware: this test set local variable dollar_foo and it cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_simple_dollar, 0); + +static int hush_test_env_dollar(struct unit_test_state *uts) +{ + env_set("env_foo", "bar"); + console_record_reset_enable(); + + ut_assertok(run_command("echo $env_foo", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("echo ${env_foo}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + /* Environment variables have precedence over local variable. */ + ut_assertok(run_command("env_foo=quux", 0)); + ut_assertok(run_command("echo ${env_foo}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + /* Clean up setting the variable. */ + env_set("env_foo", NULL); + + puts("Beware: this test set local variable env_foo and it cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_env_dollar, 0); + +static int hush_test_command_dollar(struct unit_test_state *uts) +{ + console_record_reset_enable(); + + ut_assertok(run_command(R"(dollar_bar="echo bar")", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("${dollar_bar}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_bar="echo + bar")", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_bar='echo bar + ')", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_bar='echo bar\n')", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("barn"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_bar='echo $bar'", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("$bar"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_quux=quux", 0)); + ut_assertok(run_command(R"(dollar_bar="echo $dollar_quux")", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("quux"); + ut_assert_console_end(); + + puts("Beware: this test sets local variable dollar_bar and dollar_quux and they cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_command_dollar, 0);

This commit ensures behavior of commands separated by ';', '&&' and '||'.
Signed-off-by: Francis Laniel francis.laniel@amarulasolutions.com --- test/hush/Makefile | 1 + test/hush/list.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 test/hush/list.c
diff --git a/test/hush/Makefile b/test/hush/Makefile index feb4f71956..ff4fe7700b 100644 --- a/test/hush/Makefile +++ b/test/hush/Makefile @@ -6,3 +6,4 @@ obj-y += cmd_ut_hush.o obj-y += if.o obj-y += dollar.o +obj-y += list.o diff --git a/test/hush/list.c b/test/hush/list.c new file mode 100644 index 0000000000..052cf2783c --- /dev/null +++ b/test/hush/list.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include <common.h> +#include <command.h> +#include <env_attr.h> +#include <test/hush.h> +#include <test/ut.h> + +static int hush_test_semicolon(struct unit_test_state *uts) +{ + /* A; B = B truth table. */ + ut_asserteq(1, run_command("false; false", 0)); + ut_assertok(run_command("false; true", 0)); + ut_assertok(run_command("true; true", 0)); + ut_asserteq(1, run_command("true; false", 0)); + + return 0; +} +HUSH_TEST(hush_test_semicolon, 0); + +static int hush_test_and(struct unit_test_state *uts) +{ + /* A && B truth table. */ + ut_asserteq(1, run_command("false && false", 0)); + ut_asserteq(1, run_command("false && true", 0)); + ut_assertok(run_command("true && true", 0)); + ut_asserteq(1, run_command("true && false", 0)); + + return 0; +} +HUSH_TEST(hush_test_and, 0); + +static int hush_test_or(struct unit_test_state *uts) +{ + /* A || B truth table. */ + ut_asserteq(1, run_command("false || false", 0)); + ut_assertok(run_command("false || true", 0)); + ut_assertok(run_command("true || true", 0)); + ut_assertok(run_command("true || false", 0)); + + return 0; +} +HUSH_TEST(hush_test_or, 0); + +static int hush_test_and_or(struct unit_test_state *uts) +{ + /* A && B || C truth table. */ + ut_asserteq(1, run_command("false && false || false", 0)); + ut_asserteq(1, run_command("false && false || true", 0)); + ut_asserteq(1, run_command("false && true || true", 0)); + ut_asserteq(1, run_command("false && true || false", 0)); + ut_assertok(run_command("true && true || false", 0)); + ut_asserteq(1, run_command("true && false || false", 0)); + ut_assertok(run_command("true && false || true", 0)); + ut_assertok(run_command("true && true || true", 0)); + + return 0; +} +HUSH_TEST(hush_test_and_or, 0); + +static int hush_test_or_and(struct unit_test_state *uts) +{ + /* A || B && C truth table. */ + ut_asserteq(1, run_command("false || false && false", 0)); + ut_asserteq(1, run_command("false || false && true", 0)); + ut_assertok(run_command("false || true && true", 0)); + ut_asserteq(1, run_command("false || true && false", 0)); + ut_assertok(run_command("true || true && false", 0)); + ut_assertok(run_command("true || false && false", 0)); + ut_assertok(run_command("true || false && true", 0)); + ut_assertok(run_command("true || true && true", 0)); + + return 0; +} +HUSH_TEST(hush_test_or_and, 0);

The added tests ensure correct behavior of for, while and until loops.
Signed-off-by: Francis Laniel francis.laniel@amarulasolutions.com --- test/hush/Makefile | 1 + test/hush/loop.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/hush/loop.c
diff --git a/test/hush/Makefile b/test/hush/Makefile index ff4fe7700b..a2d98815e5 100644 --- a/test/hush/Makefile +++ b/test/hush/Makefile @@ -7,3 +7,4 @@ obj-y += cmd_ut_hush.o obj-y += if.o obj-y += dollar.o obj-y += list.o +obj-y += loop.o diff --git a/test/hush/loop.c b/test/hush/loop.c new file mode 100644 index 0000000000..519c78ef7e --- /dev/null +++ b/test/hush/loop.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include <common.h> +#include <command.h> +#include <env_attr.h> +#include <test/hush.h> +#include <test/ut.h> + +static int hush_test_for(struct unit_test_state *uts) +{ + console_record_reset_enable(); + + ut_assertok(run_command("for loop_i in foo bar quux; do echo $loop_i; done", 0)); + ut_assert_nextline("foo"); + ut_assert_nextline("bar"); + ut_assert_nextline("quux"); + ut_assert_console_end(); + + puts("Beware: this test set local variable loop_i and it cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_for, 0); + +static int hush_test_while(struct unit_test_state *uts) +{ + console_record_reset_enable(); + + /* Exit status is that of test, so 1 since test is false to quit the loop. */ + ut_asserteq(1, run_command("while test -z "$loop_foo"; do echo bar; loop_foo=quux; done", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + puts("Beware: this test set local variable loop_foo and it cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_while, 0); + +static int hush_test_until(struct unit_test_state *uts) +{ + console_record_reset_enable(); + env_set("loop_bar", "bar"); + + /* + * WARNING We have to use environment variable because it is not possible + * resetting local variable. + */ + ut_assertok(run_command("until test -z "$loop_bar"; do echo quux; setenv loop_bar; done", 0)); + ut_assert_nextline("quux"); + ut_assert_console_end(); + + /* + * Loop normally resets foo environment variable, but we reset it here in + * case the test failed. + */ + env_set("loop_bar", NULL); + return 0; +} +HUSH_TEST(hush_test_until, 0);
participants (1)
-
Francis Laniel