|
19 | 19 | #include <tuple> |
20 | 20 | #include <vector> |
21 | 21 |
|
| 22 | +#include "rcutils/repl_str.h" |
| 23 | +#include "rcutils/strdup.h" |
| 24 | + |
22 | 25 | #include "rcl/expand_topic_name.h" |
23 | 26 |
|
24 | 27 | #include "rcl/error_handling.h" |
25 | 28 |
|
| 29 | +#include "rmw/validate_namespace.h" |
| 30 | +#include "rmw/validate_node_name.h" |
| 31 | + |
26 | 32 | #include "./allocator_testing_utils.h" |
| 33 | +#include "../mocking_utils/patch.hpp" |
27 | 34 |
|
28 | 35 | using namespace std::string_literals; |
29 | 36 |
|
@@ -137,6 +144,90 @@ TEST(test_expand_topic_name, invalid_arguments) { |
137 | 144 | ASSERT_EQ(RCL_RET_OK, ret); |
138 | 145 | } |
139 | 146 |
|
| 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 | + |
140 | 231 | TEST(test_expand_topic_name, various_valid_topics) { |
141 | 232 | rcl_ret_t ret; |
142 | 233 | rcl_allocator_t allocator = rcl_get_default_allocator(); |
|
0 commit comments