diff --git a/rcl/test/CMakeLists.txt b/rcl/test/CMakeLists.txt index 411bcbf..3aac54c 100644 --- a/rcl/test/CMakeLists.txt +++ b/rcl/test/CMakeLists.txt @@ -353,7 +353,7 @@ call_for_each_rmw_implementation(test_target) rcl_add_custom_gtest(test_validate_enclave_name SRCS rcl/test_validate_enclave_name.cpp APPEND_LIBRARY_DIRS ${extra_lib_dirs} - LIBRARIES ${PROJECT_NAME} + LIBRARIES ${PROJECT_NAME} mimick ) rcl_add_custom_gtest(test_domain_id diff --git a/rcl/test/rcl/test_validate_enclave_name.cpp b/rcl/test/rcl/test_validate_enclave_name.cpp index 408bdcc..0cc4b1c 100644 --- a/rcl/test/rcl/test_validate_enclave_name.cpp +++ b/rcl/test/rcl/test_validate_enclave_name.cpp @@ -17,11 +17,17 @@ #include #include +#include "rcutils/snprintf.h" + #include "rcl/rcl.h" #include "rcl/validate_enclave_name.h" #include "rcl/error_handling.h" +#include "rmw/validate_namespace.h" + +#include "../mocking_utils/patch.hpp" + TEST(TestValidateEnclaveName, test_validate) { int validation_result; size_t invalid_index; @@ -47,6 +53,59 @@ TEST(TestValidateEnclaveName, test_validate) { RCL_RET_OK, rcl_validate_enclave_name("/foo/bar", &validation_result, &invalid_index)); EXPECT_EQ(RCL_ENCLAVE_NAME_VALID, validation_result); + + { + auto mock = mocking_utils::patch( + "lib:rcl", rmw_validate_namespace_with_size, + [&](auto, auto, int * result, size_t * index) { + if (index) { + *index = 0u; + } + *result = RMW_NAMESPACE_INVALID_TOO_LONG; + return RMW_RET_OK; + }); + + // When applying RMW namespace validation rules, an enclave name may be too + // long for an RMW namespace but not necessarily for an enclave name. + EXPECT_EQ( + RCL_RET_OK, + rcl_validate_enclave_name("/foo/baz", &validation_result, &invalid_index)); + EXPECT_EQ(RCL_ENCLAVE_NAME_VALID, validation_result); + } +} + +TEST(TestValidateEnclaveName, test_validate_on_internal_error) { + int validation_result; + size_t invalid_index; + + { + auto mock = mocking_utils::patch_to_fail( + "lib:rcl", rmw_validate_namespace_with_size, "internal error", RMW_RET_ERROR); + + EXPECT_EQ( + RCL_RET_ERROR, + rcl_validate_enclave_name("/foo", &validation_result, &invalid_index)); + EXPECT_TRUE(rcl_error_is_set()); + rcl_reset_error(); + } + + { + auto mock = mocking_utils::patch( + "lib:rcl", rmw_validate_namespace_with_size, + [&](auto, auto, int * result, size_t * index) { + if (index) { + *index = 0u; + } + *result = -1; + return RMW_RET_OK; + }); + + EXPECT_EQ( + RCL_RET_ERROR, + rcl_validate_enclave_name("/foo", &validation_result, &invalid_index)); + EXPECT_TRUE(rcl_error_is_set()); + rcl_reset_error(); + } } TEST(TestValidateEnclaveName, test_validation_string) {