summaryrefslogtreecommitdiff
path: root/test/common
diff options
context:
space:
mode:
Diffstat (limited to 'test/common')
-rw-r--r--test/common/Makefile6
-rw-r--r--test/common/cmd_ut_common.c21
-rw-r--r--test/common/cread.c105
-rw-r--r--test/common/cyclic.c34
-rw-r--r--test/common/event.c109
-rw-r--r--test/common/test_autoboot.c95
6 files changed, 370 insertions, 0 deletions
diff --git a/test/common/Makefile b/test/common/Makefile
new file mode 100644
index 00000000000..12c65f8c951
--- /dev/null
+++ b/test/common/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+obj-y += cmd_ut_common.o
+obj-$(CONFIG_AUTOBOOT) += test_autoboot.o
+obj-$(CONFIG_CYCLIC) += cyclic.o
+obj-$(CONFIG_EVENT_DYNAMIC) += event.o
+obj-y += cread.o
diff --git a/test/common/cmd_ut_common.c b/test/common/cmd_ut_common.c
new file mode 100644
index 00000000000..2f03a58af47
--- /dev/null
+++ b/test/common/cmd_ut_common.c
@@ -0,0 +1,21 @@
+// 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 <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/cread.c b/test/common/cread.c
new file mode 100644
index 00000000000..e159caed041
--- /dev/null
+++ b/test/common/cread.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2023 Google LLC
+ */
+
+#include <cli.h>
+#include <time.h>
+#include <test/common.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int cli_ch_test(struct unit_test_state *uts)
+{
+ struct cli_ch_state s_cch, *cch = &s_cch;
+
+ cli_ch_init(cch);
+
+ /* should be nothing to return at first */
+ ut_asserteq(0, cli_ch_process(cch, 0));
+
+ /* check normal entry */
+ ut_asserteq('a', cli_ch_process(cch, 'a'));
+ ut_asserteq('b', cli_ch_process(cch, 'b'));
+ ut_asserteq('c', cli_ch_process(cch, 'c'));
+ ut_asserteq(0, cli_ch_process(cch, 0));
+
+ /* send an invalid escape sequence */
+ ut_asserteq(0, cli_ch_process(cch, '\e'));
+ ut_asserteq(0, cli_ch_process(cch, '['));
+
+ /*
+ * with the next char it sees that the sequence is invalid, so starts
+ * emitting it
+ */
+ ut_asserteq('\e', cli_ch_process(cch, 'X'));
+
+ /* now we set 0 bytes to empty the buffer */
+ ut_asserteq('[', cli_ch_process(cch, 0));
+ ut_asserteq('X', cli_ch_process(cch, 0));
+ ut_asserteq(0, cli_ch_process(cch, 0));
+
+ /* things are normal again */
+ ut_asserteq('a', cli_ch_process(cch, 'a'));
+ ut_asserteq(0, cli_ch_process(cch, 0));
+
+ /* unexpected 'Esc' */
+ ut_asserteq('a', cli_ch_process(cch, 'a'));
+ ut_asserteq(0, cli_ch_process(cch, '\e'));
+ ut_asserteq('b', cli_ch_process(cch, 'b'));
+ ut_asserteq(0, cli_ch_process(cch, 0));
+
+ return 0;
+}
+COMMON_TEST(cli_ch_test, 0);
+
+static int cread_test(struct unit_test_state *uts)
+{
+ int duration;
+ ulong start;
+ char buf[10];
+
+ /*
+ * useful for debugging
+ *
+ * gd->flags &= ~GD_FLG_RECORD;
+ * print_buffer(0, buf, 1, 7, 0);
+ */
+
+ console_record_reset_enable();
+
+ /* simple input */
+ *buf = '\0';
+ ut_asserteq(4, console_in_puts("abc\n"));
+ ut_asserteq(3, cli_readline_into_buffer("-> ", buf, 1));
+ ut_asserteq_str("abc", buf);
+
+ /* try an escape sequence (cursor left after the 'c') */
+ *buf = '\0';
+ ut_asserteq(8, console_in_puts("abc\e[Dx\n"));
+ ut_asserteq(4, cli_readline_into_buffer("-> ", buf, 1));
+ ut_asserteq_str("abxc", buf);
+
+ /* invalid escape sequence */
+ *buf = '\0';
+ ut_asserteq(8, console_in_puts("abc\e[Xx\n"));
+ ut_asserteq(7, cli_readline_into_buffer("-> ", buf, 1));
+ ut_asserteq_str("abc\e[Xx", buf);
+
+ /* unexpected 'Esc' */
+ *buf = '\0';
+ ut_asserteq(7, console_in_puts("abc\eXx\n"));
+ ut_asserteq(5, cli_readline_into_buffer("-> ", buf, 1));
+ ut_asserteq_str("abcXx", buf);
+
+ /* check timeout, should be between 1000 and 1050ms */
+ start = get_timer(0);
+ *buf = '\0';
+ ut_asserteq(-2, cli_readline_into_buffer("-> ", buf, 1));
+ duration = get_timer(start) - 1000;
+ ut_assert(duration >= 0);
+ ut_assert(duration < 50);
+
+ return 0;
+}
+COMMON_TEST(cread_test, 0);
diff --git a/test/common/cyclic.c b/test/common/cyclic.c
new file mode 100644
index 00000000000..461f8cf91f4
--- /dev/null
+++ b/test/common/cyclic.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2022 Stefan Roese <sr@denx.de>
+ */
+
+#include <cyclic.h>
+#include <dm.h>
+#include <test/common.h>
+#include <test/test.h>
+#include <test/ut.h>
+#include <watchdog.h>
+#include <linux/delay.h>
+
+/* Test that cyclic function is called */
+static bool cyclic_active = false;
+
+static void cyclic_test(void *ctx)
+{
+ cyclic_active = true;
+}
+
+static int dm_test_cyclic_running(struct unit_test_state *uts)
+{
+ cyclic_active = false;
+ ut_assertnonnull(cyclic_register(cyclic_test, 10 * 1000, "cyclic_demo",
+ NULL));
+
+ /* Execute all registered cyclic functions */
+ schedule();
+ ut_asserteq(true, cyclic_active);
+
+ return 0;
+}
+COMMON_TEST(dm_test_cyclic_running, 0);
diff --git a/test/common/event.c b/test/common/event.c
new file mode 100644
index 00000000000..de433d34f22
--- /dev/null
+++ b/test/common/event.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Unit tests for event handling
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <dm.h>
+#include <event.h>
+#include <test/common.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+struct test_state {
+ struct udevice *dev;
+ int val;
+};
+
+static bool called;
+
+static int h_adder(void *ctx, struct event *event)
+{
+ struct event_data_test *data = &event->data.test;
+ struct test_state *test_state = ctx;
+
+ test_state->val += data->signal;
+
+ return 0;
+}
+
+static int h_adder_simple(void)
+{
+ called = true;
+
+ return 0;
+}
+EVENT_SPY_SIMPLE(EVT_TEST, h_adder_simple);
+
+static int test_event_base(struct unit_test_state *uts)
+{
+ struct test_state state;
+ int signal;
+
+ state.val = 12;
+ ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state));
+
+ signal = 17;
+
+ /* Check that the handler is called */
+ ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal)));
+ ut_asserteq(12 + 17, state.val);
+
+ return 0;
+}
+COMMON_TEST(test_event_base, 0);
+
+static int test_event_simple(struct unit_test_state *uts)
+{
+ called = false;
+
+ /* Check that the handler is called */
+ ut_assertok(event_notify_null(EVT_TEST));
+ ut_assert(called);
+
+ return 0;
+}
+COMMON_TEST(test_event_simple, 0);
+
+static int h_probe(void *ctx, struct event *event)
+{
+ struct test_state *test_state = ctx;
+
+ test_state->dev = event->data.dm.dev;
+ switch (event->type) {
+ case EVT_DM_PRE_PROBE:
+ test_state->val |= 1;
+ break;
+ case EVT_DM_POST_PROBE:
+ test_state->val |= 2;
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+static int test_event_probe(struct unit_test_state *uts)
+{
+ struct test_state state;
+ struct udevice *dev;
+
+ if (!IS_ENABLED(SANDBOX))
+ return -EAGAIN;
+
+ state.val = 0;
+ ut_assertok(event_register("pre", EVT_DM_PRE_PROBE, h_probe, &state));
+ ut_assertok(event_register("post", EVT_DM_POST_PROBE, h_probe, &state));
+
+ /* Probe a device */
+ ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
+
+ /* Check that the handler is called */
+ ut_asserteq(3, state.val);
+
+ return 0;
+}
+COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
diff --git a/test/common/test_autoboot.c b/test/common/test_autoboot.c
new file mode 100644
index 00000000000..4ba1dcc8091
--- /dev/null
+++ b/test/common/test_autoboot.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 Steffen Jaeckel
+ *
+ * Unit tests for autoboot functionality
+ */
+
+#include <autoboot.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)
+{
+ bool old_val;
+ /* 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);
+
+ /* turn on keyed autoboot for the test, if possible */
+ old_val = autoboot_set_keyed(true);
+ autoboot_command("echo Autoboot password unlock not successful");
+ old_val = autoboot_set_keyed(old_val);
+
+ 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);