Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "phosphor-ldap-config/ldap_configuration.hpp" |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 3 | #include "phosphor-ldap-config/ldap_serialize.hpp" |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 4 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <phosphor-logging/elog-errors.hpp> |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <xyz/openbmc_project/Common/error.hpp> |
| 9 | #include <sdbusplus/bus.hpp> |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 12 | |
| 13 | #include <filesystem> |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 14 | #include <fstream> |
| 15 | #include <string> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace ldap |
| 21 | { |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 22 | namespace fs = std::filesystem; |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 23 | namespace ldap_base = sdbusplus::xyz::openbmc_project::User::Ldap::server; |
| 24 | using Config = phosphor::ldap::Config; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 25 | static constexpr const char* dbusPersistFile = "Config"; |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 26 | |
| 27 | class TestLDAPConfig : public testing::Test |
| 28 | { |
| 29 | public: |
| 30 | TestLDAPConfig() : bus(sdbusplus::bus::new_default()) |
| 31 | { |
| 32 | } |
| 33 | void SetUp() override |
| 34 | { |
| 35 | using namespace phosphor::ldap; |
| 36 | char tmpldap[] = "/tmp/ldap_test.XXXXXX"; |
| 37 | dir = fs::path(mkdtemp(tmpldap)); |
| 38 | fs::path tslCacertFilePath{TLS_CACERT_FILE}; |
| 39 | tslCacertFile = tslCacertFilePath.filename().c_str(); |
| 40 | fs::path confFilePath{LDAP_CONFIG_FILE}; |
| 41 | ldapconfFile = confFilePath.filename().c_str(); |
| 42 | std::fstream fs; |
| 43 | fs.open(dir / defaultNslcdFile, std::fstream::out); |
| 44 | fs.close(); |
| 45 | fs.open(dir / nsSwitchFile, std::fstream::out); |
| 46 | fs.close(); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void TearDown() override |
| 50 | { |
| 51 | fs::remove_all(dir); |
| 52 | } |
| 53 | |
| 54 | protected: |
| 55 | fs::path dir; |
| 56 | std::string tslCacertFile; |
| 57 | std::string ldapconfFile; |
| 58 | sdbusplus::bus::bus bus; |
| 59 | }; |
| 60 | |
| 61 | class MockConfigMgr : public phosphor::ldap::ConfigMgr |
| 62 | { |
| 63 | public: |
| 64 | MockConfigMgr(sdbusplus::bus::bus& bus, const char* path, |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 65 | const char* filePath, const char* dbusPersistentFile, |
| 66 | const char* caCertFile) : |
| 67 | phosphor::ldap::ConfigMgr(bus, path, filePath, dbusPersistentFile, |
| 68 | caCertFile) |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 69 | { |
| 70 | } |
| 71 | MOCK_METHOD1(restartService, void(const std::string& service)); |
| 72 | MOCK_METHOD1(stopService, void(const std::string& service)); |
| 73 | std::unique_ptr<Config>& getConfigPtr() |
| 74 | { |
| 75 | return configPtr; |
| 76 | } |
| 77 | |
| 78 | void restore(const char* filePath) |
| 79 | { |
| 80 | phosphor::ldap::ConfigMgr::restore(filePath); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | friend class TestLDAPConfig; |
| 85 | }; |
| 86 | |
| 87 | TEST_F(TestLDAPConfig, testCreate) |
| 88 | { |
| 89 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 90 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 91 | auto dbusPersistentFilePath = |
| 92 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 93 | |
| 94 | if (fs::exists(configFilePath)) |
| 95 | { |
| 96 | fs::remove(configFilePath); |
| 97 | } |
| 98 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 99 | MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 100 | dbusPersistentFilePath.c_str(), |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 101 | tlsCacertfile.c_str()); |
Nagaraju Goruganti | bc4f180 | 2018-11-09 03:34:12 -0600 | [diff] [blame] | 102 | EXPECT_CALL(manager, restartService("nslcd.service")).Times(1); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 103 | EXPECT_CALL(manager, restartService("nscd.service")).Times(1); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 104 | manager.createConfig( |
| 105 | "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 106 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 107 | ldap_base::Create::Type::ActiveDirectory, "uid", "gid"); |
| 108 | manager.getConfigPtr()->enabled(true); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 109 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 110 | EXPECT_TRUE(fs::exists(configFilePath)); |
| 111 | EXPECT_EQ(manager.getConfigPtr()->lDAPServerURI(), "ldap://9.194.251.136/"); |
| 112 | EXPECT_EQ(manager.getConfigPtr()->lDAPBindDN(), "cn=Users,dc=com"); |
| 113 | EXPECT_EQ(manager.getConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp"); |
| 114 | EXPECT_EQ(manager.getConfigPtr()->lDAPSearchScope(), |
| 115 | ldap_base::Config::SearchScope::sub); |
| 116 | EXPECT_EQ(manager.getConfigPtr()->lDAPType(), |
| 117 | ldap_base::Config::Type::ActiveDirectory); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 118 | EXPECT_EQ(manager.getConfigPtr()->userNameAttribute(), "uid"); |
| 119 | EXPECT_EQ(manager.getConfigPtr()->groupNameAttribute(), "gid"); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST_F(TestLDAPConfig, testRestores) |
| 123 | { |
| 124 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 125 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 126 | auto dbusPersistentFilePath = |
| 127 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 128 | |
| 129 | if (fs::exists(configFilePath)) |
| 130 | { |
| 131 | fs::remove(configFilePath); |
| 132 | } |
| 133 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 134 | MockConfigMgr* managerPtr = new MockConfigMgr( |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 135 | bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
| 136 | dbusPersistentFilePath.c_str(), tlsCacertfile.c_str()); |
| 137 | EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 138 | EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 139 | managerPtr->createConfig( |
| 140 | "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 141 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 142 | ldap_base::Create::Type::ActiveDirectory, "uid", "gid"); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 143 | managerPtr->getConfigPtr()->enabled(false); |
| 144 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 145 | EXPECT_TRUE(fs::exists(configFilePath)); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 146 | EXPECT_FALSE(managerPtr->getConfigPtr()->enabled()); |
| 147 | managerPtr->getConfigPtr()->enabled(true); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 148 | // Delete LDAP configuration |
| 149 | managerPtr->deleteObject(); |
| 150 | EXPECT_TRUE(fs::exists(configFilePath)); |
| 151 | // Restore from configFilePath |
| 152 | managerPtr->restore(configFilePath.c_str()); |
| 153 | // validate restored properties |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 154 | EXPECT_TRUE(managerPtr->getConfigPtr()->enabled()); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 155 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(), |
| 156 | "ldap://9.194.251.138/"); |
| 157 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(), "cn=Users,dc=com"); |
| 158 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp"); |
| 159 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(), |
| 160 | ldap_base::Config::SearchScope::sub); |
| 161 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(), |
| 162 | ldap_base::Config::Type::ActiveDirectory); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 163 | EXPECT_EQ(managerPtr->getConfigPtr()->userNameAttribute(), "uid"); |
| 164 | EXPECT_EQ(managerPtr->getConfigPtr()->groupNameAttribute(), "gid"); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 165 | delete managerPtr; |
| 166 | } |
| 167 | |
| 168 | TEST_F(TestLDAPConfig, testLDAPServerURI) |
| 169 | { |
| 170 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 171 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 172 | auto dbusPersistentFilePath = |
| 173 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 174 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 175 | if (fs::exists(configFilePath)) |
| 176 | { |
| 177 | fs::remove(configFilePath); |
| 178 | } |
| 179 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 180 | MockConfigMgr* managerPtr = new MockConfigMgr( |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 181 | bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
| 182 | dbusPersistentFilePath.c_str(), tlsCacertfile.c_str()); |
| 183 | EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 184 | EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2); |
| 185 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 186 | managerPtr->createConfig( |
| 187 | "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 188 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 189 | ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2"); |
| 190 | managerPtr->getConfigPtr()->enabled(true); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 191 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 192 | // Change LDAP Server URI |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 193 | managerPtr->getConfigPtr()->lDAPServerURI("ldap://9.194.251.139/"); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 194 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(), |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 195 | "ldap://9.194.251.139/"); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 196 | // Change LDAP Server URI |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 197 | EXPECT_THROW( |
| 198 | managerPtr->getConfigPtr()->lDAPServerURI("ldaps://9.194.251.139/"), |
| 199 | NoCACertificate); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 200 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(), |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 201 | "ldap://9.194.251.139/"); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 202 | // Delete LDAP configuration |
| 203 | managerPtr->deleteObject(); |
| 204 | |
| 205 | managerPtr->restore(configFilePath.c_str()); |
| 206 | // Check LDAP Server URI |
| 207 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPServerURI(), |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 208 | "ldap://9.194.251.139/"); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 209 | delete managerPtr; |
| 210 | } |
| 211 | |
| 212 | TEST_F(TestLDAPConfig, testLDAPBindDN) |
| 213 | { |
| 214 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 215 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 216 | auto dbusPersistentFilePath = |
| 217 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 218 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 219 | if (fs::exists(configFilePath)) |
| 220 | { |
| 221 | fs::remove(configFilePath); |
| 222 | } |
| 223 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 224 | MockConfigMgr* managerPtr = new MockConfigMgr( |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 225 | bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
| 226 | dbusPersistentFilePath.c_str(), tlsCacertfile.c_str()); |
| 227 | EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 228 | EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2); |
| 229 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 230 | managerPtr->createConfig( |
| 231 | "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 232 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 233 | ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2"); |
| 234 | managerPtr->getConfigPtr()->enabled(true); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 235 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 236 | // Change LDAP BindDN |
| 237 | managerPtr->getConfigPtr()->lDAPBindDN( |
| 238 | "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com"); |
| 239 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(), |
| 240 | "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com"); |
| 241 | // Change LDAP BindDN |
| 242 | EXPECT_THROW( |
| 243 | { |
| 244 | try |
| 245 | { |
| 246 | managerPtr->getConfigPtr()->lDAPBindDN(""); |
| 247 | } |
| 248 | catch (const InvalidArgument& e) |
| 249 | { |
| 250 | throw; |
| 251 | } |
| 252 | }, |
| 253 | InvalidArgument); |
| 254 | // Delete LDAP configuration |
| 255 | managerPtr->deleteObject(); |
| 256 | |
| 257 | managerPtr->restore(configFilePath.c_str()); |
| 258 | // Check LDAP BindDN after restoring |
| 259 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBindDN(), |
| 260 | "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com"); |
| 261 | delete managerPtr; |
| 262 | } |
| 263 | |
| 264 | TEST_F(TestLDAPConfig, testLDAPBaseDN) |
| 265 | { |
| 266 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 267 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 268 | auto dbusPersistentFilePath = |
| 269 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 270 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 271 | if (fs::exists(configFilePath)) |
| 272 | { |
| 273 | fs::remove(configFilePath); |
| 274 | } |
| 275 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 276 | MockConfigMgr* managerPtr = new MockConfigMgr( |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 277 | bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
| 278 | dbusPersistentFilePath.c_str(), tlsCacertfile.c_str()); |
| 279 | EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 280 | EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 281 | managerPtr->createConfig( |
| 282 | "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 283 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 284 | ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2"); |
| 285 | managerPtr->getConfigPtr()->enabled(true); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 286 | // Change LDAP BaseDN |
| 287 | managerPtr->getConfigPtr()->lDAPBaseDN( |
| 288 | "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com"); |
| 289 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(), |
| 290 | "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com"); |
| 291 | // Change LDAP BaseDN |
| 292 | EXPECT_THROW( |
| 293 | { |
| 294 | try |
| 295 | { |
| 296 | managerPtr->getConfigPtr()->lDAPBaseDN(""); |
| 297 | } |
| 298 | catch (const InvalidArgument& e) |
| 299 | { |
| 300 | throw; |
| 301 | } |
| 302 | }, |
| 303 | InvalidArgument); |
| 304 | // Delete LDAP configuration |
| 305 | managerPtr->deleteObject(); |
| 306 | |
| 307 | managerPtr->restore(configFilePath.c_str()); |
| 308 | // Check LDAP BaseDN after restoring |
| 309 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPBaseDN(), |
| 310 | "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com"); |
| 311 | delete managerPtr; |
| 312 | } |
| 313 | |
| 314 | TEST_F(TestLDAPConfig, testSearchScope) |
| 315 | { |
| 316 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 317 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 318 | auto dbusPersistentFilePath = |
| 319 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 320 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 321 | if (fs::exists(configFilePath)) |
| 322 | { |
| 323 | fs::remove(configFilePath); |
| 324 | } |
| 325 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 326 | MockConfigMgr* managerPtr = new MockConfigMgr( |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 327 | bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
| 328 | dbusPersistentFilePath.c_str(), tlsCacertfile.c_str()); |
| 329 | EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 330 | EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 331 | managerPtr->createConfig( |
| 332 | "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 333 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 334 | ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2"); |
| 335 | managerPtr->getConfigPtr()->enabled(true); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 336 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 337 | // Change LDAP SearchScope |
| 338 | managerPtr->getConfigPtr()->lDAPSearchScope( |
| 339 | ldap_base::Config::SearchScope::one); |
| 340 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(), |
| 341 | ldap_base::Config::SearchScope::one); |
| 342 | // Delete LDAP configuration |
| 343 | managerPtr->deleteObject(); |
| 344 | |
| 345 | managerPtr->restore(configFilePath.c_str()); |
| 346 | // Check LDAP SearchScope after restoring |
| 347 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPSearchScope(), |
| 348 | ldap_base::Config::SearchScope::one); |
| 349 | delete managerPtr; |
| 350 | } |
| 351 | |
| 352 | TEST_F(TestLDAPConfig, testLDAPType) |
| 353 | { |
| 354 | auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 355 | auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 356 | auto dbusPersistentFilePath = |
| 357 | std::string(dir.c_str()) + "/" + dbusPersistFile; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 358 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 359 | if (fs::exists(configFilePath)) |
| 360 | { |
| 361 | fs::remove(configFilePath); |
| 362 | } |
| 363 | EXPECT_FALSE(fs::exists(configFilePath)); |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 364 | MockConfigMgr* managerPtr = new MockConfigMgr( |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 365 | bus, LDAP_CONFIG_ROOT, configFilePath.c_str(), |
| 366 | dbusPersistentFilePath.c_str(), tlsCacertfile.c_str()); |
| 367 | EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 368 | EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 369 | managerPtr->createConfig( |
| 370 | "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp", |
| 371 | "MyLdap12", ldap_base::Create::SearchScope::sub, |
| 372 | ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2"); |
| 373 | managerPtr->getConfigPtr()->enabled(true); |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 374 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 375 | // Change LDAP type |
| 376 | managerPtr->getConfigPtr()->lDAPType(ldap_base::Config::Type::OpenLdap); |
| 377 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(), |
| 378 | ldap_base::Config::Type::OpenLdap); |
| 379 | // Delete LDAP configuration |
| 380 | managerPtr->deleteObject(); |
| 381 | |
| 382 | managerPtr->restore(configFilePath.c_str()); |
| 383 | // Check LDAP type after restoring |
| 384 | EXPECT_EQ(managerPtr->getConfigPtr()->lDAPType(), |
| 385 | ldap_base::Config::Type::OpenLdap); |
| 386 | delete managerPtr; |
| 387 | } |
| 388 | } // namespace ldap |
| 389 | } // namespace phosphor |