]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk_internal/mock.h
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef SPDK_INTERNAL_MOCK_H
35 #define SPDK_INTERNAL_MOCK_H
37 #include "spdk/stdinc.h"
39 #define MOCK_STRUCT_INIT(...) \
42 #define DEFINE_RETURN_MOCK(fn, ret) \
43 bool ut_ ## fn ## _mocked = false; \
47 * For controlling mocked function behavior, setting
48 * and getting values from the stub, the _P macros are
49 * for mocking functions that return pointer values.
51 #define MOCK_SET(fn, val) \
52 ut_ ## fn ## _mocked = true; \
55 #define MOCK_GET(fn) \
58 #define MOCK_CLEAR(fn) \
59 ut_ ## fn ## _mocked = false
61 #define MOCK_CLEAR_P(fn) \
62 ut_ ## fn ## _mocked = false; \
65 /* for proving to *certain* static analysis tools that we didn't reset the mock function. */
66 #define MOCK_CLEARED_ASSERT(fn) \
67 SPDK_CU_ASSERT_FATAL(ut_ ## fn ## _mocked == false)
69 /* for declaring function protoypes for wrappers */
70 #define DECLARE_WRAPPER(fn, ret, args) \
71 extern bool ut_ ## fn ## _mocked; \
72 extern ret ut_ ## fn; \
73 ret __wrap_ ## fn args; ret __real_ ## fn args
75 /* for defining the implmentation of wrappers for syscalls */
76 #define DEFINE_WRAPPER(fn, ret, dargs, pargs) \
77 DEFINE_RETURN_MOCK(fn, ret); \
78 __attribute__((used)) ret __wrap_ ## fn dargs \
80 if (!ut_ ## fn ## _mocked) { \
81 return __real_ ## fn pargs; \
83 return MOCK_GET(fn); \
87 /* DEFINE_STUB is for defining the implmentation of stubs for SPDK funcs. */
88 #define DEFINE_STUB(fn, ret, dargs, val) \
89 bool ut_ ## fn ## _mocked = true; \
90 ret ut_ ## fn = val; \
94 return MOCK_GET(fn); \
97 /* DEFINE_STUB_V macro is for stubs that don't have a return value */
98 #define DEFINE_STUB_V(fn, dargs) \
104 #define HANDLE_RETURN_MOCK(fn) \
105 if (ut_ ## fn ## _mocked) { \
110 /* declare wrapper protos (alphabetically please) here */
111 DECLARE_WRAPPER(calloc
, void *, (size_t nmemb
, size_t size
));
113 DECLARE_WRAPPER(pthread_mutex_init
, int,
114 (pthread_mutex_t
*mtx
, const pthread_mutexattr_t
*attr
));
116 DECLARE_WRAPPER(pthread_mutexattr_init
, int,
117 (pthread_mutexattr_t
*attr
));
119 DECLARE_WRAPPER(recvmsg
, ssize_t
, (int sockfd
, struct msghdr
*msg
, int flags
));
121 DECLARE_WRAPPER(sendmsg
, ssize_t
, (int sockfd
, const struct msghdr
*msg
, int flags
));
123 DECLARE_WRAPPER(writev
, ssize_t
, (int fd
, const struct iovec
*iov
, int iovcnt
));
125 /* unlink is done a bit differently. */
126 extern char *g_unlink_path
;
127 extern void (*g_unlink_callback
)(void);
128 /* If g_unlink_path is NULL, __wrap_unlink will return ENOENT.
129 * If the __wrap_unlink() parameter does not match g_unlink_path, it will return ENOENT.
130 * If g_unlink_path does match, and g_unlink_callback has been set, g_unlink_callback will
131 * be called before returning 0.
133 int __wrap_unlink(const char *path
);
135 #endif /* SPDK_INTERNAL_MOCK_H */