blob: 5f2f82e3462afd6d0eeea3281a15d9749e7ba70e [file] [log] [blame]
Patrick Ventureffcc5502018-11-16 12:32:38 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Ventureffcc5502018-11-16 12:32:38 -08006
Patrick Venture481cd4a2019-05-17 17:53:11 -07007#include <memory>
Patrick Ventureffcc5502018-11-16 12:32:38 -08008#include <vector>
9
10#include <gtest/gtest.h>
11
Patrick Venture1d5a31c2019-05-20 11:38:22 -070012namespace ipmi_flash
Patrick Ventureffcc5502018-11-16 12:32:38 -080013{
Patrick Venturecabc1172018-11-16 16:14:26 -080014using ::testing::_;
15using ::testing::IsNull;
16using ::testing::NotNull;
Patrick Ventureffcc5502018-11-16 12:32:38 -080017using ::testing::Return;
Patrick Venturecabc1172018-11-16 16:14:26 -080018using ::testing::StrEq;
19using ::testing::StrictMock;
Patrick Ventureffcc5502018-11-16 12:32:38 -080020
Patrick Venture481cd4a2019-05-17 17:53:11 -070021class FirmwareHandlerCommitTest : public ::testing::Test
22{
23 protected:
24 ImageHandlerMock imageMock1, imageMock2;
25 std::vector<HandlerPack> blobs;
26 std::vector<DataHandlerPack> data;
27
28 void SetUp() override
29 {
30 blobs = {
31 {hashBlobId, &imageMock1},
32 {"asdf", &imageMock2},
33 };
34
35 data = {
36 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
37 };
38 }
39};
40
41TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage)
Patrick Ventureffcc5502018-11-16 12:32:38 -080042{
43 /* Verify the flash image returns failure on this command. It's a fairly
44 * artificial test.
45 */
Patrick Ventureffcc5502018-11-16 12:32:38 -080046
Patrick Venture3ecb3502019-05-17 11:03:51 -070047 /* Verify it doesn't get called by using StrictMock. */
48 std::unique_ptr<VerificationInterface> verifyMock =
49 std::make_unique<StrictMock<VerificationMock>>();
Patrick Venture4eb55952018-11-16 15:36:24 -080050
51 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070052 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -080053
54 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
55
56 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070057 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
58 "asdf"));
Patrick Ventureffcc5502018-11-16 12:32:38 -080059
60 EXPECT_FALSE(handler->commit(0, {}));
61}
62
Patrick Venture481cd4a2019-05-17 17:53:11 -070063TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile)
Patrick Ventureffcc5502018-11-16 12:32:38 -080064{
65 /* Verify the hash file returns failure on this command. It's a fairly
66 * artificial test.
67 */
Patrick Ventureffcc5502018-11-16 12:32:38 -080068
Patrick Venture3ecb3502019-05-17 11:03:51 -070069 /* Verify it doesn't get called by using StrictMock. */
70 std::unique_ptr<VerificationInterface> verifyMock =
71 std::make_unique<StrictMock<VerificationMock>>();
Patrick Venture4eb55952018-11-16 15:36:24 -080072
73 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070074 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -080075
Patrick Venture7dad86f2019-05-17 08:52:20 -070076 EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Ventureffcc5502018-11-16 12:32:38 -080077
78 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070079 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070080 hashBlobId));
Patrick Ventureffcc5502018-11-16 12:32:38 -080081
82 EXPECT_FALSE(handler->commit(0, {}));
83}
84
Patrick Venture481cd4a2019-05-17 17:53:11 -070085TEST_F(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob)
Patrick Ventureffcc5502018-11-16 12:32:38 -080086{
87 /* Verify the verify blob lets you call this command, and it returns
88 * success.
89 */
Patrick Venture3ecb3502019-05-17 11:03:51 -070090 auto verifyMock = CreateVerifyMock();
91 auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
Patrick Venture4eb55952018-11-16 15:36:24 -080092
93 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070094 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -080095
Patrick Venture1d5a31c2019-05-20 11:38:22 -070096 EXPECT_TRUE(handler->open(0, blobs::OpenFlags::write, verifyBlobId));
Patrick Ventureffcc5502018-11-16 12:32:38 -080097
Patrick Venture3ecb3502019-05-17 11:03:51 -070098 EXPECT_CALL(*verifyMockPtr, triggerVerification())
99 .WillRepeatedly(Return(true));
100
Patrick Ventureffcc5502018-11-16 12:32:38 -0800101 EXPECT_TRUE(handler->commit(0, {}));
102}
103
Patrick Venture481cd4a2019-05-17 17:53:11 -0700104TEST_F(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800105{
Patrick Venturebe198702019-05-15 09:46:02 -0700106 /* Verify you cannot call the commit() command once verification is
107 * started, after which it will just return true.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800108 */
Patrick Venture3ecb3502019-05-17 11:03:51 -0700109 auto verifyMock = CreateVerifyMock();
110 auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
Patrick Venture4eb55952018-11-16 15:36:24 -0800111
112 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700113 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800114
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700115 EXPECT_TRUE(handler->open(0, blobs::OpenFlags::write, verifyBlobId));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800116
Patrick Venture3ecb3502019-05-17 11:03:51 -0700117 EXPECT_CALL(*verifyMockPtr, triggerVerification())
118 .WillRepeatedly(Return(true));
Patrick Venturecabc1172018-11-16 16:14:26 -0800119
Patrick Ventureffcc5502018-11-16 12:32:38 -0800120 EXPECT_TRUE(handler->commit(0, {}));
Patrick Venturebe198702019-05-15 09:46:02 -0700121 EXPECT_TRUE(handler->commit(0, {}));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800122}
123
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700124} // namespace ipmi_flash