From f870b48ec87c6f41e1f25343d6cf2f09c43a5647 Mon Sep 17 00:00:00 2001 From: Tom Joseph Date: Mon, 19 Nov 2018 09:55:45 +0530 Subject: Add unit tests for ldap mapper application Change-Id: I2d75a4f2e27f6e6640e8a16cc7834116b260f547 Signed-off-by: Tom Joseph --- .gitignore | 4 ++ test/Makefile.am | 9 +++ test/ldap_mapper_test.cpp | 151 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 test/ldap_mapper_test.cpp diff --git a/.gitignore b/.gitignore index cc2c852..04d8cc5 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,7 @@ Makefile /test/test-suite.log /test/utest.log /test/utest.trs +/test/ldap_mapper_test +/test/ldap_mapper_test.log +/test/ldap_mapper_test.trs + diff --git a/test/Makefile.am b/test/Makefile.am index 5026e30..e3c1366 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -34,3 +34,12 @@ ldap_config_test_LDFLAGS = $(utest_LDFLAGS) \ -lgmock ldap_config_test_SOURCES = ldap_config_test.cpp utils_test.cpp ldap_config_test_LDADD = $(top_builddir)/phosphor-ldap-config/ldap_configuration.o $(top_builddir)/phosphor-ldap-config/utils.o + +check_PROGRAMS += ldap_mapper_test +ldap_mapper_test_CPPFLAGS = $(utest_CPPFLAGS) +ldap_mapper_test_CXXFLAGS = $(utest_CXXFLAGS) +ldap_mapper_test_LDFLAGS = $(utest_LDFLAGS) +ldap_mapper_test_SOURCES = ldap_mapper_test.cpp +ldap_mapper_test_LDADD = $(top_builddir)/phosphor-ldap-mapper/ldap_mapper_entry.o \ + $(top_builddir)/phosphor-ldap-mapper/ldap_mapper_mgr.o \ + $(top_builddir)/phosphor-ldap-mapper/ldap_mapper_serialize.o diff --git a/test/ldap_mapper_test.cpp b/test/ldap_mapper_test.cpp new file mode 100644 index 0000000..2b06cce --- /dev/null +++ b/test/ldap_mapper_test.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include "phosphor-ldap-mapper/ldap_mapper_entry.hpp" +#include "phosphor-ldap-mapper/ldap_mapper_mgr.hpp" +#include "phosphor-ldap-mapper/ldap_mapper_serialize.hpp" +#include +#include +#include "config.h" + +namespace phosphor +{ +namespace user +{ + +namespace fs = std::experimental::filesystem; + +class TestSerialization : public testing::Test +{ + public: + TestSerialization() : bus(sdbusplus::bus::new_default()) + { + } + + void SetUp() override + { + char tempDir[] = "/tmp/privmapper_test.XXXXXX"; + dir = fs::path(mkdtemp(tempDir)); + } + + void TearDown() override + { + fs::remove_all(dir); + } + + fs::path dir; + sdbusplus::bus::bus bus; +}; + +TEST_F(TestSerialization, testPersistPath) +{ + LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, + TestSerialization::dir.c_str()); + std::string groupName = "admin"; + std::string privilege = "priv-admin"; + size_t entryId = 1; + auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId); + + auto entry = std::make_unique( + TestSerialization::bus, dbusPath.c_str(), + (TestSerialization::dir).c_str(), groupName, privilege, manager); + auto outPath = serialize(*entry, entryId, TestSerialization::dir); + EXPECT_EQ(outPath, TestSerialization::dir / std::to_string(entryId)); +} + +TEST_F(TestSerialization, testPersistData) +{ + std::string groupName = "admin"; + std::string privilege = "priv-admin"; + size_t entryId = 1; + auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId); + LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, + TestSerialization::dir.c_str()); + + auto input = std::make_unique( + bus, dbusPath.c_str(), TestSerialization::dir.c_str(), groupName, + privilege, manager); + auto outPath = serialize(*input, entryId, TestSerialization::dir); + + auto output = std::make_unique( + bus, dbusPath.c_str(), (TestSerialization::dir).c_str(), manager); + auto rc = deserialize(outPath, *output); + + EXPECT_EQ(rc, true); + EXPECT_EQ(output->groupName(), groupName); + EXPECT_EQ(output->privilege(), privilege); +} + +TEST_F(TestSerialization, testRestore) +{ + std::string groupName = "admin"; + std::string privilege = "priv-admin"; + namespace fs = std::experimental::filesystem; + size_t entryId = 1; + LDAPMapperMgr manager1(TestSerialization::bus, mapperMgrRoot, + (TestSerialization::dir).c_str()); + EXPECT_NO_THROW(manager1.create(groupName, privilege)); + + EXPECT_EQ(fs::exists(TestSerialization::dir / std::to_string(entryId)), + true); + LDAPMapperMgr manager2(TestSerialization::bus, mapperMgrRoot, + (TestSerialization::dir).c_str()); + EXPECT_NO_THROW(manager2.restore()); + EXPECT_NO_THROW(manager2.deletePrivilegeMapper(entryId)); + EXPECT_EQ(fs::exists(TestSerialization::dir / std::to_string(entryId)), + false); +} + +TEST_F(TestSerialization, testPrivilegeMapperCreation) +{ + std::string groupName = "admin"; + std::string privilege = "priv-admin"; + LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, + (TestSerialization::dir).c_str()); + EXPECT_NO_THROW(manager.create(groupName, privilege)); +} + +TEST_F(TestSerialization, testDuplicateGroupName) +{ + std::string groupName = "admin"; + std::string privilege = "priv-admin"; + using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User:: + Common::Error::PrivilegeMappingExists; + LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, + (TestSerialization::dir).c_str()); + auto objectPath = manager.create(groupName, privilege); + EXPECT_THROW(manager.create(groupName, privilege), PrivilegeMappingExists); +} + +TEST_F(TestSerialization, testValidPrivilege) +{ + std::string groupName = "admin"; + std::string privilege = "priv-admin"; + size_t entryId = 1; + auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId); + LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, + TestSerialization::dir.c_str()); + + auto entry = std::make_unique( + TestSerialization::bus, dbusPath.c_str(), + (TestSerialization::dir).c_str(), groupName, privilege, manager); + + EXPECT_NO_THROW(entry->privilege("priv-operator")); + EXPECT_NO_THROW(entry->privilege("priv-user")); + EXPECT_NO_THROW(entry->privilege("priv-callback")); +} + +TEST_F(TestSerialization, testInvalidPrivilege) +{ + std::string groupName = "admin"; + std::string privilege = "priv-test"; + using InvalidArgument = + sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; + LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, + (TestSerialization::dir).c_str()); + EXPECT_THROW(manager.create(groupName, privilege), InvalidArgument); +} + +} // namespace user +} // namespace phosphor -- cgit v1.2.1