blob: b9f80adf53331c6794c35bfe9f3ad6f2965b904b [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{
Patrick Venture55646de2019-05-16 10:06:26 -070034 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070035 .WillOnce(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070036 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
Patrick Venture55646de2019-05-16 10:06:26 -070037
Patrick Venture1d5a31c2019-05-20 11:38:22 -070038 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
Patrick Venture55646de2019-05-16 10:06:26 -070039}
40
Patrick Venture1f09d412019-06-19 16:01:06 -070041TEST_F(UpdateHandlerTest, SendFileSuccess)
Patrick Venture55646de2019-05-16 10:06:26 -070042{
43 /* Call sendFile to verify it does what we expect. */
Patrick Venture55646de2019-05-16 10:06:26 -070044 std::string firmwareImage = "image.bin";
45
46 std::uint16_t supported =
47 static_cast<std::uint16_t>(
Patrick Venture84778b82019-06-26 20:11:09 -070048 ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
49 static_cast<std::uint16_t>(
50 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
Patrick Venture55646de2019-05-16 10:06:26 -070051
52 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture84778b82019-06-26 20:11:09 -070053 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070054
Patrick Venture1f09d412019-06-19 16:01:06 -070055 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
Patrick Venture55646de2019-05-16 10:06:26 -070056 .WillOnce(Return(session));
57
Patrick Venture1f09d412019-06-19 16:01:06 -070058 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
Patrick Venture55646de2019-05-16 10:06:26 -070059 .WillOnce(Return(true));
60
61 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
62
Patrick Venture1d5a31c2019-05-20 11:38:22 -070063 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070064}
65
Patrick Venture1f09d412019-06-19 16:01:06 -070066TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
Patrick Ventureaa32a362018-12-13 10:52:33 -080067{
Patrick Venture1f09d412019-06-19 16:01:06 -070068 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
Patrick Ventureaa32a362018-12-13 10:52:33 -080069 .WillOnce(Return(session));
Patrick Venture55646de2019-05-16 10:06:26 -070070 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture1f09d412019-06-19 16:01:06 -070071 ipmiblob::StatResponse verificationResponse = {};
72 /* the other details of the response are ignored, and should be. */
73 verificationResponse.metadata.push_back(
74 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Venture7dcca5d2019-05-15 12:32:33 -070075
Patrick Venture1f09d412019-06-19 16:01:06 -070076 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
77 .WillOnce(Return(verificationResponse));
78 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
79
Brandon Kim6749ba12019-09-19 13:31:37 -070080 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
Patrick Venture1f09d412019-06-19 16:01:06 -070081}
82
83class UpdaterTest : public ::testing::Test
84{
85 protected:
86 ipmiblob::BlobInterfaceMock blobMock;
87 std::uint16_t session = 0xbeef;
Brandon Kim6749ba12019-09-19 13:31:37 -070088 bool defaultIgnore = false;
Patrick Venture1f09d412019-06-19 16:01:06 -070089};
90
Patrick Venture1f09d412019-06-19 16:01:06 -070091TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
92{
93 const std::string image = "image.bin";
94 const std::string signature = "signature.bin";
95 UpdateHandlerMock handler;
96
97 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
98 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
99 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700100 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700101 .WillOnce(Return(true));
Brandon Kim6749ba12019-09-19 13:31:37 -0700102 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700103 .WillOnce(Return(true));
104
Brandon Kim6749ba12019-09-19 13:31:37 -0700105 updaterMain(&handler, image, signature, "static", defaultIgnore);
106}
107
108TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate)
109{
110 const std::string image = "image.bin";
111 const std::string signature = "signature.bin";
112 UpdateHandlerMock handler;
113 bool updateIgnore = true;
114
115 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
116 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
117 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
118 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
119 .WillOnce(Return(true));
120 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore))
121 .WillOnce(Return(true));
122
123 updaterMain(&handler, image, signature, "static", updateIgnore);
Patrick Venture1f09d412019-06-19 16:01:06 -0700124}
Patrick Venture9b534f02018-12-13 16:10:02 -0800125
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700126TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
127{
128 const std::string image = "image.bin";
129 const std::string signature = "signature.bin";
130 UpdateHandlerMock handler;
131
132 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
133 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
134 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700135 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700136 .WillOnce(Return(false));
137 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
138
Brandon Kim6749ba12019-09-19 13:31:37 -0700139 EXPECT_THROW(
140 updaterMain(&handler, image, signature, "static", defaultIgnore),
141 ToolException);
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700142}
143
Patrick Venture9b534f02018-12-13 16:10:02 -0800144} // namespace host_tool