blob: a438be7a5141fe95569c8c404b5cf968c9f58bea [file] [log] [blame]
Patrick Venturecbe51492018-08-07 14:09:17 -07001#include "flash-ipmi.hpp"
2
3#include <cstdio>
4#include <cstring>
Patrick Venture605f75f2018-08-07 16:27:05 -07005#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturecbe51492018-08-07 14:09:17 -07006#include <string>
7#include <vector>
8
Patrick Venture1aedab22018-09-10 14:41:45 -07009#include <gtest/gtest.h>
10
Patrick Venturecbe51492018-08-07 14:09:17 -070011#define THIRTYTWO_MIB 33554432
12
13class FlashIpmiHashDataTest : public ::testing::Test
14{
15 protected:
16 FlashIpmiHashDataTest() = default;
17
18 void SetUp() override
19 {
20 name = std::tmpnam(nullptr);
21 name2 = std::tmpnam(nullptr);
22 }
23 void TearDown() override
24 {
25 (void)std::remove(name.c_str());
26 (void)std::remove(name2.c_str());
27 }
28
29 std::string name;
30 std::string name2;
31};
32
33TEST_F(FlashIpmiHashDataTest, CalledOutOfSequenceFails)
34{
35 // Verify that if you try to write to the hash before starting, it'll fail.
36 // Presently, start() will open the hash file.
37
38 std::vector<uint8_t> bytes = {0xaa, 0x55};
39
Patrick Venture605f75f2018-08-07 16:27:05 -070040 sdbusplus::SdBusMock sdbus_mock;
41 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
42
43 FlashUpdate updater(std::move(bus_mock), name, "", name2);
Patrick Venturecbe51492018-08-07 14:09:17 -070044 EXPECT_FALSE(updater.hashData(0, bytes));
45}
46
47TEST_F(FlashIpmiHashDataTest, CalledWithDataSucceeds)
48{
49 // Verify the normal use case works.
50 std::vector<uint8_t> bytes = {0xaa, 0x55};
51
Patrick Venture605f75f2018-08-07 16:27:05 -070052 sdbusplus::SdBusMock sdbus_mock;
53 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
54
55 FlashUpdate updater(std::move(bus_mock), name, "", name2);
Patrick Venturecbe51492018-08-07 14:09:17 -070056 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
57 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
58 EXPECT_TRUE(updater.hashData(0, bytes));
59
60 auto file = std::fopen(name2.c_str(), "r");
61 EXPECT_TRUE(file);
62
63 uint8_t buffer[2];
64 auto read = std::fread(buffer, 1, bytes.size(), file);
65 EXPECT_EQ(read, bytes.size());
66 EXPECT_EQ(0, std::memcmp(buffer, bytes.data(), bytes.size()));
67 std::fclose(file);
68}
69
70TEST_F(FlashIpmiHashDataTest, CalledNonZeroOffsetSucceeds)
71{
72 // Skipping bytes in POSIX can create a sparse file. In this case,
73 // let's allow the behavior. If we'd rather, we can have writeBlock
74 // check that the offset is where we expect.
75
76 std::vector<uint8_t> bytes = {0xaa, 0x55};
77
Patrick Venture605f75f2018-08-07 16:27:05 -070078 sdbusplus::SdBusMock sdbus_mock;
79 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
80
81 FlashUpdate updater(std::move(bus_mock), name, "", name2);
Patrick Venturecbe51492018-08-07 14:09:17 -070082 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
83 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
84 EXPECT_TRUE(updater.hashData(2, bytes));
85
86 auto file = std::fopen(name2.c_str(), "r");
87 EXPECT_TRUE(file);
88
89 uint8_t buffer[4];
90 auto read = std::fread(buffer, 1, sizeof(buffer), file);
91 EXPECT_EQ(read, sizeof(buffer));
92 EXPECT_EQ(0, std::memcmp(&buffer[2], bytes.data(), bytes.size()));
93 std::fclose(file);
94}