xref: /openbmc/phosphor-ipmi-flash/bmc/firmware-handler/test/file_handler_unittest.cpp (revision ded66d0f7317f6e43a5301da6c3d9dda80b17250)
1 #include "file_handler.hpp"
2 
3 #include <cstdint>
4 #include <cstdio>
5 #include <fstream>
6 #include <vector>
7 
8 #include <gtest/gtest.h>
9 
10 namespace ipmi_flash
11 {
12 
13 static constexpr auto TESTPATH = "test.output";
14 
15 class FileHandlerOpenTest : public ::testing::Test
16 {
17   protected:
18     void TearDown() override
19     {
20         (void)std::remove(TESTPATH);
21     }
22 };
23 
24 TEST_F(FileHandlerOpenTest, VerifyItIsHappy)
25 {
26     /* Opening a fail may create it? */
27 
28     FileHandler handler(TESTPATH);
29     EXPECT_TRUE(handler.open(""));
30 
31     /* Calling open twice fails the second time. */
32     EXPECT_FALSE(handler.open(""));
33 }
34 
35 TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites)
36 {
37     /* Verify writing bytes writes them... flushing data can be an issue here,
38      * so we close first.
39      */
40     FileHandler handler(TESTPATH);
41     EXPECT_TRUE(handler.open(""));
42 
43     std::vector<std::uint8_t> bytes = {0x01, 0x02};
44     std::uint32_t offset = 0;
45 
46     EXPECT_TRUE(handler.write(offset, bytes));
47     handler.close();
48 
49     std::ifstream data;
50     data.open(TESTPATH, std::ios::binary);
51     char expectedBytes[2];
52     data.read(&expectedBytes[0], sizeof(expectedBytes));
53     EXPECT_EQ(expectedBytes[0], bytes[0]);
54     EXPECT_EQ(expectedBytes[1], bytes[1]);
55     /* annoyingly the memcmp was failing... but it's the same data. */
56 }
57 
58 } // namespace ipmi_flash
59