blob: b5798300d7ca4ca15443b737b0c1fc125bbbd122 [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(""));
30
31 /* Calling open twice fails the second time. */
32 EXPECT_FALSE(handler.open(""));
33}
34
Jason Ling56a22732020-10-23 19:53:17 -070035TEST_F(FileHandlerOpenTest, VerifyOpenForReadFailsWithNoFile)
36{
37 FileHandler handler(TESTPATH);
38 EXPECT_FALSE(handler.open("", std::ios::in));
39}
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);
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
90TEST_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
109TEST_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
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700124} // namespace ipmi_flash