[PATCH v3 4/6] test: cmd: hash: add unit test for sm3_256
Heiko Schocher
hs at nabladev.com
Tue Nov 18 05:30:40 CET 2025
add simple test for sm3 256 hash
Signed-off-by: Heiko Schocher <hs at nabladev.com>
---
I wonder why this tests are under DM and not under CMD
Should we move them to CMD ?
Ignored checkpatch warnings for too long lines.
ignore checkpatch warning:
test/cmd/hash.c:152: check: Please use a blank line after function/struct/union/enum declarations
as this is in the last line, and no new lines at the end
allowed.
Changes in v3:
use CMD_TEST instead of DM_TEST, as Heinrich confirmed
test/cmd/hash.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/test/cmd/hash.c b/test/cmd/hash.c
index bb96380c351..ced18319f34 100644
--- a/test/cmd/hash.c
+++ b/test/cmd/hash.c
@@ -9,6 +9,7 @@
#include <env.h>
#include <dm.h>
#include <dm/test.h>
+#include <test/cmd.h>
#include <test/test.h>
#include <test/ut.h>
@@ -103,3 +104,49 @@ static int dm_test_cmd_hash_sha256(struct unit_test_state *uts)
return 0;
}
DM_TEST(dm_test_cmd_hash_sha256, UTF_CONSOLE);
+
+static int cmd_test_hash_sm3_256(struct unit_test_state *uts)
+{
+ const char *sum = "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b";
+
+ if (!CONFIG_IS_ENABLED(SM3)) {
+ ut_assert(run_command("hash sm3_256 $loadaddr 0", 0));
+
+ return 0;
+ }
+
+ ut_assertok(run_command("hash sm3_256 $loadaddr 0", 0));
+ console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+ ut_asserteq_ptr(uts->actual_str,
+ strstr(uts->actual_str, "sm3_256 for "));
+ ut_assert(strstr(uts->actual_str, sum));
+ ut_assert_console_end();
+
+ ut_assertok(run_command("hash sm3_256 $loadaddr 0 foo; echo $foo", 0));
+ console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+ ut_asserteq_ptr(uts->actual_str,
+ strstr(uts->actual_str, "sm3_256 for "));
+ ut_assert(strstr(uts->actual_str, sum));
+ ut_assertok(ut_check_console_line(uts, sum));
+
+ if (!CONFIG_IS_ENABLED(HASH_VERIFY)) {
+ ut_assert(run_command("hash -v sm3_256 $loadaddr 0 foo", 0));
+ ut_assertok(ut_check_console_line(uts,
+ "hash - compute hash message digest"));
+
+ return 0;
+ }
+
+ ut_assertok(run_command("hash -v sm3_256 $loadaddr 0 foo", 0));
+ ut_assert_console_end();
+
+ env_set("foo",
+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
+ ut_assert(run_command("hash -v sm3_256 $loadaddr 0 foo", 0));
+ console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+ ut_assert(strstr(uts->actual_str, "!="));
+ ut_assert_console_end();
+
+ return 0;
+}
+CMD_TEST(cmd_test_hash_sm3_256, UTF_CONSOLE);
--
2.20.1
More information about the U-Boot
mailing list