firmware: start implementing close method

The close method is used to indicate something is done, for instance,
uploading a firmware image or tarball.

Change-Id: I9e972ea12282e700863d51db497c6294040cd2ca
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/firmware_handler.cpp b/firmware_handler.cpp
index 09f8b10..3ce65aa 100644
--- a/firmware_handler.cpp
+++ b/firmware_handler.cpp
@@ -391,11 +391,30 @@
  */
 bool FirmwareBlobHandler::close(uint16_t session)
 {
+    auto item = lookup.find(session);
+    if (item == lookup.end())
+    {
+        return false;
+    }
+
+    if (item->second->dataHandler)
+    {
+        item->second->dataHandler->close();
+    }
+    item->second->imageHandler->close();
+    item->second->state = Session::State::closed;
+    /* Do not delete the active blob_id from the list of blob_ids, because that
+     * blob_id indicates there is data stored.  Delete will destroy it.
+     */
+
+    lookup.erase(item);
+
     fileOpen = false;
 
-    /* TODO: implement other aspects of closing out a session. */
-
-    return false;
+    /* TODO: implement other aspects of closing out a session, such as changing
+     * global firmware state.
+     */
+    return true;
 }
 
 bool FirmwareBlobHandler::expire(uint16_t session)
diff --git a/hash_handler.cpp b/hash_handler.cpp
index ce9208b..820ffbf 100644
--- a/hash_handler.cpp
+++ b/hash_handler.cpp
@@ -14,6 +14,11 @@
     return false;
 }
 
+void HashFileHandler::close()
+{
+    return;
+}
+
 bool HashFileHandler::write(std::uint32_t offset,
                             const std::vector<std::uint8_t>& data)
 {
diff --git a/hash_handler.hpp b/hash_handler.hpp
index c006194..66f68a4 100644
--- a/hash_handler.hpp
+++ b/hash_handler.hpp
@@ -19,7 +19,7 @@
     HashFileHandler() = default;
 
     bool open(const std::string& path) override;
-
+    void close() override;
     bool write(std::uint32_t offset,
                const std::vector<std::uint8_t>& data) override;
 
diff --git a/image_handler.hpp b/image_handler.hpp
index 28675ce..ed3455b 100644
--- a/image_handler.hpp
+++ b/image_handler.hpp
@@ -26,6 +26,11 @@
     virtual bool open(const std::string& path) = 0;
 
     /**
+     * close the image.
+     */
+    virtual void close() = 0;
+
+    /**
      * write data to the staged file.
      *
      * @param[in] offset - 0-based offset into the file.
diff --git a/static_handler.cpp b/static_handler.cpp
index 8cb20e0..c4fa602 100644
--- a/static_handler.cpp
+++ b/static_handler.cpp
@@ -14,6 +14,11 @@
     return false;
 }
 
+void StaticLayoutHandler::close()
+{
+    return;
+}
+
 bool StaticLayoutHandler::write(std::uint32_t offset,
                                 const std::vector<std::uint8_t>& data)
 {
diff --git a/static_handler.hpp b/static_handler.hpp
index edb3df5..0126315 100644
--- a/static_handler.hpp
+++ b/static_handler.hpp
@@ -20,7 +20,7 @@
         stagedFilename(temporaryName){};
 
     bool open(const std::string& path) override;
-
+    void close() override;
     bool write(std::uint32_t offset,
                const std::vector<std::uint8_t>& data) override;
 
diff --git a/test/Makefile.am b/test/Makefile.am
index aeb3758..e914c14 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -15,7 +15,8 @@
 	firmware_canhandle_unittest \
 	firmware_open_unittest \
 	firmware_write_unittest \
-	firmware_writemeta_unittest
+	firmware_writemeta_unittest \
+	firmware_close_unittest
 
 TESTS = $(check_PROGRAMS)
 
@@ -36,3 +37,6 @@
 
 firmware_writemeta_unittest_SOURCES = firmware_writemeta_unittest.cpp
 firmware_writemeta_unittest_LDADD = $(top_builddir)/firmware_handler.o
+
+firmware_close_unittest_SOURCES = firmware_close_unittest.cpp
+firmware_close_unittest_LDADD = $(top_builddir)/firmware_handler.o
diff --git a/test/firmware_close_unittest.cpp b/test/firmware_close_unittest.cpp
new file mode 100644
index 0000000..5bbd333
--- /dev/null
+++ b/test/firmware_close_unittest.cpp
@@ -0,0 +1,96 @@
+#include "data_mock.hpp"
+#include "firmware_handler.hpp"
+#include "image_mock.hpp"
+
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace blobs
+{
+using ::testing::Eq;
+using ::testing::Return;
+using ::testing::StrEq;
+
+TEST(FirmwareHandlerCloseTest, CloseSuceedsWithDataHandler)
+{
+    /* Boring test where you open a blob_id, then verify that when it's closed
+     * everything looks right.
+     */
+    DataHandlerMock dataMock;
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {FirmwareBlobHandler::hashBlobID, &imageMock},
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+        {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
+    EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
+        .WillOnce(Return(true));
+
+    EXPECT_TRUE(handler->open(
+        0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
+        FirmwareBlobHandler::hashBlobID));
+
+    /* The active hash blob_id was added. */
+    auto currentBlobs = handler->getBlobIds();
+    EXPECT_EQ(3, currentBlobs.size());
+    EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
+                            FirmwareBlobHandler::activeHashBlobID));
+
+    /* Set up close() expectations. */
+    EXPECT_CALL(dataMock, close());
+    EXPECT_CALL(imageMock, close());
+    EXPECT_TRUE(handler->close(0));
+
+    /* Close does not delete the active blob id.  This indicates that there is
+     * data queued.
+     */
+}
+
+TEST(FirmwareHandlerCloseTest, CloseSuceedsWithoutDataHandler)
+{
+    /* Boring test where you open a blob_id using ipmi, so there's no data
+     * handler, and it's closed and everything looks right.
+     */
+    DataHandlerMock dataMock;
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {FirmwareBlobHandler::hashBlobID, &imageMock},
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+        {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
+        .WillOnce(Return(true));
+
+    EXPECT_TRUE(handler->open(
+        0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
+        FirmwareBlobHandler::hashBlobID));
+
+    /* The active hash blob_id was added. */
+    auto currentBlobs = handler->getBlobIds();
+    EXPECT_EQ(3, currentBlobs.size());
+    EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
+                            FirmwareBlobHandler::activeHashBlobID));
+
+    /* Set up close() expectations. */
+    EXPECT_CALL(imageMock, close());
+    EXPECT_TRUE(handler->close(0));
+}
+
+} // namespace blobs
diff --git a/test/firmware_open_unittest.cpp b/test/firmware_open_unittest.cpp
index 78c5d69..1975740 100644
--- a/test/firmware_open_unittest.cpp
+++ b/test/firmware_open_unittest.cpp
@@ -166,8 +166,7 @@
         1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
         FirmwareBlobHandler::hashBlobID));
 
-    /* Close the file, currently ignoring its return value. */
-    handler->close(0);
+    EXPECT_TRUE(handler->close(0));
 
     EXPECT_CALL(imageMock1, open(StrEq(FirmwareBlobHandler::hashBlobID)))
         .WillOnce(Return(true));
@@ -210,7 +209,7 @@
     /* Close only active session, to verify it's failing on attempt to open a
      * specific blob_id.
      */
-    handler->close(0);
+    EXPECT_TRUE(handler->close(0));
 
     EXPECT_FALSE(handler->open(
         1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
diff --git a/test/image_mock.hpp b/test/image_mock.hpp
index 6fbd87d..8259aa9 100644
--- a/test/image_mock.hpp
+++ b/test/image_mock.hpp
@@ -13,6 +13,7 @@
     virtual ~ImageHandlerMock() = default;
 
     MOCK_METHOD1(open, bool(const std::string&));
+    MOCK_METHOD0(close, void());
     MOCK_METHOD2(write, bool(std::uint32_t, const std::vector<std::uint8_t>&));
 };