Skip to content

Commit

Permalink
Just remove rcpputils::fs dependency (#72)
Browse files Browse the repository at this point in the history
* Remove rcpputils::fs dependency

Signed-off-by: Kenta Yonekura <yoneken@ieee.org>
  • Loading branch information
yoneken authored Oct 5, 2023
1 parent f4b7aa0 commit d3095f8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 64 deletions.
1 change: 0 additions & 1 deletion rmw_dds_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ find_package(rosidl_runtime_c REQUIRED)

ament_add_default_options()
ament_export_dependencies(ament_cmake_core)
ament_export_dependencies(rcpputils)
ament_export_dependencies(rcutils)
ament_export_dependencies(rmw)

Expand Down
14 changes: 7 additions & 7 deletions rmw_dds_common/src/security.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <filesystem>
#include <string>
#include <utility>
#include <unordered_map>

#include "rcpputils/filesystem_helper.hpp"
#include "rmw_dds_common/security.hpp"

namespace rmw_dds_common
Expand All @@ -40,21 +40,21 @@ bool get_security_files(
};

for (const std::pair<const std::string, std::string> & el : required_files) {
rcpputils::fs::path full_path(secure_root);
std::filesystem::path full_path(secure_root);
full_path /= el.second;
if (!full_path.is_regular_file()) {
if (!std::filesystem::is_regular_file(full_path)) {
result.clear();
return false;
}

result[el.first] = prefix + full_path.string();
result[el.first] = prefix + full_path.generic_string();
}

for (const std::pair<const std::string, std::string> & el : optional_files) {
rcpputils::fs::path full_path(secure_root);
std::filesystem::path full_path(secure_root);
full_path /= el.second;
if (full_path.is_regular_file()) {
result[el.first] = prefix + full_path.string();
if (std::filesystem::is_regular_file(full_path)) {
result[el.first] = prefix + full_path.generic_string();
}
}

Expand Down
112 changes: 56 additions & 56 deletions rmw_dds_common/test/test_security.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,165 +13,165 @@
// limitations under the License.

#include <array>
#include <filesystem>
#include <fstream>
#include <string>
#include <unordered_map>

#include "gtest/gtest.h"

#include "rcpputils/filesystem_helper.hpp"
#include "rmw_dds_common/security.hpp"

TEST(test_security, files_exist_no_prefix)
{
rcpputils::fs::path dir = rcpputils::fs::path("./test_folder");
rcpputils::fs::remove_all(dir);
EXPECT_TRUE(rcpputils::fs::create_directories(dir));
EXPECT_TRUE(rcpputils::fs::exists(dir));
EXPECT_TRUE(rcpputils::fs::is_directory(dir));
std::filesystem::path dir = std::filesystem::path("./test_folder");
std::filesystem::remove_all(dir);
EXPECT_TRUE(std::filesystem::create_directories(dir));
EXPECT_TRUE(std::filesystem::exists(dir));
EXPECT_TRUE(std::filesystem::is_directory(dir));

std::array<std::string, 6> required_files = {
"identity_ca.cert.pem", "cert.pem", "key.pem",
"permissions_ca.cert.pem", "governance.p7s", "permissions.p7s"
};
for (const std::string & filename : required_files) {
rcpputils::fs::path full_path = dir / filename;
std::ofstream output_buffer{full_path.string()};
std::filesystem::path full_path = dir / filename;
std::ofstream output_buffer{full_path.generic_string()};
output_buffer << "test";
ASSERT_TRUE(rcpputils::fs::exists(full_path));
ASSERT_TRUE(std::filesystem::exists(full_path));
}

std::unordered_map<std::string, std::string> security_files;
ASSERT_TRUE(rmw_dds_common::get_security_files("", dir.string(), security_files));
ASSERT_TRUE(rmw_dds_common::get_security_files("", dir.generic_string(), security_files));

EXPECT_EQ(
security_files["IDENTITY_CA"],
rcpputils::fs::path("./test_folder/identity_ca.cert.pem").string());
std::filesystem::path("./test_folder/identity_ca.cert.pem").generic_string());
EXPECT_EQ(
security_files["CERTIFICATE"],
rcpputils::fs::path("./test_folder/cert.pem").string());
std::filesystem::path("./test_folder/cert.pem").generic_string());
EXPECT_EQ(
security_files["PRIVATE_KEY"],
rcpputils::fs::path("./test_folder/key.pem").string());
std::filesystem::path("./test_folder/key.pem").generic_string());
EXPECT_EQ(
security_files["PERMISSIONS_CA"],
rcpputils::fs::path("./test_folder/permissions_ca.cert.pem").string());
std::filesystem::path("./test_folder/permissions_ca.cert.pem").generic_string());
EXPECT_EQ(
security_files["GOVERNANCE"],
rcpputils::fs::path("./test_folder/governance.p7s").string());
std::filesystem::path("./test_folder/governance.p7s").generic_string());
EXPECT_EQ(
security_files["PERMISSIONS"],
rcpputils::fs::path("./test_folder/permissions.p7s").string());
std::filesystem::path("./test_folder/permissions.p7s").generic_string());
}

TEST(test_security, files_exist_with_prefix)
{
rcpputils::fs::path dir = rcpputils::fs::path("./test_folder");
rcpputils::fs::remove_all(dir);
EXPECT_TRUE(rcpputils::fs::create_directories(dir));
EXPECT_TRUE(rcpputils::fs::exists(dir));
EXPECT_TRUE(rcpputils::fs::is_directory(dir));
std::filesystem::path dir = std::filesystem::path("./test_folder");
std::filesystem::remove_all(dir);
EXPECT_TRUE(std::filesystem::create_directories(dir));
EXPECT_TRUE(std::filesystem::exists(dir));
EXPECT_TRUE(std::filesystem::is_directory(dir));

std::array<std::string, 6> required_files = {
"identity_ca.cert.pem", "cert.pem", "key.pem",
"permissions_ca.cert.pem", "governance.p7s", "permissions.p7s"
};
for (const std::string & filename : required_files) {
rcpputils::fs::path full_path = dir / filename;
std::ofstream output_buffer{full_path.string()};
std::filesystem::path full_path = dir / filename;
std::ofstream output_buffer{full_path.generic_string()};
output_buffer << "test";
ASSERT_TRUE(rcpputils::fs::exists(full_path));
ASSERT_TRUE(std::filesystem::exists(full_path));
}

std::unordered_map<std::string, std::string> security_files;
ASSERT_TRUE(rmw_dds_common::get_security_files("file://", dir.string(), security_files));
ASSERT_TRUE(rmw_dds_common::get_security_files("file://", dir.generic_string(), security_files));

EXPECT_EQ(
security_files["IDENTITY_CA"],
"file://" + rcpputils::fs::path("./test_folder/identity_ca.cert.pem").string());
"file://" + std::filesystem::path("./test_folder/identity_ca.cert.pem").generic_string());
EXPECT_EQ(
security_files["CERTIFICATE"],
"file://" + rcpputils::fs::path("./test_folder/cert.pem").string());
"file://" + std::filesystem::path("./test_folder/cert.pem").generic_string());
EXPECT_EQ(
security_files["PRIVATE_KEY"],
"file://" + rcpputils::fs::path("./test_folder/key.pem").string());
"file://" + std::filesystem::path("./test_folder/key.pem").generic_string());
EXPECT_EQ(
security_files["PERMISSIONS_CA"],
"file://" + rcpputils::fs::path("./test_folder/permissions_ca.cert.pem").string());
"file://" + std::filesystem::path("./test_folder/permissions_ca.cert.pem").generic_string());
EXPECT_EQ(
security_files["GOVERNANCE"],
"file://" + rcpputils::fs::path("./test_folder/governance.p7s").string());
"file://" + std::filesystem::path("./test_folder/governance.p7s").generic_string());
EXPECT_EQ(
security_files["PERMISSIONS"],
"file://" + rcpputils::fs::path("./test_folder/permissions.p7s").string());
"file://" + std::filesystem::path("./test_folder/permissions.p7s").generic_string());
}

TEST(test_security, file_missing)
{
rcpputils::fs::path dir = rcpputils::fs::path("./test_folder");
rcpputils::fs::remove_all(dir);
EXPECT_TRUE(rcpputils::fs::create_directories(dir));
EXPECT_TRUE(rcpputils::fs::exists(dir));
EXPECT_TRUE(rcpputils::fs::is_directory(dir));
std::filesystem::path dir = std::filesystem::path("./test_folder");
std::filesystem::remove_all(dir);
EXPECT_TRUE(std::filesystem::create_directories(dir));
EXPECT_TRUE(std::filesystem::exists(dir));
EXPECT_TRUE(std::filesystem::is_directory(dir));

std::array<std::string, 5> required_files = {
"identity_ca.cert.pem", "cert.pem", "key.pem",
"permissions_ca.cert.pem", "governance.p7s"
};
for (const std::string & filename : required_files) {
rcpputils::fs::path full_path = dir / filename;
std::ofstream output_buffer{full_path.string()};
std::filesystem::path full_path = dir / filename;
std::ofstream output_buffer{full_path.generic_string()};
output_buffer << "test";
ASSERT_TRUE(rcpputils::fs::exists(full_path));
ASSERT_TRUE(std::filesystem::exists(full_path));
}

std::unordered_map<std::string, std::string> security_files;
ASSERT_FALSE(rmw_dds_common::get_security_files("", dir.string(), security_files));
ASSERT_FALSE(rmw_dds_common::get_security_files("", dir.generic_string(), security_files));
ASSERT_EQ(security_files.size(), 0UL);
}

TEST(test_security, optional_file_exist)
{
rcpputils::fs::path dir = rcpputils::fs::path("./test_folder");
rcpputils::fs::remove_all(dir);
EXPECT_TRUE(rcpputils::fs::create_directories(dir));
EXPECT_TRUE(rcpputils::fs::exists(dir));
EXPECT_TRUE(rcpputils::fs::is_directory(dir));
std::filesystem::path dir = std::filesystem::path("./test_folder");
std::filesystem::remove_all(dir);
EXPECT_TRUE(std::filesystem::create_directories(dir));
EXPECT_TRUE(std::filesystem::exists(dir));
EXPECT_TRUE(std::filesystem::is_directory(dir));

std::array<std::string, 7> required_files = {
"identity_ca.cert.pem", "cert.pem", "key.pem",
"permissions_ca.cert.pem", "governance.p7s", "permissions.p7s", "crl.pem",
};
for (const std::string & filename : required_files) {
rcpputils::fs::path full_path = dir / filename;
std::ofstream output_buffer{full_path.string()};
std::filesystem::path full_path = dir / filename;
std::ofstream output_buffer{full_path.generic_string()};
output_buffer << "test";
ASSERT_TRUE(rcpputils::fs::exists(full_path));
ASSERT_TRUE(std::filesystem::exists(full_path));
}

std::unordered_map<std::string, std::string> security_files;
ASSERT_TRUE(rmw_dds_common::get_security_files("", dir.string(), security_files));
ASSERT_TRUE(rmw_dds_common::get_security_files("", dir.generic_string(), security_files));

EXPECT_EQ(
security_files["IDENTITY_CA"],
rcpputils::fs::path("./test_folder/identity_ca.cert.pem").string());
std::filesystem::path("./test_folder/identity_ca.cert.pem").generic_string());
EXPECT_EQ(
security_files["CERTIFICATE"],
rcpputils::fs::path("./test_folder/cert.pem").string());
std::filesystem::path("./test_folder/cert.pem").generic_string());
EXPECT_EQ(
security_files["PRIVATE_KEY"],
rcpputils::fs::path("./test_folder/key.pem").string());
std::filesystem::path("./test_folder/key.pem").generic_string());
EXPECT_EQ(
security_files["PERMISSIONS_CA"],
rcpputils::fs::path("./test_folder/permissions_ca.cert.pem").string());
std::filesystem::path("./test_folder/permissions_ca.cert.pem").generic_string());
EXPECT_EQ(
security_files["GOVERNANCE"],
rcpputils::fs::path("./test_folder/governance.p7s").string());
std::filesystem::path("./test_folder/governance.p7s").generic_string());
EXPECT_EQ(
security_files["PERMISSIONS"],
rcpputils::fs::path("./test_folder/permissions.p7s").string());
std::filesystem::path("./test_folder/permissions.p7s").generic_string());

EXPECT_EQ(
security_files["CRL"],
rcpputils::fs::path("./test_folder/crl.pem").string());
std::filesystem::path("./test_folder/crl.pem").generic_string());
}

0 comments on commit d3095f8

Please sign in to comment.