From ae91f73c68d3f8b022ac284a03851058adf23e27 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Fri, 3 Jul 2020 16:59:18 -0300 Subject: [PATCH 01/21] Add test using mimick vendored library Signed-off-by: Jorge Perez --- CMakeLists.txt | 2 ++ package.xml | 2 ++ test/test_strerror.cpp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7ffe516..77ea7465 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,6 +168,8 @@ if(BUILD_TESTING) "${CMAKE_CURRENT_BINARY_DIR}/include/rcutils/logging_macros.h") endif() + find_package(mimick_vendor REQUIRED) + find_package(osrf_testing_tools_cpp REQUIRED) get_target_property(memory_tools_test_env_vars osrf_testing_tools_cpp::memory_tools LIBRARY_PRELOAD_ENVIRONMENT_VARIABLE) diff --git a/package.xml b/package.xml index 7f9dcb67..1bb43534 100644 --- a/package.xml +++ b/package.xml @@ -15,6 +15,8 @@ ament_cmake_pytest ament_lint_common ament_lint_auto + mimick + mimick_vendor launch launch_testing launch_testing_ament_cmake diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index eeeb5a05..3ec4dc19 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -24,6 +24,7 @@ #include #include "rcutils/strerror.h" +#include "mimick_vendor/mimic.h" TEST(test_strerror, get_error) { // cleaning possible errors @@ -54,3 +55,34 @@ TEST(test_strerror, get_error) { "' did not cause the expected error message."; #endif } + +/* + Define the blueprint of a mock identified by `strerror_r_proto` + strerror_r signature: + int strerror_r(int errnum, char *buf, size_t buflen); +*/ +mmk_mock_define (strerror_r_mock, int, char *, size_t); + +/* Mocking test example */ +TEST(test_strerror, get_error) { + /* Mock the strerror_r function in the current module using + the `strerror_r_mock` blueprint. */ + mmk_mock("strerror_r@self", strerror_r_mock); + + /* Tell the mock to return NULL and set errno to ENOMEM + whatever the given parameter is. */ + mmk_when(strerror_r(mmk_any(int, char *, size_t)), + .then_return = EINVAL); + + // Now normal usage of the function returning unexpected EINVAL + // error for the internal strerror_r + // This works only for POSIX + + // Set the error "No such file or directory" + errno = 2; + char error_string[1024]; + rcutils_strerror(error_string, sizeof(error_string)); + ASSERT_STREQ(error_string, "Failed to get error"); + + mmk_reset(strerror_r); +} From 18b53d1fb267ea3f9cfdbb2cd65d90137a6ddb2d Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 6 Jul 2020 18:49:30 -0300 Subject: [PATCH 02/21] Add path to mimick_vendor Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 3ec4dc19..06530374 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -24,7 +24,7 @@ #include #include "rcutils/strerror.h" -#include "mimick_vendor/mimic.h" +#include "mimick_vendor/mimick.h" TEST(test_strerror, get_error) { // cleaning possible errors From 56782e7d1b595270d459eda27dc2cc1c34173bf3 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Tue, 7 Jul 2020 12:13:25 -0300 Subject: [PATCH 03/21] Link test to mimick library Signed-off-by: Jorge Perez --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 77ea7465..cfda1c95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,7 +285,7 @@ if(BUILD_TESTING) test/test_strerror.cpp ) if(TARGET test_strerror) - target_link_libraries(test_strerror ${PROJECT_NAME}) + target_link_libraries(test_strerror ${PROJECT_NAME} mimick) endif() rcutils_custom_add_gtest(test_string_array From 2927385877e7fbede5ba74e61f3e679f5b9a88cf Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Tue, 7 Jul 2020 12:16:27 -0300 Subject: [PATCH 04/21] Fix library usage issues Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 06530374..85c88d29 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -24,7 +24,10 @@ #include #include "rcutils/strerror.h" -#include "mimick_vendor/mimick.h" + +extern "C" { +#include "mimick.h" +} TEST(test_strerror, get_error) { // cleaning possible errors @@ -71,7 +74,7 @@ TEST(test_strerror, get_error) { /* Tell the mock to return NULL and set errno to ENOMEM whatever the given parameter is. */ - mmk_when(strerror_r(mmk_any(int, char *, size_t)), + mmk_when(strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), .then_return = EINVAL); // Now normal usage of the function returning unexpected EINVAL From 7a7ec42e2659421f9325e1ba733d59fa0eaa7254 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Wed, 8 Jul 2020 19:26:16 -0300 Subject: [PATCH 05/21] Fix mimick usage Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 85c88d29..622804b9 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -12,6 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifdef _GNU_SOURCE +#undef _GNU_SOURCE +#include +#else +#include +#endif + #include #include @@ -21,13 +28,9 @@ #include #endif -#include - #include "rcutils/strerror.h" -extern "C" { -#include "mimick.h" -} +#include "./mimick.h" TEST(test_strerror, get_error) { // cleaning possible errors @@ -64,18 +67,22 @@ TEST(test_strerror, get_error) { strerror_r signature: int strerror_r(int errnum, char *buf, size_t buflen); */ -mmk_mock_define (strerror_r_mock, int, char *, size_t); +mmk_mock_define(strerror_r_mock, int, char *, size_t); /* Mocking test example */ -TEST(test_strerror, get_error) { +TEST(test_strerror, test_mock) { /* Mock the strerror_r function in the current module using the `strerror_r_mock` blueprint. */ - mmk_mock("strerror_r@self", strerror_r_mock); + // mmk_mock("strerror_r@rcutils", strerror_r_mock); + // mmk_mock("__xpg_strerror_r@self", strerror_r_mock); + // mmk_mock("strerror_r@self", strerror_r_mock); + mmk_mock("__xpg_strerror_r@lib:rcutils", strerror_r_mock); /* Tell the mock to return NULL and set errno to ENOMEM whatever the given parameter is. */ - mmk_when(strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), - .then_return = EINVAL); + mmk_when( + strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), + .then_return = mmk_val(int, EINVAL)); // Now normal usage of the function returning unexpected EINVAL // error for the internal strerror_r From d3407d6839442e9568375292454a0d6b0a860c31 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Wed, 15 Jul 2020 17:01:45 -0300 Subject: [PATCH 06/21] Remove commented code Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 622804b9..e00805a5 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -73,9 +73,6 @@ mmk_mock_define(strerror_r_mock, int, char *, size_t); TEST(test_strerror, test_mock) { /* Mock the strerror_r function in the current module using the `strerror_r_mock` blueprint. */ - // mmk_mock("strerror_r@rcutils", strerror_r_mock); - // mmk_mock("__xpg_strerror_r@self", strerror_r_mock); - // mmk_mock("strerror_r@self", strerror_r_mock); mmk_mock("__xpg_strerror_r@lib:rcutils", strerror_r_mock); /* Tell the mock to return NULL and set errno to ENOMEM From 8338f9ecf4510407b4aa6d18e2949c20768e7368 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Tue, 21 Jul 2020 14:28:04 -0300 Subject: [PATCH 07/21] Add multi-platform support Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 75 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index e00805a5..c12f2449 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -12,13 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifdef _GNU_SOURCE -#undef _GNU_SOURCE -#include -#else -#include -#endif - #include #include @@ -28,6 +21,7 @@ #include #endif +#include "rcutils/macros.h" #include "rcutils/strerror.h" #include "./mimick.h" @@ -64,22 +58,74 @@ TEST(test_strerror, get_error) { /* Define the blueprint of a mock identified by `strerror_r_proto` - strerror_r signature: - int strerror_r(int errnum, char *buf, size_t buflen); + strerror_r possible signatures: + + * int strerror_r(int errnum, char *buf, size_t buflen); (Case 1) + * char *strerror_r(int errnum, char *buf, size_t buflen); (Case 2) + * errno_t strerror_s( char *buf, rsize_t bufsz, errno_t errnum ); (Case 3) */ -mmk_mock_define(strerror_r_mock, int, char *, size_t); + +#if defined(_WIN32) +mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); +#elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) +mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); +#else +mmk_mock_define(strerror_r_mock, int, int, char *, size_t); +#endif + +const char expected_error_msg[] = "Failed to get error"; +#if defined(_WIN32) +// Function to be called for (Case 3) +errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) +{ + (void) errnum; + unsigned char index_err = 0; + + while (buf && bufsz--) { + buf[index_err] = expected_error_msg[index_err]; + index_err++; + } + return errnum; +} +#else +// Function to be called for (Case 1) +char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) +{ + (void) errnum; + const char error_msg[] = "Failed to get error"; + unsigned char index_err = 0; + while (buf && buflen--) { + buf[index_err] = error_msg[index_err]; + index_err++; + } + return buf; +} +#endif /* Mocking test example */ TEST(test_strerror, test_mock) { /* Mock the strerror_r function in the current module using the `strerror_r_mock` blueprint. */ - mmk_mock("__xpg_strerror_r@lib:rcutils", strerror_r_mock); - +#if defined(_WIN32) + mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); +#else + mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); +#endif /* Tell the mock to return NULL and set errno to ENOMEM whatever the given parameter is. */ +#if defined(_WIN32) + mmk_when( + strerror_s(mmk_any(errno_t), mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), + .then_call = (mmk_fn) mocked_windows_strerror); +#elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) + mmk_when( + strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), + .then_call = (mmk_fn) mocked_gnu_strerror); +#else mmk_when( strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), .then_return = mmk_val(int, EINVAL)); +#endif // Now normal usage of the function returning unexpected EINVAL // error for the internal strerror_r @@ -90,6 +136,9 @@ TEST(test_strerror, test_mock) { char error_string[1024]; rcutils_strerror(error_string, sizeof(error_string)); ASSERT_STREQ(error_string, "Failed to get error"); - +#if defined(_WIN32) + mmk_reset(strerror_s); +#else mmk_reset(strerror_r); +#endif } From 2135205f524a9b3ed44970647d3423812bbb74a7 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Wed, 22 Jul 2020 11:30:07 -0300 Subject: [PATCH 08/21] Reformat to separate different platform tests Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 76 ++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index c12f2449..88323df7 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -56,6 +56,7 @@ TEST(test_strerror, get_error) { #endif } +const char expected_error_msg[] = "Failed to get error"; /* Define the blueprint of a mock identified by `strerror_r_proto` strerror_r possible signatures: @@ -67,15 +68,7 @@ TEST(test_strerror, get_error) { #if defined(_WIN32) mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); -#elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) -mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); -#else -mmk_mock_define(strerror_r_mock, int, int, char *, size_t); -#endif -const char expected_error_msg[] = "Failed to get error"; -#if defined(_WIN32) -// Function to be called for (Case 3) errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) { (void) errnum; @@ -87,8 +80,28 @@ errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) } return errnum; } -#else -// Function to be called for (Case 1) + +/* Mocking test example */ +TEST(test_strerror, test_mock) { + /* Mock the strerror_s function in the current module using + the `strerror_s_mock` blueprint. */ + mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); + /* Tell the mock to call mocked_windows_strerror instead*/ + mmk_when( + strerror_s(mmk_any(errno_t), mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), + .then_call = (mmk_fn) mocked_windows_strerror); + + // Set the error (not used by the mock) + errno = 2; + char error_string[1024]; + rcutils_strerror(error_string, sizeof(error_string)); + ASSERT_STREQ(error_string, "Failed to get error"); + mmk_reset(strerror_s); +} + +#elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) +mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); + char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) { (void) errnum; @@ -100,45 +113,44 @@ char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) } return buf; } -#endif /* Mocking test example */ TEST(test_strerror, test_mock) { /* Mock the strerror_r function in the current module using the `strerror_r_mock` blueprint. */ -#if defined(_WIN32) - mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); -#else mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); -#endif - /* Tell the mock to return NULL and set errno to ENOMEM - whatever the given parameter is. */ -#if defined(_WIN32) - mmk_when( - strerror_s(mmk_any(errno_t), mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), - .then_call = (mmk_fn) mocked_windows_strerror); -#elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) + /* Tell the mock to call mocked_gnu_strerror instead */ mmk_when( strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), .then_call = (mmk_fn) mocked_gnu_strerror); + + // Set the error (not used by the mock) + errno = 2; + char error_string[1024]; + rcutils_strerror(error_string, sizeof(error_string)); + ASSERT_STREQ(error_string, "Failed to get error"); + mmk_reset(strerror_r); +} + #else +mmk_mock_define(strerror_r_mock, int, int, char *, size_t); +/* Mocking test example */ +TEST(test_strerror, test_mock) { + /* Mock the strerror_r function in the current module using + the `strerror_r_mock` blueprint. */ + mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); + /* Tell the mock to return NULL and set errno to EINVAL + whatever the given parameter is. */ mmk_when( strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), .then_return = mmk_val(int, EINVAL)); -#endif - - // Now normal usage of the function returning unexpected EINVAL - // error for the internal strerror_r - // This works only for POSIX - // Set the error "No such file or directory" + // Set the error "No such file or directory" (not used by the mock) errno = 2; char error_string[1024]; rcutils_strerror(error_string, sizeof(error_string)); ASSERT_STREQ(error_string, "Failed to get error"); -#if defined(_WIN32) - mmk_reset(strerror_s); -#else mmk_reset(strerror_r); -#endif } + +#endif From 4e8b05d1c5660f2acf55c7ec0036e7f1016c208f Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Wed, 22 Jul 2020 11:35:18 -0300 Subject: [PATCH 09/21] Remove not needed constant Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 88323df7..8c61c74b 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -105,10 +105,9 @@ mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) { (void) errnum; - const char error_msg[] = "Failed to get error"; unsigned char index_err = 0; while (buf && buflen--) { - buf[index_err] = error_msg[index_err]; + buf[index_err] = expected_error_msg[index_err]; index_err++; } return buf; From dcf80fa41ff6c1708ab88b4c198f943078736ad6 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Wed, 22 Jul 2020 11:42:52 -0300 Subject: [PATCH 10/21] Reformat to use strncpy to copy buffer Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 8c61c74b..f276a83d 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -72,12 +72,7 @@ mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) { (void) errnum; - unsigned char index_err = 0; - - while (buf && bufsz--) { - buf[index_err] = expected_error_msg[index_err]; - index_err++; - } + strncpy(buf, expected_error_msg, (size_t) bufsz); return errnum; } @@ -105,11 +100,7 @@ mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) { (void) errnum; - unsigned char index_err = 0; - while (buf && buflen--) { - buf[index_err] = expected_error_msg[index_err]; - index_err++; - } + strncpy(buf, expected_error_msg, buflen); return buf; } From 85d4d1ad8ad8bb3b3c409d83decabf0fd538f883 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Wed, 22 Jul 2020 13:30:49 -0300 Subject: [PATCH 11/21] Replace comments /* with // Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index f276a83d..de8f8f7e 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -76,12 +76,12 @@ errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) return errnum; } -/* Mocking test example */ +// Mocking test example TEST(test_strerror, test_mock) { - /* Mock the strerror_s function in the current module using - the `strerror_s_mock` blueprint. */ + // Mock the strerror_s function in the current module using + // the `strerror_s_mock` blueprint. mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); - /* Tell the mock to call mocked_windows_strerror instead*/ + // Tell the mock to call mocked_windows_strerror instead mmk_when( strerror_s(mmk_any(errno_t), mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), .then_call = (mmk_fn) mocked_windows_strerror); @@ -104,12 +104,12 @@ char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) return buf; } -/* Mocking test example */ +// Mocking test example TEST(test_strerror, test_mock) { - /* Mock the strerror_r function in the current module using - the `strerror_r_mock` blueprint. */ + // Mock the strerror_r function in the current module using + // the `strerror_r_mock` blueprint. mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); - /* Tell the mock to call mocked_gnu_strerror instead */ + // Tell the mock to call mocked_gnu_strerror instead mmk_when( strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), .then_call = (mmk_fn) mocked_gnu_strerror); @@ -124,13 +124,14 @@ TEST(test_strerror, test_mock) { #else mmk_mock_define(strerror_r_mock, int, int, char *, size_t); -/* Mocking test example */ + +// Mocking test example TEST(test_strerror, test_mock) { - /* Mock the strerror_r function in the current module using - the `strerror_r_mock` blueprint. */ + // Mock the strerror_r function in the current module using + // the `strerror_r_mock` blueprint. mmk_mock(RCUTILS_STRINGIFY(strerror_r) "@lib:rcutils", strerror_r_mock); - /* Tell the mock to return NULL and set errno to EINVAL - whatever the given parameter is. */ + // Tell the mock to return NULL and set errno to EINVAL + // whatever the given parameter is. mmk_when( strerror_r(mmk_any(int), mmk_any(char *), mmk_any(size_t) ), .then_return = mmk_val(int, EINVAL)); @@ -142,5 +143,4 @@ TEST(test_strerror, test_mock) { ASSERT_STREQ(error_string, "Failed to get error"); mmk_reset(strerror_r); } - #endif From fe7317445edf8d30aaedc24629fc9424664639e9 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Fri, 24 Jul 2020 17:38:06 -0300 Subject: [PATCH 12/21] Remove not needed constant for MacOs Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index de8f8f7e..2f238c5e 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -56,7 +56,6 @@ TEST(test_strerror, get_error) { #endif } -const char expected_error_msg[] = "Failed to get error"; /* Define the blueprint of a mock identified by `strerror_r_proto` strerror_r possible signatures: @@ -67,6 +66,7 @@ const char expected_error_msg[] = "Failed to get error"; */ #if defined(_WIN32) +const char expected_error_msg[] = "Failed to get error"; mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) @@ -95,6 +95,7 @@ TEST(test_strerror, test_mock) { } #elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) +const char expected_error_msg[] = "Failed to get error"; mmk_mock_define(strerror_r_mock, char *, int, char *, size_t); char * mocked_gnu_strerror(int errnum, char * buf, size_t buflen) From 5b8ce8f220aeedd2c5427a59787e1014896dee5a Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 13:59:10 -0300 Subject: [PATCH 13/21] Remove extra parameter windows call Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 2f238c5e..d176a527 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -83,7 +83,7 @@ TEST(test_strerror, test_mock) { mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); // Tell the mock to call mocked_windows_strerror instead mmk_when( - strerror_s(mmk_any(errno_t), mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), + strerror_s(mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), .then_call = (mmk_fn) mocked_windows_strerror); // Set the error (not used by the mock) From 1b3ad34ea992d6b687b3eae188a10aa9d3ee735a Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 15:55:32 -0300 Subject: [PATCH 14/21] Cast function type Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index d176a527..be9c0915 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -91,7 +91,7 @@ TEST(test_strerror, test_mock) { char error_string[1024]; rcutils_strerror(error_string, sizeof(error_string)); ASSERT_STREQ(error_string, "Failed to get error"); - mmk_reset(strerror_s); + mmk_reset(reinterpret_caststrerror_s); } #elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) From fd8d93ba0e0cef0d5e743675da9753307794cb60 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 16:08:43 -0300 Subject: [PATCH 15/21] Add missing parentheses Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index be9c0915..23832fc5 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -91,7 +91,7 @@ TEST(test_strerror, test_mock) { char error_string[1024]; rcutils_strerror(error_string, sizeof(error_string)); ASSERT_STREQ(error_string, "Failed to get error"); - mmk_reset(reinterpret_caststrerror_s); + mmk_reset(reinterpret_cast(strerror_s)); } #elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) From f00a0dcae1f29a03765f1f6583939663b64e2728 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 17:03:31 -0300 Subject: [PATCH 16/21] Change reset macro to take strerror_mock Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 23832fc5..03063ab5 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -91,7 +91,7 @@ TEST(test_strerror, test_mock) { char error_string[1024]; rcutils_strerror(error_string, sizeof(error_string)); ASSERT_STREQ(error_string, "Failed to get error"); - mmk_reset(reinterpret_cast(strerror_s)); + mmk_reset(strerror_s_mock); } #elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) From 0ffe78b46196d2851a0936a68c7b8e3dd42d05f1 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 17:33:29 -0300 Subject: [PATCH 17/21] Use mallock return type to reset function Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 03063ab5..7dcb2ee9 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -80,7 +80,7 @@ errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) TEST(test_strerror, test_mock) { // Mock the strerror_s function in the current module using // the `strerror_s_mock` blueprint. - mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); + malloc_mock mock = mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); // Tell the mock to call mocked_windows_strerror instead mmk_when( strerror_s(mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), @@ -91,7 +91,7 @@ TEST(test_strerror, test_mock) { char error_string[1024]; rcutils_strerror(error_string, sizeof(error_string)); ASSERT_STREQ(error_string, "Failed to get error"); - mmk_reset(strerror_s_mock); + mmk_reset(mock); } #elif defined(_GNU_SOURCE) && (!defined(ANDROID) || __ANDROID_API__ >= 23) From 200b5e05f4b5ef7df05e2a1e341c6d8efb31755e Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 17:39:37 -0300 Subject: [PATCH 18/21] Remove test_depend to mimick Signed-off-by: Jorge Perez --- package.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/package.xml b/package.xml index 1bb43534..1d883335 100644 --- a/package.xml +++ b/package.xml @@ -15,7 +15,6 @@ ament_cmake_pytest ament_lint_common ament_lint_auto - mimick mimick_vendor launch launch_testing From a870d60eb517c78068de1ee1375f7c32b4a0046a Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 17:51:10 -0300 Subject: [PATCH 19/21] Replace mock type Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 7dcb2ee9..6886a809 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -80,7 +80,7 @@ errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) TEST(test_strerror, test_mock) { // Mock the strerror_s function in the current module using // the `strerror_s_mock` blueprint. - malloc_mock mock = mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); + strerror_s_mock mock = mmk_mock(RCUTILS_STRINGIFY(strerror_s) "@lib:rcutils", strerror_s_mock); // Tell the mock to call mocked_windows_strerror instead mmk_when( strerror_s(mmk_any(char *), mmk_any(rsize_t), mmk_any(errno_t)), From ee41b34e9a376e6702ddf5306a322c42c4260da8 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 18:04:01 -0300 Subject: [PATCH 20/21] Change function strncpy to strncpy_s Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index 6886a809..a916d866 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -72,7 +72,7 @@ mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) { (void) errnum; - strncpy(buf, expected_error_msg, (size_t) bufsz); + strncpy_s(buf, expected_error_msg, (size_t) bufsz); return errnum; } From a6cfb762f9a9ffa0e260de663c8de5b600124e52 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 27 Jul 2020 18:14:06 -0300 Subject: [PATCH 21/21] Add missing param Signed-off-by: Jorge Perez --- test/test_strerror.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_strerror.cpp b/test/test_strerror.cpp index a916d866..a5759a85 100644 --- a/test/test_strerror.cpp +++ b/test/test_strerror.cpp @@ -72,7 +72,7 @@ mmk_mock_define(strerror_s_mock, errno_t, char *, rsize_t, errno_t); errno_t mocked_windows_strerror(char * buf, rsize_t bufsz, errno_t errnum) { (void) errnum; - strncpy_s(buf, expected_error_msg, (size_t) bufsz); + strncpy_s(buf, (size_t) bufsz, expected_error_msg, (size_t) bufsz); return errnum; }