Skip to content
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

Add test coverage for GUID utilities. #387

Merged
merged 3 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions rmw_fastrtps_shared_cpp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ if(TARGET test_rmw_init_options)
ament_target_dependencies(test_rmw_init_options osrf_testing_tools_cpp rcutils rmw)
target_link_libraries(test_rmw_init_options ${PROJECT_NAME})
endif()

ament_add_gtest(test_guid_utils test_guid_utils.cpp)
if(TARGET test_guid_utils)
target_link_libraries(test_guid_utils ${PROJECT_NAME})
endif()
58 changes: 58 additions & 0 deletions rmw_fastrtps_shared_cpp/test/test_guid_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gtest/gtest.h"

#include "fastrtps/rtps/common/Guid.h"

#include "rmw_fastrtps_shared_cpp/guid_utils.hpp"

using rmw_fastrtps_shared_cpp::copy_from_byte_array_to_fastrtps_guid;
using rmw_fastrtps_shared_cpp::copy_from_fastrtps_guid_to_byte_array;

static constexpr size_t byte_array_size =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what does byte_array_size evaluate to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 16, but I preferred not to hard-code it.

eprosima::fastrtps::rtps::GuidPrefix_t::size +
eprosima::fastrtps::rtps::EntityId_t::size;

TEST(GUIDUtilsTest, bad_arguments) {
eprosima::fastrtps::rtps::GUID_t guid;
uint8_t byte_array[byte_array_size] = {0};
uint8_t * null_byte_array = nullptr;
EXPECT_DEATH(copy_from_byte_array_to_fastrtps_guid(byte_array, nullptr), "");
EXPECT_DEATH(copy_from_byte_array_to_fastrtps_guid(null_byte_array, &guid), "");
EXPECT_DEATH(copy_from_fastrtps_guid_to_byte_array(guid, null_byte_array), "");
}

TEST(GUIDUtilsTest, byte_array_to_guid_and_back) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about checking for null arrays using EXPECT_DEATH()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 7bcd434

uint8_t input_byte_array[byte_array_size] = {0};
input_byte_array[0] = 0xA5;
input_byte_array[byte_array_size - 1] = 0x4B;
eprosima::fastrtps::rtps::GUID_t guid;
copy_from_byte_array_to_fastrtps_guid(input_byte_array, &guid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While converting back and forth between the two does ensure that no data is lost, it doesn't really test that it was converted to the correct output. It is also possible that this won't catch errors if they are introduced in the header if somehow both methods correct each other's mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true, I just didn't want to pollute the test with too much information as to how it gets copied in-n-out.

uint8_t output_byte_array[byte_array_size] = {0};
copy_from_fastrtps_guid_to_byte_array(guid, output_byte_array);
EXPECT_EQ(0, memcmp(input_byte_array, output_byte_array, byte_array_size));
}

TEST(GUIDUtilsTest, guid_to_byte_array_and_back) {
eprosima::fastrtps::rtps::GuidPrefix_t prefix;
prefix.value[0] = 0xD2;
prefix.value[eprosima::fastrtps::rtps::GuidPrefix_t::size - 1] = 0x3E;
eprosima::fastrtps::rtps::GUID_t input_guid{prefix, 1234};
uint8_t byte_array[byte_array_size] = {0};
copy_from_fastrtps_guid_to_byte_array(input_guid, byte_array);
eprosima::fastrtps::rtps::GUID_t output_guid;
copy_from_byte_array_to_fastrtps_guid(byte_array, &output_guid);
EXPECT_EQ(input_guid, output_guid);
}