libsp: Add mock assert implementation

Assertion failures can be expected from tests and an exception-like
behavior is implemented to prevent executing code after a failed
assert.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I0ed0951baab40a449018d61926daded2f6a970ed
diff --git a/components/messaging/ffa/libsp/test/mock_assert.cpp b/components/messaging/ffa/libsp/test/mock_assert.cpp
new file mode 100644
index 0000000..f79021a
--- /dev/null
+++ b/components/messaging/ffa/libsp/test/mock_assert.cpp
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+ */
+
+#include "mock_assert.h"
+#include <assert.h>
+#include <CppUTest/TestHarness.h>
+#include <CppUTestExt/MockSupport.h>
+
+int expect_assert(assert_environment_t *env)
+{
+	mock().expectOneCall("__assert_fail").andReturnValue(env);
+	return 0;
+}
+
+void __assert_fail(const char *assertion, const char *file, unsigned int line,
+		   const char *function)
+{
+	(void)assertion;
+	(void)file;
+	(void)line;
+	(void)function;
+
+	assert_environment_t *env = (assert_environment_t *)mock()
+					    .actualCall("__assert_fail")
+					    .returnPointerValue();
+	longjmp(*env, 1);
+}
diff --git a/components/messaging/ffa/libsp/test/mock_assert.h b/components/messaging/ffa/libsp/test/mock_assert.h
new file mode 100644
index 0000000..33a03d5
--- /dev/null
+++ b/components/messaging/ffa/libsp/test/mock_assert.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+ */
+
+#ifndef LIBSP_TEST_MOCK_ASSERT_H_
+#define LIBSP_TEST_MOCK_ASSERT_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <setjmp.h>
+
+typedef jmp_buf assert_environment_t;
+
+/*
+ * SETUP_ASSERT_ENVIRONMENT
+ * Both expect_assert and setjmp must be called without putting them into a new
+ * function. The only way is to use an operator between them. Logical operators
+ * would introduce branches which could introduce uncovered branches. The
+ * solution is use arithmetic operators. expect_assert always return 0 so the
+ * value of the sum is determined by the setjmp return value.
+ *
+ * Example usage:
+ * assert_environment_t env;
+ *
+ * if (SETUP_ASSERT_ENVIRONMENT(env) {
+ *     function_with_assert_fail();
+ * }
+ */
+#define SETUP_ASSERT_ENVIRONMENT(env) (expect_assert(&env) + (setjmp(env) == 0))
+
+int expect_assert(assert_environment_t *env);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LIBSP_TEST_MOCK_ASSERT_H_ */
diff --git a/components/messaging/ffa/libsp/test/test_mock_assert.cpp b/components/messaging/ffa/libsp/test/test_mock_assert.cpp
new file mode 100644
index 0000000..39004d6
--- /dev/null
+++ b/components/messaging/ffa/libsp/test/test_mock_assert.cpp
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+ */
+
+#include <assert.h>
+#include <CppUTest/TestHarness.h>
+#include <CppUTestExt/MockSupport.h>
+#include "mock_assert.h"
+
+TEST_GROUP(mock_assert)
+{
+	TEST_TEARDOWN()
+	{
+		mock().checkExpectations();
+		mock().clear();
+	}
+};
+
+TEST(mock_assert, assert)
+{
+	assert_environment_t assert_env;
+
+	if (SETUP_ASSERT_ENVIRONMENT(assert_env)) {
+		assert(false);
+		FAIL("Assert jump not happened"); // Should not be called
+	}
+}
diff --git a/components/messaging/ffa/libsp/tests.cmake b/components/messaging/ffa/libsp/tests.cmake
new file mode 100644
index 0000000..f140b60
--- /dev/null
+++ b/components/messaging/ffa/libsp/tests.cmake
@@ -0,0 +1,19 @@
+#
+# Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+include(UnitTest)
+
+unit_test_add_suite(
+	NAME libsp_mock_assert
+	SOURCES
+		${CMAKE_CURRENT_LIST_DIR}/test/mock_assert.cpp
+		${CMAKE_CURRENT_LIST_DIR}/test/test_mock_assert.cpp
+	INCLUDE_DIRECTORIES
+		${CMAKE_CURRENT_LIST_DIR}/include/
+		${PROJECT_PATH}/components/common/utils/include
+	COMPILE_DEFINITIONS
+		-DARM64
+)