Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cope with base function restrictions in mocks. #753

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion rcl/test/mocking_utils/patch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,22 @@ auto make_patch(const std::string & target, std::function<SignatureT> proxy)

/// Patch a `function` to execute normally but always yield a given `return_code`
/// in a given `scope`.
/**
* \warning On some Linux distributions (e.g. CentOS), pointers to function
* reference their PLT trampolines. In such cases, it is not possible to
* call `function` from within the mock.
*/
#define inject_on_return(scope, function, return_code) \
patch( \
scope, function, ([&, base = function](auto && ... __args) { \
static_cast<void>(base(std::forward<decltype(__args)>(__args)...)); \
if (base != function) { \
static_cast<void>(base(std::forward<decltype(__args)>(__args)...)); \
} else { \
RCUTILS_SAFE_FWRITE_TO_STDERR( \
"[WARNING] mocking_utils::inject_on_return() cannot forward call to " \
"original '" RCUTILS_STRINGIFY(function) "' function before injection\n" \
" at " __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) "\n"); \
} \
return return_code; \
}))

Expand Down
7 changes: 3 additions & 4 deletions rcl/test/rcl/test_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,13 @@ TEST(TestLogging, test_failing_external_logging) {
std::stringstream stderr_sstream;
auto fwrite_mock = mocking_utils::patch(
"lib:rcl", fwrite,
([&, base = fwrite](const void * ptr, size_t size,
size_t count, FILE * stream)
[&](const void * ptr, size_t size, size_t count, FILE * stream)
{
if (sizeof(char) == size && stderr == stream) {
stderr_sstream << std::string(reinterpret_cast<const char *>(ptr), count);
}
return base(ptr, size, count, stream);
}));
return count;
});

constexpr char stderr_message[] = "internal error";
#ifdef MOCKING_UTILS_SUPPORT_VA_LIST
Expand Down
7 changes: 5 additions & 2 deletions rcl/test/rcl/test_rmw_impl_id_check_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ TEST(TestRmwCheck, test_mock_rmw_impl_check) {

auto mock = mocking_utils::patch(
"lib:rcl", rcutils_strdup,
[base = rcutils_strdup](const char * str, auto allocator)
[](const char * str, auto allocator)
{
static int counter = 1;
if (counter == 1) {
counter++;
return base(str, allocator);
char * dup = static_cast<char *>(
allocator.allocate(strlen(str) + 1, allocator.state));
memcpy(dup, str, strlen(str) + 1);
return dup;
} else {
return static_cast<char *>(NULL);
}
Expand Down