blob: e54e613939738d799872b0a2d43a290502858315 [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 Ventureaa32a362018-12-13 10:52:33 -08003#include "updater.hpp"
Patrick Venture55646de2019-05-16 10:06:26 -07004#include "updater_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07005#include "util.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08006
Patrick Venture664c5bc2019-03-07 08:09:45 -08007#include <blobs-ipmid/blobs.hpp>
8#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Ventureaa32a362018-12-13 10:52:33 -08009#include <string>
10
11#include <gtest/gtest.h>
12
Patrick Venture9b534f02018-12-13 16:10:02 -080013namespace host_tool
14{
15
Patrick Venture7dcca5d2019-05-15 12:32:33 -070016using ::testing::_;
Patrick Ventureaa32a362018-12-13 10:52:33 -080017using ::testing::Eq;
18using ::testing::Return;
Patrick Ventureb58f5612019-05-07 09:22:07 -070019using ::testing::TypedEq;
Patrick Ventureaa32a362018-12-13 10:52:33 -080020
Patrick Venture1f09d412019-06-19 16:01:06 -070021class UpdateHandlerTest : public ::testing::Test
Patrick Venture55646de2019-05-16 10:06:26 -070022{
Patrick Venture1f09d412019-06-19 16:01:06 -070023 protected:
24 const std::uint16_t session = 0xbeef;
25
Patrick Venture55646de2019-05-16 10:06:26 -070026 DataInterfaceMock handlerMock;
27 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture1f09d412019-06-19 16:01:06 -070028 UpdateHandler updater{&blobMock, &handlerMock};
29};
Patrick Venture55646de2019-05-16 10:06:26 -070030
Patrick Venture1f09d412019-06-19 16:01:06 -070031TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
32{
33 ipmiblob::StatResponse statObj = {};
Patrick Venture1d5a31c2019-05-20 11:38:22 -070034 statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi |
35 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc;
Patrick Venture55646de2019-05-16 10:06:26 -070036
37 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070038 .WillOnce(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070039 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
40 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(
41 ipmi_flash::staticLayoutBlobId)))
Patrick Venture55646de2019-05-16 10:06:26 -070042 .WillOnce(Return(statObj));
43
44 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture1d5a31c2019-05-20 11:38:22 -070045 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070046
Patrick Venture1d5a31c2019-05-20 11:38:22 -070047 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
Patrick Venture55646de2019-05-16 10:06:26 -070048}
49
Patrick Venture1f09d412019-06-19 16:01:06 -070050TEST_F(UpdateHandlerTest, SendFileSuccess)
Patrick Venture55646de2019-05-16 10:06:26 -070051{
52 /* Call sendFile to verify it does what we expect. */
Patrick Venture55646de2019-05-16 10:06:26 -070053 std::string firmwareImage = "image.bin";
54
55 std::uint16_t supported =
56 static_cast<std::uint16_t>(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070057 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) |
Patrick Venture55646de2019-05-16 10:06:26 -070058 static_cast<std::uint16_t>(blobs::OpenFlags::write);
Patrick Venture55646de2019-05-16 10:06:26 -070059
60 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture1d5a31c2019-05-20 11:38:22 -070061 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070062
Patrick Venture1f09d412019-06-19 16:01:06 -070063 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
Patrick Venture55646de2019-05-16 10:06:26 -070064 .WillOnce(Return(session));
65
Patrick Venture1f09d412019-06-19 16:01:06 -070066 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
Patrick Venture55646de2019-05-16 10:06:26 -070067 .WillOnce(Return(true));
68
69 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
70
Patrick Venture1d5a31c2019-05-20 11:38:22 -070071 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070072}
73
Patrick Venture1f09d412019-06-19 16:01:06 -070074TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
Patrick Ventureaa32a362018-12-13 10:52:33 -080075{
Patrick Venture1f09d412019-06-19 16:01:06 -070076 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
Patrick Ventureaa32a362018-12-13 10:52:33 -080077 .WillOnce(Return(session));
Patrick Venture55646de2019-05-16 10:06:26 -070078 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture1f09d412019-06-19 16:01:06 -070079 ipmiblob::StatResponse verificationResponse = {};
80 /* the other details of the response are ignored, and should be. */
81 verificationResponse.metadata.push_back(
82 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Venture7dcca5d2019-05-15 12:32:33 -070083
Patrick Venture1f09d412019-06-19 16:01:06 -070084 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
85 .WillOnce(Return(verificationResponse));
86 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
87
88 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId));
89}
90
91class UpdaterTest : public ::testing::Test
92{
93 protected:
94 ipmiblob::BlobInterfaceMock blobMock;
95 std::uint16_t session = 0xbeef;
96};
97
98TEST_F(UpdaterTest, PollStatusReturnsAfterSuccess)
99{
100 ipmiblob::StatResponse verificationResponse = {};
101 /* the other details of the response are ignored, and should be. */
102 verificationResponse.metadata.push_back(
103 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700104
105 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
106 .WillOnce(Return(verificationResponse));
107
Patrick Venture1f09d412019-06-19 16:01:06 -0700108 EXPECT_TRUE(pollStatus(session, &blobMock));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800109}
Patrick Venture1f09d412019-06-19 16:01:06 -0700110
111TEST_F(UpdaterTest, PollStatusReturnsAfterFailure)
112{
113 ipmiblob::StatResponse verificationResponse = {};
114 /* the other details of the response are ignored, and should be. */
115 verificationResponse.metadata.push_back(
116 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::failed));
117
118 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
119 .WillOnce(Return(verificationResponse));
120
121 EXPECT_FALSE(pollStatus(session, &blobMock));
122}
123
124TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
125{
126 const std::string image = "image.bin";
127 const std::string signature = "signature.bin";
128 UpdateHandlerMock handler;
129
130 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
131 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
132 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
133 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
134 .WillOnce(Return(true));
135 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId))
136 .WillOnce(Return(true));
137
138 updaterMain(&handler, image, signature);
139}
Patrick Venture9b534f02018-12-13 16:10:02 -0800140
141} // namespace host_tool