tests: add unit-tests for opening

Add unit-tests for verifying the various behaviors associated with
opening a file or hash.

Change-Id: Ie74896b1689cab43d24ebf98ed206864f252184a
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/firmware_open_unittest.cpp b/test/firmware_open_unittest.cpp
new file mode 100644
index 0000000..d77bafc
--- /dev/null
+++ b/test/firmware_open_unittest.cpp
@@ -0,0 +1,126 @@
+#include "data_mock.hpp"
+#include "firmware_handler.hpp"
+#include "image_mock.hpp"
+
+#include <memory>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace blobs
+{
+using ::testing::Return;
+
+TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
+{
+    /* The client passes write set, and a transport that's supported, and a
+     * firmware image blob_id that's supported. */
+
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
+
+    EXPECT_TRUE(handler->open(
+        0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::bt,
+        "asdf"));
+}
+
+TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
+{
+    /* The image handler for a specific type of image is allowed to return
+     * failure on open.  let's simulate that. */
+
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
+
+    EXPECT_FALSE(handler->open(
+        0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::bt,
+        "asdf"));
+}
+
+TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
+{
+    /* The client must set the file write bit. */
+
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_FALSE(
+        handler->open(0, FirmwareBlobHandler::FirmwareUpdateFlags::bt, "asdf"));
+}
+
+TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
+{
+    /* The client sends a request with a transport mechanism not supported. */
+
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_FALSE(handler->open(0, FirmwareBlobHandler::FirmwareUpdateFlags::lpc,
+                               "asdf"));
+}
+
+TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
+{
+    /* The client sends a request with an invalid image blob_id. */
+
+    ImageHandlerMock imageMock;
+
+    std::vector<HandlerPack> blobs = {
+        {"asdf", &imageMock},
+    };
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
+    };
+
+    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+    EXPECT_FALSE(handler->open(
+        0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::bt,
+        "bcdf"));
+}
+
+/* TODO: The client sends a request during verification. */
+/* TODO: The client sends a second request to open when there is already an open
+ * file.
+ */
+/* TODO: The client sends a request to open active image. */
+/* TODO: The client sends a request to open active hash. */
+
+} // namespace blobs