u-boot/test/hush/if.c
Tom Rini 2b28c3b871 Merge patch series "Modernize U-Boot shell"
Francis Laniel <francis.laniel@amarulasolutions.com> says:

During 2021 summer, Sean Anderson wrote a contribution to add a new shell, based
on LIL, to U-Boot [1, 2].
While one of the goals of this contribution was to address the fact actual
U-Boot shell, which is based on Busybox hush, is old there was a discussion
about adding a new shell versus updating the actual one [3, 4].

So, in this series, with Harald Seiler, we updated the actual U-Boot shell to
reflect what is currently in Busybox source code.
Basically, this contribution is about taking a snapshot of Busybox shell/hush.c
file (as it exists in commit 37460f5da) and adapt it to suit U-Boot needs.

This contribution was written to be as backward-compatible as possible to avoid
breaking the existing.
So, the modern hush flavor offers the same as the actual, that is to say:
1. Variable expansion.
2. Instruction lists (;, && and ||).
3. If, then and else.
4. Loops (for, while and until).
No new features offered by Busybox hush were implemented (e.g. functions).

It is possible to change the parser at runtime using the "cli" command:
=> cli print
old
=> cli set modern
=> cli print
modern
=> cli set old
The default parser is the old one.
Note that to use both parser, you would need to set both
CONFIG_HUSH_MODERN_PARSER and CONFIG_HUSH_OLD_PARSER.

In terms of testing, new unit tests were added to ut to ensure the new behavior
is the same as the old one and it does not add regression.
Nonetheless, if old behavior was buggy and fixed upstream, the fix is then added
to U-Boot [5].
In sandbox, all of these tests pass smoothly:
=> printenv board
board=sandbox
=> ut hush
Running 20 hush tests
...
Failures: 0
=> cli set modern
=> ut hush
Running 20 hush tests
...
Failures: 0

Thanks to the effort of Harald Seiler, I was successful booting a board:
=> printenv fdtfile
fdtfile=amlogic/meson-gxl-s905x-libretech-cc.dtb
=> cli get
old
=> boot
...
root@lepotato:~#
root@lepotato:~# reboot
...
=> cli set modern
=> cli get
modern
=> printenv fdtfile
fdtfile=amlogic/meson-gxl-s905x-libretech-cc.dtb
=> boot
...
root@lepotato:~#

This contribution indeed adds a lot of code and there were concern about its
size [6, 7].
With regard to the amount of code added, the cli_hush_upstream.c is 13030 lines
long but it seems a smaller subset is really used:
gcc -D__U_BOOT__ -E common/cli_hush_upstream.c | wc -l
2870
Despite this, it is better to still have the whole upstream code for the sake of
easing maintenance.
With regard to memory size, I conducted some experiments for version 8 of this
series and for a subset of arm64 boards and found the worst case to be 4K [8].
Tom Rini conducted more research on this and also found the increase to be
acceptable [9].

If you want to review it - your review will really be appreciated - here are
some information regarding the commits:
* commits marked as "test:" deal with unit tests.
* commit "cli: Add Busybox upstream hush.c file." copies Busybox shell/hush.c
into U-Boot tree, this explain why this commit contains around 12000 additions.
* commit "cli: Port Busybox 2021 hush to U-Boot." modifies previously added file
to permit us to use this as new shell.
The really good idea of #include'ing Busybox code into a wrapper file to define
some particular functions while minimizing modifications to upstream code comes
from Harald Seiler.
* commit "cmd: Add new parser command" adds a new command which permits
selecting parser at runtime.
I am not really satisfied with the fact it calls cli_init() and cli_loop() each
time the parser is set, so your reviews would be welcomed.
* Other commits focus on enabling features we need (e.g. if).
2023-12-28 14:38:25 -05:00

316 lines
9.2 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#include <command.h>
#include <env_attr.h>
#include <vsprintf.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");
env_set("ut_var_unset", "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));
sprintf(if_formatted, if_format, "test -z \"$ut_var_unset\"");
ut_asserteq(1, run_command(if_formatted, 0));
env_set("ut_var_unset", NULL);
sprintf(if_formatted, if_format, "test -z \"$ut_var_unset\"");
ut_assertok(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);