firmware: return failure on attempt to open active files

Currently, the codebase does not support opening the active files,
therefore the code needs to return failure in those two cases.  The case
regarding opening an active file:

1) You've started uploading, stopped, closed the file.
2) Decided to start again, opened the active file directly.

There is no need to support opening the active file until later.

Change-Id: I719a79fe639dbcb752ad60d03900244b35729e20
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/firmware_handler.cpp b/firmware_handler.cpp
index 31d7565..9b4e7b0 100644
--- a/firmware_handler.cpp
+++ b/firmware_handler.cpp
@@ -208,12 +208,14 @@
         /* 2a) are they opening the active image? this can only happen if they
          * already started one (due to canHandleBlob's behavior).
          */
+        return false;
     }
     else if (path == activeHashBlobID)
     {
         /* 2b) are they opening the active hash? this can only happen if they
          * already started one (due to canHandleBlob's behavior).
          */
+        return false;
     }
 
     /* How are they expecting to copy this data? */
@@ -232,10 +234,10 @@
 
     if (path == hashBlobID)
     {
-        curr = &activeHash;
-
         /* 2c) are they opening the /flash/hash ? (to start the process) */
 
+        curr = &activeHash;
+
         /* Add active hash blob_id to canHandle list. */
         blobIDs.push_back(activeHashBlobID);
     }
@@ -251,7 +253,6 @@
      * 2e) are they opening the /flash/image ? (to start the process)
      * 2...) are they opening the /flash/... ? (to start the process)
      */
-
     auto h = std::find_if(
         handlers.begin(), handlers.end(),
         [&path](const auto& iter) { return (iter.blobName == path); });
diff --git a/test/firmware_open_unittest.cpp b/test/firmware_open_unittest.cpp
index 202c054..317911c 100644
--- a/test/firmware_open_unittest.cpp
+++ b/test/firmware_open_unittest.cpp
@@ -9,6 +9,7 @@
 
 namespace blobs
 {
+using ::testing::Eq;
 using ::testing::Return;
 
 TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
@@ -32,6 +33,76 @@
 
     EXPECT_TRUE(handler->open(
         0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
+
+    /* The active image blob_id was added. */
+    auto currentBlobs = handler->getBlobIds();
+    EXPECT_EQ(3, currentBlobs.size());
+    EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
+                            FirmwareBlobHandler::activeImageBlobID));
+}
+
+TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile)
+{
+    /* Open the hash file by blob_id. */
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {FirmwareBlobHandler::hashBlobID, &imageMock},
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+    };
+
+    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));
+}
+
+TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails)
+{
+    /* Attempting to open the active image blob, when it's present will fail.
+     *
+     * TODO: We'll need another test that closes first because you can only have
+     * one file open at a time.
+     */
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {FirmwareBlobHandler::hashBlobID, &imageMock},
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+
+    EXPECT_TRUE(handler->open(
+        0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
+
+    /* The active image blob_id was added. */
+    auto currentBlobs = handler->getBlobIds();
+    EXPECT_EQ(3, currentBlobs.size());
+    EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
+                            FirmwareBlobHandler::activeImageBlobID));
+
+    EXPECT_FALSE(handler->open(
+        1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
+        FirmwareBlobHandler::activeImageBlobID));
 }
 
 TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)