blob: 5f2f82e3462afd6d0eeea3281a15d9749e7ba70e [file] [log] [blame]
#include "data_mock.hpp"
#include "firmware_handler.hpp"
#include "image_mock.hpp"
#include "util.hpp"
#include "verification_mock.hpp"
#include <memory>
#include <vector>
#include <gtest/gtest.h>
namespace ipmi_flash
{
using ::testing::_;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::StrictMock;
class FirmwareHandlerCommitTest : public ::testing::Test
{
protected:
ImageHandlerMock imageMock1, imageMock2;
std::vector<HandlerPack> blobs;
std::vector<DataHandlerPack> data;
void SetUp() override
{
blobs = {
{hashBlobId, &imageMock1},
{"asdf", &imageMock2},
};
data = {
{FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
};
}
};
TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage)
{
/* Verify the flash image returns failure on this command. It's a fairly
* artificial test.
*/
/* Verify it doesn't get called by using StrictMock. */
std::unique_ptr<VerificationInterface> verifyMock =
std::make_unique<StrictMock<VerificationMock>>();
auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
blobs, data, std::move(verifyMock));
EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
EXPECT_TRUE(handler->open(
0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
"asdf"));
EXPECT_FALSE(handler->commit(0, {}));
}
TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile)
{
/* Verify the hash file returns failure on this command. It's a fairly
* artificial test.
*/
/* Verify it doesn't get called by using StrictMock. */
std::unique_ptr<VerificationInterface> verifyMock =
std::make_unique<StrictMock<VerificationMock>>();
auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
blobs, data, std::move(verifyMock));
EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
EXPECT_TRUE(handler->open(
0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
hashBlobId));
EXPECT_FALSE(handler->commit(0, {}));
}
TEST_F(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob)
{
/* Verify the verify blob lets you call this command, and it returns
* success.
*/
auto verifyMock = CreateVerifyMock();
auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
blobs, data, std::move(verifyMock));
EXPECT_TRUE(handler->open(0, blobs::OpenFlags::write, verifyBlobId));
EXPECT_CALL(*verifyMockPtr, triggerVerification())
.WillRepeatedly(Return(true));
EXPECT_TRUE(handler->commit(0, {}));
}
TEST_F(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect)
{
/* Verify you cannot call the commit() command once verification is
* started, after which it will just return true.
*/
auto verifyMock = CreateVerifyMock();
auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
blobs, data, std::move(verifyMock));
EXPECT_TRUE(handler->open(0, blobs::OpenFlags::write, verifyBlobId));
EXPECT_CALL(*verifyMockPtr, triggerVerification())
.WillRepeatedly(Return(true));
EXPECT_TRUE(handler->commit(0, {}));
EXPECT_TRUE(handler->commit(0, {}));
}
} // namespace ipmi_flash