Skip to contentSkip to conversation

test: cmd: add binop tests and enable on sandbox

PATCH In review Maxin B. John 4 participants 12 messages Oct 29 – Nov 2

lore
Share & tools
lore
#0 lore raw

cover test: cmd: add binop tests and enable on sandbox

binop has no tests today. Add some tests to catch regressions.

Each operation (xor, or, and) is tested with hex strings, environment variables or memory as sources. Results go to an environment variable, memory or the console. The tests also cover zero padding, console wrapping, bad input, plus the heap overrun.

CMD_BINOP is enabled for sandbox/sandbox64, so both 32-bit/64-bit builds run the tests.

The tests are a follow-up to the separate "cmd: binop: validate inputs and fix heap overrun" patch.

Tested with:
./test/py/test.py --bd sandbox --build -k ut_cmd_cmd_test_binop ./test/py/test.py --bd sandbox64 --build -k ut_cmd_cmd_test_binop

Maxin B. John (2):
configs: sandbox: Enable binop command
test: cmd: add tests for the binop command

 configs/sandbox64_defconfig |   1 +
 configs/sandbox_defconfig   |   1 +
 test/cmd/Makefile           |   1 +
 test/cmd/binop.c            | 192 ++++++++++++++++++++++++++++++++++++
 4 files changed, 195 insertions(+)
 create mode 100644 test/cmd/binop.c
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 configs: sandbox: Enable binop command

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

---
 configs/sandbox64_defconfig | 1 +
 configs/sandbox_defconfig   | 1 +
 2 files changed, 2 insertions(+)
configs/sandbox64_defconfig +1 -0
index f861d3ef06db..c4ad63ce63be 100644
@@ -43,6 +43,7 @@ CONFIG_CMD_NVEDIT_EFI=y
 CONFIG_CMD_NVEDIT_INFO=y
 CONFIG_CMD_NVEDIT_LOAD=y
 CONFIG_CMD_NVEDIT_SELECT=y
+CONFIG_CMD_BINOP=y
 CONFIG_LOOPW=y
 CONFIG_CMD_MD5SUM=y
 CONFIG_CMD_MX_CYCLIC=y
configs/sandbox_defconfig +1 -0
index a3fa52460827..c63a4df8befa 100644
@@ -74,6 +74,7 @@ CONFIG_CMD_NVEDIT_EFI=y
 CONFIG_CMD_NVEDIT_INFO=y
 CONFIG_CMD_NVEDIT_LOAD=y
 CONFIG_CMD_NVEDIT_SELECT=y
+CONFIG_CMD_BINOP=y
 CONFIG_LOOPW=y
 CONFIG_CMD_MD5SUM=y
 CONFIG_CMD_MEM_SEARCH=y
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/2] configs: sandbox: Enable binop command

Quoted 4 lines · Maxin B. John

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

Which tests? I could not find any depending on CONFIG_CMD_BINOP.

HTML documentation for the command does not exist. The online documentation leaves the user clueless:

Usage:
binop op count [*]src1 [*]src2 [[*]dest]
- compute binary operation of data at/in src1 and src2 (either *memaddr, env var name or hex string) and store result in/at dest, where op is one of xor, or, and.

What is count good for?
What should a variable contain?

Can we simply eliminate the command? It is only used by a single board.

When I see comments like these in the code I am inclined to remove the board completely:

drivers/sysinfo/sysinfo-uclass.c:21:
* There is some very dodgy error handling in gazerbeam,

Best regards

Heinrich

Quoted 29 lines
---
  configs/sandbox64_defconfig | 1 +
  configs/sandbox_defconfig   | 1 +
  2 files changed, 2 insertions(+)
configs/sandbox64_defconfig +1 -0
index f861d3ef06db..c4ad63ce63be 100644
@@ -43,6 +43,7 @@ CONFIG_CMD_NVEDIT_EFI=y
  CONFIG_CMD_NVEDIT_INFO=y
  CONFIG_CMD_NVEDIT_LOAD=y
  CONFIG_CMD_NVEDIT_SELECT=y
+CONFIG_CMD_BINOP=y
  CONFIG_LOOPW=y
  CONFIG_CMD_MD5SUM=y
  CONFIG_CMD_MX_CYCLIC=y
configs/sandbox_defconfig +1 -0
index a3fa52460827..c63a4df8befa 100644
@@ -74,6 +74,7 @@ CONFIG_CMD_NVEDIT_EFI=y
  CONFIG_CMD_NVEDIT_INFO=y
  CONFIG_CMD_NVEDIT_LOAD=y
  CONFIG_CMD_NVEDIT_SELECT=y
+CONFIG_CMD_BINOP=y
  CONFIG_LOOPW=y
  CONFIG_CMD_MD5SUM=y
  CONFIG_CMD_MEM_SEARCH=y
author
#3 lore raw

Re: [PATCH 1/2] configs: sandbox: Enable binop command

Hi Heinrich,

Quoted 8 lines · Heinrich Schuchardt
Quoted 4 lines · Maxin B. John

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

Which tests? I could not find any depending on CONFIG_CMD_BINOP.

This change is a follow-up to my recently submitted binop clean-up patch:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637804.html

Following the review comments on that patch, I have also submitted tests to improve binop test coverage:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637974.html

These tests depend on CONFIG_CMD_BINOP, which is why I enabled it in sandbox and sandbox64.

Quoted 2 lines

HTML documentation for the command does not exist. The online documentation leaves the user clueless:

Agreed. The documentation for binop is insufficient and could be improved.

Quoted 11 lines

Usage:
binop op count [*]src1 [*]src2 [[*]dest]
- compute binary operation of data at/in src1 and src2 (either *memaddr, env var name or hex string) and store result in/at dest, where op is one of xor, or, and.

What is count good for?
What should a variable contain?

Can we simply eliminate the command? It is only used by a single board.

I am not aware of any other users of this command apart from the gazerbeam board.
Personally, I can work around its removal if that is what the maintainers decide.

Quoted 5 lines

When I see comments like these in the code I am inclined to remove the board completely:

drivers/sysinfo/sysinfo-uclass.c:21:
* There is some very dodgy error handling in gazerbeam,

I will leave that decision to the maintainers.

Quoted 3 lines

Best regards

Heinrich

Best Regards,
Maxin

Quoted 30 lines
Quoted 29 lines
---
  configs/sandbox64_defconfig | 1 +
  configs/sandbox_defconfig   | 1 +
  2 files changed, 2 insertions(+)
configs/sandbox64_defconfig +1 -0
index f861d3ef06db..c4ad63ce63be 100644
@@ -43,6 +43,7 @@ CONFIG_CMD_NVEDIT_EFI=y
  CONFIG_CMD_NVEDIT_INFO=y
  CONFIG_CMD_NVEDIT_LOAD=y
  CONFIG_CMD_NVEDIT_SELECT=y
+CONFIG_CMD_BINOP=y
  CONFIG_LOOPW=y
  CONFIG_CMD_MD5SUM=y
  CONFIG_CMD_MX_CYCLIC=y
configs/sandbox_defconfig +1 -0
index a3fa52460827..c63a4df8befa 100644
@@ -74,6 +74,7 @@ CONFIG_CMD_NVEDIT_EFI=y
  CONFIG_CMD_NVEDIT_INFO=y
  CONFIG_CMD_NVEDIT_LOAD=y
  CONFIG_CMD_NVEDIT_SELECT=y
+CONFIG_CMD_BINOP=y
  CONFIG_LOOPW=y
  CONFIG_CMD_MD5SUM=y
  CONFIG_CMD_MEM_SEARCH=y
#4 lore raw

Re: [PATCH 1/2] configs: sandbox: Enable binop command

Quoted 37 lines · Maxin John

Hi Heinrich,

Quoted 8 lines · Heinrich Schuchardt
Quoted 4 lines · Maxin B. John

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

Which tests? I could not find any depending on CONFIG_CMD_BINOP.

This change is a follow-up to my recently submitted binop clean-up patch:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637804.html

Following the review comments on that patch, I have also submitted tests to improve binop test coverage:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637974.html

These tests depend on CONFIG_CMD_BINOP, which is why I enabled it in sandbox and sandbox64.

Quoted 2 lines

HTML documentation for the command does not exist. The online documentation leaves the user clueless:

Agreed. The documentation for binop is insufficient and could be improved.

Quoted 10 lines

Usage:
binop op count [*]src1 [*]src2 [[*]dest]
- compute binary operation of data at/in src1 and src2 (either *memaddr, env var name or hex string) and store result in/at dest, where op is one of xor, or, and.

What is count good for?
What should a variable contain?

Quoted 6 lines
Quoted 1 line

Can we simply eliminate the command? It is only used by a single board.

I am not aware of any other users of this command apart from the gazerbeam board.
Personally, I can work around its removal if that is what the maintainers decide.

Perhaps what's missing here is what your motivation is for this cleanup?

Signature · 1 line
Tom
author
#5 lore raw

Re: [PATCH 1/2] configs: sandbox: Enable binop command

Hi Tom,

Quoted 39 lines · Tom Rini
Quoted 37 lines · Maxin John

Hi Heinrich,

Quoted 8 lines · Heinrich Schuchardt
Quoted 4 lines · Maxin B. John

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

Which tests? I could not find any depending on CONFIG_CMD_BINOP.

This change is a follow-up to my recently submitted binop clean-up patch:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637804.html

Following the review comments on that patch, I have also submitted tests to improve binop test coverage:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637974.html

These tests depend on CONFIG_CMD_BINOP, which is why I enabled it in sandbox and sandbox64.

Quoted 2 lines

HTML documentation for the command does not exist. The online documentation leaves the user clueless:

Agreed. The documentation for binop is insufficient and could be improved.

Quoted 10 lines

Usage:
binop op count [*]src1 [*]src2 [[*]dest]
- compute binary operation of data at/in src1 and src2 (either *memaddr, env var name or hex string) and store result in/at dest, where op is one of xor, or, and.

What is count good for?
What should a variable contain?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Can we simply eliminate the command? It is only used by a single board.

I am not aware of any other users of this command apart from the gazerbeam board.
Personally, I can work around its removal if that is what the maintainers decide.

Perhaps what's missing here is what your motivation is for this cleanup?

My primary motivation for the cleanup was to address existing safety issues and make the command more robust, specifically fixing corner cases such as heap corruption and crashes caused by over-sized input.

I originally enabled binop while experimenting with U-Boot on the Milk-V Duo. There, I was exploring ways to manipulate binary configuration data using binop.

I agree that the command has limited usage today, and that similar functionality can often be achieved using other U-Boot commands. I don't have a strong preference either way and am happy to go with whatever the maintainers feel is best.

Quoted 2 lines

--
Tom

Best Regards,
Maxin

#6 lore raw

Re: [PATCH 1/2] configs: sandbox: Enable binop command

Quoted 55 lines · Maxin John

Hi Tom,

Quoted 39 lines · Tom Rini
Quoted 37 lines · Maxin John

Hi Heinrich,

Quoted 8 lines · Heinrich Schuchardt
Quoted 4 lines · Maxin B. John

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

Which tests? I could not find any depending on CONFIG_CMD_BINOP.

This change is a follow-up to my recently submitted binop clean-up patch:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637804.html

Following the review comments on that patch, I have also submitted tests to improve binop test coverage:
https://lists.u-boot-project.org/pipermail/u-boot/2026-October/637974.html

These tests depend on CONFIG_CMD_BINOP, which is why I enabled it in sandbox and sandbox64.

Quoted 2 lines

HTML documentation for the command does not exist. The online documentation leaves the user clueless:

Agreed. The documentation for binop is insufficient and could be improved.

Quoted 10 lines

Usage:
binop op count [*]src1 [*]src2 [[*]dest]
- compute binary operation of data at/in src1 and src2 (either *memaddr, env var name or hex string) and store result in/at dest, where op is one of xor, or, and.

What is count good for?
What should a variable contain?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Can we simply eliminate the command? It is only used by a single board.

I am not aware of any other users of this command apart from the gazerbeam board.
Personally, I can work around its removal if that is what the maintainers decide.

Perhaps what's missing here is what your motivation is for this cleanup?

My primary motivation for the cleanup was to address existing safety issues and make the command more robust, specifically fixing corner cases such as heap corruption and crashes caused by over-sized input.

Quoted 7 lines

I originally enabled binop while experimenting with U-Boot on the Milk-V Duo. There, I was exploring ways to manipulate binary configuration data using binop.

I agree that the command has limited usage today, and that similar functionality can often be achieved using other U-Boot commands. I don't have a strong preference either way and am happy to go with whatever the maintainers feel is best.

Thanks for explaining. Yes, if you don't mind, please do finish cleaning up the command and adding a documentation page for it as well.

Signature · 1 line
Tom
Reviewed-by on v1 1/2
#7 lore raw

Re: [PATCH 1/2] configs: sandbox: Enable binop command

In reply to Maxin B. John
Quoted 10 lines · Maxin John

configs: sandbox: Enable binop command

Turn on CMD_BINOP for sandbox and sandbox64 so the binop tests are built and run in CI on both 32-bit and 64-bit builds.

 configs/sandbox64_defconfig | 1 +
 configs/sandbox_defconfig   | 1 +
 2 files changed, 2 insertions(+)
#8 lore raw

PATCH 2/2 test: cmd: add tests for the binop command

In reply to Maxin B. John

binop has had no tests so far. Add some, so that bugs like the heap overrun are caught in CI.

Each operation (xor, or, and) is run on hex strings, env vars or memory, with the result going to an env var, memory or the console.

The tests also check that:
- short inputs are zero-padded
- console output wraps after 16 bytes
- bad counts, operators, addresses or hex strings are rejected without touching the destination

There is also a test for the heap overrun.

---
 test/cmd/Makefile |   1 +
 test/cmd/binop.c  | 192 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)
 create mode 100644 test/cmd/binop.c
test/cmd/Makefile +1 -0
index e734933d5f73..79cbb291250b 100644
@@ -13,6 +13,7 @@ endif
 obj-$(CONFIG_HUSH_PARSER) += exit.o
 obj-$(CONFIG_X86) += cpuid.o msr.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
+obj-$(CONFIG_CMD_BINOP) += binop.o
 ifdef CONFIG_CONSOLE_RECORD
 obj-$(CONFIG_CMD_ACPI) += acpi.o
 endif
test/cmd/binop.c new file +192 -0

192 lines not on this page · raw message

Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH 2/2] test: cmd: add tests for the binop command

Hi Maxin,

Quoted 24 lines · Maxin John

test: cmd: add tests for the binop command

binop has had no tests so far. Add some, so that bugs like the heap overrun are caught in CI.

Each operation (xor, or, and) is run on hex strings, env vars or memory, with the result going to an env var, memory or the console.

The tests also check that:
- short inputs are zero-padded
- console output wraps after 16 bytes
- bad counts, operators, addresses or hex strings are rejected without touching the destination

There is also a test for the heap overrun.

 test/cmd/Makefile |   1 +
 test/cmd/binop.c  | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)

test/cmd/binop.c line 6

Quoted 6 lines
test/cmd/binop.c +6 -0
@@ -0,0 +1,192 @@
+	/* Output wraps after 16 bytes */
+	ut_assertok(run_command("binop xor 17 000102030405060708090a0b0c0d0e0f10 00",
+				0));
+	ut_assert_nextline("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ");
+	ut_assert_nextline("10 ");
+	ut_assert_console_end();

Please can you add a case with a count of exactly 16? With the current loop, i == 15 outputs a newline and the final puts('\n') outputs another, so the user sees a blank line after the data. If that is intended, a test should pin it down. Otherwise it is worth fixing in the binop clean-up patch, since this is the edge case most likely to go wrong.

test/cmd/binop.c line 7

Quoted 9 lines
test/cmd/binop.c +7 -0
@@ -0,0 +1,192 @@
+static int cmd_test_binop_invalid(struct unit_test_state *uts)
+{
+	/* Failed operations must not change binop_out */
+	ut_assertok(env_set("binop_out", "deadbeef"));
+
+	/* Literal hex string: too long, odd length, not hex */
+	ut_asserteq(1, run_command("binop xor 2 010203 01 binop_out", 0));

run_command() returns 1 for every failure, including a usage error, so these checks cannot tell which validation rejected the input. For example, if '*nope' were later accepted as address 0 and the command failed for some other reason, the test would still pass. It would be worth making this a UTF_CONSOLE test and checking the error message (or the usage text) after each command, as other tests in test/cmd do. What do you think?

Regards,
Simon

#10 lore raw

PATCH v2 1/1 test: cmd: add tests for the binop command

binop has had no tests so far. Add some, so that bugs like the heap overrun fixed in the previous commit are caught in CI.

Each operation (xor, or, and) is run on hex strings, env vars or memory, with the result going to an env var, memory or the console.

The tests also check that:
- short inputs are zero-padded
- console output wraps after 16 bytes, with no blank line when the count is exactly 16
- bad counts, operators, addresses or hex strings are rejected with the expected error message or usage text, without touching the destination

There is also a test for the heap overrun, which checks that the over-long value is rejected for its length.

---
Changes in v2:
- Add a console test with a count of exactly 16
- Check the error message or usage text after each failing command, so the tests show which check rejected the input

---
 test/cmd/Makefile |   1 +
 test/cmd/binop.c  | 237 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 238 insertions(+)
 create mode 100644 test/cmd/binop.c
test/cmd/Makefile +1 -0
index e734933d5f73..79cbb291250b 100644
@@ -13,6 +13,7 @@ endif
 obj-$(CONFIG_HUSH_PARSER) += exit.o
 obj-$(CONFIG_X86) += cpuid.o msr.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
+obj-$(CONFIG_CMD_BINOP) += binop.o
 ifdef CONFIG_CONSOLE_RECORD
 obj-$(CONFIG_CMD_ACPI) += acpi.o
 endif
test/cmd/binop.c new file +237 -0

237 lines not on this page · raw message

Signature · 1 line
2.47.3
author
#11 lore raw

Re: [PATCH 2/2] test: cmd: add tests for the binop command

In reply to Simon Glass

Hi Simon,

Quoted 26 lines · Simon Glass

Hi Maxin,

Quoted 21 lines · Maxin John

test: cmd: add tests for the binop command

binop has had no tests so far. Add some, so that bugs like the heap overrun are caught in CI.

Each operation (xor, or, and) is run on hex strings, env vars or memory, with the result going to an env var, memory or the console.

The tests also check that:
- short inputs are zero-padded
- console output wraps after 16 bytes
- bad counts, operators, addresses or hex strings are rejected without touching the destination

There is also a test for the heap overrun.

 test/cmd/Makefile |   1 +
 test/cmd/binop.c  | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)
Quoted 15 lines

test/cmd/binop.c line 6

Quoted 8 lines
test/cmd/binop.c +6 -0
@@ -0,0 +1,192 @@
+     /* Output wraps after 16 bytes */
+     ut_assertok(run_command("binop xor 17 000102030405060708090a0b0c0d0e0f10 00",
+                             0));
+     ut_assert_nextline("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ");
+     ut_assert_nextline("10 ");
+     ut_assert_console_end();

Please can you add a case with a count of exactly 16? With the current loop, i == 15 outputs a newline and the final puts('\n') outputs another, so the user sees a blank line after the data. If that is intended, a test should pin it down. Otherwise it is worth fixing in the binop clean-up patch, since this is the edge case most likely to go wrong.

Thanks for the review. I have addressed this in v3 of binop clean-up patch. Also added a test for exactly 16 bytes to check the extra newline.

Quoted 10 lines

test/cmd/binop.c line 7

Quoted 9 lines
test/cmd/binop.c +7 -0
@@ -0,0 +1,192 @@
+static int cmd_test_binop_invalid(struct unit_test_state *uts)
+{
+     /* Failed operations must not change binop_out */
+     ut_assertok(env_set("binop_out", "deadbeef"));
+
+     /* Literal hex string: too long, odd length, not hex */
+     ut_asserteq(1, run_command("binop xor 2 010203 01 binop_out", 0));
Quoted 7 lines

run_command() returns 1 for every failure, including a usage error, so these checks cannot tell which validation rejected the input. For example, if '*nope' were later accepted as address 0 and the command failed for some other reason, the test would still pass. It would be worth making this a UTF_CONSOLE test and checking the error message (or the usage text) after each command, as other tests in test/cmd do. What do you think?

You are right. Updated the binop tests in v2 based on this suggestion.

Quoted 2 lines

Regards,
Simon

Thanks & Regards,
Maxin