blob: a044b640c4372fb1f16dda0b6087580c32736b14 [file] [log] [blame]
Tom Josephf870b482018-11-19 09:55:45 +05301#include <gtest/gtest.h>
Gunnar Mills703131f2020-10-28 14:26:33 -05002#include <filesystem>
Tom Josephf870b482018-11-19 09:55:45 +05303#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 Teja417c0892020-08-22 08:04:01 -050011#include <sdbusplus/test/sdbus_mock.hpp>
Tom Josephf870b482018-11-19 09:55:45 +053012
13namespace phosphor
14{
15namespace user
16{
17
Tom Josephf870b482018-11-19 09:55:45 +053018class TestSerialization : public testing::Test
19{
20 public:
Ravi Teja417c0892020-08-22 08:04:01 -050021 sdbusplus::SdBusMock sdbusMock;
22
23 TestSerialization() : bus(sdbusplus::get_mocked_new(&sdbusMock))
Tom Josephf870b482018-11-19 09:55:45 +053024 {
25 }
26
27 void SetUp() override
28 {
29 char tempDir[] = "/tmp/privmapper_test.XXXXXX";
Gunnar Mills703131f2020-10-28 14:26:33 -050030 dir = std::filesystem::path(mkdtemp(tempDir));
Tom Josephf870b482018-11-19 09:55:45 +053031 }
32
33 void TearDown() override
34 {
Gunnar Mills703131f2020-10-28 14:26:33 -050035 std::filesystem::remove_all(dir);
Tom Josephf870b482018-11-19 09:55:45 +053036 }
37
Gunnar Mills703131f2020-10-28 14:26:33 -050038 std::filesystem::path dir;
Tom Josephf870b482018-11-19 09:55:45 +053039 sdbusplus::bus::bus bus;
40};
41
42TEST_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
58TEST_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
81TEST_F(TestSerialization, testRestore)
82{
83 std::string groupName = "admin";
84 std::string privilege = "priv-admin";
Tom Josephf870b482018-11-19 09:55:45 +053085 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 Mills703131f2020-10-28 14:26:33 -050090 EXPECT_EQ(std::filesystem::exists(TestSerialization::dir /
91 std::to_string(entryId)),
Tom Josephf870b482018-11-19 09:55:45 +053092 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 Mills703131f2020-10-28 14:26:33 -050097 EXPECT_EQ(std::filesystem::exists(TestSerialization::dir /
98 std::to_string(entryId)),
Tom Josephf870b482018-11-19 09:55:45 +053099 false);
100}
101
102TEST_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
111TEST_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
123TEST_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 Josephf870b482018-11-19 09:55:45 +0530138}
139
140TEST_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