unit-test: Test need_to_introspect function

Testing: Verified 100% code coverage of processing.cpp

Change-Id: I5ebdebe3fdcecbf250a23754f8b5c7db81bfaaa3
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/src/main.cpp b/src/main.cpp
index 5870aea..dea80f8 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,7 +6,6 @@
 #include <atomic>
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/container/flat_map.hpp>
-#include <boost/container/flat_set.hpp>
 #include <chrono>
 #include <iomanip>
 #include <iostream>
@@ -65,8 +64,8 @@
 
 AssociationOwnersType associationOwners;
 
-static boost::container::flat_set<std::string> service_whitelist;
-static boost::container::flat_set<std::string> service_blacklist;
+static WhiteBlackList service_whitelist;
+static WhiteBlackList service_blacklist;
 
 /** Exception thrown when a path is not found in the object list. */
 struct NotFoundException final : public sdbusplus::exception_t
@@ -540,21 +539,6 @@
         "Introspect");
 }
 
-bool need_to_introspect(const std::string& process_name)
-{
-    auto inWhitelist =
-        std::find_if(service_whitelist.begin(), service_whitelist.end(),
-                     [&process_name](const auto& prefix) {
-                         return boost::starts_with(process_name, prefix);
-                     }) != service_whitelist.end();
-
-    // This holds full service names, not prefixes
-    auto inBlacklist =
-        service_blacklist.find(process_name) != service_blacklist.end();
-
-    return inWhitelist && !inBlacklist;
-}
-
 void start_new_introspect(
     sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
     interface_map_type& interface_map, const std::string& process_name,
@@ -564,7 +548,7 @@
 #endif
     sdbusplus::asio::object_server& objectServer)
 {
-    if (need_to_introspect(process_name))
+    if (needToIntrospect(process_name, service_whitelist, service_blacklist))
     {
         std::shared_ptr<InProgressIntrospect> transaction =
             std::make_shared<InProgressIntrospect>(system_bus, io, process_name
@@ -626,7 +610,8 @@
 #endif
             for (const std::string& process_name : process_names)
             {
-                if (need_to_introspect(process_name))
+                if (needToIntrospect(process_name, service_whitelist,
+                                     service_blacklist))
                 {
                     start_new_introspect(system_bus, io, interface_map,
                                          process_name,
@@ -835,7 +820,8 @@
                     std::chrono::steady_clock::now());
 #endif
                 // New daemon added
-                if (need_to_introspect(name))
+                if (needToIntrospect(name, service_whitelist,
+                                     service_blacklist))
                 {
                     name_owners[new_owner] = name;
                     start_new_introspect(system_bus.get(), io, interface_map,
@@ -867,7 +853,8 @@
             {
                 return; // only introspect well-known
             }
-            if (need_to_introspect(well_known))
+            if (needToIntrospect(well_known, service_whitelist,
+                                 service_blacklist))
             {
                 auto& iface_list = interface_map[obj_path.str];
 
diff --git a/src/processing.cpp b/src/processing.cpp
index 923e147..be7bf12 100644
--- a/src/processing.cpp
+++ b/src/processing.cpp
@@ -21,3 +21,19 @@
     wellKnown = it->second;
     return true;
 }
+
+bool needToIntrospect(const std::string& processName,
+                      const WhiteBlackList& whiteList,
+                      const WhiteBlackList& blackList)
+{
+    auto inWhitelist =
+        std::find_if(whiteList.begin(), whiteList.end(),
+                     [&processName](const auto& prefix) {
+                         return boost::starts_with(processName, prefix);
+                     }) != whiteList.end();
+
+    // This holds full service names, not prefixes
+    auto inBlacklist = blackList.find(processName) != blackList.end();
+
+    return inWhitelist && !inBlacklist;
+}
diff --git a/src/processing.hpp b/src/processing.hpp
index 27ef4c6..00f8a6d 100644
--- a/src/processing.hpp
+++ b/src/processing.hpp
@@ -1,8 +1,12 @@
 #pragma once
 
 #include <boost/container/flat_map.hpp>
+#include <boost/container/flat_set.hpp>
 #include <string>
 
+/** @brief Define white list and black list data structure */
+using WhiteBlackList = boost::container::flat_set<std::string>;
+
 /** @brief Get well known name of input unique name
  *
  * If user passes in well known name then that will be returned.
@@ -15,4 +19,20 @@
  */
 bool getWellKnown(
     const boost::container::flat_map<std::string, std::string>& owners,
-    const std::string& request, std::string& wellKnown);
+    const std::string& request, std::string& well_known);
+
+/** @brief Determine if dbus service is something to monitor
+ *
+ * mapper supports a whitelist and blacklist concept. If a whitelist is provided
+ * as input then only dbus objects matching that list is monitored. If a
+ * blacklist is provided then objects matching it will not be monitored.
+ *
+ * @param[in] processName   - Dbus service name
+ * @param[in] whiteList     - The white list
+ * @param[in] blackList     - The black list
+ *
+ * @return True if input process_name should be monitored, false otherwise
+ */
+bool needToIntrospect(const std::string& processName,
+                      const WhiteBlackList& whiteList,
+                      const WhiteBlackList& blackList);
diff --git a/src/test/Makefile.am.include b/src/test/Makefile.am.include
index a698753..23ba45a 100644
--- a/src/test/Makefile.am.include
+++ b/src/test/Makefile.am.include
@@ -1,4 +1,8 @@
 src_test_well_known_SOURCES = %reldir%/well_known.cpp src/processing.cpp
 
+src_test_need_to_introspect_SOURCES = %reldir%/need_to_introspect.cpp \
+	src/processing.cpp
+
 check_PROGRAMS += \
-	%reldir%/well_known
+	%reldir%/well_known \
+	%reldir%/need_to_introspect
diff --git a/src/test/need_to_introspect.cpp b/src/test/need_to_introspect.cpp
new file mode 100644
index 0000000..6912619
--- /dev/null
+++ b/src/test/need_to_introspect.cpp
@@ -0,0 +1,43 @@
+#include "src/processing.hpp"
+
+#include <gtest/gtest.h>
+
+// Verify if name is empty, false is returned
+TEST(NeedToIntrospect, PassEmptyName)
+{
+    WhiteBlackList whiteList;
+    WhiteBlackList blackList;
+    std::string process_name;
+
+    EXPECT_FALSE(needToIntrospect(process_name, whiteList, blackList));
+}
+
+// Verify if name is on whitelist, true is returned
+TEST(NeedToIntrospect, ValidWhiteListName)
+{
+    WhiteBlackList whiteList = {"xyz.openbmc_project"};
+    WhiteBlackList blackList;
+    std::string process_name = "xyz.openbmc_project.State.Host";
+
+    EXPECT_TRUE(needToIntrospect(process_name, whiteList, blackList));
+}
+
+// Verify if name is on blacklist, false is returned
+TEST(NeedToIntrospect, ValidBlackListName)
+{
+    WhiteBlackList whiteList;
+    WhiteBlackList blackList = {"xyz.openbmc_project.State.Host"};
+    std::string process_name = "xyz.openbmc_project.State.Host";
+
+    EXPECT_FALSE(needToIntrospect(process_name, whiteList, blackList));
+}
+
+// Verify if name is on whitelist and blacklist, false is returned
+TEST(NeedToIntrospect, ValidWhiteAndBlackListName)
+{
+    WhiteBlackList whiteList = {"xyz.openbmc_project"};
+    WhiteBlackList blackList = {"xyz.openbmc_project.State.Host"};
+    std::string process_name = "xyz.openbmc_project.State.Host";
+
+    EXPECT_FALSE(needToIntrospect(process_name, whiteList, blackList));
+}