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