blob: 275ebe5891971e998c96781007b1a3b60e041819 [file] [log] [blame]
Patrick Williams9638afb2021-02-22 17:16:24 -06001#include "config.h"
2
Tom Josephf870b482018-11-19 09:55:45 +05303#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 Williams9638afb2021-02-22 17:16:24 -06006
7#include <stdlib.h>
8
9#include <sdbusplus/bus.hpp>
10#include <sdbusplus/test/sdbus_mock.hpp>
Tom Josephf870b482018-11-19 09:55:45 +053011#include <xyz/openbmc_project/Common/error.hpp>
12#include <xyz/openbmc_project/User/Common/error.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060013
14#include <filesystem>
15
16#include <gtest/gtest.h>
Tom Josephf870b482018-11-19 09:55:45 +053017
18namespace phosphor
19{
20namespace user
21{
22
Tom Josephf870b482018-11-19 09:55:45 +053023class TestSerialization : public testing::Test
24{
25 public:
Ravi Teja417c0892020-08-22 08:04:01 -050026 sdbusplus::SdBusMock sdbusMock;
27
28 TestSerialization() : bus(sdbusplus::get_mocked_new(&sdbusMock))
Patrick Williams9638afb2021-02-22 17:16:24 -060029 {}
Tom Josephf870b482018-11-19 09:55:45 +053030
31 void SetUp() override
32 {
33 char tempDir[] = "/tmp/privmapper_test.XXXXXX";
Gunnar Mills703131f2020-10-28 14:26:33 -050034 dir = std::filesystem::path(mkdtemp(tempDir));
Tom Josephf870b482018-11-19 09:55:45 +053035 }
36
37 void TearDown() override
38 {
Gunnar Mills703131f2020-10-28 14:26:33 -050039 std::filesystem::remove_all(dir);
Tom Josephf870b482018-11-19 09:55:45 +053040 }
41
Gunnar Mills703131f2020-10-28 14:26:33 -050042 std::filesystem::path dir;
Tom Josephf870b482018-11-19 09:55:45 +053043 sdbusplus::bus::bus bus;
44};
45
46TEST_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
62TEST_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
85TEST_F(TestSerialization, testRestore)
86{
87 std::string groupName = "admin";
88 std::string privilege = "priv-admin";
Tom Josephf870b482018-11-19 09:55:45 +053089 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 Mills703131f2020-10-28 14:26:33 -050094 EXPECT_EQ(std::filesystem::exists(TestSerialization::dir /
95 std::to_string(entryId)),
Tom Josephf870b482018-11-19 09:55:45 +053096 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 Mills703131f2020-10-28 14:26:33 -0500101 EXPECT_EQ(std::filesystem::exists(TestSerialization::dir /
102 std::to_string(entryId)),
Tom Josephf870b482018-11-19 09:55:45 +0530103 false);
104}
105
106TEST_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
115TEST_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
127TEST_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 Josephf870b482018-11-19 09:55:45 +0530142}
143
144TEST_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