Skip to content

Commit

Permalink
Drop vsnprintf mocks entirely. (#275)
Browse files Browse the repository at this point in the history
Binary API is not portable across platforms and compilation config.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored and ahcorde committed Oct 6, 2020
1 parent 769899c commit 654c1e6
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 228 deletions.
78 changes: 0 additions & 78 deletions test/mocking_utils/stdio.hpp

This file was deleted.

37 changes: 0 additions & 37 deletions test/test_char_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "rcutils/types/char_array.h"

#include "./mocking_utils/patch.hpp"
#include "./mocking_utils/stdio.hpp"

class ArrayCharTest : public ::testing::Test
{
Expand Down Expand Up @@ -130,42 +129,6 @@ TEST_F(ArrayCharTest, vsprintf_fail) {
rcutils_reset_error();
char_array.allocator = allocator;

#ifdef MOCKING_UTILS_CAN_PATCH_VSNPRINTF
size_t buffer_threshold = 0;
#ifdef _WIN32
auto vscprintf_mock = mocking_utils::patch__vscprintf(
"lib:rcutils", [&](auto && ...) {
return static_cast<int>(buffer_threshold);
});

auto vsnprintf_mock = mocking_utils::patch__vsnprintf_s(
"lib:rcutils", [&](auto && ...) {
errno = EINVAL;
return -1;
});
#else
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [&](char * buffer, size_t buffer_size, auto...) {
if (nullptr == buffer || buffer_size < buffer_threshold) {
return static_cast<int>(buffer_threshold);
}
errno = EINVAL;
return -1;
});
#endif
// Do not force char array resize.
buffer_threshold = 5;
ret = example_logger(&char_array, "Long string for the case %d", 2);
EXPECT_EQ(RCUTILS_RET_ERROR, ret);
rcutils_reset_error();

// Force char array resize.
buffer_threshold = 20;
ret = example_logger(&char_array, "Long string for the case %d", 2);
EXPECT_EQ(RCUTILS_RET_ERROR, ret);
rcutils_reset_error();
#endif // MOCKING_UTILS_CAN_PATCH_VSNPRINTF

EXPECT_EQ(RCUTILS_RET_OK, rcutils_char_array_fini(&char_array));
}

Expand Down
29 changes: 0 additions & 29 deletions test/test_format_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "./allocator_testing_utils.h"
#include "./mocking_utils/patch.hpp"
#include "./mocking_utils/stdio.hpp"

#include "rcutils/allocator.h"
#include "rcutils/format_string.h"
Expand Down Expand Up @@ -62,31 +61,3 @@ TEST(test_format_string_limit, invalid_arguments) {
formatted = rcutils_format_string_limit(failing_allocator, 10, "%s", "test");
EXPECT_STREQ(NULL, formatted);
}

#ifdef MOCKING_UTILS_CAN_PATCH_VSNPRINTF
TEST(test_format_string_limit, on_internal_error) {
#ifdef _WIN32
auto _vscprintf_mock = mocking_utils::patch__vscprintf(
"lib:rcutils", [](auto && ...) {return 1;});

auto _vsnprintf_s_mock = mocking_utils::patch__vsnprintf_s(
"lib:rcutils", [](auto && ...) {
errno = EINVAL;
return -1;
});
#else
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
errno = EINVAL;
return -1;
});
#endif

rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * formatted = rcutils_format_string_limit(allocator, 10, "%s", "test");
EXPECT_STREQ(NULL, formatted);
}
#endif // MOCKING_UTILS_CAN_PATCH_VSNPRINTF
29 changes: 0 additions & 29 deletions test/test_shared_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "./allocator_testing_utils.h"
#include "./mocking_utils/patch.hpp"
#include "./mocking_utils/stdio.hpp"

#include "rcutils/allocator.h"
#include "rcutils/error_handling.h"
Expand Down Expand Up @@ -48,34 +47,6 @@ TEST_F(TestSharedLibrary, basic_load) {
EXPECT_TRUE(lib.lib_pointer == NULL);
EXPECT_FALSE(rcutils_is_shared_library_loaded(&lib));

#ifdef MOCKING_UTILS_CAN_PATCH_VSNPRINTF
{
// Check internal errors are handled correctly
#ifdef _WIN32
auto _vscprintf_mock = mocking_utils::patch__vscprintf(
"lib:rcutils", [](auto && ...) {return 1;});

auto _vsnprintf_s_mock = mocking_utils::patch__vsnprintf_s(
"lib:rcutils", [](auto && ...) {
errno = EINVAL;
return -1;
});
#else
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
errno = EINVAL;
return -1;
});
#endif

ret = rcutils_get_platform_library_name("dummy_shared_library", library_path, 1024, false);
ASSERT_EQ(RCUTILS_RET_ERROR, ret);
}
#endif // MOCKING_UTILS_CAN_PATCH_VSNPRINTF

// Check debug name works first because rcutils_load_shared_library should be called on
// non-debug symbol name
ret = rcutils_get_platform_library_name("dummy_shared_library", library_path, 1024, true);
Expand Down
55 changes: 0 additions & 55 deletions test/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "rcutils/time.h"

#include "./mocking_utils/patch.hpp"
#include "./mocking_utils/stdio.hpp"

using osrf_testing_tools_cpp::memory_tools::disable_monitoring_in_all_threads;
using osrf_testing_tools_cpp::memory_tools::enable_monitoring_in_all_threads;
Expand Down Expand Up @@ -299,33 +298,6 @@ TEST_F(TestTimeFixture, test_rcutils_time_point_value_as_nanoseconds_string) {
ret = rcutils_time_point_value_as_nanoseconds_string(&timepoint, buffer, sizeof(buffer));
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ("-0000000000000000100", buffer);

#ifdef MOCKING_UTILS_CAN_PATCH_VSNPRINTF
#ifdef _WIN32
auto _vscprintf_mock = mocking_utils::patch__vscprintf(
"lib:rcutils", [](auto && ...) {return 1;});

auto _vsnprintf_s_mock = mocking_utils::patch__vsnprintf_s(
"lib:rcutils", [](auto && ...) {
errno = EINVAL;
return -1;
});
#else
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
errno = EINVAL;
return -1;
});
#endif

timepoint = 100;
ret = rcutils_time_point_value_as_nanoseconds_string(&timepoint, buffer, sizeof(buffer));
EXPECT_EQ(RCUTILS_RET_ERROR, ret);
rcutils_reset_error();
#endif // MOCKING_UTILS_CAN_PATCH_VSNPRINTF
}

// Tests the rcutils_time_point_value_as_seconds_string() function.
Expand Down Expand Up @@ -379,31 +351,4 @@ TEST_F(TestTimeFixture, test_rcutils_time_point_value_as_seconds_string) {
ret = rcutils_time_point_value_as_seconds_string(&timepoint, buffer, sizeof(buffer));
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ("-0000000000.000000100", buffer);

#ifdef MOCKING_UTILS_CAN_PATCH_VSNPRINTF
#ifdef _WIN32
auto _vscprintf_mock = mocking_utils::patch__vscprintf(
"lib:rcutils", [](auto && ...) {return 1;});

auto _vsnprintf_s_mock = mocking_utils::patch__vsnprintf_s(
"lib:rcutils", [](auto && ...) {
errno = EINVAL;
return -1;
});
#else
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
errno = EINVAL;
return -1;
});
#endif

timepoint = 100;
ret = rcutils_time_point_value_as_seconds_string(&timepoint, buffer, sizeof(buffer));
EXPECT_EQ(RCUTILS_RET_ERROR, ret);
rcutils_reset_error();
#endif // MOCKING_UTILS_CAN_PATCH_VSNPRINTF
}

0 comments on commit 654c1e6

Please sign in to comment.