1 #include "config.h"
2
3 #include "phosphor-ldap-config/ldap_config.hpp"
4 #include "phosphor-ldap-config/ldap_config_mgr.hpp"
5 #include "phosphor-ldap-config/ldap_mapper_entry.hpp"
6 #include "phosphor-ldap-config/ldap_mapper_serialize.hpp"
7
8 #include <sdbusplus/bus.hpp>
9
10 #include <filesystem>
11 #include <fstream>
12 #include <string>
13
14 #include <gtest/gtest.h>
15
16 namespace phosphor
17 {
18 namespace ldap
19 {
20 namespace fs = std::filesystem;
21
22 class MockConfigMgr : public phosphor::ldap::ConfigMgr
23 {
24 public:
MockConfigMgr(sdbusplus::bus_t & bus,const char * path,const char * filePath,const char * dbusPersistentFile,const char * caCertFile,const char * certFile)25 MockConfigMgr(sdbusplus::bus_t& bus, const char* path, const char* filePath,
26 const char* dbusPersistentFile, const char* caCertFile,
27 const char* certFile) :
28 phosphor::ldap::ConfigMgr(bus, path, filePath, dbusPersistentFile,
29 caCertFile, certFile)
30 {}
31
getADConfigPtr()32 std::unique_ptr<Config>& getADConfigPtr()
33 {
34 return ADConfigPtr;
35 }
36
createDefaultObjects()37 void createDefaultObjects()
38 {
39 phosphor::ldap::ConfigMgr::createDefaultObjects();
40 }
41 };
42
43 class TestLDAPMapperSerialize : public testing::Test
44 {
45 public:
TestLDAPMapperSerialize()46 TestLDAPMapperSerialize() : bus(sdbusplus::bus::new_default()) {}
47
SetUp()48 void SetUp() override
49 {
50 char tmpldap[] = "/tmp/ldap_serialize_test.XXXXXX";
51 dir = fs::path(mkdtemp(tmpldap));
52
53 fs::path tlsCacertFilePath{TLS_CACERT_PATH};
54 tlsCACertFile = tlsCacertFilePath.filename().c_str();
55 fs::path tlsCertFilePath{TLS_CERT_FILE};
56 tlsCertFile = tlsCertFilePath.filename().c_str();
57 fs::path confFilePath{LDAP_CONFIG_FILE};
58 ldapConfFile = confFilePath.filename().c_str();
59
60 std::fstream fs;
61 fs.open(dir / tlsCACertFile, std::fstream::out);
62 fs.close();
63 fs.open(dir / tlsCertFile, std::fstream::out);
64 fs.close();
65 }
66
TearDown()67 void TearDown() override
68 {
69 fs::remove_all(dir);
70 }
71
72 protected:
73 fs::path dir;
74 std::string tlsCACertFile;
75 std::string tlsCertFile;
76 std::string ldapConfFile;
77 sdbusplus::bus_t bus;
78 };
79
TEST_F(TestLDAPMapperSerialize,testSerializeMapperEntry)80 TEST_F(TestLDAPMapperSerialize, testSerializeMapperEntry)
81 {
82 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
83 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
84 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
85 auto dbusPersistentFilePath = std::string(dir.c_str());
86
87 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
88 dbusPersistentFilePath.c_str(),
89 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
90 manager.createDefaultObjects();
91
92 std::string groupName = "testGroup";
93 std::string privilege = "priv-admin";
94 size_t entryId = 1;
95 auto dbusPath = std::string(LDAP_CONFIG_ROOT) +
96 "/active_directory/role_map/" + std::to_string(entryId);
97 auto persistPath = dbusPersistentFilePath + dbusPath;
98
99 auto entry = std::make_unique<LDAPMapperEntry>(
100 bus, dbusPath.c_str(), persistPath.c_str(), groupName, privilege,
101 *(manager.getADConfigPtr()));
102
103 auto serializedPath = serialize(*entry, persistPath);
104 EXPECT_TRUE(fs::exists(serializedPath));
105 }
106
TEST_F(TestLDAPMapperSerialize,testDeserializeMapperEntry)107 TEST_F(TestLDAPMapperSerialize, testDeserializeMapperEntry)
108 {
109 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
110 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
111 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
112 auto dbusPersistentFilePath = std::string(dir.c_str());
113
114 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
115 dbusPersistentFilePath.c_str(),
116 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
117 manager.createDefaultObjects();
118
119 std::string groupName = "testGroup";
120 std::string privilege = "priv-admin";
121 size_t entryId = 1;
122 auto dbusPath = std::string(LDAP_CONFIG_ROOT) +
123 "/active_directory/role_map/" + std::to_string(entryId);
124 auto persistPath = dbusPersistentFilePath + dbusPath;
125
126 fs::path serializedPath;
127
128 {
129 auto entry1 = std::make_unique<LDAPMapperEntry>(
130 bus, dbusPath.c_str(), persistPath.c_str(), groupName, privilege,
131 *(manager.getADConfigPtr()));
132
133 serializedPath = serialize(*entry1, persistPath);
134 EXPECT_TRUE(fs::exists(serializedPath));
135 }
136
137 auto entry2 = std::make_unique<LDAPMapperEntry>(
138 bus, dbusPath.c_str(), persistPath.c_str(),
139 *(manager.getADConfigPtr()));
140
141 bool result = deserialize(serializedPath, *entry2);
142 EXPECT_TRUE(result);
143 EXPECT_EQ(entry2->groupName(), groupName);
144 EXPECT_EQ(entry2->privilege(), privilege);
145 }
146
TEST_F(TestLDAPMapperSerialize,testDeserializeNonExistentFile)147 TEST_F(TestLDAPMapperSerialize, testDeserializeNonExistentFile)
148 {
149 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
150 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
151 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
152 auto dbusPersistentFilePath = std::string(dir.c_str());
153
154 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
155 dbusPersistentFilePath.c_str(),
156 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
157 manager.createDefaultObjects();
158
159 size_t entryId = 1;
160 auto dbusPath = std::string(LDAP_CONFIG_ROOT) +
161 "/active_directory/role_map/" + std::to_string(entryId);
162 auto persistPath = dbusPersistentFilePath + dbusPath;
163
164 auto entry = std::make_unique<LDAPMapperEntry>(
165 bus, dbusPath.c_str(), persistPath.c_str(),
166 *(manager.getADConfigPtr()));
167
168 fs::path nonExistentPath = dir / "non_existent_file";
169 bool result = deserialize(nonExistentPath, *entry);
170 EXPECT_FALSE(result);
171 }
172
TEST_F(TestLDAPMapperSerialize,testSerializeCreatesDirectory)173 TEST_F(TestLDAPMapperSerialize, testSerializeCreatesDirectory)
174 {
175 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
176 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
177 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
178 auto dbusPersistentFilePath = std::string(dir.c_str());
179
180 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
181 dbusPersistentFilePath.c_str(),
182 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
183 manager.createDefaultObjects();
184
185 std::string groupName = "testGroup";
186 std::string privilege = "priv-admin";
187 size_t entryId = 1;
188 auto dbusPath = std::string(LDAP_CONFIG_ROOT) +
189 "/active_directory/role_map/" + std::to_string(entryId);
190
191 auto persistPath = dbusPersistentFilePath + "/nested/path" + dbusPath;
192
193 auto entry = std::make_unique<LDAPMapperEntry>(
194 bus, dbusPath.c_str(), persistPath.c_str(), groupName, privilege,
195 *(manager.getADConfigPtr()));
196
197 auto serializedPath = serialize(*entry, persistPath);
198 EXPECT_TRUE(fs::exists(serializedPath));
199 EXPECT_TRUE(fs::exists(serializedPath.parent_path()));
200 }
201
202 } // namespace ldap
203 } // namespace phosphor
204