summaryrefslogtreecommitdiffstats
path: root/lib/common/mock.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-03 13:39:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-03 13:39:29 +0000
commitb41961d74fe7ff2d4d4abaca92454e87c561e49f (patch)
treeb34e3826a7b649dafdbd05081140c990c96d736d /lib/common/mock.c
parentReleasing progress-linux version 2.1.7-1~progress7.99u1. (diff)
downloadpacemaker-b41961d74fe7ff2d4d4abaca92454e87c561e49f.tar.xz
pacemaker-b41961d74fe7ff2d4d4abaca92454e87c561e49f.zip
Merging upstream version 2.1.8~rc1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/common/mock.c')
-rw-r--r--lib/common/mock.c85
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