Skip to content

Commit

Permalink
Test rcl_remove_ros_args behavior with invalid input.
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic committed Oct 15, 2019
1 parent f7da89e commit 002cd09
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions rcl/test/rcl/test_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,63 @@ TEST_F(CLASSNAME(TestArgumentsFixture, RMW_IMPLEMENTATION), test_fini_twice) {
rcl_reset_error();
}

TEST_F(CLASSNAME(TestArgumentsFixture, RMW_IMPLEMENTATION), test_bad_remove_ros_args) {
const char * argv[] = {"process_name"};
int argc = sizeof(argv) / sizeof(const char *);

rcl_allocator_t default_allocator = rcl_get_default_allocator();
rcl_arguments_t parsed_args = rcl_get_zero_initialized_arguments();
rcl_ret_t ret = rcl_parse_arguments(argc, argv, default_allocator, &parsed_args);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
EXPECT_EQ(RCL_RET_OK, rcl_arguments_fini(&parsed_args));
});

const char ** nonros_argv = NULL;
int nonros_argc = 0;

EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
NULL, &parsed_args, default_allocator, &nonros_argc, &nonros_argv));

EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
argv, NULL, default_allocator, &nonros_argc, &nonros_argv));

rcl_allocator_t zero_initialized_allocator =
(rcl_allocator_t)rcutils_get_zero_initialized_allocator();
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
argv, &parsed_args, zero_initialized_allocator, &nonros_argc, &nonros_argv));

EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
argv, &parsed_args, default_allocator, NULL, &nonros_argv));

EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
argv, &parsed_args, default_allocator, &nonros_argc, NULL));

rcl_arguments_t zero_initialized_parsed_args = rcl_get_zero_initialized_arguments();
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
argv, &zero_initialized_parsed_args, default_allocator, &nonros_argc, &nonros_argv));

const char * stack_allocated_nonros_argv[] = {"--foo", "--bar"};
const char ** initialized_nonros_argv = stack_allocated_nonros_argv;

This comment has been minimized.

Copy link
@ivanpauno

ivanpauno Oct 15, 2019

Member

nit: this is not needed.
You can directly do in line 614 &stack_allocated_nonros_argv

This comment has been minimized.

Copy link
@hidmic

hidmic Oct 15, 2019

Author Contributor

Not without a cast. stack_allocated_nonros_argv has not decayed yet and thus it is of const char * ([2]) type, not const char **. It's also a bit awkward, to potentially overwrite stack_allocated_nonros_argv and lose the reference to the stack allocated array.

int initialized_nonros_argc = sizeof(stack_allocated_nonros_argv) / sizeof(const char *);

EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remove_ros_arguments(
argv, &parsed_args, default_allocator, &initialized_nonros_argc, &initialized_nonros_argv));

rcl_arguments_t no_parsed_args = rcl_get_zero_initialized_arguments();
ret = rcl_parse_arguments(0, NULL, default_allocator, &no_parsed_args);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
EXPECT_EQ(RCL_RET_OK, rcl_arguments_fini(&no_parsed_args));
});

ret = rcl_remove_ros_arguments(
NULL, &no_parsed_args, default_allocator, &nonros_argc, &nonros_argv);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
EXPECT_TRUE(NULL == nonros_argv);
EXPECT_EQ(0, nonros_argc);
}

TEST_F(CLASSNAME(TestArgumentsFixture, RMW_IMPLEMENTATION), test_remove_ros_args) {
const char * argv[] = {
"process_name", "-d", "--ros-args", "-r", "__ns:=/foo/bar", "-r", "__ns:=/fiz/buz", "--",
Expand Down

0 comments on commit 002cd09

Please sign in to comment.