Fix regex in findSensors

swampd crashs while reading configuration. Root caused that it is
introduced by incorrect finding sensors. For an example, when
it searches 'Fan_1' for inputs, it finds these two sensors:

Fan_1
Pwm_PSU1_Fan_1

where 'Pwm_PSU1_Fan_1' is an unexpected search result, so it causes crash
after printing out this message:

"sensor at dbus path [/xyz/openbmc_project/control/fanpwm/Pwm_PSU1_Fan_1]
has an interface [xyz.openbmc_project.Control.FanPwm] that does not match
the expected interface of xyz.openbmc_project.Sensor.Value"

To fix this issue, this commit modifies regex string in findSensors
function.
Unit test is updated to reflect the new behavior.

Tested:
The crash was not observed.
Updated unit test passed.

Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>
Change-Id: I4e45fac3a3242a6f3655d6873fd63ef22fd0c4dd
diff --git a/dbus/dbusutil.cpp b/dbus/dbusutil.cpp
index 4ebe65f..9132a60 100644
--- a/dbus/dbusutil.cpp
+++ b/dbus/dbusutil.cpp
@@ -77,7 +77,7 @@
                  std::vector<std::pair<std::string, std::string>>& matches)
 {
     std::smatch match;
-    std::regex reg(search + '$');
+    std::regex reg('/' + search + '$');
     for (const auto& sensor : sensors)
     {
         if (std::regex_search(sensor.first, match, reg))
diff --git a/test/dbus_util_unittest.cpp b/test/dbus_util_unittest.cpp
index a4d2e20..2f20fd6 100644
--- a/test/dbus_util_unittest.cpp
+++ b/test/dbus_util_unittest.cpp
@@ -46,9 +46,8 @@
 {
   protected:
     const std::unordered_map<std::string, std::string> sensors = {
-        {"path_a", "b"},
-        {"apple", "juice"},
-        {"other_le", "thing"},
+        {"/abcd/_a", "b"}, {"_a", "c"},          {"/abcd_a", "d"},
+        {"/_a_a", "e"},    {"one/slash", "one"}, {"other_/slash", "other"},
     };
 
     std::vector<std::pair<std::string, std::string>> results;
@@ -63,12 +62,12 @@
 
 TEST_F(FindSensorsTest, OneMatches)
 {
-    const std::string target = "a";
+    const std::string target = "_a";
 
     EXPECT_TRUE(findSensors(sensors, target, results));
 
     std::vector<std::pair<std::string, std::string>> expected_results = {
-        {"path_a", "b"},
+        {"/abcd/_a", "b"},
     };
 
     EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
@@ -76,12 +75,12 @@
 
 TEST_F(FindSensorsTest, MultipleMatches)
 {
-    const std::string target = "le";
+    const std::string target = "slash";
     EXPECT_TRUE(findSensors(sensors, target, results));
 
     std::vector<std::pair<std::string, std::string>> expected_results = {
-        {"apple", "juice"},
-        {"other_le", "thing"},
+        {"one/slash", "one"},
+        {"other_/slash", "other"},
     };
 
     EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));