diff options
Diffstat (limited to '')
-rw-r--r-- | lib/common/mock.c | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/lib/common/mock.c b/lib/common/mock.c index 6f837ad..43c6e8f 100644 --- a/lib/common/mock.c +++ b/lib/common/mock.c @@ -1,5 +1,5 @@ /* - * Copyright 2021-2023 the Pacemaker project contributors + * Copyright 2021-2024 the Pacemaker project contributors * * The version control history for this file may have further details. * @@ -25,6 +25,7 @@ #include <grp.h> #include <cmocka.h> +#include <crm/common/unittest_internal.h> #include "mock_private.h" /* This file is only used when running "make check". It is built into @@ -54,6 +55,27 @@ */ // LCOV_EXCL_START + +/* abort() + * + * Always mock abort - there's no pcmk__mock_abort tuneable to control this. + * Because abort calls _exit(), which doesn't run any of the things registered + * with atexit(), coverage numbers do not get written out. This most noticably + * affects places where we are testing that things abort when they should. + * + * The solution is this wrapper that is always enabled when we are running + * unit tests (mock.c does not get included for the regular libcrmcommon.so). + * All it does is dump coverage data and call the real abort(). + */ +_Noreturn void +__wrap_abort(void) +{ +#if (PCMK__WITH_COVERAGE == 1) + __gcov_dump(); +#endif + __real_abort(); +} + /* calloc() * * If pcmk__mock_calloc is set to true, later calls to calloc() will return @@ -103,6 +125,31 @@ __wrap_getenv(const char *name) } +/* realloc() + * + * If pcmk__mock_realloc is set to true, later calls to realloc() will return + * NULL and must be preceded by: + * + * expect_*(__wrap_realloc, ptr[, ...]); + * expect_*(__wrap_realloc, size[, ...]); + * + * expect_* functions: https://api.cmocka.org/group__cmocka__param.html + */ + +bool pcmk__mock_realloc = false; + +void * +__wrap_realloc(void *ptr, size_t size) +{ + if (!pcmk__mock_realloc) { + return __real_realloc(ptr, size); + } + check_expected_ptr(ptr); + check_expected(size); + return NULL; +} + + /* setenv() * * If pcmk__mock_setenv is set to true, later calls to setenv() must be preceded @@ -412,40 +459,4 @@ __wrap_strdup(const char *s) return NULL; } - -/* uname() - * - * If pcmk__mock_uname is set to true, later calls to uname() must be preceded - * by: - * - * expect_*(__wrap_uname, buf[, ...]); - * will_return(__wrap_uname, return_value); - * will_return(__wrap_uname, node_name_for_buf_parameter_to_uname); - * - * expect_* functions: https://api.cmocka.org/group__cmocka__param.html - */ - -bool pcmk__mock_uname = false; - -int -__wrap_uname(struct utsname *buf) -{ - if (pcmk__mock_uname) { - int retval = 0; - char *result = NULL; - - check_expected_ptr(buf); - retval = mock_type(int); - result = mock_ptr_type(char *); - - if (result != NULL) { - strcpy(buf->nodename, result); - } - return retval; - - } else { - return __real_uname(buf); - } -} - // LCOV_EXCL_STOP |