Patrick Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame] | 1 | #include "flash-ipmi.hpp" |
| 2 | |
| 3 | #include <cstdio> |
| 4 | #include <cstring> |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 5 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
Patrick Venture | 1aedab2 | 2018-09-10 14:41:45 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
Patrick Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame] | 11 | #define THIRTYTWO_MIB 33554432 |
| 12 | |
| 13 | class 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 | |
| 33 | TEST_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 Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 40 | 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 Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame] | 44 | EXPECT_FALSE(updater.hashData(0, bytes)); |
| 45 | } |
| 46 | |
| 47 | TEST_F(FlashIpmiHashDataTest, CalledWithDataSucceeds) |
| 48 | { |
| 49 | // Verify the normal use case works. |
| 50 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 51 | |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 52 | 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 Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame] | 56 | 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 | |
| 70 | TEST_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 Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 78 | 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 Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame] | 82 | 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 | } |