handler: Implement open as a pass-through function

When opening a blob id, handler finds a blobstore with matching
base id and calls its open function, passing down the path and open
flags.

Signed-off-by: Kun Yi <kunyi@google.com>
Change-Id: I6127b3c2b2c4752b5e094db61f12451089ccb20b
diff --git a/test/Makefile.am b/test/Makefile.am
index b80b6d2..5b3fe6c 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -10,6 +10,7 @@
 
 # Run all 'check' test programs
 check_PROGRAMS = \
+	handler_open_unittest \
 	handler_unittest
 TESTS = $(check_PROGRAMS)
 
@@ -17,3 +18,8 @@
 handler_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) \
 	$(top_builddir)/handler.o
 handler_unittest_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS)
+
+handler_open_unittest_SOURCES = handler_open_unittest.cpp
+handler_open_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) \
+	$(top_builddir)/handler.o
+handler_open_unittest_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS)
diff --git a/test/handler_open_unittest.cpp b/test/handler_open_unittest.cpp
new file mode 100644
index 0000000..1d69f24
--- /dev/null
+++ b/test/handler_open_unittest.cpp
@@ -0,0 +1,53 @@
+#include "handler_unittest.hpp"
+
+using ::testing::_;
+using ::testing::Return;
+using ::testing::StartsWith;
+
+using namespace std::string_literals;
+using namespace binstore;
+
+namespace blobs
+{
+
+TEST_F(BinaryStoreBlobHandlerOpenTest, FailWhenCannotHandleId)
+{
+    uint16_t flags = OpenFlags::read, sessionId = 0;
+    EXPECT_FALSE(handler.open(sessionId, flags, "/invalid/blob"s));
+}
+
+TEST_F(BinaryStoreBlobHandlerOpenTest, FailWhenStoreOpenReturnsFailure)
+{
+    auto testBaseId = "/test/"s;
+    auto testBlobId = "/test/blob0"s;
+    uint16_t flags = OpenFlags::read, sessionId = 0;
+    auto bstore = std::make_unique<MockBinaryStore>();
+
+    EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
+    EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
+        .WillRepeatedly(Return(true));
+    EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(false));
+
+    handler.addNewBinaryStore(std::move(bstore));
+
+    EXPECT_FALSE(handler.open(sessionId, flags, testBlobId));
+}
+
+TEST_F(BinaryStoreBlobHandlerOpenTest, SucceedWhenStoreOpenReturnsTrue)
+{
+    auto testBaseId = "/test/"s;
+    auto testBlobId = "/test/blob0"s;
+    uint16_t flags = OpenFlags::read, sessionId = 0;
+    auto bstore = std::make_unique<MockBinaryStore>();
+
+    EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
+    EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
+        .WillRepeatedly(Return(true));
+    EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(true));
+
+    handler.addNewBinaryStore(std::move(bstore));
+
+    EXPECT_TRUE(handler.open(sessionId, flags, testBlobId));
+}
+
+} // namespace blobs
diff --git a/test/handler_unittest.hpp b/test/handler_unittest.hpp
index f6390fc..7f5df42 100644
--- a/test/handler_unittest.hpp
+++ b/test/handler_unittest.hpp
@@ -15,4 +15,8 @@
     BinaryStoreBlobHandler handler;
 };
 
+class BinaryStoreBlobHandlerOpenTest : public BinaryStoreBlobHandlerTest
+{
+};
+
 } // namespace blobs