diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 3 | ||||
-rw-r--r-- | test/bloblist.c | 105 | ||||
-rw-r--r-- | test/boot/bootflow.c | 16 | ||||
-rw-r--r-- | test/cmd_ut.c | 6 | ||||
-rw-r--r-- | test/hush/Makefile | 10 | ||||
-rw-r--r-- | test/hush/cmd_ut_hush.c | 19 | ||||
-rw-r--r-- | test/hush/dollar.c | 225 | ||||
-rw-r--r-- | test/hush/if.c | 316 | ||||
-rw-r--r-- | test/hush/list.c | 139 | ||||
-rw-r--r-- | test/hush/loop.c | 90 | ||||
-rw-r--r-- | test/py/tests/test_hush_if_test.py | 197 | ||||
-rw-r--r-- | test/py/tests/test_ut.py | 8 |
12 files changed, 888 insertions, 246 deletions
diff --git a/test/Makefile b/test/Makefile index 6b8a1506f54..9aeef02f9ee 100644 --- a/test/Makefile +++ b/test/Makefile @@ -17,6 +17,9 @@ obj-$(CONFIG_FUZZ) += fuzz/ ifndef CONFIG_SANDBOX_VPL obj-$(CONFIG_UNIT_TEST) += lib/ endif +ifneq ($(CONFIG_HUSH_PARSER),) +obj-$(CONFIG_$(SPL_)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/bloblist.c b/test/bloblist.c index 720be7e244f..17d9dd03d07 100644 --- a/test/bloblist.c +++ b/test/bloblist.c @@ -72,15 +72,15 @@ static int bloblist_test_init(struct unit_test_state *uts) hdr = clear_bloblist(); ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR)); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); ut_asserteq_ptr(hdr, bloblist_check_magic(TEST_ADDR)); hdr->version++; ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); - ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0x10, 0)); - ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0)); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0xc, 0, 0)); + ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); ut_assertok(bloblist_finish()); @@ -106,8 +106,9 @@ static int bloblist_test_blob(struct unit_test_state *uts) /* At the start there should be no records */ hdr = clear_bloblist(); ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE)); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); - ut_asserteq(TEST_BLOBLIST_SIZE, bloblist_get_size()); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); + ut_asserteq(sizeof(struct bloblist_hdr), bloblist_get_size()); + ut_asserteq(TEST_BLOBLIST_SIZE, bloblist_get_total_size()); ut_asserteq(TEST_ADDR, bloblist_get_base()); ut_asserteq(map_to_sysmem(hdr), TEST_ADDR); @@ -144,7 +145,7 @@ static int bloblist_test_blob_ensure(struct unit_test_state *uts) /* At the start there should be no records */ clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); /* Test with an empty bloblist */ size = TEST_SIZE; @@ -176,7 +177,7 @@ static int bloblist_test_bad_blob(struct unit_test_state *uts) void *data; hdr = clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); data = hdr + 1; data += sizeof(struct bloblist_rec); ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE)); @@ -192,7 +193,7 @@ static int bloblist_test_checksum(struct unit_test_state *uts) char *data, *data2; hdr = clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); ut_assertok(bloblist_finish()); ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); @@ -205,9 +206,9 @@ static int bloblist_test_checksum(struct unit_test_state *uts) ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); hdr->flags++; - hdr->size--; + hdr->total_size--; ut_asserteq(-EFBIG, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); - hdr->size++; + hdr->total_size++; hdr->spare++; ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); @@ -217,6 +218,10 @@ static int bloblist_test_checksum(struct unit_test_state *uts) ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); hdr->chksum--; + hdr->align_log2++; + ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); + hdr->align_log2--; + /* Make sure the checksum changes when we add blobs */ data = bloblist_add(TEST_TAG, TEST_SIZE, 0); ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); @@ -237,12 +242,18 @@ static int bloblist_test_checksum(struct unit_test_state *uts) *data2 -= 1; /* - * Changing data outside the range of valid data should not affect - * the checksum. + * Changing data outside the range of valid data should affect the + * checksum. */ ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); data[TEST_SIZE]++; + ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); + data[TEST_SIZE]--; + ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); + data2[TEST_SIZE2]++; + ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); + data[TEST_SIZE]--; ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); return 0; @@ -256,7 +267,7 @@ static int bloblist_test_cmd_info(struct unit_test_state *uts) char *data, *data2; hdr = clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); data = bloblist_ensure(TEST_TAG, TEST_SIZE); data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2); @@ -264,10 +275,10 @@ static int bloblist_test_cmd_info(struct unit_test_state *uts) ut_silence_console(uts); console_record_reset(); run_command("bloblist info", 0); - ut_assert_nextline("base: %lx", (ulong)map_to_sysmem(hdr)); - ut_assert_nextline("size: 400 1 KiB"); - ut_assert_nextline("alloced: 70 112 Bytes"); - ut_assert_nextline("free: 390 912 Bytes"); + ut_assert_nextline("base: %lx", (ulong)map_to_sysmem(hdr)); + ut_assert_nextline("total size: 400 1 KiB"); + ut_assert_nextline("used size: 50 80 Bytes"); + ut_assert_nextline("free: 3b0 944 Bytes"); ut_assert_console_end(); ut_unsilence_console(uts); @@ -282,7 +293,7 @@ static int bloblist_test_cmd_list(struct unit_test_state *uts) char *data, *data2; hdr = clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); data = bloblist_ensure(TEST_TAG, TEST_SIZE); data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2); @@ -291,9 +302,9 @@ static int bloblist_test_cmd_list(struct unit_test_state *uts) console_record_reset(); run_command("bloblist list", 0); ut_assert_nextline("Address Size Tag Name"); - ut_assert_nextline("%08lx %8x 8000 SPL hand-off", + ut_assert_nextline("%08lx %8x fff000 SPL hand-off", (ulong)map_to_sysmem(data), TEST_SIZE); - ut_assert_nextline("%08lx %8x 106 Chrome OS vboot context", + ut_assert_nextline("%08lx %8x 202 Chrome OS vboot context", (ulong)map_to_sysmem(data2), TEST_SIZE2); ut_assert_console_end(); ut_unsilence_console(uts); @@ -312,7 +323,7 @@ static int bloblist_test_align(struct unit_test_state *uts) /* At the start there should be no records */ hdr = clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE)); /* Check the default alignment */ @@ -325,18 +336,18 @@ static int bloblist_test_align(struct unit_test_state *uts) data = bloblist_add(i, size, 0); ut_assertnonnull(data); addr = map_to_sysmem(data); - ut_asserteq(0, addr & (BLOBLIST_ALIGN - 1)); + ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN - 1)); /* Only the bytes in the blob data should be zeroed */ for (j = 0; j < size; j++) ut_asserteq(0, data[j]); - for (; j < BLOBLIST_ALIGN; j++) + for (; j < BLOBLIST_BLOB_ALIGN; j++) ut_asserteq(ERASE_BYTE, data[j]); } /* Check larger alignment */ for (i = 0; i < 3; i++) { - int align = 32 << i; + int align = 5 - i; data = bloblist_add(3 + i, i * 4, align); ut_assertnonnull(data); @@ -345,16 +356,16 @@ static int bloblist_test_align(struct unit_test_state *uts) } /* Check alignment with an bloblist starting on a smaller alignment */ - hdr = map_sysmem(TEST_ADDR + BLOBLIST_ALIGN, TEST_BLOBLIST_SIZE); + hdr = map_sysmem(TEST_ADDR + BLOBLIST_BLOB_ALIGN, TEST_BLOBLIST_SIZE); memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE); memset(hdr, '\0', sizeof(*hdr)); ut_assertok(bloblist_new(TEST_ADDR + BLOBLIST_ALIGN, TEST_BLOBLIST_SIZE, - 0)); + 0, 0)); - data = bloblist_add(1, 5, BLOBLIST_ALIGN * 2); + data = bloblist_add(1, 5, BLOBLIST_ALIGN_LOG2 + 1); ut_assertnonnull(data); addr = map_to_sysmem(data); - ut_asserteq(0, addr & (BLOBLIST_ALIGN * 2 - 1)); + ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN * 2 - 1)); return 0; } @@ -370,7 +381,7 @@ static int bloblist_test_reloc(struct unit_test_state *uts) ulong new_addr; ulong new_size; - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); old_ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE); /* Add one blob and then one that won't fit */ @@ -409,7 +420,7 @@ static int bloblist_test_grow(struct unit_test_state *uts) memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE); /* Create two blobs */ - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); blob1 = bloblist_add(TEST_TAG, small_size, 0); ut_assertnonnull(blob1); ut_assertok(check_zero(blob1, small_size)); @@ -421,7 +432,7 @@ static int bloblist_test_grow(struct unit_test_state *uts) ut_asserteq(sizeof(struct bloblist_hdr) + sizeof(struct bloblist_rec) * 2 + small_size * 2, - hdr->alloced); + hdr->used_size); /* Resize the first one */ ut_assertok(bloblist_resize(TEST_TAG, small_size + 4)); @@ -442,8 +453,8 @@ static int bloblist_test_grow(struct unit_test_state *uts) hdr = ptr; ut_asserteq(sizeof(struct bloblist_hdr) + sizeof(struct bloblist_rec) * 2 + small_size * 2 + - BLOBLIST_ALIGN, - hdr->alloced); + BLOBLIST_BLOB_ALIGN, + hdr->used_size); return 0; } @@ -461,7 +472,7 @@ static int bloblist_test_shrink(struct unit_test_state *uts) ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE); /* Create two blobs */ - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); blob1 = bloblist_add(TEST_TAG, small_size, 0); ut_assertnonnull(blob1); strcpy(blob1, test1_str); @@ -473,7 +484,7 @@ static int bloblist_test_shrink(struct unit_test_state *uts) hdr = ptr; ut_asserteq(sizeof(struct bloblist_hdr) + sizeof(struct bloblist_rec) * 2 + small_size * 2, - hdr->alloced); + hdr->used_size); /* Resize the first one */ new_size = small_size - BLOBLIST_ALIGN - 4; @@ -493,7 +504,7 @@ static int bloblist_test_shrink(struct unit_test_state *uts) ut_asserteq(sizeof(struct bloblist_hdr) + sizeof(struct bloblist_rec) * 2 + small_size * 2 - BLOBLIST_ALIGN, - hdr->alloced); + hdr->used_size); return 0; } @@ -511,7 +522,7 @@ static int bloblist_test_resize_fail(struct unit_test_state *uts) ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE); /* Create two blobs */ - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); blob1 = bloblist_add(TEST_TAG, small_size, 0); ut_assertnonnull(blob1); @@ -521,12 +532,12 @@ static int bloblist_test_resize_fail(struct unit_test_state *uts) hdr = ptr; ut_asserteq(sizeof(struct bloblist_hdr) + sizeof(struct bloblist_rec) * 2 + small_size * 2, - hdr->alloced); + hdr->used_size); /* Resize the first one, to check the boundary conditions */ ut_asserteq(-EINVAL, bloblist_resize(TEST_TAG, -1)); - new_size = small_size + (hdr->size - hdr->alloced); + new_size = small_size + (hdr->total_size - hdr->used_size); ut_asserteq(-ENOSPC, bloblist_resize(TEST_TAG, new_size + 1)); ut_assertok(bloblist_resize(TEST_TAG, new_size)); @@ -548,7 +559,7 @@ static int bloblist_test_resize_last(struct unit_test_state *uts) hdr = ptr; /* Create two blobs */ - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); blob1 = bloblist_add(TEST_TAG, small_size, 0); ut_assertnonnull(blob1); @@ -558,9 +569,9 @@ static int bloblist_test_resize_last(struct unit_test_state *uts) /* Check the byte after the last blob */ alloced_val = sizeof(struct bloblist_hdr) + sizeof(struct bloblist_rec) * 2 + small_size * 2; - ut_asserteq(alloced_val, hdr->alloced); + ut_asserteq(alloced_val, hdr->used_size); ut_asserteq_ptr((void *)hdr + alloced_val, blob2 + small_size); - ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->alloced)); + ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size)); /* Resize the second one, checking nothing changes */ ut_asserteq(0, bloblist_resize(TEST_TAG2, small_size + 4)); @@ -577,9 +588,9 @@ static int bloblist_test_resize_last(struct unit_test_state *uts) ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + alloced_val + 4)); /* Check that the new top of the allocated blobs has not been touched */ - alloced_val += BLOBLIST_ALIGN; - ut_asserteq(alloced_val, hdr->alloced); - ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->alloced)); + alloced_val += BLOBLIST_BLOB_ALIGN; + ut_asserteq(alloced_val, hdr->used_size); + ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size)); return 0; } @@ -593,7 +604,7 @@ static int bloblist_test_blob_maxsize(struct unit_test_state *uts) /* At the start there should be no records */ clear_bloblist(); - ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0)); /* Add a blob that takes up all space */ size = TEST_BLOBLIST_SIZE - sizeof(struct bloblist_hdr) - diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index a9b555c7794..104f49deef2 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -710,7 +710,21 @@ static int bootflow_scan_menu_boot(struct unit_test_state *uts) ut_assert_skip_to_line("(2 bootflows, 2 valid)"); ut_assert_nextline("Selected: Armbian"); - ut_assert_skip_to_line("Boot failed (err=-14)"); + + if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + /* + * With old hush, despite booti failing to boot, i.e. returning + * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as + * we are using bootmeth_script here, to return -EFAULT. + */ + ut_assert_skip_to_line("Boot failed (err=-14)"); + } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * While with modern one, run_command() propagates CMD_RET_FAILURE returned + * by booti, so we get 1 here. + */ + ut_assert_skip_to_line("Boot failed (err=1)"); + } ut_assertnonnull(std->cur_bootflow); ut_assert_console_end(); diff --git a/test/cmd_ut.c b/test/cmd_ut.c index 1b934b23295..0677ce0cd17 100644 --- a/test/cmd_ut.c +++ b/test/cmd_ut.c @@ -121,6 +121,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 #ifdef CONFIG_CMD_LOADM U_BOOT_CMD_MKENT(loadm, CONFIG_SYS_MAXARGS, 1, do_ut_loadm, "", ""), #endif @@ -216,6 +219,9 @@ U_BOOT_LONGHELP(ut, #ifdef CONFIG_CONSOLE_TRUETYPE "\nfont - font command" #endif +#if CONFIG_IS_ENABLED(HUSH_PARSER) + "\nhush - Test hush behavior" +#endif #ifdef CONFIG_CMD_LOADM "\nloadm - loadm command parameters and loading memory blob" #endif diff --git a/test/hush/Makefile b/test/hush/Makefile new file mode 100644 index 00000000000..a2d98815e50 --- /dev/null +++ b/test/hush/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# (C) Copyright 2021 +# Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + +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/cmd_ut_hush.c b/test/hush/cmd_ut_hush.c new file mode 100644 index 00000000000..abad44f3216 --- /dev/null +++ b/test/hush/cmd_ut_hush.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#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); +} diff --git a/test/hush/dollar.c b/test/hush/dollar.c new file mode 100644 index 00000000000..4caa07c192a --- /dev/null +++ b/test/hush/dollar.c @@ -0,0 +1,225 @@ +// 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 <test/hush.h> +#include <test/ut.h> +#include <asm/global_data.h> + +DECLARE_GLOBAL_DATA_PTR; + +static int hush_test_simple_dollar(struct unit_test_state *uts) +{ + console_record_reset_enable(); + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline_empty(); + ut_assert_console_end(); + + ut_assertok(run_command("echo ${dollar_foo}", 0)); + ut_assert_nextline_empty(); + 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(); + + 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("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(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * For some strange reasons, the console is not empty after + * running above command. + * So, we reset it to not have side effects for other tests. + */ + console_record_reset_enable(); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_assert_console_end(); + } + + ut_asserteq(1, run_command("dollar_foo=bar quux\"", 0)); + /* Two next lines contain error message */ + ut_assert_skipline(); + ut_assert_skipline(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* See above comments. */ + console_record_reset_enable(); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_assert_console_end(); + } + + ut_assertok(run_command("dollar_foo='bar \"quux'", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + /* + * This one is buggy. + * ut_assert_nextline("bar \"quux"); + * ut_assert_console_end(); + * + * So, let's reset output: + */ + console_record_reset_enable(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * Old parser returns an error because it waits for closing + * '\'', but this behavior is wrong as the '\'' is surrounded by + * '"', so no need to wait for a closing one. + */ + 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(); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_asserteq(1, run_command("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(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* Reset local variable. */ + ut_assertok(run_command("dollar_foo=", 0)); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + 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 priority 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); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* Reset local variable. */ + ut_assertok(run_command("env_foo=", 0)); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + 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("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("dollar_bar=\"echo\nbar\"", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_bar='echo bar\n'", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_bar='echo bar\\n'", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * This difference seems to come from a bug solved in Busybox + * hush. + * Behavior of hush 2021 is coherent with bash and other shells. + */ + ut_assert_nextline("bar\\n"); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + 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("dollar_bar=\"echo $dollar_quux\"", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("quux"); + ut_assert_console_end(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* Reset local variables. */ + ut_assertok(run_command("dollar_bar=", 0)); + ut_assertok(run_command("dollar_quux=", 0)); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + 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); diff --git a/test/hush/if.c b/test/hush/if.c new file mode 100644 index 00000000000..8939b7a6c86 --- /dev/null +++ b/test/hush/if.c @@ -0,0 +1,316 @@ +// 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); diff --git a/test/hush/list.c b/test/hush/list.c new file mode 100644 index 00000000000..210823db2f5 --- /dev/null +++ b/test/hush/list.c @@ -0,0 +1,139 @@ +// 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 <test/hush.h> +#include <test/ut.h> +#include <asm/global_data.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); + +DECLARE_GLOBAL_DATA_PTR; + +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)); + + if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_asserteq(1, run_command("false && false || true", 0)); + } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * This difference seems to come from a bug solved in Busybox + * hush. + * + * Indeed, the following expression can be seen like this: + * (false && false) || true + * So, (false && false) returns 1, the second false is not + * executed, and true is executed because of ||. + */ + ut_assertok(run_command("false && false || true", 0)); + } + + if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_asserteq(1, run_command("false && true || true", 0)); + } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * This difference seems to come from a bug solved in Busybox + * hush. + * + * Indeed, the following expression can be seen like this: + * (false && true) || true + * So, (false && true) returns 1, the true is not executed, and + * true is executed because of ||. + */ + 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_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)); + + if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_assertok(run_command("true || true && false", 0)); + } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * This difference seems to come from a bug solved in Busybox + * hush. + * + * Indeed, the following expression can be seen like this: + * (true || true) && false + * So, (true || true) returns 0, the second true is not + * executed, and then false is executed because of &&. + */ + ut_asserteq(1, run_command("true || true && false", 0)); + } + + if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + ut_assertok(run_command("true || false && false", 0)); + } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * This difference seems to come from a bug solved in Busybox + * hush. + * + * Indeed, the following expression can be seen like this: + * (true || false) && false + * So, (true || false) returns 0, the false is not executed, and + * then false is executed because of &&. + */ + 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_or_and, 0); diff --git a/test/hush/loop.c b/test/hush/loop.c new file mode 100644 index 00000000000..d734abf136d --- /dev/null +++ b/test/hush/loop.c @@ -0,0 +1,90 @@ +// 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 <test/hush.h> +#include <test/ut.h> +#include <asm/global_data.h> + +DECLARE_GLOBAL_DATA_PTR; + +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 quux; do echo $loop_i; done", 0)); + ut_assert_nextline("foo"); + ut_assert_nextline("bar"); + ut_assert_nextline("quux"); + ut_assert_nextline("quux"); + ut_assert_console_end(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* Reset local variable. */ + ut_assertok(run_command("loop_i=", 0)); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + 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(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* + * Hush 2021 always returns 0 from while loop... + * You can see code snippet near this line to have a better + * understanding: + * debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); + */ + ut_assertok(run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0)); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + /* + * 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(); + + if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { + /* Reset local variable. */ + ut_assertok(run_command("loop_foo=", 0)); + } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { + 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); 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 3b4b6fcaf40..00000000000 --- a/test/py/tests/test_hush_if_test.py +++ /dev/null @@ -1,197 +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), -) - -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() - -@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_z(u_boot_console): - """Test the -z operator""" - u_boot_console.run_command('setenv ut_var_nonexistent') - u_boot_console.run_command('setenv ut_var_exists 1') - exec_hush_if(u_boot_console, 'test -z "$ut_var_nonexistent"', True) - exec_hush_if(u_boot_console, 'test -z "$ut_var_exists"', False) - 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) - -def test_hush_var(u_boot_console): - """Test the set and unset of variables""" - u_boot_console.run_command('ut_var_nonexistent=') - u_boot_console.run_command('ut_var_exists=1') - u_boot_console.run_command('ut_var_unset=1') - exec_hush_if(u_boot_console, 'test -z "$ut_var_nonexistent"', True) - exec_hush_if(u_boot_console, 'test -z "$ut_var_exists"', False) - exec_hush_if(u_boot_console, 'test -z "$ut_var_unset"', False) - exec_hush_if(u_boot_console, 'ut_var_unset=', True) - exec_hush_if(u_boot_console, 'test -z "$ut_var_unset"', True) - u_boot_console.run_command('ut_var_exists=') - u_boot_console.run_command('ut_var_unset=') diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py index 0e3a48e73f6..c169c835e38 100644 --- a/test/py/tests/test_ut.py +++ b/test/py/tests/test_ut.py @@ -500,5 +500,11 @@ def test_ut(u_boot_console, ut_subtest): execute command 'ut foo bar' """ - output = u_boot_console.run_command('ut ' + ut_subtest) + if ut_subtest == 'hush hush_test_simple_dollar': + # ut hush hush_test_simple_dollar prints "Unknown command" on purpose. + with u_boot_console.disable_check('unknown_command'): + output = u_boot_console.run_command('ut ' + ut_subtest) + assert('Unknown command \'quux\' - try \'help\'' in output) + else: + output = u_boot_console.run_command('ut ' + ut_subtest) assert output.endswith('Failures: 0') |