From: Mehmet Fide <[email protected]>

There is no test for the bootd command. Add one to the cmd suite that
covers the documented behaviour: bootd and its "boot" alias run the
command held in the bootcmd environment variable, and the return value of
bootd is the one of that command. Also assert the recursion guard in
cmd_process(), which is part of the code bootd carries.

Signed-off-by: Mehmet Fide <[email protected]>
---
 test/cmd/Makefile |  1 +
 test/cmd/bootd.c  | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 test/cmd/bootd.c

diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index 8d36463879d..e734933d5f7 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -17,6 +17,7 @@ ifdef CONFIG_CONSOLE_RECORD
 obj-$(CONFIG_CMD_ACPI) += acpi.o
 endif
 obj-$(CONFIG_CMD_BDI) += bdinfo.o
+obj-$(CONFIG_CMD_BOOTD) += bootd.o
 obj-$(CONFIG_CMD_CONFIG) += config.o
 obj-$(CONFIG_COREBOOT_SYSINFO) += coreboot.o
 obj-$(CONFIG_CMD_FDT) += fdt.o
diff --git a/test/cmd/bootd.c b/test/cmd/bootd.c
new file mode 100644
index 00000000000..be7ca49a972
--- /dev/null
+++ b/test/cmd/bootd.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for the bootd command
+ *
+ * Copyright 2026 Mehmet Fide <[email protected]>
+ */
+
+#include <command.h>
+#include <console.h>
+#include <env.h>
+#include <malloc.h>
+#include <test/cmd.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int cmd_bootd_test(struct unit_test_state *uts)
+{
+       char *const argv[] = { "bootd", NULL };
+       const char *old = env_get("bootcmd");
+       char *saved = NULL;
+       int repeatable = 0;
+
+       if (old) {
+               saved = strdup(old);
+               ut_assertnonnull(saved);
+       }
+
+       /* bootd runs the command held in bootcmd */
+       ut_assertok(env_set("bootcmd", "echo hello bootd"));
+       ut_assertok(run_command("bootd", 0));
+       ut_assert_nextline("hello bootd");
+       ut_assert_console_end();
+
+       /* and so does its "boot" alias */
+       ut_assertok(run_command("boot", 0));
+       ut_assert_nextline("hello bootd");
+       ut_assert_console_end();
+
+       /*
+        * The return value is the one of the command in bootcmd. An unknown
+        * command fails whichever parser is built in; they word the complaint
+        * differently, so match only its start.
+        */
+       ut_assertok(env_set("bootcmd", "no_such_command"));
+       ut_asserteq(1, run_command("bootd", 0));
+       ut_assert_nextlinen("Unknown command");
+       ut_assert_console_end();
+
+       /*
+        * A bootd reached from bootd is refused rather than recursing. The
+        * guard lives in cmd_process(), and run_command() drops its flag
+        * argument when the hush parser is used, so call cmd_process()
+        * directly. bootcmd would print if the guard let it through.
+        */
+       ut_assertok(env_set("bootcmd", "echo bootcmd must not run"));
+       ut_asserteq(CMD_RET_FAILURE,
+                   cmd_process(CMD_FLAG_BOOTD, 1, argv, &repeatable, NULL));
+       ut_assert_nextline("'bootd' recursion detected");
+       ut_assert_console_end();
+
+       ut_assertok(env_set("bootcmd", saved));
+       free(saved);
+
+       return 0;
+}
+CMD_TEST(cmd_bootd_test, UTF_CONSOLE);
-- 
2.54.0

Reply via email to