blob: e160b29fe481ce2a77810e6472556245acf3acf3 [file] [log] [blame]
#include "flash-ipmi.hpp"
#include <cstdio>
#include <cstring>
#include <gtest/gtest.h>
#include <string>
#include <vector>
#define THIRTYTWO_MIB 33554432
class FlashIpmiFlashDataTest : public ::testing::Test
{
protected:
FlashIpmiFlashDataTest() = default;
void SetUp() override
{
name = std::tmpnam(nullptr);
}
void TearDown() override
{
(void)std::remove(name.c_str());
}
std::string name;
};
TEST_F(FlashIpmiFlashDataTest, CalledOutOfSequenceFails)
{
// Verify that there is sanity checking
std::vector<uint8_t> bytes = {0xaa, 0x55};
FlashUpdate updater(name, "");
EXPECT_FALSE(updater.flashFinish());
// Verify the file doesn't exist.
auto file = std::fopen(name.c_str(), "r");
EXPECT_FALSE(file);
}
TEST_F(FlashIpmiFlashDataTest, CalledInSequenceSucceeds)
{
// Verify that under normal usage it closes the file.
std::vector<uint8_t> bytes = {0xaa, 0x55};
FlashUpdate updater(name, "");
updater.start(THIRTYTWO_MIB);
EXPECT_TRUE(updater.flashFinish());
// Verify we can open the file, so we know it didn't get deleted.
auto file = std::fopen(name.c_str(), "r");
EXPECT_TRUE(file);
std::fclose(file);
}
TEST_F(FlashIpmiFlashDataTest, CalledTwiceFails)
{
// Verify that under normal usage it closes the file, and therefore cannot
// be closed twice.
std::vector<uint8_t> bytes = {0xaa, 0x55};
FlashUpdate updater(name, "");
updater.start(THIRTYTWO_MIB);
EXPECT_TRUE(updater.flashFinish());
EXPECT_FALSE(updater.flashFinish());
// Verify we can open the file, so we know it didn't get deleted.
auto file = std::fopen(name.c_str(), "r");
EXPECT_TRUE(file);
std::fclose(file);
}