mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2026-05-05 20:56:12 +02:00
It turns out that there is lots of code in the wild, including in the
U-Boot tree itself, which used to rely on
test -n $somevar
to yield false when $somevar is not defined or empty. See for example
all the occurrences of 'test -n $fdtfile'. That was really only a
quirk of the implementation that refused calls with argc < 3, and not
because it was interpreted as
test -n "$somevar"
which is how this should be spelled.
While not exactly conforming to POSIX, we can accomodate such scripts
by special-casing a single argument "-n" to be interpreted as if it
comes from code as above with empty $somevar.
Since we only just added the ability to test a string for emptiness
using the single-argument form, it is very unlikely that there is code
doing
test "$str"
which would now fail if $str happens to be exactly "-n"; such a test
should really always be spelled
test -n "$str"
Fixes: 8b0619579b2 ("cmd: test: fix handling of single-argument form of test")
Reported-by: Franz Schnyder <franz.schnyder@toradex.com>
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>
399 lines
12 KiB
C
399 lines
12 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* (C) Copyright 2021
|
|
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
|
|
*/
|
|
|
|
#include <command.h>
|
|
#include <env.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));
|
|
|
|
sprintf(if_formatted, if_format, "test");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "test ''");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "test 'abc'");
|
|
ut_assertok(run_command(if_formatted, 0));
|
|
|
|
/* Special case: 'test -n' interpreted as 'test -n ""'. */
|
|
sprintf(if_formatted, if_format, "test '-n'");
|
|
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);
|
|
|
|
static int hush_test_lbracket_alias(struct unit_test_state *uts)
|
|
{
|
|
char if_formatted[128];
|
|
const char *missing_rbracket_error = "[: missing terminating ]";
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa = aaa ]");
|
|
ut_assertok(run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa = bbb ]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa = aaa");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa = bbb");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa = aaa]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa = bbb]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa != aaa -o bbb != bbb ]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ aaa != aaa -o bbb = bbb ]");
|
|
ut_assertok(run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ ! aaa != aaa -o ! bbb != bbb ]");
|
|
ut_assertok(run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ ! aaa != aaa -o ! bbb = bbb ]");
|
|
ut_assertok(run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ ]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
sprintf(if_formatted, if_format, "[ '' ]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ ''");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
sprintf(if_formatted, if_format, "[ 'abc' ]");
|
|
ut_assertok(run_command(if_formatted, 0));
|
|
|
|
sprintf(if_formatted, if_format, "[ 'abc'");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
ut_assert_nextline(missing_rbracket_error);
|
|
|
|
/* Special case: '[ -n ]' interpreted as '[ -n "" ]'. */
|
|
sprintf(if_formatted, if_format, "[ -n ]");
|
|
ut_asserteq(1, run_command(if_formatted, 0));
|
|
|
|
return 0;
|
|
}
|
|
HUSH_TEST(hush_test_lbracket_alias, UTF_CONSOLE);
|