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