extend file_handler to support reads

Problem: the upcomming version handler will need to read from files.
Currently file hander (image handler) does not support reads.

Solution: Add read support by providing an optional mode parameter.

Tests added:
FileHanderTest added to
- Test out open for reads
- Test out reading out bytes and verifying size and content
- Test out trying to read beyond EOF
- Test out offset reads that go beyond EOF

Tested:
Existing unit tests pass
New unit tests for reading all pass

Signed-off-by: Jason Ling <jasonling@google.com>
Change-Id: Ie416a6b4b452d8d04fa158bd55989d07a891896f
diff --git a/bmc/firmware-handler/test/file_handler_unittest.cpp b/bmc/firmware-handler/test/file_handler_unittest.cpp
index ab12125..b579830 100644
--- a/bmc/firmware-handler/test/file_handler_unittest.cpp
+++ b/bmc/firmware-handler/test/file_handler_unittest.cpp
@@ -21,7 +21,7 @@
     }
 };
 
-TEST_F(FileHandlerOpenTest, VerifyItIsHappy)
+TEST_F(FileHandlerOpenTest, VerifyDefaultOpenIsHappy)
 {
     /* Opening a fail may create it? */
 
@@ -32,6 +32,21 @@
     EXPECT_FALSE(handler.open(""));
 }
 
+TEST_F(FileHandlerOpenTest, VerifyOpenForReadFailsWithNoFile)
+{
+    FileHandler handler(TESTPATH);
+    EXPECT_FALSE(handler.open("", std::ios::in));
+}
+
+TEST_F(FileHandlerOpenTest, VerifyOpenForReadSucceedsWithFile)
+{
+    std::ofstream testfile;
+    testfile.open(TESTPATH, std::ios::out);
+    testfile << "Hello world";
+    FileHandler handler(TESTPATH);
+    EXPECT_TRUE(handler.open("", std::ios::in));
+}
+
 TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites)
 {
     /* Verify writing bytes writes them... flushing data can be an issue here,
@@ -55,4 +70,55 @@
     /* annoyingly the memcmp was failing... but it's the same data. */
 }
 
+TEST_F(FileHandlerOpenTest, VerifySimpleRead)
+{
+    std::ofstream testfile;
+    testfile.open(TESTPATH, std::ios::out);
+    std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
+                                             0x5, 0x6, 0x7, 0x8, 0x9};
+    testfile.write(reinterpret_cast<const char*>(testPattern.data()),
+                   testPattern.size());
+    testfile.close();
+    FileHandler handler(TESTPATH);
+    EXPECT_TRUE(handler.open("", std::ios::in));
+    auto result = handler.read(0, 10);
+    ASSERT_TRUE(result);
+    EXPECT_EQ(result->size(), 10);
+    EXPECT_EQ(*result, testPattern);
+}
+
+TEST_F(FileHandlerOpenTest, VerifyTruncatedAndOffsetReads)
+{
+    std::ofstream testfile;
+    testfile.open(TESTPATH, std::ios::out);
+    std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
+                                             0x5, 0x6, 0x7, 0x8, 0x9};
+    std::vector<std::uint8_t> expectedResult(testPattern.begin() + 3,
+                                             testPattern.end());
+
+    testfile.write(reinterpret_cast<const char*>(testPattern.data()),
+                   testPattern.size());
+    testfile.close();
+    FileHandler handler(TESTPATH);
+    EXPECT_TRUE(handler.open("", std::ios::in));
+    auto result = handler.read(3, 10);
+    ASSERT_TRUE(result);
+    EXPECT_EQ(*result, expectedResult);
+}
+
+TEST_F(FileHandlerOpenTest, VerifyBadOffsetReadsFail)
+{
+    std::ofstream testfile;
+    testfile.open(TESTPATH, std::ios::out);
+    std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
+                                             0x5, 0x6, 0x7, 0x8, 0x9};
+    testfile.write(reinterpret_cast<const char*>(testPattern.data()),
+                   testPattern.size());
+    testfile.close();
+    FileHandler handler(TESTPATH);
+    EXPECT_TRUE(handler.open("", std::ios::in));
+    auto result = handler.read(11, 10);
+    EXPECT_FALSE(result);
+}
+
 } // namespace ipmi_flash