xref: /openbmc/phosphor-ipmi-flash/bmc/firmware-handler/test/file_handler_unittest.cpp (revision 56a2273fc7e609ddac670a57d55089d24448025a)
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, VerifyDefaultOpenIsHappy)
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, VerifyOpenForReadFailsWithNoFile)
36 {
37     FileHandler handler(TESTPATH);
38     EXPECT_FALSE(handler.open("", std::ios::in));
39 }
40 
41 TEST_F(FileHandlerOpenTest, VerifyOpenForReadSucceedsWithFile)
42 {
43     std::ofstream testfile;
44     testfile.open(TESTPATH, std::ios::out);
45     testfile << "Hello world";
46     FileHandler handler(TESTPATH);
47     EXPECT_TRUE(handler.open("", std::ios::in));
48 }
49 
50 TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites)
51 {
52     /* Verify writing bytes writes them... flushing data can be an issue here,
53      * so we close first.
54      */
55     FileHandler handler(TESTPATH);
56     EXPECT_TRUE(handler.open(""));
57 
58     std::vector<std::uint8_t> bytes = {0x01, 0x02};
59     std::uint32_t offset = 0;
60 
61     EXPECT_TRUE(handler.write(offset, bytes));
62     handler.close();
63 
64     std::ifstream data;
65     data.open(TESTPATH, std::ios::binary);
66     char expectedBytes[2];
67     data.read(&expectedBytes[0], sizeof(expectedBytes));
68     EXPECT_EQ(expectedBytes[0], bytes[0]);
69     EXPECT_EQ(expectedBytes[1], bytes[1]);
70     /* annoyingly the memcmp was failing... but it's the same data. */
71 }
72 
73 TEST_F(FileHandlerOpenTest, VerifySimpleRead)
74 {
75     std::ofstream testfile;
76     testfile.open(TESTPATH, std::ios::out);
77     std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
78                                              0x5, 0x6, 0x7, 0x8, 0x9};
79     testfile.write(reinterpret_cast<const char*>(testPattern.data()),
80                    testPattern.size());
81     testfile.close();
82     FileHandler handler(TESTPATH);
83     EXPECT_TRUE(handler.open("", std::ios::in));
84     auto result = handler.read(0, 10);
85     ASSERT_TRUE(result);
86     EXPECT_EQ(result->size(), 10);
87     EXPECT_EQ(*result, testPattern);
88 }
89 
90 TEST_F(FileHandlerOpenTest, VerifyTruncatedAndOffsetReads)
91 {
92     std::ofstream testfile;
93     testfile.open(TESTPATH, std::ios::out);
94     std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
95                                              0x5, 0x6, 0x7, 0x8, 0x9};
96     std::vector<std::uint8_t> expectedResult(testPattern.begin() + 3,
97                                              testPattern.end());
98 
99     testfile.write(reinterpret_cast<const char*>(testPattern.data()),
100                    testPattern.size());
101     testfile.close();
102     FileHandler handler(TESTPATH);
103     EXPECT_TRUE(handler.open("", std::ios::in));
104     auto result = handler.read(3, 10);
105     ASSERT_TRUE(result);
106     EXPECT_EQ(*result, expectedResult);
107 }
108 
109 TEST_F(FileHandlerOpenTest, VerifyBadOffsetReadsFail)
110 {
111     std::ofstream testfile;
112     testfile.open(TESTPATH, std::ios::out);
113     std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
114                                              0x5, 0x6, 0x7, 0x8, 0x9};
115     testfile.write(reinterpret_cast<const char*>(testPattern.data()),
116                    testPattern.size());
117     testfile.close();
118     FileHandler handler(TESTPATH);
119     EXPECT_TRUE(handler.open("", std::ios::in));
120     auto result = handler.read(11, 10);
121     EXPECT_FALSE(result);
122 }
123 
124 } // namespace ipmi_flash
125