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 /* used to signify pass through */ 40 #define MOCK_PASS_THRU (0xdeadbeef) 41 #define MOCK_PASS_THRU_P (void*)0xdeadbeef 42 /* helper for initializing struct value with mock macros */ 43 #define MOCK_STRUCT_INIT(...) \ 44 { __VA_ARGS__ } 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, ret, val) \ 52 ut_ ## fn = (ret)val 53 54 #define MOCK_SET_P(fn, ret, val) \ 55 ut_p_ ## fn = (ret)val 56 57 #define MOCK_GET(fn) \ 58 ut_ ## fn 59 60 #define MOCK_GET_P(fn) \ 61 ut_p_ ## fn 62 63 /* for declaring function protoypes for wrappers */ 64 #define DECLARE_WRAPPER(fn, ret, args) \ 65 extern ret ut_ ## fn; \ 66 ret __wrap_ ## fn args; ret __real_ ## fn args; 67 68 /* for defining the implmentation of wrappers for syscalls */ 69 #define DEFINE_WRAPPER(fn, ret, dargs, pargs, val) \ 70 ret ut_ ## fn = val; \ 71 ret __wrap_ ## fn dargs \ 72 { \ 73 if (ut_ ## fn == (ret)MOCK_PASS_THRU) { \ 74 return __real_ ## fn pargs; \ 75 } else { \ 76 return MOCK_GET(fn); \ 77 } \ 78 } 79 80 /* DEFINE_STUB is for defining the implmentation of stubs for SPDK funcs. */ 81 #define DEFINE_STUB(fn, ret, dargs, val) \ 82 ret ut_ ## fn = val; \ 83 ret fn dargs; \ 84 ret fn dargs \ 85 { \ 86 return MOCK_GET(fn); \ 87 } 88 89 /* DEFINE_STUB_P macro is for stubs that return pointer values */ 90 #define DEFINE_STUB_P(fn, ret, dargs, val) \ 91 ret ut_ ## fn = val; \ 92 ret* ut_p_ ## fn = &(ut_ ## fn); \ 93 ret* fn dargs; \ 94 ret* fn dargs \ 95 { \ 96 return MOCK_GET_P(fn); \ 97 } 98 99 /* DEFINE_STUB_V macro is for stubs that don't have a return value */ 100 #define DEFINE_STUB_V(fn, dargs) \ 101 void fn dargs; \ 102 void fn dargs \ 103 { \ 104 } 105 106 /* DEFINE_STUB_VP macro is for stubs that return void pointer values */ 107 #define DEFINE_STUB_VP(fn, dargs, val) \ 108 void* ut_p_ ## fn = val; \ 109 void* fn dargs; \ 110 void* fn dargs \ 111 { \ 112 return MOCK_GET_P(fn); \ 113 } 114 115 /* declare wrapper protos (alphabetically please) here */ 116 DECLARE_WRAPPER(calloc, void *, (size_t nmemb, size_t size)); 117 118 DECLARE_WRAPPER(pthread_mutex_init, int, 119 (pthread_mutex_t *mtx, const pthread_mutexattr_t *attr)); 120 121 DECLARE_WRAPPER(pthread_mutexattr_init, int, 122 (pthread_mutexattr_t *attr)); 123 124 DECLARE_WRAPPER(pthread_self, pthread_t, (void)); 125 126 #endif /* SPDK_INTERNAL_MOCK_H */ 127