Skip to content

Commit

Permalink
Extend rcl_expand_topic_name() API test coverage. (#758)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored Aug 20, 2020
1 parent 686ba35 commit 6d824bc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
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

0 comments on commit 6d824bc

Please sign in to comment.