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