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 /* 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 \ 79 { \ 80 if (!ut_ ## fn ## _mocked) { \ 81 return __real_ ## fn pargs; \ 82 } else { \ 83 return MOCK_GET(fn); \ 84 } \ 85 } 86 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; \ 91 ret fn dargs; \ 92 ret fn dargs \ 93 { \ 94 return MOCK_GET(fn); \ 95 } 96 97 /* DEFINE_STUB_V macro is for stubs that don't have a return value */ 98 #define DEFINE_STUB_V(fn, dargs) \ 99 void fn dargs; \ 100 void fn dargs \ 101 { \ 102 } 103 104 #define HANDLE_RETURN_MOCK(fn) \ 105 if (ut_ ## fn ## _mocked) { \ 106 return ut_ ## fn; \ 107 } 108 109 110 /* declare wrapper protos (alphabetically please) here */ 111 DECLARE_WRAPPER(calloc, void *, (size_t nmemb, size_t size)); 112 113 DECLARE_WRAPPER(pthread_mutex_init, int, 114 (pthread_mutex_t *mtx, const pthread_mutexattr_t *attr)); 115 116 DECLARE_WRAPPER(pthread_mutexattr_init, int, 117 (pthread_mutexattr_t *attr)); 118 119 #endif /* SPDK_INTERNAL_MOCK_H */ 120