tests/console: Add tests for log message routing behavior

Change-Id: Id978cfe4fa45fef9edbc3d3b55606ff6973521c5
Signed-off-by: Patrick Georgi <pgeorgi@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/55356
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Jakub Czapiga <jacz@semihalf.com>
diff --git a/src/console/init.c b/src/console/init.c
index a2ceb58..6fa4849 100644
--- a/src/console/init.c
+++ b/src/console/init.c
@@ -13,7 +13,8 @@
 static int console_inited;
 static int console_loglevel;
 
-static inline int get_log_level(void)
+int get_log_level(void);
+int get_log_level(void)
 {
 	if (console_inited == 0)
 		return -1;
diff --git a/tests/console/Makefile.inc b/tests/console/Makefile.inc
new file mode 100644
index 0000000..2218652
--- /dev/null
+++ b/tests/console/Makefile.inc
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+tests-y += routing-with-cbmemcons-test
+tests-y += routing-without-cbmemcons-test
+
+routing-with-cbmemcons-test-srcs += tests/console/routing-test.c
+routing-with-cbmemcons-test-srcs += src/console/init.c
+routing-with-cbmemcons-test-config += CONFIG_CONSOLE_CBMEM=1
+routing-with-cbmemcons-test-mocks += get_log_level
+
+routing-without-cbmemcons-test-srcs += tests/console/routing-test.c
+routing-without-cbmemcons-test-srcs += src/console/init.c
+routing-without-cbmemcons-test-config += CONFIG_CONSOLE_CBMEM=0
+routing-without-cbmemcons-test-mocks += get_log_level
diff --git a/tests/console/routing-test.c b/tests/console/routing-test.c
new file mode 100644
index 0000000..33bf167
--- /dev/null
+++ b/tests/console/routing-test.c
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <console/console.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <tests/test.h>
+
+/* stub */
+static int log_level = 0;
+int get_log_level(void)
+{
+	return log_level;
+}
+
+struct log_combinations_t {
+	int log_lvl;
+	int msg_lvl;
+	int behavior;
+} combinations[] = {
+	{.log_lvl = -1, .msg_lvl = BIOS_ERR, .behavior = CONSOLE_LOG_NONE},
+	{.log_lvl = -1, .msg_lvl = BIOS_SPEW, .behavior = CONSOLE_LOG_NONE},
+
+	{.log_lvl = BIOS_DEBUG, .msg_lvl = BIOS_ERR, .behavior = CONSOLE_LOG_ALL},
+	{.log_lvl = BIOS_DEBUG, .msg_lvl = BIOS_DEBUG, .behavior = CONSOLE_LOG_ALL},
+	{.log_lvl = BIOS_DEBUG, .msg_lvl = BIOS_SPEW, .behavior = CONSOLE_LOG_NONE},
+
+	{.log_lvl = BIOS_SPEW, .msg_lvl = BIOS_ERR, .behavior = CONSOLE_LOG_ALL},
+	{.log_lvl = BIOS_SPEW, .msg_lvl = BIOS_DEBUG, .behavior = CONSOLE_LOG_ALL},
+	{.log_lvl = BIOS_SPEW, .msg_lvl = BIOS_SPEW, .behavior = CONSOLE_LOG_ALL},
+
+#if CONFIG(CONSOLE_CBMEM)
+	{.log_lvl = BIOS_WARNING, .msg_lvl = BIOS_ERR, .behavior = CONSOLE_LOG_ALL},
+	{.log_lvl = BIOS_WARNING, .msg_lvl = BIOS_DEBUG, .behavior = CONSOLE_LOG_FAST},
+	{.log_lvl = BIOS_WARNING, .msg_lvl = BIOS_SPEW, .behavior = CONSOLE_LOG_NONE},
+
+#else
+	{.log_lvl = BIOS_WARNING, .msg_lvl = BIOS_ERR, .behavior = CONSOLE_LOG_ALL},
+	{.log_lvl = BIOS_WARNING, .msg_lvl = BIOS_DEBUG, .behavior = CONSOLE_LOG_NONE},
+	{.log_lvl = BIOS_WARNING, .msg_lvl = BIOS_SPEW, .behavior = CONSOLE_LOG_NONE},
+#endif
+};
+
+
+static void test_console_log_level(void **state)
+{
+	for (int i = 0; i < ARRAY_SIZE(combinations); i++) {
+		log_level = combinations[i].log_lvl;
+		assert_int_equal(combinations[i].behavior,
+			console_log_level(combinations[i].msg_lvl));
+	}
+}
+
+int main(void)
+{
+	const struct CMUnitTest tests[] = {
+		cmocka_unit_test(test_console_log_level),
+	};
+
+	return cmocka_run_group_tests(tests, NULL, NULL);
+}