bmc: move from data section objs to owned

Move from objects created ahead of purpose to owned objects.  This is a
step towards integrating with the json support.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I738a5edd40724f17245911af1080c350f029fef1
diff --git a/bmc/firmware_handler.cpp b/bmc/firmware_handler.cpp
index d193a4d..fede302 100644
--- a/bmc/firmware_handler.cpp
+++ b/bmc/firmware_handler.cpp
@@ -39,7 +39,7 @@
 
 std::unique_ptr<blobs::GenericBlobInterface>
     FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        const std::vector<HandlerPack>& firmwares,
+        std::vector<HandlerPack>&& firmwares,
         const std::vector<DataHandlerPack>& transports, ActionMap&& actionPacks)
 {
     /* There must be at least one. */
@@ -71,8 +71,9 @@
         bitmask |= item.bitmask;
     }
 
-    return std::make_unique<FirmwareBlobHandler>(
-        firmwares, blobs, transports, bitmask, std::move(actionPacks));
+    return std::make_unique<FirmwareBlobHandler>(std::move(firmwares), blobs,
+                                                 transports, bitmask,
+                                                 std::move(actionPacks));
 }
 
 /* Check if the path is in our supported list (or active list). */
@@ -507,7 +508,7 @@
 
     curr->flags = flags;
     curr->dataHandler = d->handler;
-    curr->imageHandler = h->handler;
+    curr->imageHandler = h->handler.get();
 
     lookup[session] = curr;
 
diff --git a/bmc/firmware_handler.hpp b/bmc/firmware_handler.hpp
index 727c68e..472bb8e 100644
--- a/bmc/firmware_handler.hpp
+++ b/bmc/firmware_handler.hpp
@@ -107,7 +107,7 @@
      */
     static std::unique_ptr<blobs::GenericBlobInterface>
         CreateFirmwareBlobHandler(
-            const std::vector<HandlerPack>& firmwares,
+            std::vector<HandlerPack>&& firmwares,
             const std::vector<DataHandlerPack>& transports,
             ActionMap&& actionPacks);
 
@@ -121,11 +121,11 @@
      * @param[in] verification - pointer to object for triggering verification
      * @param[in] update - point to object for triggering the update
      */
-    FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares,
+    FirmwareBlobHandler(std::vector<HandlerPack>&& firmwares,
                         const std::vector<std::string>& blobs,
                         const std::vector<DataHandlerPack>& transports,
                         std::uint16_t bitmask, ActionMap&& actionPacks) :
-        handlers(firmwares),
+        handlers(std::move(firmwares)),
         blobIDs(blobs), transports(transports), bitmask(bitmask),
         activeImage(activeImageBlobId), activeHash(activeHashBlobId),
         verifyImage(verifyBlobId), updateImage(updateBlobId), lookup(),
diff --git a/bmc/image_handler.hpp b/bmc/image_handler.hpp
index b7d682b..52d682f 100644
--- a/bmc/image_handler.hpp
+++ b/bmc/image_handler.hpp
@@ -48,10 +48,25 @@
     virtual int getSize() = 0;
 };
 
-struct HandlerPack
+class HandlerPack
 {
+  public:
+    HandlerPack(const std::string& name,
+                std::unique_ptr<ImageHandlerInterface> handler) :
+        blobName(name),
+        handler(std::move(handler))
+    {
+    }
+
+    HandlerPack() = default;
+    ~HandlerPack() = default;
+    HandlerPack(const HandlerPack&) = delete;
+    HandlerPack& operator=(const HandlerPack&) = delete;
+    HandlerPack(HandlerPack&&) = default;
+    HandlerPack& operator=(HandlerPack&&) = default;
+
     std::string blobName;
-    ImageHandlerInterface* handler;
+    std::unique_ptr<ImageHandlerInterface> handler;
 };
 
 } // namespace ipmi_flash
diff --git a/bmc/main.cpp b/bmc/main.cpp
index 9f1088a..5f16ff6 100644
--- a/bmc/main.cpp
+++ b/bmc/main.cpp
@@ -41,18 +41,6 @@
 namespace
 {
 
-FileHandler hashHandler(HASH_FILENAME);
-
-#ifdef ENABLE_STATIC_LAYOUT
-FileHandler staticLayoutHandler(STATIC_HANDLER_STAGED_NAME);
-#endif
-#ifdef ENABLE_TARBALL_UBI
-FileHandler ubitarballHandler(TARBALL_STAGED_NAME);
-#endif
-#ifdef ENABLE_HOST_BIOS
-FileHandler biosHandler(BIOS_STAGED_NAME);
-#endif
-
 /* The maximum external buffer size we expect is 64KB. */
 static constexpr std::size_t memoryRegionSize = 64 * 1024UL;
 
@@ -76,19 +64,6 @@
 #endif
 #endif
 
-std::vector<HandlerPack> supportedFirmware = {
-    {hashBlobId, &hashHandler},
-#ifdef ENABLE_STATIC_LAYOUT
-    {staticLayoutBlobId, &staticLayoutHandler},
-#endif
-#ifdef ENABLE_TARBALL_UBI
-    {ubiTarballBlobId, &ubitarballHandler},
-#endif
-#ifdef ENABLE_HOST_BIOS
-    {biosBlobId, &biosHandler},
-#endif
-};
-
 std::vector<DataHandlerPack> supportedTransports = {
     {FirmwareFlags::UpdateFlags::ipmi, nullptr},
 #ifdef ENABLE_PCI_BRIDGE
@@ -166,8 +141,31 @@
     }
 #endif
 
+    std::vector<ipmi_flash::HandlerPack> supportedFirmware;
+
+    supportedFirmware.push_back(std::move(ipmi_flash::HandlerPack(
+        ipmi_flash::hashBlobId,
+        std::make_unique<ipmi_flash::FileHandler>(HASH_FILENAME))));
+
+#ifdef ENABLE_STATIC_LAYOUT
+    supportedFirmware.push_back(std::move(
+        ipmi_flash::HandlerPack(ipmi_flash::staticLayoutBlobId,
+                                std::make_unique<ipmi_flash::FileHandler>(
+                                    STATIC_HANDLER_STAGED_NAME))));
+#endif
+#ifdef ENABLE_TARBALL_UBI
+    supportedFirmware.push_back(std::move(ipmi_flash::HandlerPack(
+        ipmi_flash::ubiTarballBlobId,
+        std::make_unique<ipmi_flash::FileHandler>(TARBALL_STAGED_NAME))));
+#endif
+#ifdef ENABLE_HOST_BIOS
+    supportedFirmware.push_back(std::move(ipmi_flash::HandlerPack(
+        ipmi_flash::biosBlobId,
+        std::make_unique<ipmi_flash::FileHandler>(BIOS_STAGED_NAME))));
+#endif
+
     auto handler = ipmi_flash::FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        ipmi_flash::supportedFirmware, ipmi_flash::supportedTransports,
+        std::move(supportedFirmware), ipmi_flash::supportedTransports,
         std::move(actionPacks));
 
     if (!handler)
diff --git a/bmc/test/firmware_canhandle_unittest.cpp b/bmc/test/firmware_canhandle_unittest.cpp
index 80bcce9..c542139 100644
--- a/bmc/test/firmware_canhandle_unittest.cpp
+++ b/bmc/test/firmware_canhandle_unittest.cpp
@@ -24,17 +24,20 @@
 
     ImageHandlerMock imageMock;
 
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-        {"bcdf", &imageMock},
-    };
+    std::vector<HandlerPack> blobs;
+    blobs.push_back(std::move(
+        HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
+    blobs.push_back(
+        std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
+    blobs.push_back(
+        std::move(HandlerPack("bcdf", std::make_unique<ImageHandlerMock>())));
+
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr},
     };
 
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("asdf")));
+        std::move(blobs), data, std::move(CreateActionMap("asdf")));
 
     for (const auto& item : items)
     {
diff --git a/bmc/test/firmware_close_unittest.cpp b/bmc/test/firmware_close_unittest.cpp
index d4e7cf8..87de86e 100644
--- a/bmc/test/firmware_close_unittest.cpp
+++ b/bmc/test/firmware_close_unittest.cpp
@@ -30,7 +30,7 @@
      * everything looks right.
      */
     EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
-    EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
+    EXPECT_CALL(*hashImageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc,
@@ -44,7 +44,7 @@
 
     /* Set up close() expectations. */
     EXPECT_CALL(dataMock, close());
-    EXPECT_CALL(imageMock, close());
+    EXPECT_CALL(*hashImageMock, close());
     EXPECT_TRUE(handler->close(0));
 
     /* Close does not delete the active blob id.  This indicates that there is
@@ -57,7 +57,7 @@
     /* Boring test where you open a blob_id using ipmi, so there's no data
      * handler, and it's closed and everything looks right.
      */
-    EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
+    EXPECT_CALL(*hashImageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi,
@@ -70,7 +70,7 @@
                             activeHashBlobId));
 
     /* Set up close() expectations. */
-    EXPECT_CALL(imageMock, close());
+    EXPECT_CALL(*hashImageMock, close());
     EXPECT_TRUE(handler->close(0));
 }
 
diff --git a/bmc/test/firmware_commit_unittest.cpp b/bmc/test/firmware_commit_unittest.cpp
index 16d9881..51592a9 100644
--- a/bmc/test/firmware_commit_unittest.cpp
+++ b/bmc/test/firmware_commit_unittest.cpp
@@ -24,16 +24,20 @@
 class FirmwareHandlerCommitTest : public ::testing::Test
 {
   protected:
-    ImageHandlerMock imageMock1, imageMock2;
+    ImageHandlerMock *imageMock1, *imageMock2;
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data;
 
     void SetUp() override
     {
-        blobs = {
-            {hashBlobId, &imageMock1},
-            {"asdf", &imageMock2},
-        };
+        std::unique_ptr<ImageHandlerInterface> image =
+            std::make_unique<ImageHandlerMock>();
+        imageMock1 = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack("asdf", std::move(image))));
 
         data = {
             {FirmwareFlags::UpdateFlags::ipmi, nullptr},
@@ -52,9 +56,9 @@
         std::make_unique<StrictMock<TriggerMock>>();
 
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("asdf")));
+        std::move(blobs), data, std::move(CreateActionMap("asdf")));
 
-    EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock2, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
@@ -73,9 +77,9 @@
         std::make_unique<StrictMock<TriggerMock>>();
 
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("asdf")));
+        std::move(blobs), data, std::move(CreateActionMap("asdf")));
 
-    EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi,
diff --git a/bmc/test/firmware_createhandler_unittest.cpp b/bmc/test/firmware_createhandler_unittest.cpp
index 2800f2e..8eb36c6 100644
--- a/bmc/test/firmware_createhandler_unittest.cpp
+++ b/bmc/test/firmware_createhandler_unittest.cpp
@@ -5,6 +5,8 @@
 #include "triggerable_mock.hpp"
 #include "util.hpp"
 
+#include <memory>
+
 #include <gtest/gtest.h>
 
 namespace ipmi_flash
@@ -25,9 +27,9 @@
     //    StrictMock<SdJournalMock> journalMock;
     //    SwapJouralHandler(&journalMock);
 
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-    };
+    std::vector<HandlerPack> blobs;
+    blobs.push_back(std::move(
+        HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
 
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr},
@@ -35,7 +37,7 @@
     };
 
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("abcd")));
+        std::move(blobs), data, std::move(CreateActionMap("abcd")));
 
     //    EXPECT_EQ(handler, nullptr);
     EXPECT_FALSE(handler == nullptr);
diff --git a/bmc/test/firmware_handler_unittest.cpp b/bmc/test/firmware_handler_unittest.cpp
index 29df507..700a75a 100644
--- a/bmc/test/firmware_handler_unittest.cpp
+++ b/bmc/test/firmware_handler_unittest.cpp
@@ -5,6 +5,7 @@
 #include "util.hpp"
 
 #include <algorithm>
+#include <memory>
 #include <vector>
 
 #include <gtest/gtest.h>
@@ -30,35 +31,39 @@
 {
     ImageHandlerMock imageMock;
 
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
+    std::vector<HandlerPack> blobs;
+    blobs.push_back(std::move(
+        HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
+    blobs.push_back(
+        std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
 
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, {}, std::move(CreateActionMap("asdf")));
+        std::move(blobs), {}, std::move(CreateActionMap("asdf")));
     EXPECT_EQ(handler, nullptr);
 }
 TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness)
 {
-    /* This works fine only if you also pass in the hash handler. */
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {"asdf", &imageMock},
-    };
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr},
     };
 
+    /* This works fine only if you also pass in the hash handler. */
+    std::vector<HandlerPack> blobs;
+    blobs.push_back(
+        std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
+
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("asdf")));
+        std::move(blobs), data, std::move(CreateActionMap("asdf")));
     EXPECT_EQ(handler, nullptr);
 
-    blobs.push_back({hashBlobId, &imageMock});
+    std::vector<HandlerPack> blobs2;
+    blobs2.push_back(
+        std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
+    blobs2.push_back(std::move(
+        HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
 
     handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("asdf")));
+        std::move(blobs2), data, std::move(CreateActionMap("asdf")));
 
     auto result = handler->getBlobIds();
     std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
diff --git a/bmc/test/firmware_multiplebundle_unittest.cpp b/bmc/test/firmware_multiplebundle_unittest.cpp
index 2c450f4..1cbda1a 100644
--- a/bmc/test/firmware_multiplebundle_unittest.cpp
+++ b/bmc/test/firmware_multiplebundle_unittest.cpp
@@ -30,11 +30,19 @@
   protected:
     void SetUp() override
     {
-        blobs = {
-            {hashBlobId, &hashImageMock},
-            {staticLayoutBlobId, &staticImageMock},
-            {biosBlobId, &biosImageMock},
-        };
+        std::unique_ptr<ImageHandlerInterface> image =
+            std::make_unique<ImageHandlerMock>();
+        hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        staticImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(
+            std::move(HandlerPack(staticLayoutBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        biosImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(biosBlobId, std::move(image))));
 
         std::unique_ptr<TriggerableActionInterface> bmcPrepareMock =
             std::make_unique<TriggerMock>();
@@ -80,7 +88,7 @@
         packs[biosBlobId] = std::move(biosPack);
 
         handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-            blobs, data, std::move(packs));
+            std::move(blobs), data, std::move(packs));
     }
 
     void expectedState(FirmwareBlobHandler::UpdateState state)
@@ -89,7 +97,7 @@
         EXPECT_EQ(state, realHandler->getCurrentState());
     }
 
-    ImageHandlerMock hashImageMock, staticImageMock, biosImageMock;
+    ImageHandlerMock *hashImageMock, *staticImageMock, *biosImageMock;
 
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data = {
@@ -110,42 +118,42 @@
     /* You can only have one file open at a time, and the first firmware file
      * you open locks it down
      */
-    EXPECT_CALL(staticImageMock, open(staticLayoutBlobId))
+    EXPECT_CALL(*staticImageMock, open(staticLayoutBlobId))
         .WillOnce(Return(true));
     EXPECT_CALL(*bmcPrepareMockPtr, trigger()).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
     expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
-    EXPECT_CALL(staticImageMock, close()).WillOnce(Return());
+    EXPECT_CALL(*staticImageMock, close()).WillOnce(Return());
     handler->close(session);
 
     expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
-    EXPECT_CALL(biosImageMock, open(biosBlobId)).Times(0);
+    EXPECT_CALL(*biosImageMock, open(biosBlobId)).Times(0);
     EXPECT_FALSE(handler->open(session, flags, biosBlobId));
 }
 
 TEST_F(IpmiOnlyTwoFirmwaresTest, OpeningHashBeforeBiosSucceeds)
 {
     /* Opening the hash blob does nothing special in this regard. */
-    EXPECT_CALL(hashImageMock, open(hashBlobId)).WillOnce(Return(true));
+    EXPECT_CALL(*hashImageMock, open(hashBlobId)).WillOnce(Return(true));
     EXPECT_TRUE(handler->open(session, flags, hashBlobId));
 
     expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
-    EXPECT_CALL(hashImageMock, close()).WillOnce(Return());
+    EXPECT_CALL(*hashImageMock, close()).WillOnce(Return());
     handler->close(session);
 
     expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
     ASSERT_FALSE(handler->canHandleBlob(verifyBlobId));
 
-    EXPECT_CALL(biosImageMock, open(biosBlobId)).WillOnce(Return(true));
+    EXPECT_CALL(*biosImageMock, open(biosBlobId)).WillOnce(Return(true));
     EXPECT_TRUE(handler->open(session, flags, biosBlobId));
 
     expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
-    EXPECT_CALL(biosImageMock, close()).WillOnce(Return());
+    EXPECT_CALL(*biosImageMock, close()).WillOnce(Return());
     handler->close(session);
 }
 
diff --git a/bmc/test/firmware_sessionstat_unittest.cpp b/bmc/test/firmware_sessionstat_unittest.cpp
index dcb16d9..9623246 100644
--- a/bmc/test/firmware_sessionstat_unittest.cpp
+++ b/bmc/test/firmware_sessionstat_unittest.cpp
@@ -27,13 +27,13 @@
     /* Verifying running stat if the type of data session is IPMI returns no
      * metadata.
      */
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
 
     int size = 512;
-    EXPECT_CALL(imageMock, getSize()).WillOnce(Return(size));
+    EXPECT_CALL(*imageMock, getSize()).WillOnce(Return(size));
 
     blobs::BlobMeta meta;
     EXPECT_TRUE(handler->stat(0, &meta));
@@ -50,13 +50,13 @@
      * simply implementing read().
      */
     EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
 
     int size = 512;
-    EXPECT_CALL(imageMock, getSize()).WillOnce(Return(size));
+    EXPECT_CALL(*imageMock, getSize()).WillOnce(Return(size));
     std::vector<std::uint8_t> mBytes = {0x01, 0x02};
     EXPECT_CALL(dataMock, readMeta()).WillOnce(Return(mBytes));
 
diff --git a/bmc/test/firmware_stat_unittest.cpp b/bmc/test/firmware_stat_unittest.cpp
index b71d5c7..6e97cc8 100644
--- a/bmc/test/firmware_stat_unittest.cpp
+++ b/bmc/test/firmware_stat_unittest.cpp
@@ -4,12 +4,16 @@
 #include "triggerable_mock.hpp"
 #include "util.hpp"
 
+#include <memory>
 #include <vector>
 
 #include <gtest/gtest.h>
 
 namespace ipmi_flash
 {
+namespace
+{
+
 TEST(FirmwareHandlerStatTest, StatOnInactiveBlobIDReturnsTransport)
 {
     /* Test that the metadata information returned matches expectations for this
@@ -19,22 +23,23 @@
      * the input for this function.
      */
 
-    ImageHandlerMock imageMock;
+    std::vector<HandlerPack> blobs;
+    blobs.push_back(std::move(
+        HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
+    blobs.push_back(
+        std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
 
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr},
     };
 
     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, std::move(CreateActionMap("asdf")));
+        std::move(blobs), data, std::move(CreateActionMap("asdf")));
 
     blobs::BlobMeta meta;
     EXPECT_TRUE(handler->stat("asdf", &meta));
     EXPECT_EQ(FirmwareFlags::UpdateFlags::ipmi, meta.blobState);
 }
 
+} // namespace
 } // namespace ipmi_flash
diff --git a/bmc/test/firmware_state_notyetstarted_tarball_unittest.cpp b/bmc/test/firmware_state_notyetstarted_tarball_unittest.cpp
index 3ad118c..e171646 100644
--- a/bmc/test/firmware_state_notyetstarted_tarball_unittest.cpp
+++ b/bmc/test/firmware_state_notyetstarted_tarball_unittest.cpp
@@ -5,6 +5,7 @@
 #include "firmware_handler.hpp"
 #include "firmware_unittest.hpp"
 
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -22,10 +23,15 @@
   protected:
     void SetUp() override
     {
-        blobs = {
-            {hashBlobId, &imageMock},
-            {ubiTarballBlobId, &imageMock},
-        };
+        std::unique_ptr<ImageHandlerInterface> image =
+            std::make_unique<ImageHandlerMock>();
+        hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(
+            std::move(HandlerPack(ubiTarballBlobId, std::move(image))));
 
         std::unique_ptr<TriggerableActionInterface> verifyMock =
             std::make_unique<TriggerMock>();
@@ -44,7 +50,7 @@
         packs[ubiTarballBlobId] = std::move(actionPack);
 
         handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-            blobs, data, std::move(packs));
+            std::move(blobs), data, std::move(packs));
     }
 
     void expectedState(FirmwareBlobHandler::UpdateState state)
@@ -55,12 +61,19 @@
 
     void openToInProgress(const std::string& blobId)
     {
-        EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
+        if (blobId == hashBlobId)
+        {
+            EXPECT_CALL(*hashImageMock, open(blobId)).WillOnce(Return(true));
+        }
+        else
+        {
+            EXPECT_CALL(*imageMock, open(blobId)).WillOnce(Return(true));
+        }
         EXPECT_TRUE(handler->open(session, flags, blobId));
         expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
     }
 
-    ImageHandlerMock imageMock;
+    ImageHandlerMock *hashImageMock, *imageMock;
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
diff --git a/bmc/test/firmware_state_notyetstarted_unittest.cpp b/bmc/test/firmware_state_notyetstarted_unittest.cpp
index 3c0eb67..8aa811f 100644
--- a/bmc/test/firmware_state_notyetstarted_unittest.cpp
+++ b/bmc/test/firmware_state_notyetstarted_unittest.cpp
@@ -92,7 +92,7 @@
  */
 TEST_F(FirmwareHandlerNotYetStartedTest, OpenStaticImageFileVerifyStateChange)
 {
-    EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock2, open(staticLayoutBlobId)).WillOnce(Return(true));
     EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
@@ -104,7 +104,7 @@
 
 TEST_F(FirmwareHandlerNotYetStartedTest, OpenHashFileVerifyStateChange)
 {
-    EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true));
+    EXPECT_CALL(*hashImageMock, open(hashBlobId)).WillOnce(Return(true));
     /* Opening the hash blob id doesn't trigger a preparation, only a firmware
      * blob.
      */
diff --git a/bmc/test/firmware_state_uploadinprogress_unittest.cpp b/bmc/test/firmware_state_uploadinprogress_unittest.cpp
index 63169db..72da8bb 100644
--- a/bmc/test/firmware_state_uploadinprogress_unittest.cpp
+++ b/bmc/test/firmware_state_uploadinprogress_unittest.cpp
@@ -121,7 +121,7 @@
      */
     openToInProgress(staticLayoutBlobId);
 
-    EXPECT_CALL(imageMock, getSize()).WillOnce(Return(32));
+    EXPECT_CALL(*imageMock2, getSize()).WillOnce(Return(32));
 
     blobs::BlobMeta meta, expectedMeta = {};
     expectedMeta.size = 32;
@@ -224,7 +224,8 @@
 {
     openToInProgress(staticLayoutBlobId);
     std::vector<std::uint8_t> bytes = {0x01, 0x02};
-    EXPECT_CALL(imageMock, write(0, ContainerEq(bytes))).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock2, write(0, ContainerEq(bytes)))
+        .WillOnce(Return(true));
     EXPECT_TRUE(handler->write(session, 0, bytes));
 }
 
@@ -232,7 +233,8 @@
 {
     openToInProgress(hashBlobId);
     std::vector<std::uint8_t> bytes = {0x01, 0x02};
-    EXPECT_CALL(imageMock, write(0, ContainerEq(bytes))).WillOnce(Return(true));
+    EXPECT_CALL(*hashImageMock, write(0, ContainerEq(bytes)))
+        .WillOnce(Return(true));
     EXPECT_TRUE(handler->write(session, 0, bytes));
 }
 
diff --git a/bmc/test/firmware_state_verificationpending_unittest.cpp b/bmc/test/firmware_state_verificationpending_unittest.cpp
index 64d82ca..92d5c6b 100644
--- a/bmc/test/firmware_state_verificationpending_unittest.cpp
+++ b/bmc/test/firmware_state_verificationpending_unittest.cpp
@@ -227,7 +227,7 @@
     /* Verifies it isn't triggered again. */
     EXPECT_CALL(*prepareMockPtr, trigger()).Times(0);
 
-    EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock2, open(staticLayoutBlobId)).WillOnce(Return(true));
     EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
     expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
diff --git a/bmc/test/firmware_unittest.hpp b/bmc/test/firmware_unittest.hpp
index 129b940..6bf515d 100644
--- a/bmc/test/firmware_unittest.hpp
+++ b/bmc/test/firmware_unittest.hpp
@@ -26,10 +26,18 @@
   protected:
     void SetUp() override
     {
-        blobs = {
-            {hashBlobId, &imageMock},
-            {staticLayoutBlobId, &imageMock},
-        };
+        /* Unfortunately, since the FirmwareHandler object ends up owning the
+         * handlers, we can't just share handlers.
+         */
+        std::unique_ptr<ImageHandlerInterface> image =
+            std::make_unique<ImageHandlerMock>();
+        hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(
+            std::move(HandlerPack(staticLayoutBlobId, std::move(image))));
 
         std::unique_ptr<TriggerableActionInterface> prepareMock =
             std::make_unique<TriggerMock>();
@@ -52,7 +60,7 @@
         packs[staticLayoutBlobId] = std::move(actionPack);
 
         handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-            blobs, data, std::move(packs));
+            std::move(blobs), data, std::move(packs));
     }
 
     void expectedState(FirmwareBlobHandler::UpdateState state)
@@ -63,7 +71,15 @@
 
     void openToInProgress(const std::string& blobId)
     {
-        EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
+        if (blobId == hashBlobId)
+        {
+            EXPECT_CALL(*hashImageMock, open(blobId)).WillOnce(Return(true));
+        }
+        else
+        {
+            EXPECT_CALL(*imageMock2, open(blobId)).WillOnce(Return(true));
+        }
+
         if (blobId != hashBlobId)
         {
             EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
@@ -76,7 +92,14 @@
     {
         openToInProgress(blobId);
 
-        EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
+        if (blobId == hashBlobId)
+        {
+            EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
+        }
+        else
+        {
+            EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return());
+        }
         handler->close(session);
         expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
     }
@@ -98,7 +121,7 @@
         getToVerificationPending(staticLayoutBlobId);
 
         openToInProgress(hashBlobId);
-        EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
+        EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
         handler->close(session);
         expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
@@ -148,11 +171,14 @@
         expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
     }
 
-    ImageHandlerMock imageMock;
+    ImageHandlerMock *hashImageMock, *imageMock2;
+
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
+
     std::unique_ptr<blobs::GenericBlobInterface> handler;
+
     TriggerMock* prepareMockPtr;
     TriggerMock* verifyMockPtr;
     TriggerMock* updateMockPtr;
@@ -167,7 +193,7 @@
 class IpmiOnlyFirmwareTest : public ::testing::Test
 {
   protected:
-    ImageHandlerMock imageMock;
+    ImageHandlerMock *hashImageMock, *imageMock;
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data = {
         {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
@@ -175,12 +201,17 @@
 
     void SetUp() override
     {
-        blobs = {
-            {hashBlobId, &imageMock},
-            {"asdf", &imageMock},
-        };
+        std::unique_ptr<ImageHandlerInterface> image =
+            std::make_unique<ImageHandlerMock>();
+        hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack("asdf", std::move(image))));
+
         handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-            blobs, data, std::move(CreateActionMap("asdf")));
+            std::move(blobs), data, std::move(CreateActionMap("asdf")));
     }
 };
 
@@ -188,23 +219,28 @@
 {
   protected:
     DataHandlerMock dataMock;
-    ImageHandlerMock imageMock;
+    ImageHandlerMock *hashImageMock, *imageMock;
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data;
     std::unique_ptr<blobs::GenericBlobInterface> handler;
 
     void SetUp() override
     {
-        blobs = {
-            {hashBlobId, &imageMock},
-            {"asdf", &imageMock},
-        };
+        std::unique_ptr<ImageHandlerInterface> image =
+            std::make_unique<ImageHandlerMock>();
+        hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
+
+        image = std::make_unique<ImageHandlerMock>();
+        imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
+        blobs.push_back(std::move(HandlerPack("asdf", std::move(image))));
+
         data = {
             {FirmwareFlags::UpdateFlags::ipmi, nullptr},
             {FirmwareFlags::UpdateFlags::lpc, &dataMock},
         };
         handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-            blobs, data, std::move(CreateActionMap("asdf")));
+            std::move(blobs), data, std::move(CreateActionMap("asdf")));
     }
 };
 
diff --git a/bmc/test/firmware_write_unittest.cpp b/bmc/test/firmware_write_unittest.cpp
index b3353c9..481f3ec 100644
--- a/bmc/test/firmware_write_unittest.cpp
+++ b/bmc/test/firmware_write_unittest.cpp
@@ -14,6 +14,9 @@
 
 namespace ipmi_flash
 {
+namespace
+{
+
 using ::testing::Eq;
 using ::testing::Return;
 
@@ -28,14 +31,14 @@
 TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
 {
     /* Verify if data type ipmi, it calls write with the bytes. */
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
 
     std::vector<std::uint8_t> bytes = {0xaa, 0x55};
 
-    EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
     EXPECT_TRUE(handler->write(0, 0, bytes));
 }
 
@@ -43,7 +46,7 @@
 {
     /* Verify if data type non-ipmi, it calls write with the length. */
     EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
@@ -57,7 +60,7 @@
     std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
 
     EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
-    EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
     EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
 }
 
@@ -66,7 +69,7 @@
     /* Verify the data type non-ipmi, if the request's structure doesn't match,
      * return failure. */
     EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
@@ -83,4 +86,5 @@
     EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
 }
 
+} // namespace
 } // namespace ipmi_flash
diff --git a/bmc/test/firmware_writemeta_unittest.cpp b/bmc/test/firmware_writemeta_unittest.cpp
index cb1c965..ef292b0 100644
--- a/bmc/test/firmware_writemeta_unittest.cpp
+++ b/bmc/test/firmware_writemeta_unittest.cpp
@@ -12,6 +12,9 @@
 
 namespace ipmi_flash
 {
+namespace
+{
+
 using ::testing::Eq;
 using ::testing::Return;
 
@@ -21,7 +24,7 @@
 
 TEST_F(FirmwareHandlerWriteMetaTest, WriteConfigParametersFailIfOverIPMI)
 {
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
@@ -34,7 +37,7 @@
 TEST_F(FirmwareHandlerWriteMetaTest, WriteConfigParametersPassedThrough)
 {
     EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
-    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
@@ -45,4 +48,5 @@
     EXPECT_TRUE(handler->writeMeta(0, 0, bytes));
 }
 
+} // namespace
 } // namespace ipmi_flash