blob: b3d023f7ac6529eb22a6f0cc9d25ef276e37eced [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
12namespace blobs
13{
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(
57 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
58
59 EXPECT_FALSE(handler->commit(0, {}));
60}
61
Patrick Venture481cd4a2019-05-17 17:53:11 -070062TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile)
Patrick Ventureffcc5502018-11-16 12:32:38 -080063{
64 /* Verify the hash file returns failure on this command. It's a fairly
65 * artificial test.
66 */
Patrick Ventureffcc5502018-11-16 12:32:38 -080067
Patrick Venture3ecb3502019-05-17 11:03:51 -070068 /* Verify it doesn't get called by using StrictMock. */
69 std::unique_ptr<VerificationInterface> verifyMock =
70 std::make_unique<StrictMock<VerificationMock>>();
Patrick Venture4eb55952018-11-16 15:36:24 -080071
72 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070073 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -080074
Patrick Venture7dad86f2019-05-17 08:52:20 -070075 EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Ventureffcc5502018-11-16 12:32:38 -080076
77 EXPECT_TRUE(handler->open(
78 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070079 hashBlobId));
Patrick Ventureffcc5502018-11-16 12:32:38 -080080
81 EXPECT_FALSE(handler->commit(0, {}));
82}
83
Patrick Venture481cd4a2019-05-17 17:53:11 -070084TEST_F(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob)
Patrick Ventureffcc5502018-11-16 12:32:38 -080085{
86 /* Verify the verify blob lets you call this command, and it returns
87 * success.
88 */
Patrick Venture3ecb3502019-05-17 11:03:51 -070089 auto verifyMock = CreateVerifyMock();
90 auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
Patrick Venture4eb55952018-11-16 15:36:24 -080091
92 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070093 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -080094
Patrick Venture7dad86f2019-05-17 08:52:20 -070095 EXPECT_TRUE(handler->open(0, OpenFlags::write, verifyBlobId));
Patrick Ventureffcc5502018-11-16 12:32:38 -080096
Patrick Venture3ecb3502019-05-17 11:03:51 -070097 EXPECT_CALL(*verifyMockPtr, triggerVerification())
98 .WillRepeatedly(Return(true));
99
Patrick Ventureffcc5502018-11-16 12:32:38 -0800100 EXPECT_TRUE(handler->commit(0, {}));
101}
102
Patrick Venture481cd4a2019-05-17 17:53:11 -0700103TEST_F(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800104{
Patrick Venturebe198702019-05-15 09:46:02 -0700105 /* Verify you cannot call the commit() command once verification is
106 * started, after which it will just return true.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800107 */
Patrick Venture3ecb3502019-05-17 11:03:51 -0700108 auto verifyMock = CreateVerifyMock();
109 auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
Patrick Venture4eb55952018-11-16 15:36:24 -0800110
111 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700112 blobs, data, std::move(verifyMock));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800113
Patrick Venture7dad86f2019-05-17 08:52:20 -0700114 EXPECT_TRUE(handler->open(0, OpenFlags::write, verifyBlobId));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800115
Patrick Venture3ecb3502019-05-17 11:03:51 -0700116 EXPECT_CALL(*verifyMockPtr, triggerVerification())
117 .WillRepeatedly(Return(true));
Patrick Venturecabc1172018-11-16 16:14:26 -0800118
Patrick Ventureffcc5502018-11-16 12:32:38 -0800119 EXPECT_TRUE(handler->commit(0, {}));
Patrick Venturebe198702019-05-15 09:46:02 -0700120 EXPECT_TRUE(handler->commit(0, {}));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800121}
122
123} // namespace blobs