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> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | #define THIRTYTWO_MIB 33554432 |
| 10 | |
| 11 | class FlashIpmiHashDataTest : public ::testing::Test |
| 12 | { |
| 13 | protected: |
| 14 | FlashIpmiHashDataTest() = default; |
| 15 | |
| 16 | void SetUp() override |
| 17 | { |
| 18 | name = std::tmpnam(nullptr); |
| 19 | name2 = std::tmpnam(nullptr); |
| 20 | } |
| 21 | void TearDown() override |
| 22 | { |
| 23 | (void)std::remove(name.c_str()); |
| 24 | (void)std::remove(name2.c_str()); |
| 25 | } |
| 26 | |
| 27 | std::string name; |
| 28 | std::string name2; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(FlashIpmiHashDataTest, CalledOutOfSequenceFails) |
| 32 | { |
| 33 | // Verify that there is sanity checking |
| 34 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 35 | |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame] | 36 | FlashUpdate updater(name, "", name2); |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 37 | EXPECT_FALSE(updater.hashFinish()); |
| 38 | |
| 39 | // Verify the file doesn't exist. |
| 40 | auto file = std::fopen(name2.c_str(), "r"); |
| 41 | EXPECT_FALSE(file); |
| 42 | } |
| 43 | |
| 44 | TEST_F(FlashIpmiHashDataTest, CalledInSequenceSucceeds) |
| 45 | { |
| 46 | // Verify that under normal usage it closes the file. |
| 47 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 48 | |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame] | 49 | FlashUpdate updater(name, "", name2); |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 50 | EXPECT_TRUE(updater.start(THIRTYTWO_MIB)); |
| 51 | EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB)); |
| 52 | EXPECT_TRUE(updater.hashFinish()); |
| 53 | |
| 54 | // Verify we can open the file, so we know it didn't get deleted. |
| 55 | auto file = std::fopen(name2.c_str(), "r"); |
| 56 | EXPECT_TRUE(file); |
| 57 | std::fclose(file); |
| 58 | } |
| 59 | |
| 60 | TEST_F(FlashIpmiHashDataTest, CalledTwiceFails) |
| 61 | { |
| 62 | // Verify that under normal usage it closes the file, and therefore cannot |
| 63 | // be closed twice. |
| 64 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 65 | |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame] | 66 | FlashUpdate updater(name, "", name2); |
Patrick Venture | d5f590f | 2018-08-07 14:18:09 -0700 | [diff] [blame] | 67 | EXPECT_TRUE(updater.start(THIRTYTWO_MIB)); |
| 68 | EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB)); |
| 69 | EXPECT_TRUE(updater.hashFinish()); |
| 70 | |
| 71 | EXPECT_FALSE(updater.hashFinish()); |
| 72 | |
| 73 | // Verify we can open the file, so we know it didn't get deleted. |
| 74 | auto file = std::fopen(name2.c_str(), "r"); |
| 75 | EXPECT_TRUE(file); |
| 76 | std::fclose(file); |
| 77 | } |