Add mocks for common debug features
tf_log and do_panic calls can be tested through the functions of
common/mock_debug.h. The console driver dependency has been removed.
Change-Id: I3a0da45c851aee4ae7a8599e21816237d9932e72
Signed-off-by: Imre Kis <imre.kis@arm.com>
diff --git a/mocks/common/debug.cpp b/mocks/common/debug.cpp
new file mode 100644
index 0000000..4a9d7f2
--- /dev/null
+++ b/mocks/common/debug.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <stdarg.h>
+#include <assert.h>
+#include <stdio.h>
+#include <setjmp.h>
+extern "C" {
+#include <common/debug.h>
+}
+
+#include "CppUTestExt/MockSupport.h"
+#include "common/mock_debug.h"
+
+/* Set the default maximum log level to the `LOG_LEVEL` build flag */
+static unsigned int max_log_level = LOG_LEVEL_VERBOSE;
+
+int expect_panic(panic_environment_t *env) {
+ mock("debug").expectOneCall("do_panic").andReturnValue(env);
+ return 1;
+}
+
+void expect_tf_log(const char *str) {
+ mock().expectOneCall("tf_log").withStringParameter("str", str);
+}
+
+extern "C" {
+void do_panic(void) {
+ panic_environment_t *env = (panic_environment_t *)mock("debug").actualCall("do_panic").returnPointerValue();
+ longjmp(*env, 1);
+}
+
+void tf_log(const char *fmt, ...)
+{
+ unsigned int log_level;
+ va_list args;
+ const char *prefix_str;
+
+ /* We expect the LOG_MARKER_* macro as the first character */
+ log_level = fmt[0];
+
+ /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
+ assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
+ assert((log_level % 10U) == 0U);
+
+ if (log_level > max_log_level)
+ return;
+
+ char log_buffer[1024];
+ va_start(args, fmt);
+ (void)vsnprintf(log_buffer, sizeof(log_buffer), fmt + 1, args);
+ va_end(args);
+
+ mock().actualCall("tf_log").withStringParameter("str", log_buffer);
+}
+
+/*
+ * The helper function to set the log level dynamically by platform. The
+ * maximum log level is determined by `LOG_LEVEL` build flag at compile time
+ * and this helper can set a lower (or equal) log level than the one at compile.
+ */
+void tf_log_set_max_level(unsigned int log_level)
+{
+ assert(log_level <= LOG_LEVEL_VERBOSE);
+ assert((log_level % 10U) == 0U);
+
+ /* Cap log_level to the compile time maximum. */
+ if (log_level <= (unsigned int)LOG_LEVEL_VERBOSE)
+ max_log_level = log_level;
+}
+} // extern "C"
+
diff --git a/mocks/common/tf_log.c b/mocks/common/tf_log.c
deleted file mode 100644
index 08d3cf4..0000000
--- a/mocks/common/tf_log.c
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <stdarg.h>
-#include <assert.h>
-#include <stdio.h>
-
-#include <common/debug.h>
-#include <plat/common/platform.h>
-
-/* Set the default maximum log level to the `LOG_LEVEL` build flag */
-static unsigned int max_log_level = LOG_LEVEL;
-
-/*
- * The common log function which is invoked by TF-A code.
- * This function should not be directly invoked and is meant to be
- * only used by the log macros defined in debug.h. The function
- * expects the first character in the format string to be one of the
- * LOG_MARKER_* macros defined in debug.h.
- */
-void tf_log(const char *fmt, ...)
-{
- unsigned int log_level;
- va_list args;
- const char *prefix_str;
-
- /* We expect the LOG_MARKER_* macro as the first character */
- log_level = fmt[0];
-
- /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
- assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
- assert((log_level % 10U) == 0U);
-
- if (log_level > max_log_level)
- return;
-
- prefix_str = plat_log_get_prefix(log_level);
-
- while (*prefix_str != '\0') {
- (void)putchar(*prefix_str);
- prefix_str++;
- }
-
- va_start(args, fmt);
- (void)vprintf(fmt + 1, args);
- va_end(args);
-}
-
-/*
- * The helper function to set the log level dynamically by platform. The
- * maximum log level is determined by `LOG_LEVEL` build flag at compile time
- * and this helper can set a lower (or equal) log level than the one at compile.
- */
-void tf_log_set_max_level(unsigned int log_level)
-{
- assert(log_level <= LOG_LEVEL_VERBOSE);
- assert((log_level % 10U) == 0U);
-
- /* Cap log_level to the compile time maximum. */
- if (log_level <= (unsigned int)LOG_LEVEL)
- max_log_level = log_level;
-}
diff --git a/mocks/include/common/debug.h b/mocks/include/common/debug.h
index 245e698..6ee625d 100644
--- a/mocks/include/common/debug.h
+++ b/mocks/include/common/debug.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
@@ -34,8 +34,6 @@
#include <stdbool.h>
#include <stdio.h>
-#include <drivers/console.h>
-
/*
* Define Log Markers corresponding to each log level which will
* be embedded in the format string and is expected by tf_log() to determine
@@ -100,7 +98,6 @@
#define panic() \
do { \
backtrace(__func__); \
- (void)console_flush(); \
do_panic(); \
} while (false)
diff --git a/mocks/include/common/mock_debug.h b/mocks/include/common/mock_debug.h
new file mode 100644
index 0000000..cf6f711
--- /dev/null
+++ b/mocks/include/common/mock_debug.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <setjmp.h>
+
+typedef jmp_buf panic_environment_t;
+
+#define SETUP_PANIC_ENVIRONMENT(env) (expect_panic(&env) && (setjmp(env) == 0))
+
+int expect_panic(panic_environment_t *env);
+void expect_tf_log(const char *str);