bugfix: Fix invalid data handling in deserialization

When loading from sysfile, it is possible that the 'size'
read is junk data when there was no persisted blobs, and is too large causing
'length_error' or 'bad_alloc' to be raised. Improve handling by

1) Returning empty string if 'size' specified is 0 or too large.
2) Ignoring all but system_error which is thrown from sysfile read operation.
3) Not commiting the empty blob when no persisted data is found.
4) Adding more unit tests.

Resolves openbmc/phosphor-ipmi-blobs-binarystore#1

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: Iee16cf5254242856efe6bcca59ef2ca7c4f09c7c
diff --git a/test/handler_unittest.cpp b/test/handler_unittest.cpp
index 31ccdc6..b6d204e 100644
--- a/test/handler_unittest.cpp
+++ b/test/handler_unittest.cpp
@@ -13,6 +13,8 @@
 
 #include "binaryblob.pb.h"
 
+#include <gtest/gtest.h>
+
 using ::testing::_;
 using ::testing::AtLeast;
 using ::testing::ElementsAreArray;
@@ -178,4 +180,25 @@
     EXPECT_EQ(handler.getBlobIds(), expectedIdList);
 }
 
+TEST_F(BinaryStoreBlobHandlerBasicTest, CreatingFromEmptySysfile)
+{
+    const std::string emptyData;
+    EXPECT_NO_THROW(handler.addNewBinaryStore(BinaryStore::createFromConfig(
+        basicTestBaseId, std::make_unique<FakeSysFile>(emptyData), 0)));
+    EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId));
+}
+
+TEST_F(BinaryStoreBlobHandlerBasicTest, CreatingFromJunkData)
+{
+    boost::endian::little_uint64_t tooLarge = 0xffffffffffffffffull;
+    const std::string junkDataWithLargeSize(tooLarge.data(), sizeof(tooLarge));
+    EXPECT_GE(tooLarge, junkDataWithLargeSize.max_size());
+
+    EXPECT_NO_THROW(handler.addNewBinaryStore(BinaryStore::createFromConfig(
+        basicTestBaseId, std::make_unique<FakeSysFile>(junkDataWithLargeSize),
+        0)));
+
+    EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId));
+}
+
 } // namespace blobs