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

Add cov tests 95% #744

Merged
merged 24 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 18 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
8 changes: 4 additions & 4 deletions rcl/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function(test_target_function)
SRCS rcl/test_init.cpp
ENV ${rmw_implementation_env_var} ${memory_tools_ld_preload_env_var}
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
LIBRARIES ${PROJECT_NAME} osrf_testing_tools_cpp::memory_tools
LIBRARIES ${PROJECT_NAME} mimick osrf_testing_tools_cpp::memory_tools
AMENT_DEPENDENCIES ${rmw_implementation}
)

Expand Down Expand Up @@ -288,7 +288,7 @@ function(test_target_function)
SRCS rcl/test_rmw_impl_id_check_func.cpp
ENV ${rmw_implementation_env_var}
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
LIBRARIES ${PROJECT_NAME}
LIBRARIES ${PROJECT_NAME} mimick
AMENT_DEPENDENCIES ${rmw_implementation}
)

Expand Down Expand Up @@ -361,7 +361,7 @@ rcl_add_custom_gtest(test_validate_enclave_name
rcl_add_custom_gtest(test_domain_id
SRCS rcl/test_domain_id.cpp
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
LIBRARIES ${PROJECT_NAME}
LIBRARIES ${PROJECT_NAME} mimick
)

rcl_add_custom_gtest(test_localhost
Expand Down Expand Up @@ -399,7 +399,7 @@ rcl_add_custom_gtest(test_common
rcl_add_custom_gtest(test_log_level
SRCS rcl/test_log_level.cpp
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
LIBRARIES ${PROJECT_NAME}
LIBRARIES ${PROJECT_NAME} mimick
AMENT_DEPENDENCIES "osrf_testing_tools_cpp"
)

Expand Down
12 changes: 12 additions & 0 deletions rcl/test/rcl/test_domain_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "rcl/error_handling.h"
#include "rcutils/env.h"

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

TEST(TestGetDomainId, test_nominal) {
ASSERT_TRUE(rcutils_set_env("ROS_DOMAIN_ID", "42"));
size_t domain_id = RCL_DEFAULT_DOMAIN_ID;
Expand Down Expand Up @@ -50,3 +52,13 @@ TEST(TestGetDomainId, test_nominal) {

EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_get_default_domain_id(nullptr));
}

TEST(TestGetDomainId, test_mock_get_default_domain_id) {
auto mock = mocking_utils::patch_and_return(
"lib:rcl", rcutils_get_env, "argument env_name is null");
size_t domain_id = RCL_DEFAULT_DOMAIN_ID;
EXPECT_EQ(RCL_RET_ERROR, rcl_get_default_domain_id(&domain_id));
EXPECT_EQ(RCL_DEFAULT_DOMAIN_ID, domain_id);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();
}
108 changes: 107 additions & 1 deletion rcl/test/rcl/test_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "./allocator_testing_utils.h"
#include "../src/rcl/init_options_impl.h"
#include "../mocking_utils/patch.hpp"
Blast545 marked this conversation as resolved.
Show resolved Hide resolved

#ifdef RMW_IMPLEMENTATION
# define CLASSNAME_(NAME, SUFFIX) NAME ## __ ## SUFFIX
Expand Down Expand Up @@ -104,16 +105,34 @@ struct FakeTestArgv
*/
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_init) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());

// fini a not empty options
rcl_ret_t ret = rcl_init_options_fini(&init_options);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();

// Expected usage
ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str;
});

// Already init
ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
EXPECT_EQ(RCL_RET_ALREADY_INIT, ret) << rcl_get_error_string().str;
rcl_reset_error();

// nullptr
ret = rcl_init_options_init(nullptr, rcl_get_default_allocator());
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();

// nullptr
ret = rcl_init_options_fini(nullptr);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();
}

/* Tests calling rcl_init() with invalid arguments fails.
Expand Down Expand Up @@ -364,6 +383,7 @@ TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_get_instance_id)

TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_access) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_init_options_t not_ini_init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
Expand All @@ -375,14 +395,21 @@ TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_acce
ASSERT_NE(nullptr, options);
EXPECT_EQ(0u, options->instance_id);
EXPECT_EQ(nullptr, options->impl);
EXPECT_EQ(NULL, rcl_init_options_get_rmw_init_options(nullptr));
EXPECT_EQ(NULL, rcl_init_options_get_rmw_init_options(&not_ini_init_options));

const rcl_allocator_t * options_allocator = rcl_init_options_get_allocator(&init_options);
EXPECT_TRUE(rcutils_allocator_is_valid(options_allocator));
EXPECT_EQ(NULL, rcl_init_options_get_allocator(nullptr));
EXPECT_EQ(NULL, rcl_init_options_get_allocator(&not_ini_init_options));

size_t domain_id;
ret = rcl_init_options_get_domain_id(NULL, &domain_id);
ASSERT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();
ret = rcl_init_options_get_domain_id(&not_ini_init_options, &domain_id);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();
ret = rcl_init_options_get_domain_id(&init_options, NULL);
ASSERT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();
Expand All @@ -392,6 +419,9 @@ TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_acce
ret = rcl_init_options_set_domain_id(NULL, domain_id);
ASSERT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();
ret = rcl_init_options_set_domain_id(&not_ini_init_options, domain_id);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret) << rcl_get_error_string().str;
rcl_reset_error();

ret = rcl_init_options_get_domain_id(&init_options, &domain_id);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
Expand All @@ -403,10 +433,86 @@ TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_acce
EXPECT_EQ(0U, domain_id);

rcl_init_options_t init_options_dst = rcl_get_zero_initialized_init_options();

// nullptr copy cases
EXPECT_EQ(
RCL_RET_INVALID_ARGUMENT, rcl_init_options_copy(nullptr, &init_options_dst));
EXPECT_EQ(
RCL_RET_INVALID_ARGUMENT, rcl_init_options_copy(&init_options, nullptr));

// Expected usage copy
ASSERT_EQ(RCL_RET_OK, rcl_init_options_copy(&init_options, &init_options_dst));
ret = rcl_init_options_get_domain_id(&init_options_dst, &domain_id);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
EXPECT_EQ(0U, domain_id);
EXPECT_EQ(RCL_RET_ALREADY_INIT, rcl_init_options_copy(&init_options, &init_options_dst));
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options_dst));
}

MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, ==)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blast545 nit: same as below and other PRs, an explanatory comment is nice.

MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, <)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, >)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, !=)

// Tests rcl_init_options_init() mocked to fail
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_mocked_rcl_init_options_ini) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
auto mock = mocking_utils::patch_and_return("lib:rcl", rmw_init_options_init, RMW_RET_ERROR);
EXPECT_EQ(RCL_RET_ERROR, rcl_init_options_init(&init_options, rcl_get_default_allocator()));
rcl_reset_error();
}

// Tests rcl_init_options_fini() mocked to fail
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_mocked_rcl_init_options_fini) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
auto mock = mocking_utils::patch_and_return("lib:rcl", rmw_init_options_fini, RMW_RET_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blast545 in this case I'd recommend using mocking_utils::inject_on_return(), to ensure rmw_init_options_fini() gets actually called or we'll leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed 402b106

EXPECT_EQ(RCL_RET_ERROR, rcl_init_options_fini(&init_options));
rcl_reset_error();
}

// Mock rcl_init_options_copy to fail
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_copy_mocked_fail_fini) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str;
});
rcl_init_options_t init_options_dst = rcl_get_zero_initialized_init_options();
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options_dst)) << rcl_get_error_string().str;
});

auto mock = mocking_utils::patch_and_return("lib:rcl", rmw_init_options_fini, RMW_RET_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blast545 same about using mocking_utils::inject_on_return().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed 402b106

EXPECT_EQ(RCL_RET_ERROR, rcl_init_options_copy(&init_options, &init_options_dst));
rcl_reset_error();
}

// Mock rcl_init_options_copy to fail
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_copy_fail_rmw_copy) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str;
});
rcl_init_options_t init_options_dst = rcl_get_zero_initialized_init_options();
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
// dst is in a invalid state after failed copy
EXPECT_EQ(
RCL_RET_INVALID_ARGUMENT,
rcl_init_options_fini(&init_options_dst)) << rcl_get_error_string().str;
rcl_reset_error();
});

// rmw_init_options_copy error is logged
auto mock = mocking_utils::patch_and_return("lib:rcl", rmw_init_options_copy, RMW_RET_ERROR);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_init_options_copy(&init_options, &init_options_dst));
rcl_reset_error();
}
Loading