Skip to content

Commit 31f5c99

Browse files
hidmicahcorde
authored andcommitted
Extend rcl_expand_topic_name() API test coverage. (#758)
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
1 parent 6d95545 commit 31f5c99

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

rcl/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ rcl_add_custom_gtest(test_validate_topic_name
377377
rcl_add_custom_gtest(test_expand_topic_name
378378
SRCS rcl/test_expand_topic_name.cpp
379379
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
380-
LIBRARIES ${PROJECT_NAME}
380+
LIBRARIES ${PROJECT_NAME} mimick
381381
)
382382

383383
rcl_add_custom_gtest(test_security

rcl/test/rcl/test_expand_topic_name.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@
1919
#include <tuple>
2020
#include <vector>
2121

22+
#include "rcutils/repl_str.h"
23+
#include "rcutils/strdup.h"
24+
2225
#include "rcl/expand_topic_name.h"
2326

2427
#include "rcl/error_handling.h"
2528

29+
#include "rmw/validate_namespace.h"
30+
#include "rmw/validate_node_name.h"
31+
2632
#include "./allocator_testing_utils.h"
33+
#include "../mocking_utils/patch.hpp"
2734

2835
using namespace std::string_literals;
2936

@@ -137,6 +144,90 @@ TEST(test_expand_topic_name, invalid_arguments) {
137144
ASSERT_EQ(RCL_RET_OK, ret);
138145
}
139146

147+
// Define dummy comparison operators for rcutils_allocator_t type
148+
// to use with the Mimick mocking library
149+
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, ==)
150+
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, !=)
151+
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, <)
152+
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, >)
153+
154+
TEST(test_expand_topic_name, internal_error) {
155+
constexpr char node_name[] = "bar";
156+
constexpr char ns[] = "/foo";
157+
158+
rcutils_string_map_t subs = rcutils_get_zero_initialized_string_map();
159+
rcutils_ret_t uret = rcutils_string_map_init(&subs, 0, rcutils_get_default_allocator());
160+
ASSERT_EQ(RCUTILS_RET_OK, uret) << rcutils_get_error_string().str;
161+
rcl_ret_t ret = rcl_get_default_topic_name_substitutions(&subs);
162+
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
163+
rcl_allocator_t allocator = rcl_get_default_allocator();
164+
char * expanded_topic_name = nullptr;
165+
166+
{
167+
constexpr char topic_name[] = "/test";
168+
auto mock = mocking_utils::patch_to_fail(
169+
"lib:rcl", rmw_validate_node_name, "internal error", RMW_RET_ERROR);
170+
ret = rcl_expand_topic_name(
171+
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
172+
EXPECT_EQ(RCL_RET_ERROR, ret);
173+
EXPECT_TRUE(rcl_error_is_set());
174+
rcl_reset_error();
175+
}
176+
177+
{
178+
constexpr char topic_name[] = "/test";
179+
auto mock = mocking_utils::patch_to_fail(
180+
"lib:rcl", rmw_validate_namespace, "internal error", RMW_RET_ERROR);
181+
ret = rcl_expand_topic_name(
182+
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
183+
EXPECT_EQ(RCL_RET_ERROR, ret);
184+
EXPECT_TRUE(rcl_error_is_set());
185+
rcl_reset_error();
186+
}
187+
188+
{
189+
constexpr char topic_name_with_valid_substitution[] = "{node}/test";
190+
auto mock = mocking_utils::patch_to_fail(
191+
"lib:rcl", rcutils_strndup, "failed to allocate", nullptr);
192+
ret = rcl_expand_topic_name(
193+
topic_name_with_valid_substitution, node_name, ns,
194+
&subs, allocator, &expanded_topic_name);
195+
EXPECT_EQ(RCL_RET_BAD_ALLOC, ret);
196+
EXPECT_TRUE(rcl_error_is_set());
197+
rcl_reset_error();
198+
199+
constexpr char topic_name_with_unknown_substitution[] = "{unknown}/test";
200+
ret = rcl_expand_topic_name(
201+
topic_name_with_unknown_substitution, node_name, ns,
202+
&subs, allocator, &expanded_topic_name);
203+
EXPECT_EQ(RCL_RET_UNKNOWN_SUBSTITUTION, ret);
204+
EXPECT_TRUE(rcl_error_is_set());
205+
rcl_reset_error();
206+
}
207+
208+
{
209+
constexpr char topic_name[] = "{node}/test";
210+
auto mock = mocking_utils::patch_to_fail(
211+
"lib:rcl", rcutils_repl_str, "failed to allocate", nullptr);
212+
ret = rcl_expand_topic_name(
213+
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
214+
EXPECT_EQ(RCL_RET_BAD_ALLOC, ret);
215+
EXPECT_TRUE(rcl_error_is_set());
216+
rcl_reset_error();
217+
}
218+
219+
{
220+
constexpr char topic_name[] = "/test";
221+
auto mock = mocking_utils::patch_to_fail(
222+
"lib:rcl", rcutils_strdup, "failed to allocate", nullptr);
223+
ret = rcl_expand_topic_name(
224+
topic_name, node_name, ns, &subs, allocator, &expanded_topic_name);
225+
EXPECT_EQ(RCL_RET_BAD_ALLOC, ret);
226+
EXPECT_TRUE(rcl_error_is_set());
227+
rcl_reset_error();
228+
}
229+
}
230+
140231
TEST(test_expand_topic_name, various_valid_topics) {
141232
rcl_ret_t ret;
142233
rcl_allocator_t allocator = rcl_get_default_allocator();

0 commit comments

Comments
 (0)