blob: 05cea20ebfb29fb51d9a99b078a501716f7f1363 [file] [log] [blame]
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06001#include "config.h"
Ratan Gupta37fb3fe2019-04-13 12:54:18 +05302#include "phosphor-ldap-config/ldap_config.hpp"
Ratan Guptae1f4db62019-04-11 18:57:42 +05303#include "phosphor-ldap-config/ldap_config_mgr.hpp"
Ratan Gupta37fb3fe2019-04-13 12:54:18 +05304#include "phosphor-ldap-config/ldap_config_serialize.hpp"
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06005
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06006#include <phosphor-logging/log.hpp>
7#include <phosphor-logging/elog-errors.hpp>
8#include <sdbusplus/bus.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10#include <sdbusplus/bus.hpp>
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
Ratan Gupta95a29312019-02-18 20:34:10 +053013
14#include <filesystem>
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060015#include <fstream>
16#include <string>
17#include <sys/types.h>
18
19namespace phosphor
20{
21namespace ldap
22{
Ratan Gupta95a29312019-02-18 20:34:10 +053023namespace fs = std::filesystem;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060024namespace ldap_base = sdbusplus::xyz::openbmc_project::User::Ldap::server;
25using Config = phosphor::ldap::Config;
Ratan Gupta95a29312019-02-18 20:34:10 +053026static constexpr const char* dbusPersistFile = "Config";
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060027
28class TestLDAPConfig : public testing::Test
29{
30 public:
31 TestLDAPConfig() : bus(sdbusplus::bus::new_default())
32 {
33 }
34 void SetUp() override
35 {
36 using namespace phosphor::ldap;
37 char tmpldap[] = "/tmp/ldap_test.XXXXXX";
38 dir = fs::path(mkdtemp(tmpldap));
39 fs::path tslCacertFilePath{TLS_CACERT_FILE};
40 tslCacertFile = tslCacertFilePath.filename().c_str();
41 fs::path confFilePath{LDAP_CONFIG_FILE};
42 ldapconfFile = confFilePath.filename().c_str();
43 std::fstream fs;
44 fs.open(dir / defaultNslcdFile, std::fstream::out);
45 fs.close();
46 fs.open(dir / nsSwitchFile, std::fstream::out);
47 fs.close();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060048 }
49
50 void TearDown() override
51 {
52 fs::remove_all(dir);
53 }
54
55 protected:
56 fs::path dir;
57 std::string tslCacertFile;
58 std::string ldapconfFile;
59 sdbusplus::bus::bus bus;
60};
61
62class MockConfigMgr : public phosphor::ldap::ConfigMgr
63{
64 public:
65 MockConfigMgr(sdbusplus::bus::bus& bus, const char* path,
Ratan Gupta95a29312019-02-18 20:34:10 +053066 const char* filePath, const char* dbusPersistentFile,
67 const char* caCertFile) :
68 phosphor::ldap::ConfigMgr(bus, path, filePath, dbusPersistentFile,
69 caCertFile)
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060070 {
71 }
72 MOCK_METHOD1(restartService, void(const std::string& service));
73 MOCK_METHOD1(stopService, void(const std::string& service));
74 std::unique_ptr<Config>& getConfigPtr()
75 {
76 return configPtr;
77 }
78
Ratan Gupta3a1c2742019-03-20 06:49:42 +053079 std::string configBindPassword()
80 {
81 return getConfigPtr()->lDAPBindPassword;
82 }
83
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060084 void restore(const char* filePath)
85 {
86 phosphor::ldap::ConfigMgr::restore(filePath);
87 return;
88 }
89
90 friend class TestLDAPConfig;
91};
92
93TEST_F(TestLDAPConfig, testCreate)
94{
95 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -060096 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +053097 auto dbusPersistentFilePath =
98 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060099
100 if (fs::exists(configFilePath))
101 {
102 fs::remove(configFilePath);
103 }
104 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600105 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta95a29312019-02-18 20:34:10 +0530106 dbusPersistentFilePath.c_str(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600107 tlsCacertfile.c_str());
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530108 EXPECT_CALL(manager, restartService("nslcd.service")).Times(2);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600109 EXPECT_CALL(manager, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600110 manager.createConfig(
111 "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp",
112 "MyLdap12", ldap_base::Create::SearchScope::sub,
113 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
114 manager.getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530115
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600116 EXPECT_TRUE(fs::exists(configFilePath));
117 EXPECT_EQ(manager.getConfigPtr()->lDAPServerURI(), "ldap://9.194.251.136/");
118 EXPECT_EQ(manager.getConfigPtr()->lDAPBindDN(), "cn=Users,dc=com");
119 EXPECT_EQ(manager.getConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp");
120 EXPECT_EQ(manager.getConfigPtr()->lDAPSearchScope(),
121 ldap_base::Config::SearchScope::sub);
122 EXPECT_EQ(manager.getConfigPtr()->lDAPType(),
123 ldap_base::Config::Type::ActiveDirectory);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600124 EXPECT_EQ(manager.getConfigPtr()->userNameAttribute(), "uid");
125 EXPECT_EQ(manager.getConfigPtr()->groupNameAttribute(), "gid");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530126 EXPECT_EQ(manager.getConfigPtr()->lDAPBindDNPassword(), "");
127 EXPECT_EQ(manager.configBindPassword(), "MyLdap12");
128 // change the password
129 manager.getConfigPtr()->lDAPBindDNPassword("MyLdap14");
130 EXPECT_EQ(manager.getConfigPtr()->lDAPBindDNPassword(), "");
131 EXPECT_EQ(manager.configBindPassword(), "MyLdap14");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600132}
133
134TEST_F(TestLDAPConfig, testRestores)
135{
136 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600137 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530138 auto dbusPersistentFilePath =
139 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600140
141 if (fs::exists(configFilePath))
142 {
143 fs::remove(configFilePath);
144 }
145 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600146 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530147 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
148 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
149 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600150 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600151 managerPtr->createConfig(
152 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
153 "MyLdap12", ldap_base::Create::SearchScope::sub,
154 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta95a29312019-02-18 20:34:10 +0530155 managerPtr->getConfigPtr()->enabled(false);
156
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600157 EXPECT_TRUE(fs::exists(configFilePath));
Ratan Gupta95a29312019-02-18 20:34:10 +0530158 EXPECT_FALSE(managerPtr->getConfigPtr()->enabled());
159 managerPtr->getConfigPtr()->enabled(true);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600160 // Delete LDAP configuration
161 managerPtr->deleteObject();
162 EXPECT_TRUE(fs::exists(configFilePath));
163 // Restore from configFilePath
164 managerPtr->restore(configFilePath.c_str());
165 // validate restored properties
Ratan Gupta95a29312019-02-18 20:34:10 +0530166 EXPECT_TRUE(managerPtr->getConfigPtr()->enabled());
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600167 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
168 "ldap://9.194.251.138/");
169 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(), "cn=Users,dc=com");
170 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp");
171 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(),
172 ldap_base::Config::SearchScope::sub);
173 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(),
174 ldap_base::Config::Type::ActiveDirectory);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600175 EXPECT_EQ(managerPtr->getConfigPtr()->userNameAttribute(), "uid");
176 EXPECT_EQ(managerPtr->getConfigPtr()->groupNameAttribute(), "gid");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530177 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDNPassword(), "");
178 EXPECT_EQ(managerPtr->configBindPassword(), "MyLdap12");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600179 delete managerPtr;
180}
181
182TEST_F(TestLDAPConfig, testLDAPServerURI)
183{
184 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600185 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530186 auto dbusPersistentFilePath =
187 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600188
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600189 if (fs::exists(configFilePath))
190 {
191 fs::remove(configFilePath);
192 }
193 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600194 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530195 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
196 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
197 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600198 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
199
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600200 managerPtr->createConfig(
201 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
202 "MyLdap12", ldap_base::Create::SearchScope::sub,
203 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
204 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530205
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600206 // Change LDAP Server URI
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600207 managerPtr->getConfigPtr()->lDAPServerURI("ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600208 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600209 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600210 // Change LDAP Server URI
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600211 EXPECT_THROW(
212 managerPtr->getConfigPtr()->lDAPServerURI("ldaps://9.194.251.139/"),
213 NoCACertificate);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600214 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600215 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600216 // Delete LDAP configuration
217 managerPtr->deleteObject();
218
219 managerPtr->restore(configFilePath.c_str());
220 // Check LDAP Server URI
221 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600222 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600223 delete managerPtr;
224}
225
226TEST_F(TestLDAPConfig, testLDAPBindDN)
227{
228 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600229 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530230 auto dbusPersistentFilePath =
231 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600232
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600233 if (fs::exists(configFilePath))
234 {
235 fs::remove(configFilePath);
236 }
237 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600238 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530239 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
240 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
241 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600242 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
243
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600244 managerPtr->createConfig(
245 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
246 "MyLdap12", ldap_base::Create::SearchScope::sub,
247 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
248 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530249
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600250 // Change LDAP BindDN
251 managerPtr->getConfigPtr()->lDAPBindDN(
252 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
253 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(),
254 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
255 // Change LDAP BindDN
256 EXPECT_THROW(
257 {
258 try
259 {
260 managerPtr->getConfigPtr()->lDAPBindDN("");
261 }
262 catch (const InvalidArgument& e)
263 {
264 throw;
265 }
266 },
267 InvalidArgument);
268 // Delete LDAP configuration
269 managerPtr->deleteObject();
270
271 managerPtr->restore(configFilePath.c_str());
272 // Check LDAP BindDN after restoring
273 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(),
274 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
275 delete managerPtr;
276}
277
278TEST_F(TestLDAPConfig, testLDAPBaseDN)
279{
280 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600281 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530282 auto dbusPersistentFilePath =
283 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600284
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600285 if (fs::exists(configFilePath))
286 {
287 fs::remove(configFilePath);
288 }
289 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600290 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530291 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
292 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
293 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600294 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600295 managerPtr->createConfig(
296 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
297 "MyLdap12", ldap_base::Create::SearchScope::sub,
298 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
299 managerPtr->getConfigPtr()->enabled(true);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600300 // Change LDAP BaseDN
301 managerPtr->getConfigPtr()->lDAPBaseDN(
302 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
303 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(),
304 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
305 // Change LDAP BaseDN
306 EXPECT_THROW(
307 {
308 try
309 {
310 managerPtr->getConfigPtr()->lDAPBaseDN("");
311 }
312 catch (const InvalidArgument& e)
313 {
314 throw;
315 }
316 },
317 InvalidArgument);
318 // Delete LDAP configuration
319 managerPtr->deleteObject();
320
321 managerPtr->restore(configFilePath.c_str());
322 // Check LDAP BaseDN after restoring
323 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(),
324 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
325 delete managerPtr;
326}
327
328TEST_F(TestLDAPConfig, testSearchScope)
329{
330 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600331 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530332 auto dbusPersistentFilePath =
333 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600334
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600335 if (fs::exists(configFilePath))
336 {
337 fs::remove(configFilePath);
338 }
339 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600340 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530341 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
342 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
343 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600344 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600345 managerPtr->createConfig(
346 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
347 "MyLdap12", ldap_base::Create::SearchScope::sub,
348 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
349 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530350
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600351 // Change LDAP SearchScope
352 managerPtr->getConfigPtr()->lDAPSearchScope(
353 ldap_base::Config::SearchScope::one);
354 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(),
355 ldap_base::Config::SearchScope::one);
356 // Delete LDAP configuration
357 managerPtr->deleteObject();
358
359 managerPtr->restore(configFilePath.c_str());
360 // Check LDAP SearchScope after restoring
361 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(),
362 ldap_base::Config::SearchScope::one);
363 delete managerPtr;
364}
365
366TEST_F(TestLDAPConfig, testLDAPType)
367{
368 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600369 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530370 auto dbusPersistentFilePath =
371 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600372
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600373 if (fs::exists(configFilePath))
374 {
375 fs::remove(configFilePath);
376 }
377 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600378 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530379 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
380 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
381 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600382 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600383 managerPtr->createConfig(
384 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
385 "MyLdap12", ldap_base::Create::SearchScope::sub,
386 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
387 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530388
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600389 // Change LDAP type
390 managerPtr->getConfigPtr()->lDAPType(ldap_base::Config::Type::OpenLdap);
391 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(),
392 ldap_base::Config::Type::OpenLdap);
393 // Delete LDAP configuration
394 managerPtr->deleteObject();
395
396 managerPtr->restore(configFilePath.c_str());
397 // Check LDAP type after restoring
398 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(),
399 ldap_base::Config::Type::OpenLdap);
400 delete managerPtr;
401}
402} // namespace ldap
403} // namespace phosphor