From 9a96de07b85c39aca55004b3ecbd7e15e99b99b6 Mon Sep 17 00:00:00 2001 From: Tomasz Gromadzki Date: Mon, 11 Mar 2024 20:53:49 +0100 Subject: [PATCH] test: UT for core_log_set_function() Signed-off-by: Tomasz Gromadzki --- src/test/core_log/TESTS.py | 4 +++ src/test/core_log/core_log.c | 41 ++++++++++++++++++++++++++--- src/test/core_log/core_log_common.c | 29 ++++++++++++++++++++ src/test/core_log/core_log_common.h | 5 ++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/test/core_log/TESTS.py b/src/test/core_log/TESTS.py index 8b509638e2c..c84ad1578e3 100755 --- a/src/test/core_log/TESTS.py +++ b/src/test/core_log/TESTS.py @@ -46,3 +46,7 @@ class TEST5(CORE_LOG): class TEST6(CORE_LOG): test_case = 'test_happy_day' + + +class TEST7(CORE_LOG): + test_case = 'test_set_custom_function' diff --git a/src/test/core_log/core_log.c b/src/test/core_log/core_log.c index 75aa59891f6..5801f02ef0c 100644 --- a/src/test/core_log/core_log.c +++ b/src/test/core_log/core_log.c @@ -2,7 +2,8 @@ /* Copyright 2024, Intel Corporation */ /* - * core_log.c -- unit test for core_log() and core_log_va() + * core_log.c -- unit test for core_log(), core_log_va() and + * core_log_set_function() */ #include @@ -172,11 +173,25 @@ test_level_gt_threshold(const struct test_case *tc, int argc, char *argv[]) } static int -test_happy_day(const struct test_case *tc, int argc, char *argv[]) +test_happy_day_helper(core_log_function *log_function) { /* Pass the message all the way to the logging function. */ core_log_set_threshold(CORE_LOG_THRESHOLD, CORE_LOG_LEVEL_ERROR); + /* + * Disable the validation as custom_log_function() may be called from + * core_log_set_function(). + */ + if (log_function == CORE_LOG_USE_DEFAULT_FUNCTION) { + FUNC_MOCK_RCOUNTER_SET(core_log_default_function, + NOT_VALIDATED_CALL); + } else { + FUNC_MOCK_RCOUNTER_SET(custom_log_function, + NOT_VALIDATED_CALL); + } + + core_log_set_function(log_function); + reset_mocks(); /* set the expectations */ @@ -195,11 +210,30 @@ test_happy_day(const struct test_case *tc, int argc, char *argv[]) UT_ASSERTeq(RCOUNTER(last_error_msg_get), CALLED); UT_ASSERTeq(RCOUNTER(vsnprintf), CALLED); UT_ASSERTeq(RCOUNTER(__xpg_strerror_r), CALLED); - UT_ASSERTeq(RCOUNTER(core_log_default_function), CALLED); + if (log_function == CORE_LOG_USE_DEFAULT_FUNCTION) { + UT_ASSERTeq(RCOUNTER(core_log_default_function), CALLED); + UT_ASSERTeq(RCOUNTER(custom_log_function), NOT_CALLED); + } else { + UT_ASSERTeq(RCOUNTER(core_log_default_function), NOT_CALLED); + UT_ASSERTeq(RCOUNTER(custom_log_function), CALLED); + } return NO_ARGS_CONSUMED; } +static int +test_happy_day(const struct test_case *tc, int argc, char *argv[]) +{ + return test_happy_day_helper(CORE_LOG_USE_DEFAULT_FUNCTION); +} + +/* Happy day scenario with custom logging function */ +static int +test_set_custom_function(const struct test_case *tc, int argc, char *argv[]) +{ + return test_happy_day_helper(custom_log_function); +} + static struct test_case test_cases[] = { TEST_CASE(test_CORE_LOG_LEVEL_ERROR_LAST), TEST_CASE(test_vsnprintf_fail), @@ -208,6 +242,7 @@ static struct test_case test_cases[] = { TEST_CASE(test_strerror_r_fail), TEST_CASE(test_level_gt_threshold), TEST_CASE(test_happy_day), + TEST_CASE(test_set_custom_function), }; #define NTESTS ARRAY_SIZE(test_cases) diff --git a/src/test/core_log/core_log_common.c b/src/test/core_log/core_log_common.c index 727ce318fbf..63c1f7c1651 100644 --- a/src/test/core_log/core_log_common.c +++ b/src/test/core_log/core_log_common.c @@ -84,6 +84,34 @@ FUNC_MOCK_RUN_DEFAULT { } FUNC_MOCK_END +FUNC_MOCK_NONSTATIC(custom_log_function, void, enum core_log_level level, + const char *file_name, const int line_no, const char *function_name, + const char *message) + FUNC_MOCK_RUN(VALIDATED_CALL) { + UT_ASSERTeq(level, Log_function_.exp_level); + UT_ASSERTstreq(file_name, FILE_NAME); + UT_ASSERTeq(line_no, LINE_NO); + UT_ASSERTstreq(function_name, FUNC_NAME); + if (Common.use_last_error_msg) { + UT_ASSERTeq(message, LAST_ERROR_MSG_MOCK); + } else { + UT_ASSERTne(message, LAST_ERROR_MSG_MOCK); + } + return; + } +FUNC_MOCK_RUN_DEFAULT { + _FUNC_REAL(custom_log_function)(level, file_name, line_no, + function_name, message); +} +FUNC_MOCK_END + +void +custom_log_function(enum core_log_level level, const char *file_name, + const int line_no, const char *function_name, const char *message) +{ + SUPPRESS_UNUSED(level, file_name, line_no, function_name, message); +} + /* helpers */ void @@ -93,6 +121,7 @@ reset_mocks(void) FUNC_MOCK_RCOUNTER_SET(vsnprintf, VALIDATED_CALL); FUNC_MOCK_RCOUNTER_SET(__xpg_strerror_r, VALIDATED_CALL); FUNC_MOCK_RCOUNTER_SET(core_log_default_function, VALIDATED_CALL); + FUNC_MOCK_RCOUNTER_SET(custom_log_function, VALIDATED_CALL); } void diff --git a/src/test/core_log/core_log_common.h b/src/test/core_log/core_log_common.h index b2f6909af27..c07c2384ef6 100644 --- a/src/test/core_log/core_log_common.h +++ b/src/test/core_log/core_log_common.h @@ -23,6 +23,7 @@ #define VALIDATED_CALL 127 #define NOT_CALLED VALIDATED_CALL #define CALLED (VALIDATED_CALL + 1) +#define NOT_VALIDATED_CALL 0 extern struct common_ctx { bool use_last_error_msg; @@ -49,6 +50,7 @@ FUNC_MOCK_EXTERN(last_error_msg_get); FUNC_MOCK_EXTERN(vsnprintf); FUNC_MOCK_EXTERN(__xpg_strerror_r); FUNC_MOCK_EXTERN(core_log_default_function); +FUNC_MOCK_EXTERN(custom_log_function); /* helpers */ @@ -60,3 +62,6 @@ void test_strerror_r_fail_helper(bool before_glibc_2_13); void test_log_function_call_helper(enum core_log_level level, bool call_log_function); + +void custom_log_function(enum core_log_level level, const char *file_name, + const int line_no, const char *function_name, const char *message);