phosphor-ldap-conf: add unit tests

Added uinit tests to create and to restore config file.

Change-Id: Idf5231d46542cda1ff84241aa67aadd91a4788d6
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/phosphor-ldap-config/ldap_configuration.cpp b/phosphor-ldap-config/ldap_configuration.cpp
index 1369362..31e4e75 100644
--- a/phosphor-ldap-config/ldap_configuration.cpp
+++ b/phosphor-ldap-config/ldap_configuration.cpp
@@ -49,10 +49,12 @@
     parent.deleteObject();
     try
     {
-        fs::copy_file(defaultNslcdFile, LDAP_CONFIG_FILE,
+        fs::path configDir = fs::path(configFilePath.c_str()).parent_path();
+
+        fs::copy_file(configDir / defaultNslcdFile, LDAP_CONFIG_FILE,
                       fs::copy_options::overwrite_existing);
 
-        fs::copy_file(linuxNsSwitchFile, nsSwitchFile,
+        fs::copy_file(configDir / linuxNsSwitchFile, configDir / nsSwitchFile,
                       fs::copy_options::overwrite_existing);
     }
     catch (const std::exception& e)
@@ -236,6 +238,10 @@
     {
         throw;
     }
+    catch (const InvalidArgument& e)
+    {
+        throw;
+    }
     catch (const std::exception& e)
     {
         log<level::ERR>(e.what());
@@ -270,6 +276,10 @@
     {
         throw;
     }
+    catch (const InvalidArgument& e)
+    {
+        throw;
+    }
     catch (const std::exception& e)
     {
         log<level::ERR>(e.what());
@@ -415,7 +425,8 @@
     deleteObject();
     try
     {
-        fs::copy_file(LDAPNsSwitchFile, nsSwitchFile,
+        fs::path configDir = fs::path(configFilePath.c_str()).parent_path();
+        fs::copy_file(configDir / LDAPNsSwitchFile, configDir / nsSwitchFile,
                       fs::copy_options::overwrite_existing);
     }
     catch (const std::exception& e)
@@ -442,7 +453,7 @@
     if (!fs::exists(filePath))
     {
         log<level::ERR>("Config file doesn't exists",
-                        entry("LDAP_CONFIG_FILE=%s", LDAP_CONFIG_FILE));
+                        entry("LDAP_CONFIG_FILE=%s", configFilePath.c_str()));
         return;
     }
 
diff --git a/phosphor-ldap-config/ldap_configuration.hpp b/phosphor-ldap-config/ldap_configuration.hpp
index 034aab8..0dfb56d 100644
--- a/phosphor-ldap-config/ldap_configuration.hpp
+++ b/phosphor-ldap-config/ldap_configuration.hpp
@@ -16,10 +16,10 @@
 {
 namespace ldap
 {
-static constexpr auto defaultNslcdFile = "/etc/nslcd.conf.default";
-static constexpr auto nsSwitchFile = "/etc/nsswitch.conf";
-static constexpr auto LDAPNsSwitchFile = "/etc/nsswitch_ldap.conf";
-static constexpr auto linuxNsSwitchFile = "/etc/nsswitch_linux.conf";
+static constexpr auto defaultNslcdFile = "nslcd.conf.default";
+static constexpr auto nsSwitchFile = "nsswitch.conf";
+static constexpr auto LDAPNsSwitchFile = "nsswitch_ldap.conf";
+static constexpr auto linuxNsSwitchFile = "nsswitch_linux.conf";
 
 using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
@@ -144,13 +144,16 @@
      *  @param[in] bus - Bus to attach to.
      *  @param[in] path - Path to attach at.
      *  @param[in] filePath - LDAP configuration file.
+     *  @param[in] caCertfile - LDAP's CA certificate file.
      */
-    ConfigMgr(sdbusplus::bus::bus& bus, const char* path) :
-        CreateIface(bus, path, true), bus(bus)
+    ConfigMgr(sdbusplus::bus::bus& bus, const char* path,
+              const char* filePath) :
+        CreateIface(bus, path, true),
+        configFilePath(filePath), bus(bus)
     {
         try
         {
-            restore(LDAP_CONFIG_FILE);
+            restore(configFilePath.c_str());
             emit_object_added();
         }
         catch (const std::exception& e)
@@ -193,8 +196,10 @@
      */
     void deleteObject();
 
-  private:
+  protected:
     std::string configFilePath{};
+    std::string tlsCacertfile{};
+
     /** @brief Persistent sdbusplus D-Bus bus connection. */
     sdbusplus::bus::bus& bus;
 
diff --git a/phosphor-ldap-config/main.cpp b/phosphor-ldap-config/main.cpp
index 6bba619..e285786 100644
--- a/phosphor-ldap-config/main.cpp
+++ b/phosphor-ldap-config/main.cpp
@@ -11,11 +11,12 @@
     using namespace phosphor::logging;
     using namespace sdbusplus::xyz::openbmc_project::Common::Error;
     namespace fs = std::experimental::filesystem;
+    fs::path configDir = fs::path(LDAP_CONFIG_FILE).parent_path();
 
-    if (!fs::exists(phosphor::ldap::defaultNslcdFile) ||
-        !fs::exists(phosphor::ldap::nsSwitchFile) ||
-        (!fs::exists(phosphor::ldap::LDAPNsSwitchFile) &&
-         !fs::exists(phosphor::ldap::linuxNsSwitchFile)))
+    if (!fs::exists(configDir / phosphor::ldap::defaultNslcdFile) ||
+        !fs::exists(configDir / phosphor::ldap::nsSwitchFile) ||
+        (!fs::exists(configDir / phosphor::ldap::LDAPNsSwitchFile) &&
+         !fs::exists(configDir / phosphor::ldap::linuxNsSwitchFile)))
     {
         log<level::ERR>("Error starting LDAP Config App, configfile(s) are "
                         "missing, exiting!!!");
@@ -26,7 +27,7 @@
     // Add sdbusplus ObjectManager for the 'root' path of the LDAP config.
     sdbusplus::server::manager::manager objManager(bus, LDAP_CONFIG_ROOT);
 
-    phosphor::ldap::ConfigMgr mgr(bus, LDAP_CONFIG_ROOT);
+    phosphor::ldap::ConfigMgr mgr(bus, LDAP_CONFIG_ROOT, LDAP_CONFIG_FILE);
 
     bus.request_name(LDAP_CONFIG_BUSNAME);