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

Extend rcl_expand_topic_name() API test coverage. #758

Merged
merged 1 commit into from
Aug 20, 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
2 changes: 1 addition & 1 deletion rcl/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ rcl_add_custom_gtest(test_validate_topic_name
rcl_add_custom_gtest(test_expand_topic_name
SRCS rcl/test_expand_topic_name.cpp
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
LIBRARIES ${PROJECT_NAME}
LIBRARIES ${PROJECT_NAME} mimick
)

rcl_add_custom_gtest(test_security
Expand Down
91 changes: 91 additions & 0 deletions rcl/test/rcl/test_expand_topic_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
#include <tuple>
#include <vector>

#include "rcutils/repl_str.h"
#include "rcutils/strdup.h"

#include "rcl/expand_topic_name.h"

#include "rcl/error_handling.h"

#include "rmw/validate_namespace.h"
#include "rmw/validate_node_name.h"

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

using namespace std::string_literals;

Expand Down Expand Up @@ -137,6 +144,90 @@ TEST(test_expand_topic_name, invalid_arguments) {
ASSERT_EQ(RCL_RET_OK, ret);
}

// Define dummy comparison operators for rcutils_allocator_t type
// to use with the Mimick mocking 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, >)

TEST(test_expand_topic_name, internal_error) {
constexpr char node_name[] = "bar";
constexpr char ns[] = "/foo";

rcutils_string_map_t subs = rcutils_get_zero_initialized_string_map();
rcutils_ret_t uret = rcutils_string_map_init(&subs, 0, rcutils_get_default_allocator());
ASSERT_EQ(RCUTILS_RET_OK, uret) << rcutils_get_error_string().str;
rcl_ret_t ret = rcl_get_default_topic_name_substitutions(&subs);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
rcl_allocator_t allocator = rcl_get_default_allocator();
char * expanded_topic_name = nullptr;

{
constexpr char topic_name[] = "/test";
auto mock = mocking_utils::patch_to_fail(
"lib:rcl", rmw_validate_node_name, "internal error", RMW_RET_ERROR);
ret = rcl_expand_topic_name(
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
EXPECT_EQ(RCL_RET_ERROR, ret);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();
}

{
constexpr char topic_name[] = "/test";
auto mock = mocking_utils::patch_to_fail(
"lib:rcl", rmw_validate_namespace, "internal error", RMW_RET_ERROR);
ret = rcl_expand_topic_name(
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
EXPECT_EQ(RCL_RET_ERROR, ret);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();
}

{
constexpr char topic_name_with_valid_substitution[] = "{node}/test";
auto mock = mocking_utils::patch_to_fail(
"lib:rcl", rcutils_strndup, "failed to allocate", nullptr);
ret = rcl_expand_topic_name(
topic_name_with_valid_substitution, node_name, ns,
&subs, allocator, &expanded_topic_name);
EXPECT_EQ(RCL_RET_BAD_ALLOC, ret);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();

constexpr char topic_name_with_unknown_substitution[] = "{unknown}/test";
ret = rcl_expand_topic_name(
topic_name_with_unknown_substitution, node_name, ns,
&subs, allocator, &expanded_topic_name);
EXPECT_EQ(RCL_RET_UNKNOWN_SUBSTITUTION, ret);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();
}

{
constexpr char topic_name[] = "{node}/test";
auto mock = mocking_utils::patch_to_fail(
"lib:rcl", rcutils_repl_str, "failed to allocate", nullptr);
ret = rcl_expand_topic_name(
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
EXPECT_EQ(RCL_RET_BAD_ALLOC, ret);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();
}

{
constexpr char topic_name[] = "/test";
auto mock = mocking_utils::patch_to_fail(
"lib:rcl", rcutils_strdup, "failed to allocate", nullptr);
ret = rcl_expand_topic_name(
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
EXPECT_EQ(RCL_RET_BAD_ALLOC, ret);
EXPECT_TRUE(rcl_error_is_set());
rcl_reset_error();
}
}

TEST(test_expand_topic_name, various_valid_topics) {
rcl_ret_t ret;
rcl_allocator_t allocator = rcl_get_default_allocator();
Expand Down