blob: 0c9e6b55a592bdcf19edc7ab43afc4e9b87b3804 [file] [log] [blame]
Patrick Ventureaa32a362018-12-13 10:52:33 -08001#include "data_interface_mock.hpp"
Patrick Venture84778b82019-06-26 20:11:09 -07002#include "flags.hpp"
Patrick Venture1f09d412019-06-19 16:01:06 -07003#include "status.hpp"
Patrick Venture5f2fcc42019-06-20 07:21:05 -07004#include "tool_errors.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08005#include "updater.hpp"
Patrick Venture55646de2019-05-16 10:06:26 -07006#include "updater_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07007#include "util.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08008
Patrick Venture664c5bc2019-03-07 08:09:45 -08009#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Ventureaa32a362018-12-13 10:52:33 -080010#include <string>
11
12#include <gtest/gtest.h>
13
Patrick Venture9b534f02018-12-13 16:10:02 -080014namespace host_tool
15{
16
Patrick Venture7dcca5d2019-05-15 12:32:33 -070017using ::testing::_;
Patrick Ventureaa32a362018-12-13 10:52:33 -080018using ::testing::Eq;
19using ::testing::Return;
Patrick Ventureb58f5612019-05-07 09:22:07 -070020using ::testing::TypedEq;
Patrick Ventureaa32a362018-12-13 10:52:33 -080021
Patrick Venture1f09d412019-06-19 16:01:06 -070022class UpdateHandlerTest : public ::testing::Test
Patrick Venture55646de2019-05-16 10:06:26 -070023{
Patrick Venture1f09d412019-06-19 16:01:06 -070024 protected:
25 const std::uint16_t session = 0xbeef;
26
Patrick Venture55646de2019-05-16 10:06:26 -070027 DataInterfaceMock handlerMock;
28 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture1f09d412019-06-19 16:01:06 -070029 UpdateHandler updater{&blobMock, &handlerMock};
30};
Patrick Venture55646de2019-05-16 10:06:26 -070031
Patrick Venture1f09d412019-06-19 16:01:06 -070032TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
33{
34 ipmiblob::StatResponse statObj = {};
Patrick Venture84778b82019-06-26 20:11:09 -070035 statObj.blob_state = ipmi_flash::FirmwareFlags::UpdateFlags::ipmi |
36 ipmi_flash::FirmwareFlags::UpdateFlags::lpc;
Patrick Venture55646de2019-05-16 10:06:26 -070037
38 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070039 .WillOnce(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070040 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
41 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(
42 ipmi_flash::staticLayoutBlobId)))
Patrick Venture55646de2019-05-16 10:06:26 -070043 .WillOnce(Return(statObj));
44
45 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture84778b82019-06-26 20:11:09 -070046 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070047
Patrick Venture1d5a31c2019-05-20 11:38:22 -070048 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
Patrick Venture55646de2019-05-16 10:06:26 -070049}
50
Patrick Venture1f09d412019-06-19 16:01:06 -070051TEST_F(UpdateHandlerTest, SendFileSuccess)
Patrick Venture55646de2019-05-16 10:06:26 -070052{
53 /* Call sendFile to verify it does what we expect. */
Patrick Venture55646de2019-05-16 10:06:26 -070054 std::string firmwareImage = "image.bin";
55
56 std::uint16_t supported =
57 static_cast<std::uint16_t>(
Patrick Venture84778b82019-06-26 20:11:09 -070058 ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
59 static_cast<std::uint16_t>(
60 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
Patrick Venture55646de2019-05-16 10:06:26 -070061
62 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture84778b82019-06-26 20:11:09 -070063 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070064
Patrick Venture1f09d412019-06-19 16:01:06 -070065 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
Patrick Venture55646de2019-05-16 10:06:26 -070066 .WillOnce(Return(session));
67
Patrick Venture1f09d412019-06-19 16:01:06 -070068 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
Patrick Venture55646de2019-05-16 10:06:26 -070069 .WillOnce(Return(true));
70
71 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
72
Patrick Venture1d5a31c2019-05-20 11:38:22 -070073 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070074}
75
Patrick Venture1f09d412019-06-19 16:01:06 -070076TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
Patrick Ventureaa32a362018-12-13 10:52:33 -080077{
Patrick Venture1f09d412019-06-19 16:01:06 -070078 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
Patrick Ventureaa32a362018-12-13 10:52:33 -080079 .WillOnce(Return(session));
Patrick Venture55646de2019-05-16 10:06:26 -070080 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture1f09d412019-06-19 16:01:06 -070081 ipmiblob::StatResponse verificationResponse = {};
82 /* the other details of the response are ignored, and should be. */
83 verificationResponse.metadata.push_back(
84 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Venture7dcca5d2019-05-15 12:32:33 -070085
Patrick Venture1f09d412019-06-19 16:01:06 -070086 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
87 .WillOnce(Return(verificationResponse));
88 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
89
90 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId));
91}
92
93class UpdaterTest : public ::testing::Test
94{
95 protected:
96 ipmiblob::BlobInterfaceMock blobMock;
97 std::uint16_t session = 0xbeef;
98};
99
Patrick Venture1f09d412019-06-19 16:01:06 -0700100TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
101{
102 const std::string image = "image.bin";
103 const std::string signature = "signature.bin";
104 UpdateHandlerMock handler;
105
106 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
107 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
108 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
109 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
110 .WillOnce(Return(true));
111 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId))
112 .WillOnce(Return(true));
113
Patrick Venture9f937c42019-06-21 07:55:54 -0700114 updaterMain(&handler, image, signature, "static");
Patrick Venture1f09d412019-06-19 16:01:06 -0700115}
Patrick Venture9b534f02018-12-13 16:10:02 -0800116
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700117TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
118{
119 const std::string image = "image.bin";
120 const std::string signature = "signature.bin";
121 UpdateHandlerMock handler;
122
123 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
124 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
125 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
126 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
127 .WillOnce(Return(false));
128 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
129
Patrick Venture9f937c42019-06-21 07:55:54 -0700130 EXPECT_THROW(updaterMain(&handler, image, signature, "static"),
131 ToolException);
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700132}
133
Patrick Venture9b534f02018-12-13 16:10:02 -0800134} // namespace host_tool