blob: 5b3cb5b52cb4062aca5420178876636a04027327 [file] [log] [blame]
Patrick Ventureaa32a362018-12-13 10:52:33 -08001#include "data_interface_mock.hpp"
Patrick Venture1f09d412019-06-19 16:01:06 -07002#include "status.hpp"
Patrick Venture5f2fcc42019-06-20 07:21:05 -07003#include "tool_errors.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08004#include "updater.hpp"
Patrick Venture55646de2019-05-16 10:06:26 -07005#include "updater_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07006#include "util.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08007
Patrick Venture664c5bc2019-03-07 08:09:45 -08008#include <blobs-ipmid/blobs.hpp>
9#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 Venture1d5a31c2019-05-20 11:38:22 -070035 statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi |
36 ipmi_flash::FirmwareBlobHandler::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 Venture1d5a31c2019-05-20 11:38:22 -070046 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::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 Venture1d5a31c2019-05-20 11:38:22 -070058 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) |
Patrick Venture55646de2019-05-16 10:06:26 -070059 static_cast<std::uint16_t>(blobs::OpenFlags::write);
Patrick Venture55646de2019-05-16 10:06:26 -070060
61 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture1d5a31c2019-05-20 11:38:22 -070062 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070063
Patrick Venture1f09d412019-06-19 16:01:06 -070064 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
Patrick Venture55646de2019-05-16 10:06:26 -070065 .WillOnce(Return(session));
66
Patrick Venture1f09d412019-06-19 16:01:06 -070067 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
Patrick Venture55646de2019-05-16 10:06:26 -070068 .WillOnce(Return(true));
69
70 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
71
Patrick Venture1d5a31c2019-05-20 11:38:22 -070072 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070073}
74
Patrick Venture1f09d412019-06-19 16:01:06 -070075TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
Patrick Ventureaa32a362018-12-13 10:52:33 -080076{
Patrick Venture1f09d412019-06-19 16:01:06 -070077 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
Patrick Ventureaa32a362018-12-13 10:52:33 -080078 .WillOnce(Return(session));
Patrick Venture55646de2019-05-16 10:06:26 -070079 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture1f09d412019-06-19 16:01:06 -070080 ipmiblob::StatResponse verificationResponse = {};
81 /* the other details of the response are ignored, and should be. */
82 verificationResponse.metadata.push_back(
83 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Venture7dcca5d2019-05-15 12:32:33 -070084
Patrick Venture1f09d412019-06-19 16:01:06 -070085 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
86 .WillOnce(Return(verificationResponse));
87 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
88
89 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId));
90}
91
92class UpdaterTest : public ::testing::Test
93{
94 protected:
95 ipmiblob::BlobInterfaceMock blobMock;
96 std::uint16_t session = 0xbeef;
97};
98
99TEST_F(UpdaterTest, PollStatusReturnsAfterSuccess)
100{
101 ipmiblob::StatResponse verificationResponse = {};
102 /* the other details of the response are ignored, and should be. */
103 verificationResponse.metadata.push_back(
104 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700105
106 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
107 .WillOnce(Return(verificationResponse));
108
Patrick Venture1f09d412019-06-19 16:01:06 -0700109 EXPECT_TRUE(pollStatus(session, &blobMock));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800110}
Patrick Venture1f09d412019-06-19 16:01:06 -0700111
112TEST_F(UpdaterTest, PollStatusReturnsAfterFailure)
113{
114 ipmiblob::StatResponse verificationResponse = {};
115 /* the other details of the response are ignored, and should be. */
116 verificationResponse.metadata.push_back(
117 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::failed));
118
119 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
120 .WillOnce(Return(verificationResponse));
121
122 EXPECT_FALSE(pollStatus(session, &blobMock));
123}
124
125TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
126{
127 const std::string image = "image.bin";
128 const std::string signature = "signature.bin";
129 UpdateHandlerMock handler;
130
131 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
132 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
133 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
134 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
135 .WillOnce(Return(true));
136 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId))
137 .WillOnce(Return(true));
138
139 updaterMain(&handler, image, signature);
140}
Patrick Venture9b534f02018-12-13 16:10:02 -0800141
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700142TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
143{
144 const std::string image = "image.bin";
145 const std::string signature = "signature.bin";
146 UpdateHandlerMock handler;
147
148 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
149 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
150 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
151 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
152 .WillOnce(Return(false));
153 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
154
155 EXPECT_THROW(updaterMain(&handler, image, signature), ToolException);
156}
157
Patrick Venture9b534f02018-12-13 16:10:02 -0800158} // namespace host_tool