Skip to content

Commit

Permalink
Add cov tests 95% (#744)
Browse files Browse the repository at this point in the history
* Add mocking tools
* Add mocked tests fail init/fini
* Reformat tests with helper macro
* Add nullptr/invalid tests
* Add domain_id mocked tests
* Add mimick support for test_log_level
* Add fini/ini tests log_level
* Add fini nullptr test
* Add mocked tests rmw_impl_id_check
* Add tests log_level
* Add bad_alloc test
* Add ini/fini for size 0
* Add tests add_logger_setting
* Add tests logger_settings
* Remove extra deallocation
* Replace allocation with captured rcutils_strdup

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>
  • Loading branch information
Blast545 authored Aug 18, 2020
1 parent 6967075 commit 626c82d
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 5 deletions.
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();
}
104 changes: 103 additions & 1 deletion rcl/test/rcl/test_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rcutils/snprintf.h"

#include "./allocator_testing_utils.h"
#include "../mocking_utils/patch.hpp"
#include "../src/rcl/init_options_impl.h"

#ifdef RMW_IMPLEMENTATION
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,82 @@ 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));
}

// Define dummy comparison operators for rcutils_allocator_t type for use with the Mimick Library
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, >)
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::inject_on_return("lib:rcl", rmw_init_options_fini, RMW_RET_ERROR);
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();
auto mock = mocking_utils::inject_on_return("lib:rcl", rmw_init_options_fini, RMW_RET_ERROR);
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

0 comments on commit 626c82d

Please sign in to comment.