Skip to content

Commit ecd08bc

Browse files
committed
Fault injection tests for rcl_yaml
Signed-off-by: Stephen Brawner <[email protected]>
1 parent cf14a57 commit ecd08bc

File tree

3 files changed

+107
-45
lines changed

3 files changed

+107
-45
lines changed

rcl_yaml_param_parser/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ if(BUILD_TESTING)
6767
"rcutils"
6868
"osrf_testing_tools_cpp"
6969
)
70+
target_compile_definitions(test_namespace PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
7071
target_link_libraries(test_namespace ${PROJECT_NAME})
7172
endif()
7273

@@ -79,6 +80,7 @@ if(BUILD_TESTING)
7980
"rcutils"
8081
"osrf_testing_tools_cpp"
8182
)
83+
target_compile_definitions(test_node_params PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
8284
target_link_libraries(test_node_params ${PROJECT_NAME})
8385
endif()
8486

@@ -101,6 +103,7 @@ if(BUILD_TESTING)
101103
"rcutils"
102104
"osrf_testing_tools_cpp"
103105
)
106+
target_compile_definitions(test_parse PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
104107
target_link_libraries(test_parse ${PROJECT_NAME} mimick)
105108
endif()
106109

@@ -114,6 +117,7 @@ if(BUILD_TESTING)
114117
"osrf_testing_tools_cpp"
115118
)
116119
target_link_libraries(test_parser ${PROJECT_NAME})
120+
target_compile_definitions(test_parser PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
117121
endif()
118122

119123
ament_add_gtest(test_yaml_variant
@@ -125,6 +129,7 @@ if(BUILD_TESTING)
125129
"rcutils"
126130
"osrf_testing_tools_cpp"
127131
)
132+
target_compile_definitions(test_yaml_variant PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
128133
target_link_libraries(test_yaml_variant ${PROJECT_NAME})
129134
endif()
130135

rcl_yaml_param_parser/test/test_namespace.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,52 @@ TEST(TestNamespace, replace_ns) {
9696
EXPECT_STREQ(expected_param_ns, ns_tracker.parameter_ns);
9797
EXPECT_EQ(3u, ns_tracker.num_parameter_ns);
9898
}
99+
100+
TEST(TestNamespace, replace_ns_maybe_fail) {
101+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
102+
namespace_tracker_t ns_tracker;
103+
ns_tracker.node_ns = rcutils_strdup("node1/node2", allocator);
104+
ASSERT_STREQ("node1/node2", ns_tracker.node_ns);
105+
ns_tracker.parameter_ns = rcutils_strdup("param1.param2", allocator);
106+
ASSERT_STREQ("param1.param2", ns_tracker.parameter_ns);
107+
ns_tracker.num_node_ns = 2;
108+
ns_tracker.num_parameter_ns = 2;
109+
110+
char * expected_ns = rcutils_strdup("new_ns1/new_ns2/new_ns3", allocator);
111+
ASSERT_STREQ("new_ns1/new_ns2/new_ns3", expected_ns);
112+
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
113+
{
114+
allocator.deallocate(expected_ns, allocator.state);
115+
});
116+
117+
char * expected_param_ns =
118+
rcutils_strdup("new_param1.new_param2.new_param3", allocator);
119+
ASSERT_STREQ("new_param1.new_param2.new_param3", expected_param_ns);
120+
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
121+
{
122+
allocator.deallocate(expected_param_ns, allocator.state);
123+
});
124+
125+
RCUTILS_FAULT_INJECTION_TEST(
126+
{
127+
rcutils_ret_t ret =
128+
replace_ns(&ns_tracker, expected_ns, 3, NS_TYPE_NODE, allocator);
129+
if (RCUTILS_RET_OK != ret) {
130+
EXPECT_EQ(nullptr, ns_tracker.node_ns);
131+
rcutils_reset_error();
132+
} else {
133+
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
134+
EXPECT_STREQ(expected_ns, ns_tracker.node_ns);
135+
EXPECT_EQ(3u, ns_tracker.num_node_ns);
136+
}
137+
138+
ret = replace_ns(&ns_tracker, expected_param_ns, 3, NS_TYPE_PARAM, allocator);
139+
if (RCUTILS_RET_OK != ret) {
140+
EXPECT_EQ(nullptr, ns_tracker.parameter_ns);
141+
rcutils_reset_error();
142+
} else {
143+
EXPECT_STREQ(expected_param_ns, ns_tracker.parameter_ns);
144+
EXPECT_EQ(3u, ns_tracker.num_parameter_ns);
145+
}
146+
});
147+
}

rcl_yaml_param_parser/test/test_parser.cpp

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "gtest/gtest.h"
15+
#include <gtest/gtest.h>
16+
17+
#include <yaml.h>
18+
19+
#include <string>
20+
#include <vector>
21+
1622
#include "osrf_testing_tools_cpp/scope_exit.hpp"
1723
#include "rcl_yaml_param_parser/parser.h"
1824
#include "rcutils/error_handling.h"
1925
#include "rcutils/filesystem.h"
26+
#include "rcutils/testing/fault_injection.h"
2027
#include "./time_bomb_allocator_testing_utils.h"
2128

2229
TEST(RclYamlParamParser, node_init_fini) {
@@ -308,54 +315,55 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) {
308315
{
309316
allocator.deallocate(test_path, allocator.state);
310317
});
311-
char * path = rcutils_join_path(test_path, "correct_config.yaml", allocator);
312-
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
313-
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
314-
{
315-
allocator.deallocate(path, allocator.state);
316-
});
317-
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
318-
319-
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
320-
EXPECT_TRUE(rcl_parse_yaml_file(path, params_hdl));
321-
rcl_yaml_node_struct_fini(params_hdl);
322-
params_hdl = NULL;
323-
324-
// Check sporadic failing malloc calls
325-
for (int i = 0; i < 100; ++i) {
326-
params_hdl = rcl_yaml_node_struct_init(get_time_bomb_allocator());
327-
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
328-
329-
set_time_bomb_allocator_malloc_count(params_hdl->allocator, i);
330-
bool res = rcl_parse_yaml_file(path, params_hdl);
331-
// Not verifying res is true or false here, because eventually it will come back with an ok
332-
// result. We're just trying to make sure that bad allocations are properly handled
333-
if (res) {
334-
// This is already freed in the case of a non-ok error in rcl_parse_yaml_file
335-
rcl_yaml_node_struct_fini(params_hdl);
336-
params_hdl = NULL;
337-
}
338-
}
339318

340-
// Check sporadic failing calloc calls
341-
for (int i = 0; i < 100; ++i) {
342-
params_hdl = rcl_yaml_node_struct_init(get_time_bomb_allocator());
343-
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
344-
345-
set_time_bomb_allocator_calloc_count(params_hdl->allocator, i);
346-
347-
bool res = rcl_parse_yaml_file(path, params_hdl);
348-
// Not verifying res is true or false here, because eventually it will come back with an ok
349-
// result. We're just trying to make sure that bad allocations are properly handled
350-
if (res) {
351-
// This is already freed in the case of a non-ok error in rcl_parse_yaml_file
352-
rcl_yaml_node_struct_fini(params_hdl);
353-
params_hdl = NULL;
354-
}
319+
const std::vector<std::string> filenames = {
320+
"correct_config.yaml",
321+
"empty_string.yaml",
322+
"indented_name_space.yaml",
323+
"max_num_params.yaml",
324+
"multi_ns_correct.yaml",
325+
"no_alias_support.yaml",
326+
"no_value1.yaml",
327+
"overlay.yaml",
328+
"params_with_no_node.yaml",
329+
"root_ns.yaml",
330+
"seq_map1.yaml",
331+
"seq_map2.yaml",
332+
"string_array_with_quoted_number.yaml"
333+
};
334+
335+
for (auto & filename : filenames) {
336+
char * path = rcutils_join_path(test_path, filename.c_str(), allocator);
337+
std::cout << "Reading: " << path << std::endl;
338+
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
339+
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
340+
{
341+
allocator.deallocate(path, allocator.state);
342+
});
343+
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
344+
345+
// Check sporadic failing malloc calls
346+
RCUTILS_FAULT_INJECTION_TEST(
347+
{
348+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
349+
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
350+
if (NULL == params_hdl) {
351+
continue;
352+
}
353+
354+
bool res = rcl_parse_yaml_file(path, params_hdl);
355+
// Not verifying res is true or false here, because eventually it will come back with an ok
356+
// result. We're just trying to make sure that bad allocations are properly handled
357+
358+
if (res) {
359+
// This is already freed in the case of a non-ok error in rcl_parse_yaml_file
360+
rcl_yaml_node_struct_fini(params_hdl);
361+
params_hdl = NULL;
362+
}
363+
});
355364
}
356365
}
357366

358-
359367
int32_t main(int32_t argc, char ** argv)
360368
{
361369
::testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)