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