-
Notifications
You must be signed in to change notification settings - Fork 181
Add fault-injection unit tests (coverage part 2/3) #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,16 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "osrf_testing_tools_cpp/scope_exit.hpp" | ||
| #include "rcl_yaml_param_parser/parser.h" | ||
| #include "rcutils/error_handling.h" | ||
| #include "rcutils/filesystem.h" | ||
| #include "rcutils/testing/fault_injection.h" | ||
| #include "./time_bomb_allocator_testing_utils.h" | ||
|
|
||
| TEST(RclYamlParamParser, node_init_fini) { | ||
|
|
@@ -308,46 +313,51 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) { | |
| { | ||
| allocator.deallocate(test_path, allocator.state); | ||
| }); | ||
| char * path = rcutils_join_path(test_path, "correct_config.yaml", allocator); | ||
| ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str; | ||
| OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( | ||
| { | ||
| allocator.deallocate(path, allocator.state); | ||
| }); | ||
| ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path; | ||
|
|
||
| rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator); | ||
| EXPECT_TRUE(rcl_parse_yaml_file(path, params_hdl)); | ||
| rcl_yaml_node_struct_fini(params_hdl); | ||
| params_hdl = NULL; | ||
|
|
||
| // Check sporadic failing malloc calls | ||
| for (int i = 0; i < 100; ++i) { | ||
| params_hdl = rcl_yaml_node_struct_init(get_time_bomb_allocator()); | ||
| ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str; | ||
|
|
||
| set_time_bomb_allocator_malloc_count(params_hdl->allocator, i); | ||
| bool res = rcl_parse_yaml_file(path, params_hdl); | ||
| // Not verifying res is true or false here, because eventually it will come back with an ok | ||
| // result. We're just trying to make sure that bad allocations are properly handled | ||
| (void)res; | ||
| rcl_yaml_node_struct_fini(params_hdl); | ||
| params_hdl = NULL; | ||
| } | ||
|
|
||
| // Check sporadic failing calloc calls | ||
| for (int i = 0; i < 100; ++i) { | ||
| params_hdl = rcl_yaml_node_struct_init(get_time_bomb_allocator()); | ||
| ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str; | ||
|
|
||
| set_time_bomb_allocator_calloc_count(params_hdl->allocator, i); | ||
|
|
||
| bool res = rcl_parse_yaml_file(path, params_hdl); | ||
| // Not verifying res is true or false here, because eventually it will come back with an ok | ||
| // result. We're just trying to make sure that bad allocations are properly handled | ||
| (void)res; | ||
| rcl_yaml_node_struct_fini(params_hdl); | ||
| params_hdl = NULL; | ||
| const std::vector<std::string> filenames = { | ||
| "correct_config.yaml", | ||
| "empty_string.yaml", | ||
| "indented_name_space.yaml", | ||
| "max_num_params.yaml", | ||
| "multi_ns_correct.yaml", | ||
| "no_alias_support.yaml", | ||
| "no_value1.yaml", | ||
| "overlay.yaml", | ||
| "params_with_no_node.yaml", | ||
| "root_ns.yaml", | ||
| "seq_map1.yaml", | ||
| "seq_map2.yaml", | ||
| "string_array_with_quoted_number.yaml" | ||
| }; | ||
|
|
||
| for (auto & filename : filenames) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brawner nit: can be made
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think const is already deduced by auto since filenames is const. |
||
| SCOPED_TRACE(filename); | ||
| char * path = rcutils_join_path(test_path, filename.c_str(), allocator); | ||
| ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str; | ||
| OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( | ||
| { | ||
| allocator.deallocate(path, allocator.state); | ||
| }); | ||
| ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path; | ||
|
|
||
| RCUTILS_FAULT_INJECTION_TEST( | ||
| { | ||
| rcutils_allocator_t allocator = rcutils_get_default_allocator(); | ||
| rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator); | ||
| if (NULL == params_hdl) { | ||
| continue; | ||
| } | ||
|
|
||
| bool res = rcl_parse_yaml_file(path, params_hdl); | ||
| // Not verifying res is true or false here, because eventually it will come back with an ok | ||
| // result. We're just trying to make sure that bad allocations are properly handled | ||
| (void)res; | ||
|
|
||
| // If `rcutils_string_array_fini` fails, there will be a small memory leak here. | ||
| // However, it's necessary for coverage | ||
| rcl_yaml_node_struct_fini(params_hdl); | ||
| params_hdl = NULL; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.