1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 16 * distribution. 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. 20 * 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. 32 */ 33 34 #ifndef SPDK_INTERNAL_MOCK_H 35 #define SPDK_INTERNAL_MOCK_H 36 37 #include "spdk/stdinc.h" 38 39 #define MOCK_STRUCT_INIT(...) \ 40 { __VA_ARGS__ } 41 42 #define DEFINE_RETURN_MOCK(fn, ret) \ 43 bool ut_ ## fn ## _mocked = false; \ 44 ret ut_ ## fn 45 46 /* 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. 50 */ 51 #define MOCK_SET(fn, val) \ 52 ut_ ## fn ## _mocked = true; \ 53 ut_ ## fn = val 54 55 #define MOCK_GET(fn) \ 56 ut_ ## fn 57 58 #define MOCK_CLEAR(fn) \ 59 ut_ ## fn ## _mocked = false 60 61 #define MOCK_CLEAR_P(fn) \ 62 ut_ ## fn ## _mocked = false; \ 63 ut_ ## fn = NULL 64 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) 68 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 74 75 /* 76 * For defining the implementation of wrappers for syscalls. 77 * Avoid nested macro calls to prevent macro expansion of fn. 78 */ 79 #define DEFINE_WRAPPER(fn, ret, dargs, pargs) \ 80 bool ut_ ## fn ## _mocked = false; \ 81 ret ut_ ## fn; \ 82 __attribute__((used)) ret __wrap_ ## fn dargs \ 83 { \ 84 if (!ut_ ## fn ## _mocked) { \ 85 return __real_ ## fn pargs; \ 86 } else { \ 87 return ut_ ## fn; \ 88 } \ 89 } 90 91 /* DEFINE_STUB is for defining the implmentation of stubs for SPDK funcs. */ 92 #define DEFINE_STUB(fn, ret, dargs, val) \ 93 bool ut_ ## fn ## _mocked = true; \ 94 ret ut_ ## fn = val; \ 95 ret fn dargs; \ 96 ret fn dargs \ 97 { \ 98 return MOCK_GET(fn); \ 99 } 100 101 /* DEFINE_STUB_V macro is for stubs that don't have a return value */ 102 #define DEFINE_STUB_V(fn, dargs) \ 103 void fn dargs; \ 104 void fn dargs \ 105 { \ 106 } 107 108 #define HANDLE_RETURN_MOCK(fn) \ 109 if (ut_ ## fn ## _mocked) { \ 110 return ut_ ## fn; \ 111 } 112 113 114 /* declare wrapper protos (alphabetically please) here */ 115 DECLARE_WRAPPER(calloc, void *, (size_t nmemb, size_t size)); 116 117 DECLARE_WRAPPER(pthread_mutex_init, int, 118 (pthread_mutex_t *mtx, const pthread_mutexattr_t *attr)); 119 120 DECLARE_WRAPPER(pthread_mutexattr_init, int, 121 (pthread_mutexattr_t *attr)); 122 123 DECLARE_WRAPPER(recvmsg, ssize_t, (int sockfd, struct msghdr *msg, int flags)); 124 125 DECLARE_WRAPPER(sendmsg, ssize_t, (int sockfd, const struct msghdr *msg, int flags)); 126 127 DECLARE_WRAPPER(writev, ssize_t, (int fd, const struct iovec *iov, int iovcnt)); 128 129 /* unlink is done a bit differently. */ 130 extern char *g_unlink_path; 131 extern void (*g_unlink_callback)(void); 132 /* If g_unlink_path is NULL, __wrap_unlink will return ENOENT. 133 * If the __wrap_unlink() parameter does not match g_unlink_path, it will return ENOENT. 134 * If g_unlink_path does match, and g_unlink_callback has been set, g_unlink_callback will 135 * be called before returning 0. 136 */ 137 int __wrap_unlink(const char *path); 138 139 #endif /* SPDK_INTERNAL_MOCK_H */ 140