Skip to content

Commit

Permalink
refactored success scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Abby Xu <abbyxu@amazon.com>
  • Loading branch information
xabxx committed May 3, 2019
1 parent b792b9c commit 7fe5674
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions rcl/src/rcl/security_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ char * rcl_get_secure_root(
// Check which lookup method to use and invoke the relevant function.
const char * ros_security_lookup_type = NULL;
if (rcutils_get_env(ROS_SECURITY_LOOKUP_TYPE_VAR_NAME, &ros_security_lookup_type)) {
allocator->deallocate(ros_secure_root_env, allocator->state);
return NULL;
}
if (0 == strcmp(ros_security_lookup_type,
Expand Down
27 changes: 12 additions & 15 deletions rcl/test/rcl/test_security_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <gtest/gtest.h>

#include <string>
#include <algorithm>
#include "rcl/security_directory.h"
#include "rcutils/filesystem.h"
#include "osrf_testing_tools_cpp/scope_exit.hpp"
Expand All @@ -25,6 +26,11 @@
#define TEST_NODE_NAME "dummy_node"
#define TEST_NODE_NAMESPACE ROOT_NAMESPACE TEST_SECURITY_DIRECTORY_RESOURCES_DIR_NAME

char * root_path;
char * secure_root;
char * base_lookup_dir_fqn;
char envstring[512] = {0};

static int putenv_wrapper(const char * env_var)
{
#ifdef _WIN32
Expand Down Expand Up @@ -78,20 +84,18 @@ class TestGetSecureRoot : public ::testing::Test
});
}

void get_base_lookup_dir_fqn(std::string & path,
const char * resource_dir, const char * resource_dir_name)
void set_base_lookup_dir_fqn(const char * resource_dir, const char * resource_dir_name)
{
base_lookup_dir_fqn = rcutils_join_path(resource_dir,
resource_dir_name, allocator);
std::string putenv_input = ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME "=";
putenv_input += base_lookup_dir_fqn;
path = putenv_input;
memcpy(envstring, putenv_input.c_str(),
std::min(putenv_input.length(), sizeof(envstring) - 1));
putenv_wrapper(envstring);
}

rcl_allocator_t allocator;
char * root_path;
char * secure_root;
char * base_lookup_dir_fqn;
};

TEST_F(TestGetSecureRoot, failureScenarios) {
Expand Down Expand Up @@ -149,10 +153,7 @@ TEST_F(TestGetSecureRoot, successScenarios_root_exactMatch) {
secure_root = rcl_get_secure_root(TEST_NODE_NAME, TEST_NODE_NAMESPACE, &allocator);

/* Include the namespace as part of the root security directory and test root namespace */
std::string path;
get_base_lookup_dir_fqn(path, TEST_RESOURCES_DIRECTORY, TEST_SECURITY_DIRECTORY_RESOURCES_DIR_NAME);
putenv_wrapper(path.c_str());

set_base_lookup_dir_fqn(TEST_RESOURCES_DIRECTORY, TEST_SECURITY_DIRECTORY_RESOURCES_DIR_NAME);
/* --------------------------
* Namespace : Root
* Match type : Exact
Expand All @@ -164,17 +165,13 @@ TEST_F(TestGetSecureRoot, successScenarios_root_exactMatch) {
ASSERT_STREQ(root_path, secure_root);
}


TEST_F(TestGetSecureRoot, successScenarios_root_prefixMatch) {
putenv_wrapper(ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME "=" TEST_RESOURCES_DIRECTORY);
putenv_wrapper(ROS_SECURITY_LOOKUP_TYPE_VAR_NAME "=MATCH_PREFIX");
secure_root = rcl_get_secure_root(TEST_NODE_NAME, TEST_NODE_NAMESPACE, &allocator);

/* Include the namespace as part of the root security directory and test root namespace */
std::string path;
get_base_lookup_dir_fqn(path, TEST_RESOURCES_DIRECTORY, TEST_SECURITY_DIRECTORY_RESOURCES_DIR_NAME);
putenv_wrapper(path.c_str());

set_base_lookup_dir_fqn(TEST_RESOURCES_DIRECTORY, TEST_SECURITY_DIRECTORY_RESOURCES_DIR_NAME);
/* --------------------------
* Namespace : Root
* Match type : Prefix
Expand Down

0 comments on commit 7fe5674

Please sign in to comment.