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:
TearDown()18 void TearDown() override
19 {
20 (void)std::remove(TESTPATH);
21 }
22 };
23
TEST_F(FileHandlerOpenTest,VerifyDefaultOpenIsHappy)24 TEST_F(FileHandlerOpenTest, VerifyDefaultOpenIsHappy)
25 {
26 /* Opening a fail may create it? */
27
28 FileHandler handler(TESTPATH);
29 EXPECT_TRUE(handler.open(""));
30 EXPECT_TRUE(handler.open(""));
31 }
32
TEST_F(FileHandlerOpenTest,VerifyOpenForReadFailsWithNoFile)33 TEST_F(FileHandlerOpenTest, VerifyOpenForReadFailsWithNoFile)
34 {
35 FileHandler handler(TESTPATH);
36 EXPECT_EQ(handler.getSize(), 0);
37 EXPECT_FALSE(handler.open("", std::ios::in));
38 EXPECT_EQ(handler.getSize(), 0);
39 }
40
TEST_F(FileHandlerOpenTest,VerifyOpenForReadSucceedsWithFile)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
TEST_F(FileHandlerOpenTest,VerifyWriteDataWrites)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
TEST_F(FileHandlerOpenTest,VerifySimpleRead)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_EQ(handler.getSize(), testPattern.size());
84 EXPECT_TRUE(handler.open("", std::ios::in));
85 auto result = handler.read(0, 10);
86 EXPECT_EQ(handler.getSize(), testPattern.size());
87 ASSERT_TRUE(result);
88 EXPECT_EQ(result->size(), 10);
89 EXPECT_EQ(*result, testPattern);
90 }
91
TEST_F(FileHandlerOpenTest,VerifyTruncatedAndOffsetReads)92 TEST_F(FileHandlerOpenTest, VerifyTruncatedAndOffsetReads)
93 {
94 std::ofstream testfile;
95 testfile.open(TESTPATH, std::ios::out);
96 std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
97 0x5, 0x6, 0x7, 0x8, 0x9};
98 std::vector<std::uint8_t> expectedResult(testPattern.begin() + 3,
99 testPattern.end());
100
101 testfile.write(reinterpret_cast<const char*>(testPattern.data()),
102 testPattern.size());
103 testfile.close();
104 FileHandler handler(TESTPATH);
105 EXPECT_TRUE(handler.open("", std::ios::in));
106 auto result = handler.read(3, 10);
107 ASSERT_TRUE(result);
108 EXPECT_EQ(*result, expectedResult);
109 }
110
TEST_F(FileHandlerOpenTest,VerifyBadOffsetReadsFail)111 TEST_F(FileHandlerOpenTest, VerifyBadOffsetReadsFail)
112 {
113 std::ofstream testfile;
114 testfile.open(TESTPATH, std::ios::out);
115 std::vector<std::uint8_t> testPattern = {0x0, 0x1, 0x2, 0x3, 0x4,
116 0x5, 0x6, 0x7, 0x8, 0x9};
117 testfile.write(reinterpret_cast<const char*>(testPattern.data()),
118 testPattern.size());
119 testfile.close();
120 FileHandler handler(TESTPATH);
121 EXPECT_TRUE(handler.open("", std::ios::in));
122 auto result = handler.read(11, 10);
123 EXPECT_FALSE(result);
124 }
125
126 } // namespace ipmi_flash
127