|
| 1 | +// Copyright 2020 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef RCUTILS__TESTING__FAULT_INJECTION_H_ |
| 16 | +#define RCUTILS__TESTING__FAULT_INJECTION_H_ |
| 17 | +#include <stdbool.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdint.h> |
| 20 | + |
| 21 | +#include "rcutils/macros.h" |
| 22 | +#include "rcutils/visibility_control.h" |
| 23 | + |
| 24 | +#ifdef __cplusplus |
| 25 | +extern "C" |
| 26 | +{ |
| 27 | +#endif |
| 28 | + |
| 29 | +#define RCUTILS_FAULT_INJECTION_NEVER_FAIL -1 |
| 30 | + |
| 31 | +#define RCUTILS_FAULT_INJECTION_FAIL_NOW 0 |
| 32 | + |
| 33 | +RCUTILS_PUBLIC |
| 34 | +RCUTILS_WARN_UNUSED |
| 35 | +bool |
| 36 | +rcutils_fault_injection_is_test_complete(void); |
| 37 | + |
| 38 | +/** |
| 39 | + * \brief Atomically set the fault injection counter. |
| 40 | + * |
| 41 | + * This is typically not the preferred method of interacting directly with the fault injection |
| 42 | + * logic, instead use `RCUTILS_FAULT_INJECTION_TEST` instead. |
| 43 | + * |
| 44 | + * This function may also be used for pausing code inside of a `RCUTILS_FAULT_INJECTION_TEST` with |
| 45 | + * something like the following: |
| 46 | + * |
| 47 | + * RCUTILS_FAULT_INJECTION_TEST({ |
| 48 | + * ... // code to run with fault injection |
| 49 | + * int64_t count = rcutils_fault_injection_get_count(); |
| 50 | + * rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); |
| 51 | + * ... // code to run without fault injection |
| 52 | + * rcutils_fault_injection_set_count(count); |
| 53 | + * ... // code to run with fault injection |
| 54 | + * }); |
| 55 | + * |
| 56 | + * \param count The count to set the fault injection counter to. If count is negative, then fault |
| 57 | + * injection errors will be disabled. The counter is globally initialized to |
| 58 | + * RCUTILS_FAULT_INJECTION_NEVER_FAIL. |
| 59 | + */ |
| 60 | +RCUTILS_PUBLIC |
| 61 | +void |
| 62 | +rcutils_fault_injection_set_count(int count); |
| 63 | + |
| 64 | +/** |
| 65 | + * \brief Atomically get the fault injection counter value |
| 66 | + * |
| 67 | + * This function is typically not used directly but instead indirectly inside an |
| 68 | + * `RCUTILS_FAULT_INJECTION_TEST` |
| 69 | + */ |
| 70 | +RCUTILS_PUBLIC |
| 71 | +RCUTILS_WARN_UNUSED |
| 72 | +int_least64_t |
| 73 | +rcutils_fault_injection_get_count(void); |
| 74 | + |
| 75 | +/** |
| 76 | + * \brief Implementation of fault injection decrementer |
| 77 | + * |
| 78 | + * This is included inside of macros, so it needs to be exported as a public function, but it |
| 79 | + * should not be used directly. |
| 80 | + */ |
| 81 | +RCUTILS_PUBLIC |
| 82 | +RCUTILS_WARN_UNUSED |
| 83 | +int_least64_t |
| 84 | +_rcutils_fault_injection_maybe_fail(void); |
| 85 | + |
| 86 | +/** |
| 87 | + * \def RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR |
| 88 | + * \brief This macro checks and decrements a static global variable atomic counter and returns |
| 89 | + * `return_value_on_error` if 0. |
| 90 | + * |
| 91 | + * This macro is not a function itself, so when this macro returns it will cause the calling |
| 92 | + * function to return with the return value. |
| 93 | + * |
| 94 | + * Set the counter with `RCUTILS_FAULT_INJECTION_SET_COUNT`. If the count is less than 0, then |
| 95 | + * `RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR` will not cause an early return. |
| 96 | + * |
| 97 | + * This macro is thread-safe, and ensures that at most one invocation results in a failure for each |
| 98 | + * time the fault injection counter is set with `RCUTILS_FAULT_INJECTION_SET_COUNT` |
| 99 | + * |
| 100 | + * \param return_value_on_error the value to return in the case of fault injected failure. |
| 101 | + */ |
| 102 | +#define RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR(return_value_on_error) \ |
| 103 | + if (RCUTILS_FAULT_INJECTION_FAIL_NOW == _rcutils_fault_injection_maybe_fail()) { \ |
| 104 | + printf( \ |
| 105 | + "%s:%d Injecting fault and returning " #return_value_on_error "\n", __FILE__, __LINE__); \ |
| 106 | + return return_value_on_error; \ |
| 107 | + } |
| 108 | + |
| 109 | +/** |
| 110 | + * \def RCUTILS_FAULT_INJECTION_MAYBE_FAIL |
| 111 | + * \brief This macro checks and decrements a static global variable atomic counter and executes |
| 112 | + * `failure_code` if the counter is 0 inside a scoped block (any variables declared in |
| 113 | + * failure_code) will not be avaliable outside of this scoped block. |
| 114 | + * |
| 115 | + * This macro is not a function itself, so it will cause the calling function to execute the code |
| 116 | + * from within an if loop. |
| 117 | + * |
| 118 | + * Set the counter with `RCUTILS_FAULT_INJECTION_SET_COUNT`. If the count is less than 0, then |
| 119 | + * `RCUTILS_FAULT_INJECTION_MAYBE_FAIL` will not execute the failure code. |
| 120 | + * |
| 121 | + * This macro is thread-safe, and ensures that at most one invocation results in a failure for each |
| 122 | + * time the fault injection counter is set with `RCUTILS_FAULT_INJECTION_SET_COUNT` |
| 123 | + * |
| 124 | + * \param failure_code the code to execute in the case of fault injected failure. |
| 125 | + */ |
| 126 | +#define RCUTILS_FAULT_INJECTION_MAYBE_FAIL(failure_code) \ |
| 127 | + if (RCUTILS_FAULT_INJECTION_FAIL_NOW == _rcutils_fault_injection_maybe_fail()) { \ |
| 128 | + printf( \ |
| 129 | + "%s:%d Injecting fault and executing " #failure_code "\n", __FILE__, __LINE__); \ |
| 130 | + failure_code; \ |
| 131 | + } |
| 132 | + |
| 133 | +/** |
| 134 | + * \def RCUTILS_FAULT_INJECTION_TEST |
| 135 | + * |
| 136 | + * The fault injection macro for use with unit tests to check that `code` can tolerate injected |
| 137 | + * failures at all points along the execution path where the indicating macros |
| 138 | + * `RCUTILS_CAN_RETURN_WITH_ERROR_OF` and `RCUTILS_CAN_FAIL_WITH` are located. |
| 139 | + * |
| 140 | + * This macro is intended to be used within a gtest function macro like 'TEST', 'TEST_F', etc. |
| 141 | + * |
| 142 | + * `code` is executed within a do-while loop and therefore any variables declared within are in |
| 143 | + * their own scope block. |
| 144 | + * |
| 145 | + * Here's a simple example: |
| 146 | + * RCUTILS_FAULT_INJECTION_TEST( |
| 147 | + * rcl_ret_t ret = rcl_init(argc, argv, options, context); |
| 148 | + * if (RCL_RET_OK == ret) |
| 149 | + * { |
| 150 | + * ret = rcl_shutdown(context); |
| 151 | + * } |
| 152 | + * }); |
| 153 | + * |
| 154 | + * In this example, you will need have conditional execution based on the return value of |
| 155 | + * `rcl_init`. If it failed, then it wouldn't make sense to call rcl_shutdown. In your own test, |
| 156 | + * there might be similar logic that requires conditional checks. The goal of writing this test |
| 157 | + * is less about checking the behavior is consistent, but instead that failures do not cause |
| 158 | + * program crashes, memory errors, or unnecessary memory leaks. |
| 159 | + */ |
| 160 | +#define RCUTILS_FAULT_INJECTION_TEST(code) \ |
| 161 | + do { \ |
| 162 | + int fault_injection_count = 0; \ |
| 163 | + do { \ |
| 164 | + rcutils_fault_injection_set_count(fault_injection_count++); \ |
| 165 | + code; \ |
| 166 | + } while (!rcutils_fault_injection_is_test_complete()); \ |
| 167 | + rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); \ |
| 168 | + } while (0) |
| 169 | + |
| 170 | +#ifdef __cplusplus |
| 171 | +} |
| 172 | +#endif |
| 173 | + |
| 174 | +#endif // RCUTILS__TESTING__FAULT_INJECTION_H_ |
0 commit comments