|
21 | 21 | #include <dirent.h> |
22 | 22 | #endif |
23 | 23 |
|
24 | | -#include <string> |
25 | | - |
| 24 | +#include "rcutils/macros.h" |
26 | 25 | #include "rcutils/strerror.h" |
27 | 26 |
|
| 27 | +#include "./mimick.h" |
| 28 | + |
28 | 29 | TEST(test_strerror, get_error) { |
29 | 30 | // cleaning possible errors |
30 | 31 | errno = 0; |
@@ -54,3 +55,93 @@ TEST(test_strerror, get_error) { |
54 | 55 | "' did not cause the expected error message."; |
55 | 56 | #endif |
56 | 57 | } |
| 58 | + |
| 59 | +/* |
| 60 | + Define the blueprint of a mock identified by `strerror_r_proto` |
| 61 | + strerror_r possible signatures: |
| 62 | +
|
| 63 | + * int strerror_r(int errnum, char *buf, size_t buflen); (Case 1) |
| 64 | + * char *strerror_r(int errnum, char *buf, size_t buflen); (Case 2) |
| 65 | + * errno_t strerror_s( char *buf, rsize_t bufsz, errno_t errnum ); (Case 3) |
| 66 | +*/ |
| 67 | + |
| 68 | +#if defined(_WIN32) |
| 69 | +const char expected_error_msg[] = "Failed to get error"; |
| 70 | +mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); |
| 71 | + |
| 72 | +errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) |
| 73 | +{ |
| 74 | + (void) errnum; |
| 75 | + strncpy_s(buf, (size_t) bufsz, expected_error_msg, (size_t) bufsz); |
| 76 | + return errnum; |
| 77 | +} |
| 78 | + |
| 79 | +// Mocking test example |
| 80 | +TEST(test_strerror, test_mock) { |
| 81 | + // Mock the strerror_s function in the current module using |
| 82 | + // the `strerror_s_mock` blueprint. |
| 83 | + strerror_s_mock mock = mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); |
| 84 | + // Tell the mock to call mocked_windows_strerror instead |
| 85 | + mmk_when( |
| 86 | + strerror_s(mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), |
| 87 | + .then_call = (mmk_fn) mocked_windows_strerror); |
| 88 | + |
| 89 | + // Set the error (not used by the mock) |
| 90 | + errno = 2; |
| 91 | + char error_string[1024]; |
| 92 | + rcutils_strerror(error_string, sizeof(error_string)); |
| 93 | + ASSERT_STREQ(error_string, "Failed to get error"); |
| 94 | + mmk_reset(mock); |
| 95 | +} |
| 96 | + |
| 97 | +#elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) |
| 98 | +const char expected_error_msg[] = "Failed to get error"; |
| 99 | +mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); |
| 100 | + |
| 101 | +char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) |
| 102 | +{ |
| 103 | + (void) errnum; |
| 104 | + strncpy(buf, expected_error_msg, buflen); |
| 105 | + return buf; |
| 106 | +} |
| 107 | + |
| 108 | +// Mocking test example |
| 109 | +TEST(test_strerror, test_mock) { |
| 110 | + // Mock the strerror_r function in the current module using |
| 111 | + // the `strerror_r_mock` blueprint. |
| 112 | + mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); |
| 113 | + // Tell the mock to call mocked_gnu_strerror instead |
| 114 | + mmk_when( |
| 115 | + strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), |
| 116 | + .then_call = (mmk_fn) mocked_gnu_strerror); |
| 117 | + |
| 118 | + // Set the error (not used by the mock) |
| 119 | + errno = 2; |
| 120 | + char error_string[1024]; |
| 121 | + rcutils_strerror(error_string, sizeof(error_string)); |
| 122 | + ASSERT_STREQ(error_string, "Failed to get error"); |
| 123 | + mmk_reset(strerror_r); |
| 124 | +} |
| 125 | + |
| 126 | +#else |
| 127 | +mmk_mock_define(strerror_r_mock, int, int, char *, size_t); |
| 128 | + |
| 129 | +// Mocking test example |
| 130 | +TEST(test_strerror, test_mock) { |
| 131 | + // Mock the strerror_r function in the current module using |
| 132 | + // the `strerror_r_mock` blueprint. |
| 133 | + mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); |
| 134 | + // Tell the mock to return NULL and set errno to EINVAL |
| 135 | + // whatever the given parameter is. |
| 136 | + mmk_when( |
| 137 | + strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), |
| 138 | + .then_return = mmk_val(int, EINVAL)); |
| 139 | + |
| 140 | + // Set the error "No such file or directory" (not used by the mock) |
| 141 | + errno = 2; |
| 142 | + char error_string[1024]; |
| 143 | + rcutils_strerror(error_string, sizeof(error_string)); |
| 144 | + ASSERT_STREQ(error_string, "Failed to get error"); |
| 145 | + mmk_reset(strerror_r); |
| 146 | +} |
| 147 | +#endif |
0 commit comments