From f5bd891cf5afb30085d1a5e93d86bd022c097238 Mon Sep 17 00:00:00 2001 From: Tom Joseph Date: Mon, 19 Nov 2018 09:49:21 +0530 Subject: Refactor mapper application to enable unit tests Change-Id: I58cac8879f93ce49bfb654a1bf559d7f77b5b486 Signed-off-by: Tom Joseph --- phosphor-ldap-mapper/ldap_mapper_entry.cpp | 11 ++++++----- phosphor-ldap-mapper/ldap_mapper_entry.hpp | 12 +++++++++--- phosphor-ldap-mapper/ldap_mapper_mgr.cpp | 17 ++++++++++------- phosphor-ldap-mapper/ldap_mapper_mgr.hpp | 7 ++++++- phosphor-ldap-mapper/ldap_mapper_serialize.cpp | 3 +-- phosphor-ldap-mapper/ldap_mapper_serialize.hpp | 5 ++++- phosphor-ldap-mapper/main.cpp | 3 ++- 7 files changed, 38 insertions(+), 20 deletions(-) diff --git a/phosphor-ldap-mapper/ldap_mapper_entry.cpp b/phosphor-ldap-mapper/ldap_mapper_entry.cpp index 95f381d..8410942 100644 --- a/phosphor-ldap-mapper/ldap_mapper_entry.cpp +++ b/phosphor-ldap-mapper/ldap_mapper_entry.cpp @@ -20,12 +20,13 @@ using InvalidArgument = using Argument = xyz::openbmc_project::Common::InvalidArgument; LDAPMapperEntry::LDAPMapperEntry(sdbusplus::bus::bus &bus, const char *path, + const char *filePath, const std::string &groupName, const std::string &privilege, LDAPMapperMgr &parent) : Ifaces(bus, path, true), id(std::stol(std::experimental::filesystem::path(path).filename())), - manager(parent) + manager(parent), persistPath(filePath) { Ifaces::privilege(privilege, true); Ifaces::groupName(groupName, true); @@ -33,10 +34,10 @@ LDAPMapperEntry::LDAPMapperEntry(sdbusplus::bus::bus &bus, const char *path, } LDAPMapperEntry::LDAPMapperEntry(sdbusplus::bus::bus &bus, const char *path, - LDAPMapperMgr &parent) : + const char *filePath, LDAPMapperMgr &parent) : Ifaces(bus, path, true), id(std::stol(std::experimental::filesystem::path(path).filename())), - manager(parent) + manager(parent), persistPath(filePath) { } @@ -54,7 +55,7 @@ std::string LDAPMapperEntry::groupName(std::string value) manager.checkPrivilegeMapper(value); auto val = Ifaces::groupName(value); - serialize(*this, id); + serialize(*this, id, persistPath); return val; } @@ -67,7 +68,7 @@ std::string LDAPMapperEntry::privilege(std::string value) manager.checkPrivilegeLevel(value); auto val = Ifaces::privilege(value); - serialize(*this, id); + serialize(*this, id, persistPath); return val; } diff --git a/phosphor-ldap-mapper/ldap_mapper_entry.hpp b/phosphor-ldap-mapper/ldap_mapper_entry.hpp index ea8a0bc..dea85c2 100644 --- a/phosphor-ldap-mapper/ldap_mapper_entry.hpp +++ b/phosphor-ldap-mapper/ldap_mapper_entry.hpp @@ -39,21 +39,24 @@ class LDAPMapperEntry : public Ifaces * * @param[in] bus - sdbusplus handler * @param[in] path - D-Bus path + * @param[in] filePath - serialization directory path + * @param[in] groupName - LDAP group name * @param[in] privilege - the privilege for the group * @param[in] parent - LDAP privilege mapper manager */ LDAPMapperEntry(sdbusplus::bus::bus &bus, const char *path, - const std::string &groupName, const std::string &privilege, - LDAPMapperMgr &parent); + const char *filePath, const std::string &groupName, + const std::string &privilege, LDAPMapperMgr &parent); /** @brief Constructs LDAP privilege mapper entry object * * @param[in] bus - sdbusplus handler * @param[in] path - D-Bus path + * @param[in] filePath - serialization directory path * @param[in] parent - LDAP privilege mapper manager */ LDAPMapperEntry(sdbusplus::bus::bus &bus, const char *path, - LDAPMapperMgr &parent); + const char *filePath, LDAPMapperMgr &parent); /** @brief Delete privilege mapper entry object * @@ -86,6 +89,9 @@ class LDAPMapperEntry : public Ifaces private: Id id; LDAPMapperMgr &manager; + + /** @brief serialization directory path */ + std::string persistPath; }; } // namespace user diff --git a/phosphor-ldap-mapper/ldap_mapper_mgr.cpp b/phosphor-ldap-mapper/ldap_mapper_mgr.cpp index a321331..a1d6e11 100644 --- a/phosphor-ldap-mapper/ldap_mapper_mgr.cpp +++ b/phosphor-ldap-mapper/ldap_mapper_mgr.cpp @@ -19,8 +19,10 @@ using Argument = xyz::openbmc_project::Common::InvalidArgument; using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: Error::PrivilegeMappingExists; -LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path) : - MapperMgrIface(bus, path), bus(bus), path(path) +LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path, + const char *filePath) : + MapperMgrIface(bus, path), + bus(bus), path(path), persistPath(filePath) { } @@ -37,9 +39,10 @@ ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege) // Create mapping for LDAP privilege mapper entry auto entry = std::make_unique( - bus, mapperObject.c_str(), groupName, privilege, *this); + bus, mapperObject.c_str(), persistPath.c_str(), groupName, privilege, + *this); - serialize(*entry, entryId); + serialize(*entry, entryId, persistPath); PrivilegeMapperList.emplace(entryId, std::move(entry)); @@ -49,7 +52,7 @@ ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege) void LDAPMapperMgr::deletePrivilegeMapper(Id id) { // Delete the persistent representation of the privilege mapper. - fs::path mapperPath(LDAP_MAPPER_PERSIST_PATH); + fs::path mapperPath(persistPath); mapperPath /= std::to_string(id); fs::remove(mapperPath); @@ -96,7 +99,7 @@ void LDAPMapperMgr::restore() { namespace fs = std::experimental::filesystem; - fs::path dir(LDAP_MAPPER_PERSIST_PATH); + fs::path dir(persistPath); if (!fs::exists(dir) || fs::is_empty(dir)) { return; @@ -108,7 +111,7 @@ void LDAPMapperMgr::restore() size_t idNum = std::stol(id); auto entryPath = std::string(mapperMgrRoot) + '/' + id; auto entry = std::make_unique( - bus, entryPath.c_str(), *this); + bus, entryPath.c_str(), persistPath.c_str(), *this); if (deserialize(file.path(), *entry)) { entry->Ifaces::emit_object_added(); diff --git a/phosphor-ldap-mapper/ldap_mapper_mgr.hpp b/phosphor-ldap-mapper/ldap_mapper_mgr.hpp index b2617ca..ae8f6c9 100644 --- a/phosphor-ldap-mapper/ldap_mapper_mgr.hpp +++ b/phosphor-ldap-mapper/ldap_mapper_mgr.hpp @@ -38,8 +38,10 @@ class LDAPMapperMgr : public MapperMgrIface * * @param[in] bus - sdbusplus handler * @param[in] path - D-Bus path + * @param[in] filePath - serialization directory path */ - LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path); + LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path, + const char *filePath); /** @brief Creates a mapping for the group to the privilege * @@ -92,6 +94,9 @@ class LDAPMapperMgr : public MapperMgrIface /** @brief object path for the manager object*/ const std::string path; + /** @brief serialization directory path */ + std::string persistPath; + /** @brief available privileges container */ std::set privMgr = {"priv-admin", "priv-operator", "priv-user", "priv-callback"}; diff --git a/phosphor-ldap-mapper/ldap_mapper_serialize.cpp b/phosphor-ldap-mapper/ldap_mapper_serialize.cpp index 534e0a1..3ef809e 100644 --- a/phosphor-ldap-mapper/ldap_mapper_serialize.cpp +++ b/phosphor-ldap-mapper/ldap_mapper_serialize.cpp @@ -54,9 +54,8 @@ void load(Archive& archive, LDAPMapperEntry& entry, const std::uint32_t version) privilege(privilege, true); } -fs::path serialize(const LDAPMapperEntry& entry, Id id) +fs::path serialize(const LDAPMapperEntry& entry, Id id, const fs::path& dir) { - fs::path dir(LDAP_MAPPER_PERSIST_PATH); auto path = dir / std::to_string(id); std::ofstream os(path.c_str(), std::ios::binary); cereal::BinaryOutputArchive oarchive(os); diff --git a/phosphor-ldap-mapper/ldap_mapper_serialize.hpp b/phosphor-ldap-mapper/ldap_mapper_serialize.hpp index f48f73c..5ab71c0 100644 --- a/phosphor-ldap-mapper/ldap_mapper_serialize.hpp +++ b/phosphor-ldap-mapper/ldap_mapper_serialize.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include "config.h" #include "ldap_mapper_entry.hpp" namespace phosphor @@ -14,10 +15,12 @@ namespace fs = std::experimental::filesystem; * * @param[in] entry - LDAP privilege mapper entry * @param[in] id - filename of the persisted LDAP mapper entry + * @param[in] dir - pathname of directory where the serialized privilege + * mappings are stored. * * @return fs::path - pathname of persisted error file */ -fs::path serialize(const LDAPMapperEntry& entry, Id id); +fs::path serialize(const LDAPMapperEntry& entry, Id id, const fs::path& dir); /** @brief Deserialize a persisted LDAP privilege mapper into a D-Bus object * diff --git a/phosphor-ldap-mapper/main.cpp b/phosphor-ldap-mapper/main.cpp index 15c9802..e0d2255 100644 --- a/phosphor-ldap-mapper/main.cpp +++ b/phosphor-ldap-mapper/main.cpp @@ -9,7 +9,8 @@ int main(int argc, char** argv) sdbusplus::server::manager::manager objManager( bus, phosphor::user::mapperMgrRoot); - phosphor::user::LDAPMapperMgr mapperMgr(bus, phosphor::user::mapperMgrRoot); + phosphor::user::LDAPMapperMgr mapperMgr(bus, phosphor::user::mapperMgrRoot, + LDAP_MAPPER_PERSIST_PATH); // Create a directory to persist errors. std::experimental::filesystem::create_directories(LDAP_MAPPER_PERSIST_PATH); -- cgit v1.2.1