|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -#include "gtest/gtest.h" |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +#include <yaml.h> |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
16 | 22 | #include "osrf_testing_tools_cpp/scope_exit.hpp" |
17 | 23 | #include "rcl_yaml_param_parser/parser.h" |
18 | 24 | #include "rcutils/error_handling.h" |
19 | 25 | #include "rcutils/filesystem.h" |
| 26 | +#include "rcutils/testing/fault_injection.h" |
20 | 27 | #include "./time_bomb_allocator_testing_utils.h" |
21 | 28 |
|
22 | 29 | TEST(RclYamlParamParser, node_init_fini) { |
@@ -308,54 +315,55 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) { |
308 | 315 | { |
309 | 316 | allocator.deallocate(test_path, allocator.state); |
310 | 317 | }); |
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 | | - } |
339 | 318 |
|
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 | + }); |
355 | 364 | } |
356 | 365 | } |
357 | 366 |
|
358 | | - |
359 | 367 | int32_t main(int32_t argc, char ** argv) |
360 | 368 | { |
361 | 369 | ::testing::InitGoogleTest(&argc, argv); |
|
0 commit comments