version-handler: add version-handler, blob handler

Implement version-handler: a blob handler for retrieving version
information about a blob.

Problem: ipmi-flash(firmware-handler) provides a mechanism to transfer
firmware updates, verify them and perform updates but there is no
mechanism to interrogate the firmware for its version.

Solution: version-handler provides handlers for retrieving information
about firmware blobs. Adding "version" syntax to "/flash/blob" entries
in the json configuration file enables this feature.
The mechanism to retrieve version is identical to the mechanism used by
firmware-handler to perform preparation, verification and updates (kick
off systemd targets).

Signed-off-by: Jason Ling <jasonling@google.com>
Change-Id: I28868ca8dd76d63af668d2e46b9359401d45f0bc
diff --git a/bmc/version-handler/test/Makefile.am b/bmc/version-handler/test/Makefile.am
index 94c398d..0c02ff0 100644
--- a/bmc/version-handler/test/Makefile.am
+++ b/bmc/version-handler/test/Makefile.am
@@ -3,6 +3,7 @@
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/ \
 	-I$(top_srcdir)/bmc/ \
+	-I$(top_srcdir)/bmc/test \
 	-I$(top_srcdir)/bmc/version-handler \
 	$(GTEST_CFLAGS) \
 	$(GMOCK_CFLAGS) \
@@ -22,9 +23,29 @@
 
 # Run all 'check' test programs
 check_PROGRAMS = \
-	version_json_unittest
+	version_json_unittest \
+	version_canhandle_enumerate_unittest \
+	version_createhandler_unittest \
+	version_open_unittest \
+	version_close_unittest \
+	version_read_unittest
 
 TESTS = $(check_PROGRAMS)
 
 version_json_unittest_SOURCES = version_json_unittest.cpp
 version_json_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
+
+version_canhandle_enumerate_unittest_SOURCES = version_canhandle_enumerate_unittest.cpp
+version_canhandle_enumerate_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
+
+version_createhandler_unittest_SOURCES = version_createhandler_unittest.cpp
+version_createhandler_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
+
+version_open_unittest_SOURCES = version_open_unittest.cpp
+version_open_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
+
+version_close_unittest_SOURCES = version_close_unittest.cpp
+version_close_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
+
+version_read_unittest_SOURCES = version_read_unittest.cpp
+version_read_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
diff --git a/bmc/version-handler/test/version_canhandle_enumerate_unittest.cpp b/bmc/version-handler/test/version_canhandle_enumerate_unittest.cpp
new file mode 100644
index 0000000..3637c32
--- /dev/null
+++ b/bmc/version-handler/test/version_canhandle_enumerate_unittest.cpp
@@ -0,0 +1,52 @@
+#include "flags.hpp"
+#include "image_mock.hpp"
+#include "triggerable_mock.hpp"
+#include "util.hpp"
+#include "version_handler.hpp"
+
+#include <array>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+namespace ipmi_flash
+{
+TEST(VersionHandlerCanHandleTest, VerifyGoodInfoMap)
+{
+    VersionInfoMap test;
+    std::array blobNames{"blob0", "blob1", "blob2", "blob3"};
+    for (const auto& blobName : blobNames)
+    {
+        test.try_emplace(blobName,
+                         VersionInfoPack(blobName,
+                                         std::make_unique<VersionActionPack>(
+                                             CreateTriggerMock()),
+                                         CreateImageMock()));
+    }
+    auto handler = VersionBlobHandler::create(std::move(test));
+    ASSERT_NE(handler, nullptr);
+    for (const auto& blobName : blobNames)
+    {
+        EXPECT_TRUE(handler->canHandleBlob(blobName));
+    }
+}
+
+TEST(VersionHandlerEnumerateTest, VerifyGoodInfoMap)
+{
+    VersionInfoMap test;
+    std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
+    for (const auto& blobName : blobNames)
+    {
+        test.try_emplace(blobName,
+                         VersionInfoPack(blobName,
+                                         std::make_unique<VersionActionPack>(
+                                             CreateTriggerMock()),
+                                         CreateImageMock()));
+    }
+    auto handler = VersionBlobHandler::create(std::move(test));
+    ASSERT_NE(handler, nullptr);
+    EXPECT_THAT(handler->getBlobIds(),
+                ::testing::UnorderedElementsAreArray(blobNames));
+}
+} // namespace ipmi_flash
diff --git a/bmc/version-handler/test/version_close_unittest.cpp b/bmc/version-handler/test/version_close_unittest.cpp
new file mode 100644
index 0000000..68e6add
--- /dev/null
+++ b/bmc/version-handler/test/version_close_unittest.cpp
@@ -0,0 +1,97 @@
+#include "flags.hpp"
+#include "image_mock.hpp"
+#include "triggerable_mock.hpp"
+#include "util.hpp"
+#include "version_handler.hpp"
+
+#include <array>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+using ::testing::_;
+using ::testing::Return;
+namespace ipmi_flash
+{
+
+class VersionCloseExpireBlobTest : public ::testing::Test
+{
+  protected:
+    void SetUp() override
+    {
+        VersionInfoMap vim;
+        for (const auto& blobName : blobNames)
+        {
+            auto t = CreateTriggerMock();
+            auto i = CreateImageMock();
+            tm[blobName] = reinterpret_cast<TriggerMock*>(t.get());
+            im[blobName] = reinterpret_cast<ImageHandlerMock*>(i.get());
+            vim.try_emplace(
+                blobName,
+                VersionInfoPack(
+                    blobName, std::make_unique<VersionActionPack>(std::move(t)),
+                    std::move(i)));
+        }
+        h = VersionBlobHandler::create(std::move(vim));
+        ASSERT_NE(h, nullptr);
+        for (const auto& [key, val] : tm)
+        {
+            ON_CALL(*val, trigger()).WillByDefault(Return(true));
+        }
+    }
+    std::unique_ptr<blobs::GenericBlobInterface> h;
+    std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
+    std::unordered_map<std::string, TriggerMock*> tm;
+    std::unordered_map<std::string, ImageHandlerMock*> im;
+};
+
+TEST_F(VersionCloseExpireBlobTest, VerifyOpenThenClose)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::success));
+    EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
+    EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
+    EXPECT_TRUE(h->close(0));
+}
+
+TEST_F(VersionCloseExpireBlobTest, VerifyUnopenedBlobCloseFails)
+{
+    EXPECT_CALL(*tm.at("blob0"), status()).Times(0);
+    EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
+    EXPECT_FALSE(h->close(0));
+}
+
+TEST_F(VersionCloseExpireBlobTest, VerifyDoubleCloseFails)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::success));
+    EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
+    EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
+    EXPECT_TRUE(h->close(0));
+    EXPECT_FALSE(h->close(0));
+}
+
+TEST_F(VersionCloseExpireBlobTest, VerifyBadSessionNumberCloseFails)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::success));
+    EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
+    EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
+    EXPECT_FALSE(h->close(1));
+    EXPECT_TRUE(h->close(0));
+}
+
+TEST_F(VersionCloseExpireBlobTest, VerifyRunningActionIsAborted)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::running));
+    EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
+    EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
+    EXPECT_TRUE(h->close(0));
+}
+
+} // namespace ipmi_flash
diff --git a/bmc/version-handler/test/version_createhandler_unittest.cpp b/bmc/version-handler/test/version_createhandler_unittest.cpp
new file mode 100644
index 0000000..8454ed0
--- /dev/null
+++ b/bmc/version-handler/test/version_createhandler_unittest.cpp
@@ -0,0 +1,70 @@
+#include "flags.hpp"
+#include "image_mock.hpp"
+#include "triggerable_mock.hpp"
+#include "util.hpp"
+#include "version_handler.hpp"
+
+#include <array>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+namespace ipmi_flash
+{
+
+TEST(VersionHandlerCanHandleTest, VerifyGoodInfoMapPasses)
+{
+    VersionInfoMap test;
+    std::array blobNames{"blob0", "blob1", "blob2", "blob3"};
+    for (const auto& blobName : blobNames)
+    {
+        test.try_emplace(blobName,
+                         VersionInfoPack(blobName,
+                                         std::make_unique<VersionActionPack>(
+                                             CreateTriggerMock()),
+                                         CreateImageMock()));
+    }
+    auto handler = VersionBlobHandler::create(std::move(test));
+    EXPECT_NE(handler, nullptr);
+}
+
+TEST(VersionCreateHandlerTest, VerifyEmptyInfoMapFails)
+{
+    VersionInfoMap test;
+    auto handler = VersionBlobHandler::create(std::move(test));
+    EXPECT_EQ(handler, nullptr);
+}
+
+TEST(VersionHandlerCanHandleTest, VerifyInfoMapWithNullActionPackFails)
+{
+    VersionInfoMap test;
+    test.try_emplace("blob0",
+                     VersionInfoPack("blob0", nullptr, CreateImageMock()));
+    auto handler = VersionBlobHandler::create(std::move(test));
+    EXPECT_EQ(handler, nullptr);
+}
+
+TEST(VersionHandlerCanHandleTest, VerifyInfoMapWithNullTriggerableActionFails)
+{
+    VersionInfoMap test;
+    test.try_emplace(
+        "blob0",
+        VersionInfoPack("blob0", std::make_unique<VersionActionPack>(nullptr),
+                        CreateImageMock()));
+    auto handler = VersionBlobHandler::create(std::move(test));
+    EXPECT_EQ(handler, nullptr);
+}
+
+TEST(VersionHandlerCanHandleTest, VerifyInfoMapWithNullImageHandlerFails)
+{
+    VersionInfoMap test;
+    test.try_emplace(
+        "blob0",
+        VersionInfoPack(
+            "blob0", std::make_unique<VersionActionPack>(CreateTriggerMock()),
+            nullptr));
+    auto handler = VersionBlobHandler::create(std::move(test));
+    EXPECT_EQ(handler, nullptr);
+}
+} // namespace ipmi_flash
diff --git a/bmc/version-handler/test/version_handler_unittest.cpp b/bmc/version-handler/test/version_handler_unittest.cpp
new file mode 100644
index 0000000..f27f033
--- /dev/null
+++ b/bmc/version-handler/test/version_handler_unittest.cpp
@@ -0,0 +1,267 @@
+#include "version_handler.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace ipmi_flash
+{
+namespace
+{
+using ::testing::IsEmpty;
+
+using json = nlohmann::json;
+
+TEST(VersionHandlerCreateTest, VerifyAllBlobHandlersPresent)
+{
+
+    // Need a triggerable mock
+    VersionInfoMap test;
+    test.try_emplace("blob0", "blob0",
+                     std::make_unique<VersionActionPackMock>(),
+                     std::make_unique<ImageHandlerMock>());
+    ASSERT_THAT(h, ::testing::SizeIs(1));
+    EXPECT_THAT(h[0].blobId, "/version/sink_seq");
+    EXPECT_FALSE(h[0].actions == nullptr);
+    EXPECT_FALSE(h[0].handler == nullptr);
+}
+
+TEST(VersionJsonTest, ValidConfigurationVersionBlobName)
+{
+    auto h = VersionHandlersBuilder().buildHandlerFromJson(j2);
+    ASSERT_THAT(h, ::testing::SizeIs(1));
+    EXPECT_THAT(h[0].blobId, "/version/sink_seq");
+    EXPECT_FALSE(h[0].actions == nullptr);
+    EXPECT_FALSE(h[0].handler == nullptr);
+}
+
+TEST(VersionJsonTest, MissingHandlerType)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/image",
+            "version":{
+                "handler": {
+                   "path" : "/tmp/version_info"
+                 },
+                "actions": {
+                  "open" : {
+                  "type" : "systemd",
+                  "unit" : "absolute"}
+                 }
+            }
+         }]
+    )"_json;
+    EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty());
+}
+
+TEST(VersionJsonTest, BadBlobName)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/bad/image",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions": {
+                  "open" : {
+                  "type" : "systemd",
+                  "unit" : "absolute"}
+                 }
+            }
+         }]
+    )"_json;
+    EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty());
+}
+
+TEST(VersionJsonTest, MissingActions)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/image",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 }
+            }
+         }]
+    )"_json;
+    EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty());
+}
+
+TEST(VersionJsonTest, MissingOpenAction)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/image",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions": {}
+            }
+         }]
+    )"_json;
+    EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty());
+}
+
+TEST(VersionJsonTest, OneInvalidTwoValidSucceeds)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/sink_seq0",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "systemd",
+                    "unit" : "absolute"
+                    }
+                 }
+            }
+         },
+         {
+            "blob" : "/version/sink_seq1",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "systemd",
+                    "unit" : "absolute"
+                    }
+                 }
+            }
+         },
+         {
+            "blob" : "/bad/sink_seq",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "systemd",
+                    "unit" : "absolute"
+                    }
+                 }
+            }
+         }
+         ]
+    )"_json;
+    auto h = VersionHandlersBuilder().buildHandlerFromJson(j2);
+    ASSERT_THAT(h, ::testing::SizeIs(2));
+    EXPECT_THAT(h[0].blobId, "/version/sink_seq0");
+    EXPECT_THAT(h[1].blobId, "/version/sink_seq1");
+}
+
+TEST(VersionJsonTest, BlobNameIsTooShort)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "systemd",
+                    "unit" : "absolute"
+                    }
+                 }
+            }
+         }]
+    )"_json;
+    EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty());
+}
+
+TEST(VersionJsonTest, OpenSkipAction)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/sink_seqs",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "skip"
+                    }
+                 }
+            }
+         }]
+    )"_json;
+    auto h = VersionHandlersBuilder().buildHandlerFromJson(j2);
+    EXPECT_THAT(h, ::testing::SizeIs(1));
+    EXPECT_TRUE(h[0].blobId == "/version/sink_seqs");
+    ASSERT_FALSE(h[0].actions == nullptr);
+    EXPECT_FALSE(h[0].actions->onOpen == nullptr);
+}
+
+TEST(VersionJsonTest, OpenActionsWithDifferentModes)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/blob1",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "systemd",
+                    "unit" : "absolute",
+                    "mode" : "replace-nope"
+                    }
+                 }
+            }
+         },
+         {
+            "blob" : "/flash/blob2",
+            "version":{
+                "handler": {
+                   "type" : "file",
+                   "path" : "/tmp/version_info"
+                 },
+                "actions":{
+                    "open" :{
+                    "type" : "systemd",
+                    "unit" : "absolute",
+                    "mode" : "replace-fake"
+                    }
+                 }
+            }
+         }
+         ]
+    )"_json;
+    auto h = VersionHandlersBuilder().buildHandlerFromJson(j2);
+    ASSERT_THAT(h, ::testing::SizeIs(2));
+
+    EXPECT_FALSE(h[0].handler == nullptr);
+    EXPECT_FALSE(h[0].actions == nullptr);
+    EXPECT_THAT(h[0].blobId, "/version/blob1");
+    auto onOpen0 = reinterpret_cast<SystemdNoFile*>(h[0].actions->onOpen.get());
+    EXPECT_THAT(onOpen0->getMode(), "replace-nope");
+
+    EXPECT_FALSE(h[1].handler == nullptr);
+    EXPECT_FALSE(h[1].actions == nullptr);
+    EXPECT_THAT(h[1].blobId, "/version/blob2");
+    auto onOpen1 = reinterpret_cast<SystemdNoFile*>(h[1].actions->onOpen.get());
+    EXPECT_THAT(onOpen1->getMode(), "replace-fake");
+}
+} // namespace
+} // namespace ipmi_flash
diff --git a/bmc/version-handler/test/version_open_unittest.cpp b/bmc/version-handler/test/version_open_unittest.cpp
new file mode 100644
index 0000000..7e287e5
--- /dev/null
+++ b/bmc/version-handler/test/version_open_unittest.cpp
@@ -0,0 +1,124 @@
+#include "flags.hpp"
+#include "image_mock.hpp"
+#include "triggerable_mock.hpp"
+#include "util.hpp"
+#include "version_handler.hpp"
+
+#include <array>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+using ::testing::_;
+using ::testing::Return;
+namespace ipmi_flash
+{
+
+class VersionOpenBlobTest : public ::testing::Test
+{
+  protected:
+    void SetUp() override
+    {
+        VersionInfoMap vim;
+        for (const auto& blobName : blobNames)
+        {
+            auto t = CreateTriggerMock();
+            auto i = CreateImageMock();
+            tm[blobName] = reinterpret_cast<TriggerMock*>(t.get());
+            im[blobName] = reinterpret_cast<ImageHandlerMock*>(i.get());
+            vim.try_emplace(
+                blobName,
+                VersionInfoPack(
+                    blobName, std::make_unique<VersionActionPack>(std::move(t)),
+                    std::move(i)));
+        }
+        h = VersionBlobHandler::create(std::move(vim));
+        ASSERT_NE(h, nullptr);
+        for (const auto& [key, val] : tm)
+        {
+            /* by default no action triggers expected to be called */
+            EXPECT_CALL(*val, trigger()).Times(0);
+        }
+        for (const auto& [key, val] : im)
+        {
+            /* by default no image handler open is expected to be called */
+            EXPECT_CALL(*val, open(_, _)).Times(0);
+        }
+    }
+    std::unique_ptr<blobs::GenericBlobInterface> h;
+    std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
+    std::unordered_map<std::string, TriggerMock*> tm;
+    std::unordered_map<std::string, ImageHandlerMock*> im;
+    const std::uint16_t defaultSessionNumber{0};
+};
+
+TEST_F(VersionOpenBlobTest, VerifySingleBlobOpen)
+{
+    EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+}
+
+TEST_F(VersionOpenBlobTest, VerifyMultipleBlobOpens)
+{
+    for (const auto& [key, val] : tm)
+    {
+        /* set the expectation that every onOpen will be triggered */
+        EXPECT_CALL(*val, trigger()).Times(1).WillOnce(Return(true));
+    }
+    int i{defaultSessionNumber};
+    for (const auto& blob : blobNames)
+    {
+        EXPECT_TRUE(h->open(i++, blobs::read, blob));
+    }
+}
+
+TEST_F(VersionOpenBlobTest, VerifyOpenAfterClose)
+{
+    EXPECT_CALL(*tm.at("blob0"), trigger())
+        .Times(2)
+        .WillRepeatedly(Return(true));
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+    EXPECT_TRUE(h->close(defaultSessionNumber));
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+}
+
+TEST_F(VersionOpenBlobTest, VerifyDuplicateSessionNumberFails)
+{
+    EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
+    EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob1"));
+    /* the duplicate session number of 0
+     *  should cause a failure for the open of a different blob
+     */
+    EXPECT_FALSE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+    /* open after fail due to seq number works */
+    EXPECT_TRUE(h->open(defaultSessionNumber + 1, blobs::read, "blob0"));
+}
+
+TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails)
+{
+    EXPECT_CALL(*tm.at("blob1"), trigger())
+        .Times(1)
+        .WillRepeatedly(Return(true));
+    EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
+    EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
+}
+
+TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
+{
+    EXPECT_CALL(*tm.at("blob1"), trigger())
+        .Times(2)
+        .WillOnce(Return(false))
+        .WillOnce(Return(true));
+    EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
+    EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
+}
+
+TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
+{
+    EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
+    EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
+    EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
+}
+
+} // namespace ipmi_flash
diff --git a/bmc/version-handler/test/version_read_unittest.cpp b/bmc/version-handler/test/version_read_unittest.cpp
new file mode 100644
index 0000000..2f15e03
--- /dev/null
+++ b/bmc/version-handler/test/version_read_unittest.cpp
@@ -0,0 +1,128 @@
+#include "flags.hpp"
+#include "image_mock.hpp"
+#include "triggerable_mock.hpp"
+#include "util.hpp"
+#include "version_handler.hpp"
+
+#include <array>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+using ::testing::_;
+using ::testing::IsEmpty;
+using ::testing::Return;
+namespace ipmi_flash
+{
+
+class VersionReadBlobTest : public ::testing::Test
+{
+  protected:
+    void SetUp() override
+    {
+        VersionInfoMap vim;
+        for (const auto& blobName : blobNames)
+        {
+            auto t = CreateTriggerMock();
+            auto i = CreateImageMock();
+            tm[blobName] = reinterpret_cast<TriggerMock*>(t.get());
+            im[blobName] = reinterpret_cast<ImageHandlerMock*>(i.get());
+            vim.try_emplace(
+                blobName,
+                VersionInfoPack(
+                    blobName, std::make_unique<VersionActionPack>(std::move(t)),
+                    std::move(i)));
+        }
+        h = VersionBlobHandler::create(std::move(vim));
+        ASSERT_NE(h, nullptr);
+        for (const auto& [key, val] : tm)
+        {
+            ON_CALL(*val, trigger()).WillByDefault(Return(true));
+        }
+    }
+    std::unique_ptr<blobs::GenericBlobInterface> h;
+    std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
+    std::unordered_map<std::string, TriggerMock*> tm;
+    std::unordered_map<std::string, ImageHandlerMock*> im;
+    const std::uint16_t defaultSessionNumber{200};
+    std::vector<uint8_t> vector1{0xDE, 0xAD, 0xBE, 0xEF,
+                                 0xBA, 0xDF, 0xEE, 0x0D};
+};
+
+TEST_F(VersionReadBlobTest, VerifyValidRead)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(2)
+        .WillRepeatedly(Return(ActionStatus::success));
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+    /* file path gets bound to file_handler on creation so path parameter
+     * doesn't actually matter
+     */
+    EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in))
+        .Times(2)
+        .WillRepeatedly(Return(true));
+    EXPECT_CALL(*im.at("blob0"), read(0, 10)).WillOnce(Return(vector1));
+    EXPECT_CALL(*im.at("blob0"), read(2, 10)).WillOnce(Return(vector1));
+    EXPECT_CALL(*im.at("blob0"), close()).Times(2);
+
+    EXPECT_EQ(h->read(defaultSessionNumber, 0, 10), vector1);
+    EXPECT_EQ(h->read(defaultSessionNumber, 2, 10), vector1);
+}
+
+TEST_F(VersionReadBlobTest, VerifyUnopenedReadFails)
+{
+    EXPECT_CALL(*tm.at("blob0"), status()).Times(0);
+    EXPECT_CALL(*im.at("blob0"), open(_, _)).Times(0);
+    EXPECT_CALL(*im.at("blob0"), read(_, _)).Times(0);
+
+    EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
+}
+
+TEST_F(VersionReadBlobTest, VerifyTriggerFailureReadFails)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::failed));
+    EXPECT_CALL(*im.at("blob0"), open(_, _)).Times(0);
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+    EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
+}
+
+TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::success));
+    /* file path gets bound to file_handler on creation so path parameter
+     * doesn't actually matter
+     */
+    EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in))
+        .Times(1)
+        .WillOnce(Return(true));
+    EXPECT_CALL(*im.at("blob0"), read(_, _))
+        .Times(1)
+        .WillOnce(Return(std::nullopt));
+    EXPECT_CALL(*im.at("blob0"), close()).Times(1);
+
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+    EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
+}
+
+TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure)
+{
+    /* first call to trigger status fails, second succeeds */
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .Times(1)
+        .WillOnce(Return(ActionStatus::success));
+    /* file path gets bound to file_handler on creation so path parameter
+     * doesn't actually matter
+     */
+    EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in))
+        .Times(1)
+        .WillOnce(Return(false));
+
+    EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
+    EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
+}
+
+} // namespace ipmi_flash