Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 1 | #include "flash-ipmi.hpp" |
| 2 | |
| 3 | #include <cstdio> |
| 4 | #include <cstring> |
| 5 | #include <gtest/gtest.h> |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 6 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #define THIRTYTWO_MIB 33554432 |
| 11 | |
| 12 | class 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 | |
| 32 | TEST_F(FlashIpmiHashDataTest, CalledOutOfSequenceFails) |
| 33 | { |
| 34 | // Verify that there is sanity checking |
| 35 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 36 | |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 37 | sdbusplus::SdBusMock sdbus_mock; |
| 38 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 39 | |
| 40 | FlashUpdate updater(std::move(bus_mock), name, "", name2); |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 41 | EXPECT_FALSE(updater.hashFinish()); |
| 42 | |
| 43 | // Verify the file doesn't exist. |
| 44 | auto file = std::fopen(name2.c_str(), "r"); |
| 45 | EXPECT_FALSE(file); |
| 46 | } |
| 47 | |
| 48 | TEST_F(FlashIpmiHashDataTest, CalledInSequenceSucceeds) |
| 49 | { |
| 50 | // Verify that under normal usage it closes the file. |
| 51 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 52 | |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 53 | sdbusplus::SdBusMock sdbus_mock; |
| 54 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 55 | |
| 56 | FlashUpdate updater(std::move(bus_mock), name, "", name2); |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(updater.start(THIRTYTWO_MIB)); |
| 58 | EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB)); |
| 59 | EXPECT_TRUE(updater.hashFinish()); |
| 60 | |
| 61 | // Verify we can open the file, so we know it didn't get deleted. |
| 62 | auto file = std::fopen(name2.c_str(), "r"); |
| 63 | EXPECT_TRUE(file); |
| 64 | std::fclose(file); |
| 65 | } |
| 66 | |
| 67 | TEST_F(FlashIpmiHashDataTest, CalledTwiceFails) |
| 68 | { |
| 69 | // Verify that under normal usage it closes the file, and therefore cannot |
| 70 | // be closed twice. |
| 71 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 72 | |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 73 | sdbusplus::SdBusMock sdbus_mock; |
| 74 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 75 | |
| 76 | FlashUpdate updater(std::move(bus_mock), name, "", name2); |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(updater.start(THIRTYTWO_MIB)); |
| 78 | EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB)); |
| 79 | EXPECT_TRUE(updater.hashFinish()); |
| 80 | |
| 81 | EXPECT_FALSE(updater.hashFinish()); |
| 82 | |
| 83 | // Verify we can open the file, so we know it didn't get deleted. |
| 84 | auto file = std::fopen(name2.c_str(), "r"); |
| 85 | EXPECT_TRUE(file); |
| 86 | std::fclose(file); |
| 87 | } |