blob: 0e9a2170a3d042be8cefccc936e708a7d90ad28e [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 Venture9b37b092020-05-28 20:58:57 -070010
Patrick Ventureaa32a362018-12-13 10:52:33 -080011#include <string>
12
13#include <gtest/gtest.h>
14
Patrick Venture9b534f02018-12-13 16:10:02 -080015namespace host_tool
16{
17
Patrick Venture7dcca5d2019-05-15 12:32:33 -070018using ::testing::_;
Patrick Ventureaa32a362018-12-13 10:52:33 -080019using ::testing::Eq;
20using ::testing::Return;
Patrick Ventureb58f5612019-05-07 09:22:07 -070021using ::testing::TypedEq;
Patrick Ventureaa32a362018-12-13 10:52:33 -080022
Patrick Venture1f09d412019-06-19 16:01:06 -070023class UpdateHandlerTest : public ::testing::Test
Patrick Venture55646de2019-05-16 10:06:26 -070024{
Patrick Venture1f09d412019-06-19 16:01:06 -070025 protected:
26 const std::uint16_t session = 0xbeef;
27
Patrick Venture55646de2019-05-16 10:06:26 -070028 DataInterfaceMock handlerMock;
29 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture1f09d412019-06-19 16:01:06 -070030 UpdateHandler updater{&blobMock, &handlerMock};
31};
Patrick Venture55646de2019-05-16 10:06:26 -070032
Patrick Venture1f09d412019-06-19 16:01:06 -070033TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
34{
Patrick Venture55646de2019-05-16 10:06:26 -070035 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070036 .WillOnce(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070037 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
Patrick Venture55646de2019-05-16 10:06:26 -070038
Patrick Venture1d5a31c2019-05-20 11:38:22 -070039 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
Patrick Venture55646de2019-05-16 10:06:26 -070040}
41
Patrick Venture1f09d412019-06-19 16:01:06 -070042TEST_F(UpdateHandlerTest, SendFileSuccess)
Patrick Venture55646de2019-05-16 10:06:26 -070043{
44 /* Call sendFile to verify it does what we expect. */
Patrick Venture55646de2019-05-16 10:06:26 -070045 std::string firmwareImage = "image.bin";
46
47 std::uint16_t supported =
48 static_cast<std::uint16_t>(
Patrick Venture84778b82019-06-26 20:11:09 -070049 ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
50 static_cast<std::uint16_t>(
51 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
Patrick Venture55646de2019-05-16 10:06:26 -070052
53 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture84778b82019-06-26 20:11:09 -070054 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070055
Patrick Venture1f09d412019-06-19 16:01:06 -070056 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
Patrick Venture55646de2019-05-16 10:06:26 -070057 .WillOnce(Return(session));
58
Patrick Venture1f09d412019-06-19 16:01:06 -070059 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
Patrick Venture55646de2019-05-16 10:06:26 -070060 .WillOnce(Return(true));
61
62 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
63
Patrick Venture1d5a31c2019-05-20 11:38:22 -070064 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070065}
66
Patrick Venture1f09d412019-06-19 16:01:06 -070067TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
Patrick Ventureaa32a362018-12-13 10:52:33 -080068{
Patrick Venture1f09d412019-06-19 16:01:06 -070069 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
Patrick Ventureaa32a362018-12-13 10:52:33 -080070 .WillOnce(Return(session));
Patrick Venture55646de2019-05-16 10:06:26 -070071 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture1f09d412019-06-19 16:01:06 -070072 ipmiblob::StatResponse verificationResponse = {};
73 /* the other details of the response are ignored, and should be. */
74 verificationResponse.metadata.push_back(
75 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Venture7dcca5d2019-05-15 12:32:33 -070076
Patrick Venture1f09d412019-06-19 16:01:06 -070077 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
78 .WillOnce(Return(verificationResponse));
79 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
80
Brandon Kim6749ba12019-09-19 13:31:37 -070081 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
Patrick Venture1f09d412019-06-19 16:01:06 -070082}
83
84class UpdaterTest : public ::testing::Test
85{
86 protected:
87 ipmiblob::BlobInterfaceMock blobMock;
88 std::uint16_t session = 0xbeef;
Brandon Kim6749ba12019-09-19 13:31:37 -070089 bool defaultIgnore = false;
Patrick Venture1f09d412019-06-19 16:01:06 -070090};
91
Patrick Venture1f09d412019-06-19 16:01:06 -070092TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
93{
94 const std::string image = "image.bin";
95 const std::string signature = "signature.bin";
96 UpdateHandlerMock handler;
97
98 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
99 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
100 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700101 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700102 .WillOnce(Return(true));
Brandon Kim6749ba12019-09-19 13:31:37 -0700103 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700104 .WillOnce(Return(true));
105
Brandon Kim6749ba12019-09-19 13:31:37 -0700106 updaterMain(&handler, image, signature, "static", defaultIgnore);
107}
108
109TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate)
110{
111 const std::string image = "image.bin";
112 const std::string signature = "signature.bin";
113 UpdateHandlerMock handler;
114 bool updateIgnore = true;
115
116 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
117 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
118 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
119 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
120 .WillOnce(Return(true));
121 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore))
122 .WillOnce(Return(true));
123
124 updaterMain(&handler, image, signature, "static", updateIgnore);
Patrick Venture1f09d412019-06-19 16:01:06 -0700125}
Patrick Venture9b534f02018-12-13 16:10:02 -0800126
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700127TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
128{
129 const std::string image = "image.bin";
130 const std::string signature = "signature.bin";
131 UpdateHandlerMock handler;
132
133 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
134 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
135 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700136 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700137 .WillOnce(Return(false));
138 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
139
Brandon Kim6749ba12019-09-19 13:31:37 -0700140 EXPECT_THROW(
141 updaterMain(&handler, image, signature, "static", defaultIgnore),
142 ToolException);
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700143}
144
Patrick Venture9b534f02018-12-13 16:10:02 -0800145} // namespace host_tool