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