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