version-handler: Add session stat

We need to be able to inform the blob user of the readiness of blob data
and how large the contents are.

Change-Id: Ic90f6e344160b48e7e58cb009558586c7c2fab9b
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/bmc/version-handler/test/Makefile.am b/bmc/version-handler/test/Makefile.am
index 93daa37..b3b6290 100644
--- a/bmc/version-handler/test/Makefile.am
+++ b/bmc/version-handler/test/Makefile.am
@@ -24,7 +24,8 @@
 	version_createhandler_unittest \
 	version_open_unittest \
 	version_close_unittest \
-	version_read_unittest
+	version_read_unittest \
+	version_stat_unittest
 
 TESTS = $(check_PROGRAMS)
 
@@ -45,3 +46,6 @@
 
 version_read_unittest_SOURCES = version_read_unittest.cpp
 version_read_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
+
+version_stat_unittest_SOURCES = version_stat_unittest.cpp
+version_stat_unittest_LDADD = $(top_builddir)/bmc/version-handler/libversionblob_common.la
diff --git a/bmc/version-handler/test/version_stat_unittest.cpp b/bmc/version-handler/test/version_stat_unittest.cpp
new file mode 100644
index 0000000..b51ee68
--- /dev/null
+++ b/bmc/version-handler/test/version_stat_unittest.cpp
@@ -0,0 +1,81 @@
+#include "version_handler.hpp"
+#include "version_mock.hpp"
+
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+using ::testing::_;
+using ::testing::Return;
+
+namespace ipmi_flash
+{
+
+class VersionStatBlobTest : public ::testing::Test
+{
+  protected:
+    void SetUp() override
+    {
+        h = std::make_unique<VersionBlobHandler>(
+            createMockVersionConfigs(blobNames, &im, &tm));
+
+        EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
+        EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
+
+        blobs::BlobMeta meta;
+        EXPECT_TRUE(h->stat(0, &meta));
+        EXPECT_EQ(blobs::StateFlags::committing, meta.blobState);
+    }
+
+    std::unique_ptr<blobs::GenericBlobInterface> h;
+    std::vector<std::string> blobNames{"blob0"};
+    std::unordered_map<std::string, TriggerMock*> tm;
+    std::unordered_map<std::string, ImageHandlerMock*> im;
+};
+
+TEST_F(VersionStatBlobTest, CreateError)
+{
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .WillOnce(Return(ActionStatus::failed));
+    tm.at("blob0")->cb(*tm.at("blob0"));
+
+    blobs::BlobMeta meta;
+    EXPECT_TRUE(h->stat(0, &meta));
+    EXPECT_EQ(blobs::StateFlags::commit_error, meta.blobState);
+}
+
+class VersionStatSizeBlobTest :
+    public VersionStatBlobTest,
+    public ::testing::WithParamInterface<std::vector<uint8_t>>
+{};
+
+TEST_P(VersionStatSizeBlobTest, StatWithSize)
+{
+    const std::vector<uint8_t> data = GetParam();
+    EXPECT_CALL(*tm.at("blob0"), status())
+        .WillOnce(Return(ActionStatus::success));
+    EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
+    EXPECT_CALL(*im.at("blob0"), read(0, ::testing::Ge(data.size())))
+        .WillOnce(Return(data));
+    EXPECT_CALL(*im.at("blob0"), close()).Times(1);
+    tm.at("blob0")->cb(*tm.at("blob0"));
+
+    blobs::BlobMeta meta;
+    EXPECT_TRUE(h->stat(0, &meta));
+    EXPECT_EQ(blobs::StateFlags::committed | blobs::StateFlags::open_read,
+              meta.blobState);
+    EXPECT_EQ(data.size(), meta.size);
+}
+
+const std::vector<std::vector<uint8_t>> datas = {
+    {},
+    {0, 1, 2, 3, 4, 5, 6},
+};
+
+INSTANTIATE_TEST_SUITE_P(DifferentData, VersionStatSizeBlobTest,
+                         testing::ValuesIn(datas));
+
+} // namespace ipmi_flash
diff --git a/bmc/version-handler/version_handler.cpp b/bmc/version-handler/version_handler.cpp
index 45d9b02..0b77f80 100644
--- a/bmc/version-handler/version_handler.cpp
+++ b/bmc/version-handler/version_handler.cpp
@@ -83,11 +83,7 @@
 
 bool VersionBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
 {
-    // TODO: stat should return the blob state and in the meta data information
-    // on whether a read is successful should be contained
-    // do things like determine if systemd target is triggered
-    // then check if file can be opened for read
-    return false; /* not yet implemented */
+    return false;
 }
 
 bool VersionBlobHandler::open(uint16_t session, uint16_t flags,
@@ -151,7 +147,24 @@
 
 bool VersionBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
 {
-    return false;
+    const auto& data = sessionInfoMap.at(session)->data;
+    if (data == nullptr)
+    {
+        meta->blobState = blobs::StateFlags::committing;
+        meta->size = 0;
+    }
+    else if (!*data)
+    {
+        meta->blobState = blobs::StateFlags::commit_error;
+        meta->size = 0;
+    }
+    else
+    {
+        meta->blobState =
+            blobs::StateFlags::committed | blobs::StateFlags::open_read;
+        meta->size = (*data)->size();
+    }
+    return true;
 }
 
 bool VersionBlobHandler::expire(uint16_t session)