summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Czapiga <jacz@semihalf.com>2021-03-25 14:28:29 +0100
committerPatrick Georgi <pgeorgi@google.com>2021-04-09 06:20:04 +0000
commit45d37d5cb807cd285b0277e4c235d0e5fae28093 (patch)
treebbf43d1a5d8c4bf65094cc1e0b99243503ad766a
parent70fe2707ba0d9e1e2f51c5ae7e813052ed044a81 (diff)
include/assert.h: Use mock_assert() for ENV_TEST targets
Some tests have to be able to catch assertion errors. Adding CMocka mock_assert() enables that. Additionally fix test_imd_create_tiered_empty(), test_full_stack() and test_incorrectly_initialized_stack() by adding missing expect_assert_failure(). Signed-off-by: Jakub Czapiga <jacz@semihalf.com> Change-Id: I5e8dd1b198ee6fab61e2be3f92baf1178f79bf18 Reviewed-on: https://review.coreboot.org/c/coreboot/+/51804 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r--src/include/assert.h14
-rw-r--r--tests/lib/imd-test.c7
-rw-r--r--tests/lib/stack-test.c4
3 files changed, 20 insertions, 5 deletions
diff --git a/src/include/assert.h b/src/include/assert.h
index 944c67768a..829e73207e 100644
--- a/src/include/assert.h
+++ b/src/include/assert.h
@@ -29,12 +29,24 @@
#define __build_time_assert(x) 0
#endif
+/* CMocka function redefinition. */
+void mock_assert(const int result, const char *const expression,
+ const char *const file, const int line);
+
+#if ENV_TEST
+#define MOCK_ASSERT(result, expression) \
+ mock_assert((result), (expression), __ASSERT_FILE__, __ASSERT_LINE__)
+#else
+#define MOCK_ASSERT(result, expression)
+#endif
+
/* GCC and CAR versions */
#define ASSERT(x) { \
if (!__build_time_assert(x) && !(x)) { \
printk(BIOS_EMERG, \
"ASSERTION ERROR: file '%s', line %d\n", \
__ASSERT_FILE__, __ASSERT_LINE__); \
+ MOCK_ASSERT(!!(x), #x); \
if (CONFIG(FATAL_ASSERTS)) \
hlt(); \
} \
@@ -45,6 +57,7 @@
"ASSERTION ERROR: file '%s', line %d\n", \
__ASSERT_FILE__, __ASSERT_LINE__); \
printk(BIOS_EMERG, "%s", msg); \
+ MOCK_ASSERT(!!(x), (msg)); \
if (CONFIG(FATAL_ASSERTS)) \
hlt(); \
} \
@@ -53,6 +66,7 @@
printk(BIOS_EMERG, \
"ERROR: BUG ENCOUNTERED at file '%s', line %d\n", \
__ASSERT_FILE__, __ASSERT_LINE__); \
+ MOCK_ASSERT(0, "BUG ENCOUNTERED"); \
if (CONFIG(FATAL_ASSERTS)) \
hlt(); \
}
diff --git a/tests/lib/imd-test.c b/tests/lib/imd-test.c
index dce542cf8f..c2bcf0cd2d 100644
--- a/tests/lib/imd-test.c
+++ b/tests/lib/imd-test.c
@@ -176,9 +176,10 @@ static void test_imd_create_tiered_empty(void **state)
/* Fail when large region doesn't have capacity for more than 1 entry */
lg_region_wrong_size = sizeof(struct imd_root_pointer) + sizeof(struct imd_root) +
sizeof(struct imd_entry);
- assert_int_equal(-1, imd_create_tiered_empty(&imd, lg_region_wrong_size,
- LG_ENTRY_ALIGN, SM_ROOT_SIZE,
- SM_ENTRY_ALIGN));
+ expect_assert_failure(
+ imd_create_tiered_empty(&imd, lg_region_wrong_size, LG_ENTRY_ALIGN,
+ SM_ROOT_SIZE, SM_ENTRY_ALIGN)
+ );
assert_int_equal(0, imd_create_tiered_empty(&imd, LG_ROOT_SIZE, LG_ENTRY_ALIGN,
SM_ROOT_SIZE, SM_ENTRY_ALIGN));
diff --git a/tests/lib/stack-test.c b/tests/lib/stack-test.c
index f494c086ad..bed59aaa76 100644
--- a/tests/lib/stack-test.c
+++ b/tests/lib/stack-test.c
@@ -73,7 +73,7 @@ static void test_full_stack(void **state)
/* Expect failure when checking full stack as absence of guard value at the end of
the stack indicates stack overrun. */
- assert_int_equal(-1, checkstack(top_of_stack, 0));
+ expect_assert_failure(checkstack(top_of_stack, 0));
}
static void test_partialy_filled_stack(void **state)
@@ -115,7 +115,7 @@ static void test_incorrectly_initialized_stack(void **state)
/* Expect failure when there is no last stack guard value even if no other value was
changed. */
- assert_int_equal(-1, checkstack(top_of_stack, 0));
+ expect_assert_failure(checkstack(top_of_stack, 0));
}
int main(void)