]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
cmd: test: fix handling of single-argument form of test
authorRasmus Villemoes <ravi@prevas.dk>
Thu, 12 Mar 2026 10:01:06 +0000 (11:01 +0100)
committerTom Rini <trini@konsulko.com>
Wed, 25 Mar 2026 20:37:55 +0000 (14:37 -0600)
POSIX states that

  0 arguments:
      Exit false (1).
  1 argument:
      Exit true (0) if $1 is not null; otherwise, exit false.

and at least bash and busybox sh behave that way.

The current 'argc < 3' does the right thing for a non-existing or
empty argv[1], but not for a non-empty argv[1]. Fix that and add
corresponding test cases.

Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
Tested-by: Anshul Dalal <anshuld@ti.com>
cmd/test.c
test/hush/if.c

index f0645ef98f12615ffeeb21cfa75cf080b00cde16..0d0f090386cef58989137a9ecdc39422db897b08 100644 (file)
@@ -71,10 +71,17 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
                argc--;
        }
 
-       /* args? */
-       if (argc < 3)
+       /*
+        * Per POSIX, 'test' with 0 arguments should return 1, while
+        * 'test <arg>' should be equivalent to 'test -n <arg>',
+        * i.e. true if and only if <arg> is not empty.
+        */
+       if (argc < 2)
                return 1;
 
+       if (argc == 2)
+               return !strcmp(argv[1], "");
+
 #ifdef DEBUG
        {
                debug("test(%d):", argc);
index 5f75ea68b3216c74813354b8f3eb08ef07ba5e3a..6129e2c530c6fcab34003f780c15a23de3abafc3 100644 (file)
@@ -32,6 +32,15 @@ static int hush_test_if_base(struct unit_test_state *uts)
        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));
+
        return 0;
 }
 HUSH_TEST(hush_test_if_base, 0);
@@ -355,6 +364,27 @@ static int hush_test_lbracket_alias(struct unit_test_state *uts)
        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);
+
        return 0;
 }
 HUSH_TEST(hush_test_lbracket_alias, UTF_CONSOLE);