blob: 706a4c34d84fed4657e689db8c4cfe1b9ca00056 [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
Brandon Kim6749ba12019-09-19 13:31:37 -070090 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
Patrick Venture1f09d412019-06-19 16:01:06 -070091}
92
93class UpdaterTest : public ::testing::Test
94{
95 protected:
96 ipmiblob::BlobInterfaceMock blobMock;
97 std::uint16_t session = 0xbeef;
Brandon Kim6749ba12019-09-19 13:31:37 -070098 bool defaultIgnore = false;
Patrick Venture1f09d412019-06-19 16:01:06 -070099};
100
Patrick Venture1f09d412019-06-19 16:01:06 -0700101TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
102{
103 const std::string image = "image.bin";
104 const std::string signature = "signature.bin";
105 UpdateHandlerMock handler;
106
107 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
108 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
109 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700110 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700111 .WillOnce(Return(true));
Brandon Kim6749ba12019-09-19 13:31:37 -0700112 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700113 .WillOnce(Return(true));
114
Brandon Kim6749ba12019-09-19 13:31:37 -0700115 updaterMain(&handler, image, signature, "static", defaultIgnore);
116}
117
118TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate)
119{
120 const std::string image = "image.bin";
121 const std::string signature = "signature.bin";
122 UpdateHandlerMock handler;
123 bool updateIgnore = true;
124
125 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
126 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
127 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
128 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
129 .WillOnce(Return(true));
130 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore))
131 .WillOnce(Return(true));
132
133 updaterMain(&handler, image, signature, "static", updateIgnore);
Patrick Venture1f09d412019-06-19 16:01:06 -0700134}
Patrick Venture9b534f02018-12-13 16:10:02 -0800135
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700136TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
137{
138 const std::string image = "image.bin";
139 const std::string signature = "signature.bin";
140 UpdateHandlerMock handler;
141
142 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
143 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
144 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700145 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700146 .WillOnce(Return(false));
147 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
148
Brandon Kim6749ba12019-09-19 13:31:37 -0700149 EXPECT_THROW(
150 updaterMain(&handler, image, signature, "static", defaultIgnore),
151 ToolException);
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700152}
153
Patrick Venture9b534f02018-12-13 16:10:02 -0800154} // namespace host_tool