blob: fbe774b9f517d69d2b7d8a6fac5433f89745e1c0 [file] [log] [blame]
Patrick Venture7753d942018-11-15 13:15:36 -08001#include "file_handler.hpp"
Patrick Venturea17cf442018-11-15 09:31:51 -08002
3#include <cstdint>
4#include <cstdio>
5#include <fstream>
6#include <vector>
7
8#include <gtest/gtest.h>
9
Patrick Venture1d5a31c2019-05-20 11:38:22 -070010namespace ipmi_flash
Patrick Venturea17cf442018-11-15 09:31:51 -080011{
12
13static constexpr auto TESTPATH = "test.output";
14
Patrick Venture7753d942018-11-15 13:15:36 -080015class FileHandlerOpenTest : public ::testing::Test
Patrick Venturea17cf442018-11-15 09:31:51 -080016{
17 protected:
18 void TearDown() override
19 {
20 (void)std::remove(TESTPATH);
21 }
22};
23
Jason Ling56a22732020-10-23 19:53:17 -070024TEST_F(FileHandlerOpenTest, VerifyDefaultOpenIsHappy)
Patrick Venturea17cf442018-11-15 09:31:51 -080025{
26 /* Opening a fail may create it? */
27
Patrick Venture7753d942018-11-15 13:15:36 -080028 FileHandler handler(TESTPATH);
Patrick Venturea17cf442018-11-15 09:31:51 -080029 EXPECT_TRUE(handler.open(""));
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070030 EXPECT_TRUE(handler.open(""));
Patrick Venturea17cf442018-11-15 09:31:51 -080031}
32
Jason Ling56a22732020-10-23 19:53:17 -070033TEST_F(FileHandlerOpenTest, VerifyOpenForReadFailsWithNoFile)
34{
35 FileHandler handler(TESTPATH);
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070036 EXPECT_EQ(handler.getSize(), 0);
Jason Ling56a22732020-10-23 19:53:17 -070037 EXPECT_FALSE(handler.open("", std::ios::in));
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070038 EXPECT_EQ(handler.getSize(), 0);
Jason Ling56a22732020-10-23 19:53:17 -070039}
40
41TEST_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
Patrick Venture7753d942018-11-15 13:15:36 -080050TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites)
Patrick Venturea17cf442018-11-15 09:31:51 -080051{
52 /* Verify writing bytes writes them... flushing data can be an issue here,
53 * so we close first.
54 */
Patrick Venture7753d942018-11-15 13:15:36 -080055 FileHandler handler(TESTPATH);
Patrick Venturea17cf442018-11-15 09:31:51 -080056 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
Jason Ling56a22732020-10-23 19:53:17 -070073TEST_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);
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070083 EXPECT_EQ(handler.getSize(), testPattern.size());
Jason Ling56a22732020-10-23 19:53:17 -070084 EXPECT_TRUE(handler.open("", std::ios::in));
85 auto result = handler.read(0, 10);
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070086 EXPECT_EQ(handler.getSize(), testPattern.size());
Jason Ling56a22732020-10-23 19:53:17 -070087 ASSERT_TRUE(result);
88 EXPECT_EQ(result->size(), 10);
89 EXPECT_EQ(*result, testPattern);
90}
91
92TEST_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
111TEST_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
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700126} // namespace ipmi_flash