Make the dbushelper own its own bus handle.

The dbushelper implements an interface that should not be sdbusplus
specific. By making the implementation own the sdbusplus aspects, an
alternate implementation can be swapped in.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I4109772499421e2e6497a0fcad663ebbd1210a7c
diff --git a/test/dbus_active_unittest.cpp b/test/dbus_active_unittest.cpp
index 054ad1d..3fb1dc6 100644
--- a/test/dbus_active_unittest.cpp
+++ b/test/dbus_active_unittest.cpp
@@ -3,6 +3,7 @@
 
 #include <sdbusplus/test/sdbus_mock.hpp>
 
+#include <memory>
 #include <string>
 
 #include <gmock/gmock.h>
@@ -23,11 +24,11 @@
 
     sdbusplus::SdBusMock sdbus_mock;
     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
-    DbusHelperMock helper;
+    auto helper = std::make_unique<DbusHelperMock>();
     std::string path = "/asdf";
     std::string service = "asdfasdf.asdfasdf";
 
-    DbusActiveRead ar(bus_mock, path, service, &helper);
+    DbusActiveRead ar(bus_mock, path, service, std::move(helper));
 }
 
 TEST(DbusActiveReadTest, Read_VerifyCallsToDbusForValue)
@@ -36,13 +37,11 @@
 
     sdbusplus::SdBusMock sdbus_mock;
     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
-    DbusHelperMock helper;
+    auto helper = std::make_unique<DbusHelperMock>();
     std::string path = "/asdf";
     std::string service = "asdfasdf.asdfasdf";
 
-    DbusActiveRead ar(bus_mock, path, service, &helper);
-
-    EXPECT_CALL(helper, getProperties(_, service, path, NotNull()))
+    EXPECT_CALL(*helper, getProperties(_, service, path, NotNull()))
         .WillOnce(
             Invoke([&](sdbusplus::bus::bus& bus, const std::string& service,
                        const std::string& path, struct SensorProperties* prop) {
@@ -51,6 +50,8 @@
                 prop->unit = "x";
             }));
 
+    DbusActiveRead ar(bus_mock, path, service, std::move(helper));
+
     ReadReturn r = ar.read();
     EXPECT_EQ(10, r.value);
 }
diff --git a/test/dbus_passive_unittest.cpp b/test/dbus_passive_unittest.cpp
index 5bddf51..8eb3416 100644
--- a/test/dbus_passive_unittest.cpp
+++ b/test/dbus_passive_unittest.cpp
@@ -5,6 +5,7 @@
 #include <sdbusplus/test/sdbus_mock.hpp>
 
 #include <functional>
+#include <memory>
 #include <string>
 #include <variant>
 
@@ -35,11 +36,11 @@
     std::string type = "invalid";
     std::string id = "id";
 
-    DbusHelperMock helper;
+    auto helper = std::make_unique<DbusHelperMock>();
     auto info = conf::SensorConfig();
 
     std::unique_ptr<ReadInterface> ri = DbusPassive::createDbusPassive(
-        bus_mock, type, id, &helper, &info, nullptr);
+        bus_mock, type, id, std::move(helper), &info, nullptr);
 
     EXPECT_EQ(ri, nullptr);
 }
@@ -54,10 +55,11 @@
     std::string id = "id";
     std::string path = "/xyz/openbmc_project/sensors/unknown/id";
 
-    DbusHelperMock helper;
+    auto helper = std::make_unique<DbusHelperMock>();
     struct SensorProperties properties;
 
-    DbusPassive(bus_mock, type, id, &helper, properties, false, path, nullptr);
+    DbusPassive(bus_mock, type, id, std::move(helper), properties, false, path,
+                nullptr);
     // Success
 }
 
@@ -66,12 +68,13 @@
   protected:
     DbusPassiveTestObj() :
         sdbus_mock(),
-        bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
+        bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
+        helper(std::make_unique<DbusHelperMock>())
     {
-        EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
+        EXPECT_CALL(*helper, getService(_, StrEq(SensorIntf), StrEq(path)))
             .WillOnce(Return("asdf"));
 
-        EXPECT_CALL(helper,
+        EXPECT_CALL(*helper,
                     getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
             .WillOnce(Invoke(
                 [&](sdbusplus::bus::bus& bus, const std::string& service,
@@ -82,19 +85,19 @@
                     prop->min = 0;
                     prop->max = 0;
                 }));
-        EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
+        EXPECT_CALL(*helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
             .WillOnce(Return(false));
 
         auto info = conf::SensorConfig();
-        ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
-                                            nullptr);
+        ri = DbusPassive::createDbusPassive(bus_mock, type, id,
+                                            std::move(helper), &info, nullptr);
         passive = reinterpret_cast<DbusPassive*>(ri.get());
         EXPECT_FALSE(passive == nullptr);
     }
 
     sdbusplus::SdBusMock sdbus_mock;
     sdbusplus::bus::bus bus_mock;
-    DbusHelperMock helper;
+    std::unique_ptr<DbusHelperMock> helper;
     std::string type = "temp";
     std::string id = "id";
     std::string path = "/xyz/openbmc_project/sensors/temperature/id";
@@ -453,27 +456,28 @@
   protected:
     DbusPassiveTest3kMaxObj() :
         sdbus_mock(),
-        bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
+        bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
+        helper(std::make_unique<DbusHelperMock>())
     {
-        EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
+        EXPECT_CALL(*helper, getService(_, StrEq(SensorIntf), StrEq(path)))
             .WillOnce(Return("asdf"));
 
-        EXPECT_CALL(helper,
+        EXPECT_CALL(*helper,
                     getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
             .WillOnce(_getProps);
-        EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
+        EXPECT_CALL(*helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
             .WillOnce(Return(false));
 
         auto info = conf::SensorConfig();
-        ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
-                                            nullptr);
+        ri = DbusPassive::createDbusPassive(bus_mock, type, id,
+                                            std::move(helper), &info, nullptr);
         passive = reinterpret_cast<DbusPassive*>(ri.get());
         EXPECT_FALSE(passive == nullptr);
     }
 
     sdbusplus::SdBusMock sdbus_mock;
     sdbusplus::bus::bus bus_mock;
-    DbusHelperMock helper;
+    std::unique_ptr<DbusHelperMock> helper;
     std::string type = "temp";
     std::string id = "id";
     std::string path = "/xyz/openbmc_project/sensors/temperature/id";
@@ -496,28 +500,29 @@
   protected:
     DbusPassiveTest3kMaxIgnoredObj() :
         sdbus_mock(),
-        bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
+        bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
+        helper(std::make_unique<DbusHelperMock>())
     {
-        EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
+        EXPECT_CALL(*helper, getService(_, StrEq(SensorIntf), StrEq(path)))
             .WillOnce(Return("asdf"));
 
-        EXPECT_CALL(helper,
+        EXPECT_CALL(*helper,
                     getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
             .WillOnce(_getProps);
-        EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
+        EXPECT_CALL(*helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
             .WillOnce(Return(false));
 
         auto info = conf::SensorConfig();
         info.ignoreDbusMinMax = true;
-        ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
-                                            nullptr);
+        ri = DbusPassive::createDbusPassive(bus_mock, type, id,
+                                            std::move(helper), &info, nullptr);
         passive = reinterpret_cast<DbusPassive*>(ri.get());
         EXPECT_FALSE(passive == nullptr);
     }
 
     sdbusplus::SdBusMock sdbus_mock;
     sdbusplus::bus::bus bus_mock;
-    DbusHelperMock helper;
+    std::unique_ptr<DbusHelperMock> helper;
     std::string type = "temp";
     std::string id = "id";
     std::string path = "/xyz/openbmc_project/sensors/temperature/id";