mirror of
https://github.com/u-boot/u-boot.git
synced 2025-05-04 10:43:35 +00:00
Merge patch series "cmd: hash: correct parameter count check"
Heinrich Schuchardt <heinrich.schuchardt@canonical.com> says:
Since commit 348ea87850
("cmd: hash: fix param count check") the hash
command cannot be used without the optional variable name parameter if
CONFIG_HASH_VERIFY=y. 'hash sha1 $loadaddr $filesize' returns
CMD_RET_USAGE.
The minimum number of arguments is four no matter if verification is
enabled or not.
Fix the parameter check.
Provide a unit test.
Link: https://lore.kernel.org/r/20241102100836.103005-1-heinrich.schuchardt@canonical.com
This commit is contained in:
commit
f6ee671264
3 changed files with 103 additions and 1 deletions
|
@ -25,7 +25,7 @@ static int do_hash(struct cmd_tbl *cmdtp, int flag, int argc,
|
||||||
char *s;
|
char *s;
|
||||||
int flags = HASH_FLAG_ENV;
|
int flags = HASH_FLAG_ENV;
|
||||||
|
|
||||||
if (argc < (HARGS - 1))
|
if (argc < 4)
|
||||||
return CMD_RET_USAGE;
|
return CMD_RET_USAGE;
|
||||||
|
|
||||||
#if IS_ENABLED(CONFIG_HASH_VERIFY)
|
#if IS_ENABLED(CONFIG_HASH_VERIFY)
|
||||||
|
|
|
@ -19,6 +19,7 @@ obj-$(CONFIG_CMD_BDI) += bdinfo.o
|
||||||
obj-$(CONFIG_COREBOOT_SYSINFO) += coreboot.o
|
obj-$(CONFIG_COREBOOT_SYSINFO) += coreboot.o
|
||||||
obj-$(CONFIG_CMD_FDT) += fdt.o
|
obj-$(CONFIG_CMD_FDT) += fdt.o
|
||||||
obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
|
obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
|
||||||
|
obj-$(CONFIG_CMD_HASH) += hash.o
|
||||||
obj-$(CONFIG_CMD_HISTORY) += history.o
|
obj-$(CONFIG_CMD_HISTORY) += history.o
|
||||||
obj-$(CONFIG_CMD_LOADM) += loadm.o
|
obj-$(CONFIG_CMD_LOADM) += loadm.o
|
||||||
obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
|
obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
|
||||||
|
|
101
test/cmd/hash.c
Normal file
101
test/cmd/hash.c
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
/*
|
||||||
|
* Tests for hash command
|
||||||
|
*
|
||||||
|
* Copyright 2024, Heinrich Schuchardt <heinrich.schuchardt@canoncal.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <command.h>
|
||||||
|
#include <dm.h>
|
||||||
|
#include <dm/test.h>
|
||||||
|
#include <test/test.h>
|
||||||
|
#include <test/ut.h>
|
||||||
|
|
||||||
|
static int dm_test_cmd_hash_md5(struct unit_test_state *uts)
|
||||||
|
{
|
||||||
|
if (!CONFIG_IS_ENABLED(MD5)) {
|
||||||
|
ut_assert(run_command("hash md5 $loadaddr 0", 0));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ut_assertok(run_command("hash md5 $loadaddr 0", 0));
|
||||||
|
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
|
||||||
|
ut_asserteq_ptr(uts->actual_str,
|
||||||
|
strstr(uts->actual_str, "md5 for "));
|
||||||
|
ut_assert(strstr(uts->actual_str,
|
||||||
|
"d41d8cd98f00b204e9800998ecf8427e"));
|
||||||
|
ut_assert_console_end();
|
||||||
|
|
||||||
|
ut_assertok(run_command("hash md5 $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, "md5 for "));
|
||||||
|
ut_assert(strstr(uts->actual_str,
|
||||||
|
"d41d8cd98f00b204e9800998ecf8427e"));
|
||||||
|
ut_check_console_line(uts, "d41d8cd98f00b204e9800998ecf8427e");
|
||||||
|
|
||||||
|
if (!CONFIG_IS_ENABLED(HASH_VERIFY)) {
|
||||||
|
ut_assert(run_command("hash -v sha256 $loadaddr 0 foo", 0));
|
||||||
|
ut_check_console_line(uts, "hash - compute hash message digest");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ut_assertok(run_command("hash -v md5 $loadaddr 0 foo", 0));
|
||||||
|
ut_assert_console_end();
|
||||||
|
|
||||||
|
env_set("foo", "ffffffffffffffffffffffffffffffff");
|
||||||
|
ut_assert(run_command("hash -v md5 $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;
|
||||||
|
}
|
||||||
|
DM_TEST(dm_test_cmd_hash_md5, UTF_CONSOLE);
|
||||||
|
|
||||||
|
static int dm_test_cmd_hash_sha256(struct unit_test_state *uts)
|
||||||
|
{
|
||||||
|
if (!CONFIG_IS_ENABLED(SHA256)) {
|
||||||
|
ut_assert(run_command("hash sha256 $loadaddr 0", 0));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ut_assertok(run_command("hash sha256 $loadaddr 0", 0));
|
||||||
|
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
|
||||||
|
ut_asserteq_ptr(uts->actual_str,
|
||||||
|
strstr(uts->actual_str, "sha256 for "));
|
||||||
|
ut_assert(strstr(uts->actual_str,
|
||||||
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
|
||||||
|
ut_assert_console_end();
|
||||||
|
|
||||||
|
ut_assertok(run_command("hash sha256 $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, "sha256 for "));
|
||||||
|
ut_assert(strstr(uts->actual_str,
|
||||||
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
|
||||||
|
ut_check_console_line(uts,
|
||||||
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||||
|
|
||||||
|
if (!CONFIG_IS_ENABLED(HASH_VERIFY)) {
|
||||||
|
ut_assert(run_command("hash -v sha256 $loadaddr 0 foo", 0));
|
||||||
|
ut_check_console_line(uts, "hash - compute hash message digest");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ut_assertok(run_command("hash -v sha256 $loadaddr 0 foo", 0));
|
||||||
|
ut_assert_console_end();
|
||||||
|
|
||||||
|
env_set("foo", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||||
|
ut_assert(run_command("hash -v sha256 $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;
|
||||||
|
}
|
||||||
|
DM_TEST(dm_test_cmd_hash_sha256, UTF_CONSOLE);
|
Loading…
Add table
Reference in a new issue