Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -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 FlashIpmiFlashDataTest : public ::testing::Test |
| 12 | { |
| 13 | protected: |
| 14 | FlashIpmiFlashDataTest() = default; |
| 15 | |
| 16 | void SetUp() override |
| 17 | { |
| 18 | name = std::tmpnam(nullptr); |
| 19 | } |
| 20 | void TearDown() override |
| 21 | { |
| 22 | (void)std::remove(name.c_str()); |
| 23 | } |
| 24 | |
| 25 | std::string name; |
| 26 | }; |
| 27 | |
| 28 | TEST_F(FlashIpmiFlashDataTest, CalledOutOfSequenceFails) |
| 29 | { |
| 30 | // Verify that there is sanity checking. |
| 31 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 32 | |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame^] | 33 | FlashUpdate updater(name, ""); |
Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -0700 | [diff] [blame] | 34 | EXPECT_FALSE(updater.flashData(0, bytes)); |
| 35 | |
| 36 | // Verify the file doesn't exist. |
| 37 | auto file = std::fopen(name.c_str(), "r"); |
| 38 | EXPECT_FALSE(file); |
| 39 | } |
| 40 | |
| 41 | TEST_F(FlashIpmiFlashDataTest, CalledWithDataSucceeds) |
| 42 | { |
| 43 | // Verify that under normal usage it writes the bytes. |
| 44 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 45 | |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame^] | 46 | FlashUpdate updater(name, ""); |
Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -0700 | [diff] [blame] | 47 | updater.start(THIRTYTWO_MIB); |
| 48 | EXPECT_TRUE(updater.flashData(0, bytes)); |
| 49 | |
| 50 | auto file = std::fopen(name.c_str(), "r"); |
| 51 | EXPECT_TRUE(file); |
| 52 | |
| 53 | uint8_t buffer[2]; |
| 54 | auto read = std::fread(buffer, 1, bytes.size(), file); |
| 55 | EXPECT_EQ(read, bytes.size()); |
| 56 | EXPECT_EQ(0, std::memcmp(buffer, bytes.data(), bytes.size())); |
| 57 | std::fclose(file); |
| 58 | } |
| 59 | |
| 60 | TEST_F(FlashIpmiFlashDataTest, CalledNonZeroOffsetSucceeds) |
| 61 | { |
| 62 | // Skipping bytes in POSIX can create a sparse file. In this case, |
| 63 | // let's allow the behavior. If we'd rather, we can have writeBlock |
| 64 | // check that the offset is where we expect. |
| 65 | |
| 66 | std::vector<uint8_t> bytes = {0xaa, 0x55}; |
| 67 | |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame^] | 68 | FlashUpdate updater(name, ""); |
Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -0700 | [diff] [blame] | 69 | updater.start(THIRTYTWO_MIB); |
| 70 | EXPECT_TRUE(updater.flashData(2, bytes)); |
| 71 | |
| 72 | auto file = std::fopen(name.c_str(), "r"); |
| 73 | EXPECT_TRUE(file); |
| 74 | |
| 75 | uint8_t buffer[4]; |
| 76 | auto read = std::fread(buffer, 1, sizeof(buffer), file); |
| 77 | EXPECT_EQ(read, sizeof(buffer)); |
| 78 | EXPECT_EQ(0, std::memcmp(&buffer[2], bytes.data(), bytes.size())); |
| 79 | std::fclose(file); |
| 80 | } |