Inject sysfile dependency when creating BinaryStore

Make the factory take sysFile rather than raw fd. Test code would take
a FakeSysFile for testing file operations.

Change-Id: Id7a02203893936e4eddb2e73267b45343c6c6f08
Signed-off-by: Kun Yi <kunyi@google.com>
diff --git a/Makefile.am b/Makefile.am
index 6d0e426..2ad19a0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,9 +9,11 @@
 libbinarystoredir = ${libdir}/ipmid-providers
 libbinarystore_LTLIBRARIES = libbinarystore.la
 libbinarystore_la_SOURCES = main.cpp \
-			    handler.cpp \
 			    binaryblob.pb.cc \
-			    binarystore.cpp
+			    binarystore.cpp \
+			    sys.cpp \
+			    sys_file.cpp \
+			    handler.cpp
 
 libbinarystore_la_LDFLAGS = $(PHOSPHOR_LOGGING_LIBS) \
 			    -version-info 0:0:0 -shared \
diff --git a/binarystore.cpp b/binarystore.cpp
index 1e8475c..98a6264 100644
--- a/binarystore.cpp
+++ b/binarystore.cpp
@@ -22,11 +22,20 @@
 
 std::unique_ptr<BinaryStoreInterface>
     BinaryStore::createFromConfig(const std::string& baseBlobId,
-                                  const std::string& sysfilePath,
-                                  uint32_t offset, uint32_t maxSize)
+                                  std::unique_ptr<SysFile> file,
+                                  uint32_t maxSize)
 {
-    // TODO: implement sysFile parsing
-    return std::make_unique<BinaryStore>(baseBlobId, 0, offset, maxSize);
+    if (baseBlobId.empty() || !file)
+    {
+        return nullptr;
+    }
+
+    auto store =
+        std::make_unique<BinaryStore>(baseBlobId, std::move(file), maxSize);
+
+    store->blob_.set_blob_base_id(store->baseBlobId_);
+
+    return std::move(store);
 }
 
 std::string BinaryStore::getBaseBlobId() const
diff --git a/binarystore.hpp b/binarystore.hpp
index 4091fd4..2e5f562 100644
--- a/binarystore.hpp
+++ b/binarystore.hpp
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "sys_file.hpp"
+
 #include <unistd.h>
 
 #include <cstdint>
@@ -95,15 +97,16 @@
 class BinaryStore : public BinaryStoreInterface
 {
   public:
-    BinaryStore(const std::string& baseBlobId, int fd, uint32_t offset,
+    BinaryStore() = delete;
+    BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file,
                 uint32_t maxSize) :
         baseBlobId_(baseBlobId),
-        fd_(fd), offset_(offset), maxSize_(maxSize), currentBlob_(nullptr)
+        file_(std::move(file)), maxSize_(maxSize)
     {
     }
 
-    BinaryStore() = delete;
     ~BinaryStore() = default;
+
     BinaryStore(const BinaryStore&) = delete;
     BinaryStore& operator=(const BinaryStore&) = delete;
     BinaryStore(BinaryStore&&) = default;
@@ -122,8 +125,7 @@
     /**
      * Helper factory method to create a BinaryStore instance
      * @param baseBlobId: base id for the created instance
-     * @param sysfilePath: path to the storage location
-     * @param offset: offset into the file for serializing the final blob
+     * @param sysFile: system file object for storing binary
      * @param maxSize: max size in bytes that this BinaryStore can expand to.
      *     Writing data more than allowed size will return failure.
      * @returns unique_ptr to constructed BinaryStore. Caller should take
@@ -131,17 +133,15 @@
      */
     static std::unique_ptr<BinaryStoreInterface>
         createFromConfig(const std::string& baseBlobId,
-                         const std::string& sysfilePath, uint32_t offset,
-                         uint32_t maxSize);
+                         std::unique_ptr<SysFile> file, uint32_t maxSize);
 
   private:
     std::string baseBlobId_;
-    int fd_;
-    uint32_t offset_;
-    uint32_t maxSize_;
     binaryblobproto::BinaryBlobBase blob_;
-    binaryblobproto::BinaryBlob* currentBlob_;
+    binaryblobproto::BinaryBlob* currentBlob_ = nullptr;
     bool writable_ = false;
+    std::unique_ptr<SysFile> file_ = nullptr;
+    uint32_t maxSize_;
 };
 
 } // namespace binstore
diff --git a/binarystore_mock.hpp b/binarystore_mock.hpp
index bdbb052..bf8c811 100644
--- a/binarystore_mock.hpp
+++ b/binarystore_mock.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "binarystore.hpp"
+#include "sys_file.hpp"
 
 #include <gmock/gmock.h>
 
@@ -12,9 +13,9 @@
 class MockBinaryStore : public BinaryStoreInterface
 {
   public:
-    MockBinaryStore(const std::string& baseBlobId, int fd, uint32_t offset,
-                    uint32_t maxSize) :
-        real_store_(baseBlobId, fd, offset, maxSize)
+    MockBinaryStore(const std::string& baseBlobId,
+                    std::unique_ptr<SysFile> file, uint32_t maxSize) :
+        real_store_(baseBlobId, std::move(file), maxSize)
     {
         // Implemented calls in BinaryStore will be directed to the real object.
         ON_CALL(*this, getBaseBlobId)
diff --git a/test/Makefile.am b/test/Makefile.am
index b2fa580..f5fe63b 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -24,6 +24,7 @@
 handler_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) \
 	$(top_builddir)/handler.o \
 	$(top_builddir)/binarystore.o \
+	$(top_builddir)/sys_file.o \
 	$(top_builddir)/libbinarystore_la-binaryblob.pb.o \
 	-lprotobuf
 handler_unittest_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS)
@@ -32,6 +33,7 @@
 handler_open_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) \
 	$(top_builddir)/handler.o \
 	$(top_builddir)/binarystore.o \
+	$(top_builddir)/sys_file.o \
 	$(top_builddir)/libbinarystore_la-binaryblob.pb.o \
 	-lprotobuf
 handler_open_unittest_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS)
@@ -40,6 +42,7 @@
 handler_readwrite_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) \
 	$(top_builddir)/handler.o \
 	$(top_builddir)/binarystore.o \
+	$(top_builddir)/sys_file.o \
 	$(top_builddir)/libbinarystore_la-binaryblob.pb.o \
 	-lprotobuf
 handler_readwrite_unittest_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS)
diff --git a/test/handler_unittest.hpp b/test/handler_unittest.hpp
index ded13eb..019c7ad 100644
--- a/test/handler_unittest.hpp
+++ b/test/handler_unittest.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "binarystore_mock.hpp"
+#include "fake_sys_file.hpp"
 #include "handler.hpp"
 
 #include <memory>
@@ -13,6 +14,7 @@
 using ::testing::Contains;
 
 using namespace std::string_literals;
+using namespace binstore;
 
 namespace blobs
 {
@@ -21,18 +23,19 @@
 {
   protected:
     BinaryStoreBlobHandlerTest() = default;
-    BinaryStoreBlobHandler handler;
 
-    std::unique_ptr<binstore::MockBinaryStore>
-        defaultMockStore(const std::string& baseId)
+    std::unique_ptr<MockBinaryStore> defaultMockStore(const std::string& baseId)
     {
-        return std::make_unique<binstore::MockBinaryStore>(baseId, 0, 0, 0);
+        return std::make_unique<MockBinaryStore>(
+            baseId, std::make_unique<FakeSysFile>(), 0);
     }
 
     void addDefaultStore(const std::string& baseId)
     {
         handler.addNewBinaryStore(defaultMockStore(baseId));
     }
+
+    BinaryStoreBlobHandler handler;
 };
 
 } // namespace blobs