]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
test: add first autoboot unit tests
authorSteffen Jaeckel <jaeckel-floss@eyet-services.de>
Thu, 8 Jul 2021 13:57:40 +0000 (15:57 +0200)
committerTom Rini <trini@konsulko.com>
Fri, 23 Jul 2021 17:36:20 +0000 (13:36 -0400)
This adds tests for the crypt-based and plain SHA256-based password hashing
algorithms in the autoboot flow.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
common/Kconfig.boot
common/console.c
configs/sandbox_defconfig
include/console.h
include/test/common.h [new file with mode: 0644]
include/test/suites.h
test/Makefile
test/cmd_ut.c
test/common/Makefile [new file with mode: 0644]
test/common/cmd_ut_common.c [new file with mode: 0644]
test/common/test_autoboot.c [new file with mode: 0644]

index 49e28b2ef29602d506d5b533fac5ad96092ca89e..642dd9bcfbedad752a04f9c0eac7843b1285ed59 100644 (file)
@@ -907,7 +907,7 @@ config AUTOBOOT_STOP_STR_CRYPT
          and saved in the environment variable "bootstopkeycrypt".
 
 config AUTOBOOT_STOP_STR_SHA256
-       string "Stop autobooting via SHA256 encrypted password"
+       string "Stop autobooting via SHA256 hashed password"
        depends on AUTOBOOT_STOP_STR_ENABLE
        help
          This option adds the feature to only stop the autobooting,
index 73edb28799201c3f4a86c3777e63d82a914aa5a6..0013d183aeb5499f10cd51e99b30369f7c9165c2 100644 (file)
@@ -773,6 +773,11 @@ int console_record_avail(void)
        return membuff_avail((struct membuff *)&gd->console_out);
 }
 
+int console_in_puts(const char *str)
+{
+       return membuff_put((struct membuff *)&gd->console_in, str, strlen(str));
+}
+
 #endif
 
 /* test if ctrl-c was pressed */
index 1766dc018b648146e2a3757ff265b079eee85ae3..a1e77a511dbea8e9168af1f9d078f60cfdaeef8d 100644 (file)
@@ -287,6 +287,16 @@ CONFIG_EFI_CAPSULE_FIRMWARE_FIT=y
 CONFIG_EFI_CAPSULE_FIRMWARE_RAW=y
 CONFIG_EFI_SECURE_BOOT=y
 CONFIG_TEST_FDTDEC=y
+CONFIG_CRYPT_PW=y
+CONFIG_CRYPT_PW_SHA256=y
+CONFIG_CRYPT_PW_SHA512=y
+CONFIG_AUTOBOOT_KEYED=y
+CONFIG_AUTOBOOT_PROMPT="Enter password \"a\" in %d seconds to stop autoboot\n"
+CONFIG_AUTOBOOT_ENCRYPTION=y
+CONFIG_AUTOBOOT_STOP_STR_ENABLE=y
+CONFIG_AUTOBOOT_STOP_STR_CRYPT="$5$rounds=640000$HrpE65IkB8CM5nCL$BKT3QdF98Bo8fJpTr9tjZLZQyzqPASBY20xuK5Rent9"
+CONFIG_AUTOBOOT_NEVER_TIMEOUT=y
+CONFIG_AUTOBOOT_SHA256_FALLBACK=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
index f848bcbf03764e7a5d0524760456a37a598e2a1b..b182440fcd639be7f0a5f1e15ef1aa2c63a0d445 100644 (file)
@@ -83,6 +83,17 @@ int console_record_readline(char *str, int maxlen);
  * @return available bytes (0 if empty)
  */
 int console_record_avail(void);
+
+/**
+ * console_in_puts() - Write a string to the console input buffer
+ *
+ * This writes the given string to the console_in buffer which will then be
+ * returned if a function calls e.g. `getc()`
+ *
+ * @str: the string to write
+ * @return  the number of bytes added
+ */
+int console_in_puts(const char *str);
 #else
 static inline int console_record_init(void)
 {
@@ -114,6 +125,12 @@ static inline int console_record_avail(void)
        return 0;
 }
 
+static inline int console_in_puts(const char *str)
+{
+       /* There is never anything written */
+       return 0;
+}
+
 #endif /* !CONFIG_CONSOLE_RECORD */
 
 /**
diff --git a/include/test/common.h b/include/test/common.h
new file mode 100644 (file)
index 0000000..81260d0
--- /dev/null
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
+ * Copyright (c) 2021 Steffen Jaeckel <jaeckel-floss@eyet-services.de>
+ */
+
+#ifndef __TEST_COMMON_H__
+#define __TEST_COMMON_H__
+
+#include <test/test.h>
+
+/* Declare a new common function test */
+#define COMMON_TEST(_name, _flags) UNIT_TEST(_name, _flags, common_test)
+
+#endif /* __TEST_COMMON_H__ */
index 80b41f188c75a28246508deea2eda8dd5434dd08..d35cd83a4eb30eb9be2f156f8b4a0314756a3799 100644 (file)
@@ -31,6 +31,7 @@ int do_ut_addrmap(struct cmd_tbl *cmdtp, int flag, int argc,
 int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
                   char *const argv[]);
+int do_ut_common(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc,
                      char *const argv[]);
 int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
index 117839e58473bfa95a07d7fa0c1e2df24e77a41e..b3b2902e2e76f08ee7574854f6e5242d6d1daa0c 100644 (file)
@@ -22,6 +22,7 @@ obj-$(CONFIG_UT_TIME) += time_ut.o
 obj-y += ut.o
 
 ifeq ($(CONFIG_SPL_BUILD),)
+obj-$(CONFIG_UNIT_TEST) += common/
 obj-$(CONFIG_UNIT_TEST) += lib/
 obj-y += log/
 obj-$(CONFIG_$(SPL_)UT_UNICODE) += unicode_ut.o
index 6f174c6a07f1596b2207e4eed690fe8ec3b6d8cf..90b260f72d6c1f81510fb8c1d20dbfe17537bc9a 100644 (file)
@@ -28,6 +28,7 @@ int cmd_ut_category(const char *name, const char *prefix,
 
 static struct cmd_tbl cmd_ut_sub[] = {
        U_BOOT_CMD_MKENT(all, CONFIG_SYS_MAXARGS, 1, do_ut_all, "", ""),
+       U_BOOT_CMD_MKENT(common, CONFIG_SYS_MAXARGS, 1, do_ut_common, "", ""),
 #if defined(CONFIG_UT_DM)
        U_BOOT_CMD_MKENT(dm, CONFIG_SYS_MAXARGS, 1, do_ut_dm, "", ""),
 #endif
diff --git a/test/common/Makefile b/test/common/Makefile
new file mode 100644 (file)
index 0000000..24c9145
--- /dev/null
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0+
+obj-y += cmd_ut_common.o
+obj-$(CONFIG_AUTOBOOT) += test_autoboot.o
diff --git a/test/common/cmd_ut_common.c b/test/common/cmd_ut_common.c
new file mode 100644 (file)
index 0000000..2c02678
--- /dev/null
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
+ * Copyright (c) 2021 Steffen Jaeckel <jaeckel-floss@eyet-services.de>
+ *
+ * Unit tests for common functions
+ */
+
+#include <common.h>
+#include <command.h>
+#include <test/common.h>
+#include <test/suites.h>
+#include <test/ut.h>
+
+int do_ut_common(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+       struct unit_test *tests = UNIT_TEST_SUITE_START(common_test);
+       const int n_ents = UNIT_TEST_SUITE_COUNT(common_test);
+
+       return cmd_ut_category("common", "common_test_", tests, n_ents, argc,
+                              argv);
+}
diff --git a/test/common/test_autoboot.c b/test/common/test_autoboot.c
new file mode 100644 (file)
index 0000000..6564ac7
--- /dev/null
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 Steffen Jaeckel
+ *
+ * Unit tests for autoboot functionality
+ */
+
+#include <autoboot.h>
+#include <common.h>
+#include <test/common.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+#include <crypt.h>
+
+static int check_for_input(struct unit_test_state *uts, const char *in,
+                          bool correct)
+{
+       /* The bootdelay is set to 1 second in test_autoboot() */
+       const char *autoboot_prompt =
+               "Enter password \"a\" in 1 seconds to stop autoboot";
+
+       console_record_reset_enable();
+       console_in_puts(in);
+       autoboot_command("echo Autoboot password unlock not successful");
+       ut_assert_nextline(autoboot_prompt);
+       if (!correct)
+               ut_assert_nextline("Autoboot password unlock not successful");
+       ut_assert_console_end();
+       return 0;
+}
+
+/**
+ * test_autoboot() - unit test for autoboot
+ *
+ * @uts:       unit test state
+ * Return:     0 = success, 1 = failure
+ */
+static int test_autoboot(struct unit_test_state *uts)
+{
+       /* make sure that the bootdelay is set to something,
+        * otherwise the called functions will time out
+        */
+       ut_assertok(env_set("bootdelay", "1"));
+       bootdelay_process();
+
+       /* unset all relevant environment variables */
+       env_set("bootstopusesha256", NULL);
+       env_set("bootstopkeycrypt", NULL);
+       env_set("bootstopkeysha256", NULL);
+
+       if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) {
+               /* test the default password from CONFIG_AUTOBOOT_STOP_STR_CRYPT */
+               ut_assertok(check_for_input(uts, "a\n", true));
+               /* test a password from the `bootstopkeycrypt` environment variable */
+               ut_assertok(env_set(
+                       "bootstopkeycrypt",
+                       "$5$rounds=640000$ycgRgpnRq4lmu.eb$aZ6YJWdklvyLML13w7mEHMHJnJOux6aptnp6VlsR5a9"));
+
+               ut_assertok(check_for_input(uts, "test\n", true));
+
+               /* verify that the `bootstopusesha256` variable is treated correctly */
+               ut_assertok(env_set("bootstopusesha256", "false"));
+               ut_assertok(check_for_input(uts, "test\n", true));
+       }
+
+       if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
+               /* test the `bootstopusesha256` and `bootstopkeysha256` features */
+               ut_assertok(env_set("bootstopusesha256", "true"));
+               ut_assertok(env_set(
+                       "bootstopkeysha256",
+                       "edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb"));
+
+               ut_assertok(check_for_input(uts, "abc\n", true));
+
+               ut_assertok(env_set(
+                       "bootstopkeysha256",
+                       "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
+
+               ut_assertok(check_for_input(uts, "abc", true));
+
+               ut_assertok(check_for_input(uts, "abc\n", true));
+
+               ut_assertok(check_for_input(uts, "abd", false));
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
+COMMON_TEST(test_autoboot, 0);