Skip to content

Commit

Permalink
Use test fixtures to create SROS artifacts (#524)
Browse files Browse the repository at this point in the history
The current implementation creates SROS artifacts during the configure
phase. This is undesirable for a variety of reasons, such as:
* The ros2 binary is invoked during build, meaning that a regression in
  that tool will break the entire CI build instead of appearing as a
  test failure.
* Some of the environment is captured during the artifact generation
  and it may not be the same when the tests are invoked. Namely,
  changes to ROS_DOMAIN_ID between build and test will break.

The test fixtures work similarly to tests themselves, and are
automatically run if any tests requiring them are enabled. If the
fixture fails, the dependent tests are not attempted.

(cherry picked from commit d98fcb9)

Signed-off-by: Scott K Logan <logans@cottsay.net>
  • Loading branch information
cottsay committed Aug 8, 2023
1 parent 83a4e48 commit c451648
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions test_security/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.7)

project(test_security)

Expand Down Expand Up @@ -67,6 +67,7 @@ if(BUILD_TESTING)
set_tests_properties(
${target}${target_suffix}
PROPERTIES REQUIRED_FILES "$<TARGET_FILE:${target}${target_suffix}>"
FIXTURES_REQUIRED "sros_artifacts"
)
endif()
endfunction()
Expand Down Expand Up @@ -144,6 +145,7 @@ if(BUILD_TESTING)
set_tests_properties(
test_secure_publisher_subscriber${test_suffix}
PROPERTIES DEPENDS "test_secure_publisher_cpp__${rmw_implementation};test_secure_subscriber_cpp__${rmw_implementation}"
FIXTURES_REQUIRED "sros_artifacts"
)
endif()
endwhile()
Expand Down Expand Up @@ -184,6 +186,7 @@ if(BUILD_TESTING)
set_tests_properties(
test_secure_publisher_subscriber${test_suffix}
PROPERTIES DEPENDS "test_secure_publisher_cpp__${rmw_implementation};test_secure_subscriber_cpp__${rmw_implementation}"
FIXTURES_REQUIRED "sros_artifacts"
)
endif()
endwhile()
Expand Down Expand Up @@ -224,6 +227,7 @@ if(BUILD_TESTING)
set_tests_properties(
test_secure_publisher_subscriber${test_suffix}
PROPERTIES DEPENDS "test_secure_publisher_cpp__${rmw_implementation};test_secure_subscriber_cpp__${rmw_implementation}"
FIXTURES_REQUIRED "sros_artifacts"
)
endif()
endwhile()
Expand Down Expand Up @@ -308,28 +312,57 @@ if(BUILD_TESTING)
set(KEYSTORE_DIRECTORY_NATIVE_PATH "${KEYSTORE_DIRECTORY}")
endif()

#
# CTest Fixtures
#
# These fixtures are set up as needed at the beginning of the test run to
# support any selected tests which require them.
#
# * sros_artifacts: This fixture generates SROS2 security artifacts needed
# for the tests in this package.
#

# remove previously generated enclaves
add_test(NAME pre_clean_artifacts
COMMAND ${CMAKE_COMMAND} -E rm -rf "${KEYSTORE_DIRECTORY}/enclaves/"
)
set_tests_properties(pre_clean_artifacts PROPERTIES
FIXTURES_SETUP sros_artifacts
)

# generate security artifacts using sros2
find_program(PROGRAM ros2)

set(node_names_list "/publisher;/subscriber;/publisher_missing_key;/publisher_invalid_cert")
file(REMOVE_RECURSE "${KEYSTORE_DIRECTORY}/enclaves/publisher_invalid_cert")
set(generate_artifacts_command ${PROGRAM} security generate_artifacts -k ${KEYSTORE_DIRECTORY_NATIVE_PATH} -e ${node_names_list})
execute_process(
add_test(NAME generate_artifacts
COMMAND ${generate_artifacts_command}
RESULT_VARIABLE GENERATE_ARTIFACTS_RESULT
ERROR_VARIABLE GENERATE_ARTIFACTS_ERROR
)
if(NOT ${GENERATE_ARTIFACTS_RESULT} EQUAL 0)
message(FATAL_ERROR "Failed to generate security artifacts: ${GENERATE_ARTIFACTS_ERROR}")
endif()
set_tests_properties(generate_artifacts PROPERTIES
DEPENDS pre_clean_artifacts
FIXTURES_SETUP sros_artifacts
)

# deleting key of /publisher_missing_key
file(REMOVE "${KEYSTORE_DIRECTORY}/enclaves/publisher_missing_key/key.pem")
add_test(NAME remove_missing_key
COMMAND ${CMAKE_COMMAND} -E rm "${KEYSTORE_DIRECTORY}/enclaves/publisher_missing_key/key.pem"
)
set_tests_properties(remove_missing_key PROPERTIES
DEPENDS generate_artifacts
FIXTURES_SETUP sros_artifacts
)

# copy invalid certificate from source tree
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test/test_security_files/publisher_invalid_cert/cert.pem
DESTINATION ${KEYSTORE_DIRECTORY}/enclaves/publisher_invalid_cert/
add_test(NAME copy_invalid_cert
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/test/test_security_files/publisher_invalid_cert/cert.pem"
"${KEYSTORE_DIRECTORY}/enclaves/publisher_invalid_cert/cert.pem"
)
set_tests_properties(copy_invalid_cert PROPERTIES
DEPENDS generate_artifacts
FIXTURES_SETUP sros_artifacts
)

call_for_each_rmw_implementation(targets)
endif()
endif() # BUILD_TESTING
Expand Down

0 comments on commit c451648

Please sign in to comment.