blob: 74c81716ea6e7aaaaa8fd6bc1244c4e620dff0a4 [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));
Ratan Gupta27d4c012019-04-12 13:03:35 +053074 std::unique_ptr<Config>& getOpenLdapConfigPtr()
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060075 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053076 return openLDAPConfigPtr;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060077 }
78
Ratan Gupta3a1c2742019-03-20 06:49:42 +053079 std::string configBindPassword()
80 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053081 return getADConfigPtr()->lDAPBindPassword;
Ratan Gupta3a1c2742019-03-20 06:49:42 +053082 }
83
Ratan Gupta27d4c012019-04-12 13:03:35 +053084 std::unique_ptr<Config>& getADConfigPtr()
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060085 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053086 return ADConfigPtr;
87 }
88 void restore()
89 {
90 // TODO enable it in later commit.
91 // phosphor::ldap::ConfigMgr::restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060092 return;
93 }
94
Ratan Gupta27d4c012019-04-12 13:03:35 +053095 void createDefaultObjects()
96 {
97 phosphor::ldap::ConfigMgr::createDefaultObjects();
98 }
99
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600100 friend class TestLDAPConfig;
101};
102
103TEST_F(TestLDAPConfig, testCreate)
104{
105 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600106 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530107 auto dbusPersistentFilePath =
108 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600109
110 if (fs::exists(configFilePath))
111 {
112 fs::remove(configFilePath);
113 }
114 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600115 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta95a29312019-02-18 20:34:10 +0530116 dbusPersistentFilePath.c_str(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600117 tlsCacertfile.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530118
119 EXPECT_CALL(manager, stopService("nslcd.service")).Times(1);
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530120 EXPECT_CALL(manager, restartService("nslcd.service")).Times(2);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600121 EXPECT_CALL(manager, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600122 manager.createConfig(
123 "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp",
124 "MyLdap12", ldap_base::Create::SearchScope::sub,
125 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta27d4c012019-04-12 13:03:35 +0530126 manager.getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530127
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600128 EXPECT_TRUE(fs::exists(configFilePath));
Ratan Gupta27d4c012019-04-12 13:03:35 +0530129 EXPECT_EQ(manager.getADConfigPtr()->lDAPServerURI(),
130 "ldap://9.194.251.136/");
131 EXPECT_EQ(manager.getADConfigPtr()->lDAPBindDN(), "cn=Users,dc=com");
132 EXPECT_EQ(manager.getADConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp");
133 EXPECT_EQ(manager.getADConfigPtr()->lDAPSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600134 ldap_base::Config::SearchScope::sub);
Ratan Gupta27d4c012019-04-12 13:03:35 +0530135 EXPECT_EQ(manager.getADConfigPtr()->lDAPType(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600136 ldap_base::Config::Type::ActiveDirectory);
Ratan Gupta27d4c012019-04-12 13:03:35 +0530137
138 EXPECT_EQ(manager.getADConfigPtr()->userNameAttribute(), "uid");
139 EXPECT_EQ(manager.getADConfigPtr()->groupNameAttribute(), "gid");
140 EXPECT_EQ(manager.getADConfigPtr()->lDAPBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530141 EXPECT_EQ(manager.configBindPassword(), "MyLdap12");
142 // change the password
Ratan Gupta27d4c012019-04-12 13:03:35 +0530143 manager.getADConfigPtr()->lDAPBindDNPassword("MyLdap14");
144 EXPECT_EQ(manager.getADConfigPtr()->lDAPBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530145 EXPECT_EQ(manager.configBindPassword(), "MyLdap14");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600146}
147
Ratan Gupta27d4c012019-04-12 13:03:35 +0530148TEST_F(TestLDAPConfig, testDefaultObject)
149{
150 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
151 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
152 auto dbusPersistentFilePath =
153 std::string(dir.c_str()) + "/" + dbusPersistFile;
154
155 if (fs::exists(configFilePath))
156 {
157 fs::remove(configFilePath);
158 }
159 EXPECT_FALSE(fs::exists(configFilePath));
160
161 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
162 dbusPersistentFilePath.c_str(),
163 tlsCacertfile.c_str());
164
165 EXPECT_CALL(manager, stopService("nslcd.service")).Times(2);
166
167 manager.createDefaultObjects();
168
169 EXPECT_NE(nullptr, manager.getADConfigPtr());
170 EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
171 EXPECT_EQ(manager.getADConfigPtr()->lDAPType(),
172 ldap_base::Config::Type::ActiveDirectory);
173 EXPECT_EQ(manager.getOpenLdapConfigPtr()->lDAPType(),
174 ldap_base::Config::Type::OpenLdap);
175}
176/*
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600177TEST_F(TestLDAPConfig, testRestores)
178{
179 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600180 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530181 auto dbusPersistentFilePath =
182 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600183
184 if (fs::exists(configFilePath))
185 {
186 fs::remove(configFilePath);
187 }
188 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600189 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530190 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
191 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
192 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600193 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600194 managerPtr->createConfig(
195 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
196 "MyLdap12", ldap_base::Create::SearchScope::sub,
197 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta95a29312019-02-18 20:34:10 +0530198 managerPtr->getConfigPtr()->enabled(false);
199
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600200 EXPECT_TRUE(fs::exists(configFilePath));
Ratan Gupta95a29312019-02-18 20:34:10 +0530201 EXPECT_FALSE(managerPtr->getConfigPtr()->enabled());
202 managerPtr->getConfigPtr()->enabled(true);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600203 // Delete LDAP configuration
204 managerPtr->deleteObject();
205 EXPECT_TRUE(fs::exists(configFilePath));
206 // Restore from configFilePath
207 managerPtr->restore(configFilePath.c_str());
208 // validate restored properties
Ratan Gupta95a29312019-02-18 20:34:10 +0530209 EXPECT_TRUE(managerPtr->getConfigPtr()->enabled());
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600210 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
211 "ldap://9.194.251.138/");
212 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(), "cn=Users,dc=com");
213 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp");
214 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(),
215 ldap_base::Config::SearchScope::sub);
216 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(),
217 ldap_base::Config::Type::ActiveDirectory);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600218 EXPECT_EQ(managerPtr->getConfigPtr()->userNameAttribute(), "uid");
219 EXPECT_EQ(managerPtr->getConfigPtr()->groupNameAttribute(), "gid");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530220 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDNPassword(), "");
221 EXPECT_EQ(managerPtr->configBindPassword(), "MyLdap12");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600222 delete managerPtr;
223}
224
225TEST_F(TestLDAPConfig, testLDAPServerURI)
226{
227 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600228 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530229 auto dbusPersistentFilePath =
230 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600231
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600232 if (fs::exists(configFilePath))
233 {
234 fs::remove(configFilePath);
235 }
236 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600237 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530238 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
239 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
240 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600241 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
242
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600243 managerPtr->createConfig(
244 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
245 "MyLdap12", ldap_base::Create::SearchScope::sub,
246 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
247 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530248
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600249 // Change LDAP Server URI
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600250 managerPtr->getConfigPtr()->lDAPServerURI("ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600251 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600252 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600253 // Change LDAP Server URI
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600254 EXPECT_THROW(
255 managerPtr->getConfigPtr()->lDAPServerURI("ldaps://9.194.251.139/"),
256 NoCACertificate);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600257 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600258 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600259 // Delete LDAP configuration
260 managerPtr->deleteObject();
261
262 managerPtr->restore(configFilePath.c_str());
263 // Check LDAP Server URI
264 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600265 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600266 delete managerPtr;
267}
268
269TEST_F(TestLDAPConfig, testLDAPBindDN)
270{
271 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600272 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530273 auto dbusPersistentFilePath =
274 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600275
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600276 if (fs::exists(configFilePath))
277 {
278 fs::remove(configFilePath);
279 }
280 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600281 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530282 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
283 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
284 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600285 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
286
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600287 managerPtr->createConfig(
288 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
289 "MyLdap12", ldap_base::Create::SearchScope::sub,
290 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
291 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530292
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600293 // Change LDAP BindDN
294 managerPtr->getConfigPtr()->lDAPBindDN(
295 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
296 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(),
297 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
298 // Change LDAP BindDN
299 EXPECT_THROW(
300 {
301 try
302 {
303 managerPtr->getConfigPtr()->lDAPBindDN("");
304 }
305 catch (const InvalidArgument& e)
306 {
307 throw;
308 }
309 },
310 InvalidArgument);
311 // Delete LDAP configuration
312 managerPtr->deleteObject();
313
314 managerPtr->restore(configFilePath.c_str());
315 // Check LDAP BindDN after restoring
316 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(),
317 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
318 delete managerPtr;
319}
320
321TEST_F(TestLDAPConfig, testLDAPBaseDN)
322{
323 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600324 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530325 auto dbusPersistentFilePath =
326 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600327
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600328 if (fs::exists(configFilePath))
329 {
330 fs::remove(configFilePath);
331 }
332 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600333 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530334 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
335 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
336 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600337 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600338 managerPtr->createConfig(
339 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
340 "MyLdap12", ldap_base::Create::SearchScope::sub,
341 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
342 managerPtr->getConfigPtr()->enabled(true);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600343 // Change LDAP BaseDN
344 managerPtr->getConfigPtr()->lDAPBaseDN(
345 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
346 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(),
347 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
348 // Change LDAP BaseDN
349 EXPECT_THROW(
350 {
351 try
352 {
353 managerPtr->getConfigPtr()->lDAPBaseDN("");
354 }
355 catch (const InvalidArgument& e)
356 {
357 throw;
358 }
359 },
360 InvalidArgument);
361 // Delete LDAP configuration
362 managerPtr->deleteObject();
363
364 managerPtr->restore(configFilePath.c_str());
365 // Check LDAP BaseDN after restoring
366 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(),
367 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
368 delete managerPtr;
369}
370
371TEST_F(TestLDAPConfig, testSearchScope)
372{
373 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600374 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530375 auto dbusPersistentFilePath =
376 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600377
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600378 if (fs::exists(configFilePath))
379 {
380 fs::remove(configFilePath);
381 }
382 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600383 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530384 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
385 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
386 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600387 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600388 managerPtr->createConfig(
389 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
390 "MyLdap12", ldap_base::Create::SearchScope::sub,
391 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
392 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530393
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600394 // Change LDAP SearchScope
395 managerPtr->getConfigPtr()->lDAPSearchScope(
396 ldap_base::Config::SearchScope::one);
397 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(),
398 ldap_base::Config::SearchScope::one);
399 // Delete LDAP configuration
400 managerPtr->deleteObject();
401
402 managerPtr->restore(configFilePath.c_str());
403 // Check LDAP SearchScope after restoring
404 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(),
405 ldap_base::Config::SearchScope::one);
406 delete managerPtr;
407}
408
409TEST_F(TestLDAPConfig, testLDAPType)
410{
411 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600412 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
Ratan Gupta95a29312019-02-18 20:34:10 +0530413 auto dbusPersistentFilePath =
414 std::string(dir.c_str()) + "/" + dbusPersistFile;
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600415
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600416 if (fs::exists(configFilePath))
417 {
418 fs::remove(configFilePath);
419 }
420 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600421 MockConfigMgr* managerPtr = new MockConfigMgr(
Ratan Gupta95a29312019-02-18 20:34:10 +0530422 bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
423 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str());
424 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600425 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600426 managerPtr->createConfig(
427 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
428 "MyLdap12", ldap_base::Create::SearchScope::sub,
429 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
430 managerPtr->getConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530431
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600432 // Change LDAP type
433 managerPtr->getConfigPtr()->lDAPType(ldap_base::Config::Type::OpenLdap);
434 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(),
435 ldap_base::Config::Type::OpenLdap);
436 // Delete LDAP configuration
437 managerPtr->deleteObject();
438
439 managerPtr->restore(configFilePath.c_str());
440 // Check LDAP type after restoring
441 EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(),
442 ldap_base::Config::Type::OpenLdap);
443 delete managerPtr;
444}
Ratan Gupta27d4c012019-04-12 13:03:35 +0530445*/
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600446} // namespace ldap
447} // namespace phosphor