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/Makefile.am b/test/Makefile.am
index ecd0ad8..7ea9cd6 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,7 +12,8 @@
 check_PROGRAMS = \
 	firmware_handler_unittest \
 	firmware_stat_unittest \
-	firmware_canhandle_unittest
+	firmware_canhandle_unittest \
+	firmware_open_unittest
 
 TESTS = $(check_PROGRAMS)
 
@@ -24,3 +25,6 @@
 
 firmware_canhandle_unittest_SOURCES = firmware_canhandle_unittest.cpp
 firmware_canhandle_unittest_LDADD = $(top_builddir)/firmware_handler.o
+
+firmware_open_unittest_SOURCES = firmware_open_unittest.cpp
+firmware_open_unittest_LDADD = $(top_builddir)/firmware_handler.o
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