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/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