Skip to content

Commit

Permalink
Complete rcl enclave validation API coverage. (#751)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored and ahcorde committed Oct 30, 2020
1 parent ad8b2b7 commit 44235ee
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rcl/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions rcl/test/rcl/test_validate_enclave_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
#include <string>
#include <vector>

#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;
Expand All @@ -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) {
Expand Down

0 comments on commit 44235ee

Please sign in to comment.