blob: a2ba2df10d7eb11edf2f1a82744382040f6174bd [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
Patrick Venture1f09d412019-06-19 16:01:06 -070099TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
100{
101 const std::string image = "image.bin";
102 const std::string signature = "signature.bin";
103 UpdateHandlerMock handler;
104
105 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
106 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
107 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
108 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
109 .WillOnce(Return(true));
110 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId))
111 .WillOnce(Return(true));
112
113 updaterMain(&handler, image, signature);
114}
Patrick Venture9b534f02018-12-13 16:10:02 -0800115
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700116TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
117{
118 const std::string image = "image.bin";
119 const std::string signature = "signature.bin";
120 UpdateHandlerMock handler;
121
122 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
123 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
124 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
125 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
126 .WillOnce(Return(false));
127 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
128
129 EXPECT_THROW(updaterMain(&handler, image, signature), ToolException);
130}
131
Patrick Venture9b534f02018-12-13 16:10:02 -0800132} // namespace host_tool