diff options
-rw-r--r-- | serialize.hpp | 24 | ||||
-rw-r--r-- | test/Makefile.am | 2 | ||||
-rw-r--r-- | test/serialize_test.cpp | 23 |
3 files changed, 40 insertions, 9 deletions
diff --git a/serialize.hpp b/serialize.hpp index 4028899..82bd4bb 100644 --- a/serialize.hpp +++ b/serialize.hpp @@ -16,6 +16,18 @@ namespace manager namespace fs = std::filesystem; +namespace detail +{ +inline fs::path getStoragePath(const std::string& path, + const std::string& iface) +{ + auto p = fs::path(PIM_PERSIST_PATH); + p /= fs::path(path).relative_path(); + p /= fs::path(iface).relative_path(); + return p; +} +} // namespace detail + struct SerialOps { /** @brief Serialize inventory item path @@ -26,10 +38,8 @@ struct SerialOps */ static void serialize(const std::string& path, const std::string& iface) { - fs::path p(PIM_PERSIST_PATH); - p /= path; - fs::create_directories(p); - p /= iface; + auto p = detail::getStoragePath(path, iface); + fs::create_directories(p.parent_path()); std::ofstream os(p, std::ios::binary); } @@ -43,10 +53,8 @@ struct SerialOps static void serialize(const std::string& path, const std::string& iface, const T& object) { - fs::path p(PIM_PERSIST_PATH); - p /= path; - fs::create_directories(p); - p /= iface; + auto p = detail::getStoragePath(path, iface); + fs::create_directories(p.parent_path()); std::ofstream os(p, std::ios::binary); cereal::JSONOutputArchive oarchive(os); oarchive(object); diff --git a/test/Makefile.am b/test/Makefile.am index 36f2b4f..393ff0a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -23,7 +23,7 @@ check_PROGRAMS += utils-test serialize_test_SOURCES = serialize_test.cpp serialize_test_CFLAGS = ${GTEST_CFLAGS} ${GMOCK_CFLAGS} -serialize_test_LDADD = ${GTEST_LIBS} ${GMOCK_LIBS} +serialize_test_LDADD = ${GTEST_LIBS} ${GMOCK_LIBS} -lstdc++fs serialize_test_LDFLAGS = ${OESDK_TESTCASE_FLAGS} check_PROGRAMS += serialize-test diff --git a/test/serialize_test.cpp b/test/serialize_test.cpp index 4ed6446..7061978 100644 --- a/test/serialize_test.cpp +++ b/test/serialize_test.cpp @@ -1 +1,24 @@ #include "../serialize.hpp" + +#include <gtest/gtest.h> + +using namespace phosphor::inventory::manager; +using namespace std::string_literals; + +TEST(SerializeTest, TestStoragePathNoSlashes) +{ + auto path = "foo/bar/baz"s; + auto iface = "xyz.foo"s; + auto p1 = detail::getStoragePath(path, iface); + auto p2 = fs::path(PIM_PERSIST_PATH "/foo/bar/baz/xyz.foo"); + EXPECT_EQ(p1, p2); +} + +TEST(SerializeTest, TestStoragePathSlashes) +{ + auto path = "/foo/bar/baz"s; + auto iface = "/xyz.foo"s; + auto p1 = detail::getStoragePath(path, iface); + auto p2 = fs::path(PIM_PERSIST_PATH "/foo/bar/baz/xyz.foo"); + EXPECT_EQ(p1, p2); +} |