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 fault injection macros and test #509

Merged
merged 2 commits into from
Aug 25, 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
10 changes: 9 additions & 1 deletion rosidl_runtime_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
endif()

find_package(ament_cmake_ros REQUIRED)
find_package(rcutils REQUIRED)
find_package(rosidl_typesupport_interface REQUIRED)

add_library(${PROJECT_NAME}
Expand All @@ -26,6 +27,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")
ament_target_dependencies(${PROJECT_NAME}
"rcutils"
"rosidl_typesupport_interface")
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set_target_properties(${PROJECT_NAME} PROPERTIES
Expand All @@ -36,7 +38,11 @@ if(WIN32)
PRIVATE "ROSIDL_GENERATOR_C_BUILDING_DLL")
endif()

ament_export_dependencies(rosidl_typesupport_interface)
if(BUILD_TESTING AND NOT RCUTILS_DISABLE_FAULT_INJECTION)
target_compile_definitions(${PROJECT_NAME} PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
endif()

ament_export_dependencies(rcutils rosidl_typesupport_interface)
ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_export_targets(${PROJECT_NAME})
Expand Down Expand Up @@ -86,12 +92,14 @@ if(BUILD_TESTING)
if(TARGET test_string_functions)
target_include_directories(test_string_functions PUBLIC include)
target_link_libraries(test_string_functions ${PROJECT_NAME})
target_compile_definitions(test_string_functions PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
endif()

ament_add_gtest(test_u16string_functions test/test_u16string_functions.cpp)
if(TARGET test_u16string_functions)
target_include_directories(test_u16string_functions PUBLIC include)
target_link_libraries(test_u16string_functions ${PROJECT_NAME})
target_compile_definitions(test_u16string_functions PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
endif()
endif()

Expand Down
1 change: 1 addition & 0 deletions rosidl_runtime_c/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<buildtool_export_depend>ament_cmake</buildtool_export_depend>

<build_export_depend>rcutils</build_export_depend>
<build_export_depend>rosidl_typesupport_interface</build_export_depend>

<test_depend>ament_lint_auto</test_depend>
Expand Down
6 changes: 6 additions & 0 deletions rosidl_runtime_c/src/string_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
#include <string.h>
#include <stdio.h>

#include "rcutils/macros.h"

bool
rosidl_runtime_c__String__init(rosidl_runtime_c__String * str)
{
RCUTILS_CAN_RETURN_WITH_ERROR_OF(false);

if (!str) {
return false;
}
Expand Down Expand Up @@ -113,6 +117,8 @@ bool
rosidl_runtime_c__String__Sequence__init(
rosidl_runtime_c__String__Sequence * sequence, size_t size)
{
RCUTILS_CAN_RETURN_WITH_ERROR_OF(false);

if (!sequence) {
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions rosidl_runtime_c/src/u16string_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
#include <string.h>
#include <stdio.h>

#include "rcutils/macros.h"

bool
rosidl_runtime_c__U16String__init(rosidl_runtime_c__U16String * str)
{
RCUTILS_CAN_RETURN_WITH_ERROR_OF(false);

if (!str) {
return false;
}
Expand Down Expand Up @@ -156,6 +160,8 @@ bool
rosidl_runtime_c__U16String__Sequence__init(
rosidl_runtime_c__U16String__Sequence * sequence, size_t size)
{
RCUTILS_CAN_RETURN_WITH_ERROR_OF(false);

if (!sequence) {
return false;
}
Expand Down
16 changes: 16 additions & 0 deletions rosidl_runtime_c/test/test_string_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "rosidl_runtime_c/string.h"
#include "rosidl_runtime_c/string_functions.h"

#include "rcutils/testing/fault_injection.h"

TEST(string_functions, init_fini_empty_string) {
rosidl_runtime_c__String empty_string;
EXPECT_TRUE(rosidl_runtime_c__String__init(&empty_string));
Expand Down Expand Up @@ -152,3 +154,17 @@ TEST(string_functions, create_destroy_sequence) {
EXPECT_EQ(sequence->capacity, seq_size);
rosidl_runtime_c__String__Sequence__destroy(sequence);
}

TEST(string_functions, create_destroy_sequence_maybe_fail) {
rosidl_runtime_c__String__Sequence * sequence = nullptr;
constexpr size_t seq_size = 10u;

RCUTILS_FAULT_INJECTION_TEST(
{
sequence = rosidl_runtime_c__String__Sequence__create(seq_size);
if (nullptr != sequence) {
rosidl_runtime_c__String__Sequence__destroy(sequence);
sequence = nullptr;
}
});
}
15 changes: 15 additions & 0 deletions rosidl_runtime_c/test/test_u16string_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "rosidl_runtime_c/u16string.h"
#include "rosidl_runtime_c/u16string_functions.h"

#include "rcutils/testing/fault_injection.h"

TEST(u16string_functions, init_fini_empty_string) {
rosidl_runtime_c__U16String empty_string;
EXPECT_TRUE(rosidl_runtime_c__U16String__init(&empty_string));
Expand Down Expand Up @@ -182,3 +184,16 @@ TEST(u16string_functions, create_destroy_sequence) {
EXPECT_EQ(sequence->capacity, seq_size);
rosidl_runtime_c__U16String__Sequence__destroy(sequence);
}

TEST(string_functions, create_destroy_sequence_maybe_fail) {
rosidl_runtime_c__U16String__Sequence * sequence = nullptr;
constexpr size_t seq_size = 10u;
RCUTILS_FAULT_INJECTION_TEST(
{
sequence = rosidl_runtime_c__U16String__Sequence__create(seq_size);
if (nullptr != sequence) {
rosidl_runtime_c__U16String__Sequence__destroy(sequence);
sequence = nullptr;
}
});
}