blob: ab12125af1eeccb8e0d91c108f76d7b4514fac35 [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
Patrick Venture7753d942018-11-15 13:15:36 -080024TEST_F(FileHandlerOpenTest, VerifyItIsHappy)
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
Patrick Venture7753d942018-11-15 13:15:36 -080035TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites)
Patrick Venturea17cf442018-11-15 09:31:51 -080036{
37 /* Verify writing bytes writes them... flushing data can be an issue here,
38 * so we close first.
39 */
Patrick Venture7753d942018-11-15 13:15:36 -080040 FileHandler handler(TESTPATH);
Patrick Venturea17cf442018-11-15 09:31:51 -080041 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
Patrick Venture1d5a31c2019-05-20 11:38:22 -070058} // namespace ipmi_flash