Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 2 | #include <filesystem> |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <sdbusplus/bus.hpp> |
| 5 | #include "phosphor-ldap-mapper/ldap_mapper_entry.hpp" |
| 6 | #include "phosphor-ldap-mapper/ldap_mapper_mgr.hpp" |
| 7 | #include "phosphor-ldap-mapper/ldap_mapper_serialize.hpp" |
| 8 | #include <xyz/openbmc_project/Common/error.hpp> |
| 9 | #include <xyz/openbmc_project/User/Common/error.hpp> |
| 10 | #include "config.h" |
Ravi Teja | 417c089 | 2020-08-22 08:04:01 -0500 | [diff] [blame] | 11 | #include <sdbusplus/test/sdbus_mock.hpp> |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace user |
| 16 | { |
| 17 | |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 18 | class TestSerialization : public testing::Test |
| 19 | { |
| 20 | public: |
Ravi Teja | 417c089 | 2020-08-22 08:04:01 -0500 | [diff] [blame] | 21 | sdbusplus::SdBusMock sdbusMock; |
| 22 | |
| 23 | TestSerialization() : bus(sdbusplus::get_mocked_new(&sdbusMock)) |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
| 27 | void SetUp() override |
| 28 | { |
| 29 | char tempDir[] = "/tmp/privmapper_test.XXXXXX"; |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 30 | dir = std::filesystem::path(mkdtemp(tempDir)); |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void TearDown() override |
| 34 | { |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 35 | std::filesystem::remove_all(dir); |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 36 | } |
| 37 | |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 38 | std::filesystem::path dir; |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 39 | sdbusplus::bus::bus bus; |
| 40 | }; |
| 41 | |
| 42 | TEST_F(TestSerialization, testPersistPath) |
| 43 | { |
| 44 | LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, |
| 45 | TestSerialization::dir.c_str()); |
| 46 | std::string groupName = "admin"; |
| 47 | std::string privilege = "priv-admin"; |
| 48 | size_t entryId = 1; |
| 49 | auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId); |
| 50 | |
| 51 | auto entry = std::make_unique<LDAPMapperEntry>( |
| 52 | TestSerialization::bus, dbusPath.c_str(), |
| 53 | (TestSerialization::dir).c_str(), groupName, privilege, manager); |
| 54 | auto outPath = serialize(*entry, entryId, TestSerialization::dir); |
| 55 | EXPECT_EQ(outPath, TestSerialization::dir / std::to_string(entryId)); |
| 56 | } |
| 57 | |
| 58 | TEST_F(TestSerialization, testPersistData) |
| 59 | { |
| 60 | std::string groupName = "admin"; |
| 61 | std::string privilege = "priv-admin"; |
| 62 | size_t entryId = 1; |
| 63 | auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId); |
| 64 | LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, |
| 65 | TestSerialization::dir.c_str()); |
| 66 | |
| 67 | auto input = std::make_unique<LDAPMapperEntry>( |
| 68 | bus, dbusPath.c_str(), TestSerialization::dir.c_str(), groupName, |
| 69 | privilege, manager); |
| 70 | auto outPath = serialize(*input, entryId, TestSerialization::dir); |
| 71 | |
| 72 | auto output = std::make_unique<LDAPMapperEntry>( |
| 73 | bus, dbusPath.c_str(), (TestSerialization::dir).c_str(), manager); |
| 74 | auto rc = deserialize(outPath, *output); |
| 75 | |
| 76 | EXPECT_EQ(rc, true); |
| 77 | EXPECT_EQ(output->groupName(), groupName); |
| 78 | EXPECT_EQ(output->privilege(), privilege); |
| 79 | } |
| 80 | |
| 81 | TEST_F(TestSerialization, testRestore) |
| 82 | { |
| 83 | std::string groupName = "admin"; |
| 84 | std::string privilege = "priv-admin"; |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 85 | size_t entryId = 1; |
| 86 | LDAPMapperMgr manager1(TestSerialization::bus, mapperMgrRoot, |
| 87 | (TestSerialization::dir).c_str()); |
| 88 | EXPECT_NO_THROW(manager1.create(groupName, privilege)); |
| 89 | |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 90 | EXPECT_EQ(std::filesystem::exists(TestSerialization::dir / |
| 91 | std::to_string(entryId)), |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 92 | true); |
| 93 | LDAPMapperMgr manager2(TestSerialization::bus, mapperMgrRoot, |
| 94 | (TestSerialization::dir).c_str()); |
| 95 | EXPECT_NO_THROW(manager2.restore()); |
| 96 | EXPECT_NO_THROW(manager2.deletePrivilegeMapper(entryId)); |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 97 | EXPECT_EQ(std::filesystem::exists(TestSerialization::dir / |
| 98 | std::to_string(entryId)), |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 99 | false); |
| 100 | } |
| 101 | |
| 102 | TEST_F(TestSerialization, testPrivilegeMapperCreation) |
| 103 | { |
| 104 | std::string groupName = "admin"; |
| 105 | std::string privilege = "priv-admin"; |
| 106 | LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, |
| 107 | (TestSerialization::dir).c_str()); |
| 108 | EXPECT_NO_THROW(manager.create(groupName, privilege)); |
| 109 | } |
| 110 | |
| 111 | TEST_F(TestSerialization, testDuplicateGroupName) |
| 112 | { |
| 113 | std::string groupName = "admin"; |
| 114 | std::string privilege = "priv-admin"; |
| 115 | using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User:: |
| 116 | Common::Error::PrivilegeMappingExists; |
| 117 | LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, |
| 118 | (TestSerialization::dir).c_str()); |
| 119 | auto objectPath = manager.create(groupName, privilege); |
| 120 | EXPECT_THROW(manager.create(groupName, privilege), PrivilegeMappingExists); |
| 121 | } |
| 122 | |
| 123 | TEST_F(TestSerialization, testValidPrivilege) |
| 124 | { |
| 125 | std::string groupName = "admin"; |
| 126 | std::string privilege = "priv-admin"; |
| 127 | size_t entryId = 1; |
| 128 | auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId); |
| 129 | LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, |
| 130 | TestSerialization::dir.c_str()); |
| 131 | |
| 132 | auto entry = std::make_unique<LDAPMapperEntry>( |
| 133 | TestSerialization::bus, dbusPath.c_str(), |
| 134 | (TestSerialization::dir).c_str(), groupName, privilege, manager); |
| 135 | |
| 136 | EXPECT_NO_THROW(entry->privilege("priv-operator")); |
| 137 | EXPECT_NO_THROW(entry->privilege("priv-user")); |
Tom Joseph | f870b48 | 2018-11-19 09:55:45 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST_F(TestSerialization, testInvalidPrivilege) |
| 141 | { |
| 142 | std::string groupName = "admin"; |
| 143 | std::string privilege = "priv-test"; |
| 144 | using InvalidArgument = |
| 145 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 146 | LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot, |
| 147 | (TestSerialization::dir).c_str()); |
| 148 | EXPECT_THROW(manager.create(groupName, privilege), InvalidArgument); |
| 149 | } |
| 150 | |
| 151 | } // namespace user |
| 152 | } // namespace phosphor |