xref: /openbmc/phosphor-ipmi-flash/tools/test/tools_updater_unittest.cpp (revision 42a44c281cce08be0ca6251955f4fb73d30c8ced)
1380832ccSPatrick Venture #include "data_interface_mock.hpp"
284778b8dSPatrick Venture #include "flags.hpp"
31f09d414SPatrick Venture #include "status.hpp"
45f2fcc4eSPatrick Venture #include "tool_errors.hpp"
5380832ccSPatrick Venture #include "updater.hpp"
6380832ccSPatrick Venture #include "updater_mock.hpp"
7380832ccSPatrick Venture #include "util.hpp"
8380832ccSPatrick Venture 
9328f520fSJie Yang #include <blobs-ipmid/blobs.hpp>
108cdf964dSPatrick Venture #include <ipmiblob/blob_errors.hpp>
11380832ccSPatrick Venture #include <ipmiblob/test/blob_interface_mock.hpp>
129b37b095SPatrick Venture 
13380832ccSPatrick Venture #include <string>
148cdf964dSPatrick Venture #include <vector>
15380832ccSPatrick Venture 
168cdf964dSPatrick Venture #include <gmock/gmock.h>
17380832ccSPatrick Venture #include <gtest/gtest.h>
18380832ccSPatrick Venture 
19380832ccSPatrick Venture namespace host_tool
20380832ccSPatrick Venture {
21380832ccSPatrick Venture 
22380832ccSPatrick Venture using ::testing::_;
23380832ccSPatrick Venture using ::testing::Return;
248cdf964dSPatrick Venture using ::testing::Throw;
25380832ccSPatrick Venture using ::testing::TypedEq;
26380832ccSPatrick Venture 
271f09d414SPatrick Venture class UpdateHandlerTest : public ::testing::Test
28380832ccSPatrick Venture {
291f09d414SPatrick Venture   protected:
301f09d414SPatrick Venture     const std::uint16_t session = 0xbeef;
311f09d414SPatrick Venture 
32380832ccSPatrick Venture     DataInterfaceMock handlerMock;
33380832ccSPatrick Venture     ipmiblob::BlobInterfaceMock blobMock;
341f09d414SPatrick Venture     UpdateHandler updater{&blobMock, &handlerMock};
351f09d414SPatrick Venture };
36380832ccSPatrick Venture 
TEST_F(UpdateHandlerTest,CheckAvailableSuccess)371f09d414SPatrick Venture TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
381f09d414SPatrick Venture {
39380832ccSPatrick Venture     EXPECT_CALL(blobMock, getBlobList())
40380832ccSPatrick Venture         .WillOnce(
41380832ccSPatrick Venture             Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
42380832ccSPatrick Venture 
43380832ccSPatrick Venture     EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
44380832ccSPatrick Venture }
45380832ccSPatrick Venture 
TEST_F(UpdateHandlerTest,CheckAvailableFailure)468cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, CheckAvailableFailure)
478cdf964dSPatrick Venture {
488cdf964dSPatrick Venture     EXPECT_CALL(blobMock, getBlobList())
498cdf964dSPatrick Venture         .WillOnce(Return(std::vector<std::string>()));
508cdf964dSPatrick Venture 
518cdf964dSPatrick Venture     EXPECT_FALSE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
528cdf964dSPatrick Venture }
538cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,SendFileSuccess)541f09d414SPatrick Venture TEST_F(UpdateHandlerTest, SendFileSuccess)
55380832ccSPatrick Venture {
56380832ccSPatrick Venture     /* Call sendFile to verify it does what we expect. */
57380832ccSPatrick Venture     std::string firmwareImage = "image.bin";
58380832ccSPatrick Venture 
59380832ccSPatrick Venture     std::uint16_t supported =
60380832ccSPatrick Venture         static_cast<std::uint16_t>(
6184778b8dSPatrick Venture             ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
6284778b8dSPatrick Venture         static_cast<std::uint16_t>(
6384778b8dSPatrick Venture             ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
64380832ccSPatrick Venture 
65380832ccSPatrick Venture     EXPECT_CALL(handlerMock, supportedType())
6684778b8dSPatrick Venture         .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
67380832ccSPatrick Venture 
681f09d414SPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
69380832ccSPatrick Venture         .WillOnce(Return(session));
70380832ccSPatrick Venture 
711f09d414SPatrick Venture     EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
72380832ccSPatrick Venture         .WillOnce(Return(true));
73380832ccSPatrick Venture 
74380832ccSPatrick Venture     EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
75380832ccSPatrick Venture 
76380832ccSPatrick Venture     updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
77380832ccSPatrick Venture }
78380832ccSPatrick Venture 
TEST_F(UpdateHandlerTest,SendFileExceptsOnBlobOpening)798cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, SendFileExceptsOnBlobOpening)
808cdf964dSPatrick Venture {
818cdf964dSPatrick Venture     std::string firmwareImage = "image.bin";
828cdf964dSPatrick Venture 
838cdf964dSPatrick Venture     std::uint16_t supported =
848cdf964dSPatrick Venture         static_cast<std::uint16_t>(
858cdf964dSPatrick Venture             ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
868cdf964dSPatrick Venture         static_cast<std::uint16_t>(
878cdf964dSPatrick Venture             ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
888cdf964dSPatrick Venture 
898cdf964dSPatrick Venture     EXPECT_CALL(handlerMock, supportedType())
908cdf964dSPatrick Venture         .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
918cdf964dSPatrick Venture 
928cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
938cdf964dSPatrick Venture         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
948cdf964dSPatrick Venture 
958cdf964dSPatrick Venture     EXPECT_THROW(
968cdf964dSPatrick Venture         updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage),
978cdf964dSPatrick Venture         ToolException);
988cdf964dSPatrick Venture }
998cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,SendFileHandlerFailureCausesException)1008cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, SendFileHandlerFailureCausesException)
1018cdf964dSPatrick Venture {
1028cdf964dSPatrick Venture     std::string firmwareImage = "image.bin";
1038cdf964dSPatrick Venture 
1048cdf964dSPatrick Venture     std::uint16_t supported =
1058cdf964dSPatrick Venture         static_cast<std::uint16_t>(
1068cdf964dSPatrick Venture             ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
1078cdf964dSPatrick Venture         static_cast<std::uint16_t>(
1088cdf964dSPatrick Venture             ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
1098cdf964dSPatrick Venture 
1108cdf964dSPatrick Venture     EXPECT_CALL(handlerMock, supportedType())
1111b23b772SWilly Tu         .WillRepeatedly(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
1128cdf964dSPatrick Venture 
1138cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
1141b23b772SWilly Tu         .WillRepeatedly(Return(session));
1158cdf964dSPatrick Venture 
1168cdf964dSPatrick Venture     EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
1171b23b772SWilly Tu         .WillRepeatedly(Return(false));
1188cdf964dSPatrick Venture 
1191b23b772SWilly Tu     EXPECT_CALL(blobMock, closeBlob(session)).Times(3);
1208cdf964dSPatrick Venture 
1218cdf964dSPatrick Venture     EXPECT_THROW(
1228cdf964dSPatrick Venture         updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage),
1238cdf964dSPatrick Venture         ToolException);
1248cdf964dSPatrick Venture }
1258cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,SendFileHandlerPassWithRetries)1261b23b772SWilly Tu TEST_F(UpdateHandlerTest, SendFileHandlerPassWithRetries)
1271b23b772SWilly Tu {
1281b23b772SWilly Tu     std::string firmwareImage = "image.bin";
1291b23b772SWilly Tu 
1301b23b772SWilly Tu     std::uint16_t supported =
1311b23b772SWilly Tu         static_cast<std::uint16_t>(
1321b23b772SWilly Tu             ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
1331b23b772SWilly Tu         static_cast<std::uint16_t>(
1341b23b772SWilly Tu             ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
1351b23b772SWilly Tu 
1361b23b772SWilly Tu     EXPECT_CALL(handlerMock, supportedType())
1371b23b772SWilly Tu         .WillRepeatedly(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
1381b23b772SWilly Tu 
1391b23b772SWilly Tu     EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
1401b23b772SWilly Tu         .WillRepeatedly(Return(session));
1411b23b772SWilly Tu 
1421b23b772SWilly Tu     EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
1431b23b772SWilly Tu         .WillOnce(Return(false))
1441b23b772SWilly Tu         .WillOnce(Return(false))
1451b23b772SWilly Tu         .WillOnce(Return(true));
1461b23b772SWilly Tu 
1471b23b772SWilly Tu     EXPECT_CALL(blobMock, closeBlob(session)).Times(3);
1481b23b772SWilly Tu 
1491b23b772SWilly Tu     updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
1501b23b772SWilly Tu }
1511b23b772SWilly Tu 
TEST_F(UpdateHandlerTest,VerifyFileHandleReturnsTrueOnSuccess)1521f09d414SPatrick Venture TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
153380832ccSPatrick Venture {
1541f09d414SPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
155380832ccSPatrick Venture         .WillOnce(Return(session));
156380832ccSPatrick Venture     EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
1571f09d414SPatrick Venture     ipmiblob::StatResponse verificationResponse = {};
1581f09d414SPatrick Venture     /* the other details of the response are ignored, and should be. */
1591f09d414SPatrick Venture     verificationResponse.metadata.push_back(
1601f09d414SPatrick Venture         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
161380832ccSPatrick Venture 
1621f09d414SPatrick Venture     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
1631f09d414SPatrick Venture         .WillOnce(Return(verificationResponse));
1641f09d414SPatrick Venture     EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
1651f09d414SPatrick Venture 
1666749ba1cSBrandon Kim     EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
1671f09d414SPatrick Venture }
1681f09d414SPatrick Venture 
TEST_F(UpdateHandlerTest,VerifyFileHandleSkipsPollingIfIgnoreStatus)1698cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, VerifyFileHandleSkipsPollingIfIgnoreStatus)
1708cdf964dSPatrick Venture {
1718cdf964dSPatrick Venture     /* if ignoreStatus, it'll skip polling for a verification result. */
1728cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
1738cdf964dSPatrick Venture         .WillOnce(Return(session));
1748cdf964dSPatrick Venture     EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
1758cdf964dSPatrick Venture 
1768cdf964dSPatrick Venture     EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
1778cdf964dSPatrick Venture 
1788cdf964dSPatrick Venture     EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, true));
1798cdf964dSPatrick Venture }
1808cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,VerifyFileConvertsOpenBlobExceptionToToolException)1818cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, VerifyFileConvertsOpenBlobExceptionToToolException)
1828cdf964dSPatrick Venture {
1838cdf964dSPatrick Venture     /* On open, it can except and this is converted to a ToolException. */
1848cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
1858cdf964dSPatrick Venture         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
1868cdf964dSPatrick Venture     EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false),
1878cdf964dSPatrick Venture                  ToolException);
1888cdf964dSPatrick Venture }
1898cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,VerifyFileToolException)1903f596287SWilly Tu TEST_F(UpdateHandlerTest, VerifyFileToolException)
1913f596287SWilly Tu {
1923f596287SWilly Tu     /* On open, it can except and this is converted to a ToolException. */
1933f596287SWilly Tu     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
1943f596287SWilly Tu         .Times(3)
1953f596287SWilly Tu         .WillRepeatedly(Throw(ToolException("asdf")));
1963f596287SWilly Tu     EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false),
1973f596287SWilly Tu                  ToolException);
1983f596287SWilly Tu }
1993f596287SWilly Tu 
TEST_F(UpdateHandlerTest,VerifyFileHandleReturnsTrueOnSuccessWithRetries)2003f596287SWilly Tu TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccessWithRetries)
2013f596287SWilly Tu {
2023f596287SWilly Tu     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
2033f596287SWilly Tu         .Times(3)
2043f596287SWilly Tu         .WillRepeatedly(Return(session));
2053f596287SWilly Tu     EXPECT_CALL(blobMock, commit(session, _))
2063f596287SWilly Tu         .WillOnce(Throw(ToolException("asdf")))
2073f596287SWilly Tu         .WillOnce(Throw(ToolException("asdf")))
2083f596287SWilly Tu         .WillOnce(Return());
2093f596287SWilly Tu     ipmiblob::StatResponse verificationResponse = {};
2103f596287SWilly Tu     /* the other details of the response are ignored, and should be. */
2113f596287SWilly Tu     verificationResponse.metadata.push_back(
2123f596287SWilly Tu         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
2133f596287SWilly Tu 
2143f596287SWilly Tu     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
2153f596287SWilly Tu         .WillOnce(Return(verificationResponse));
2163f596287SWilly Tu     EXPECT_CALL(blobMock, closeBlob(session)).Times(3).WillRepeatedly(Return());
2173f596287SWilly Tu 
2183f596287SWilly Tu     EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
2193f596287SWilly Tu }
2203f596287SWilly Tu 
TEST_F(UpdateHandlerTest,VerifyFileCommitExceptionForwards)2218cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, VerifyFileCommitExceptionForwards)
2228cdf964dSPatrick Venture {
2238cdf964dSPatrick Venture     /* On commit, it can except. */
2248cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
2258cdf964dSPatrick Venture         .WillOnce(Return(session));
2268cdf964dSPatrick Venture     EXPECT_CALL(blobMock, commit(session, _))
2278cdf964dSPatrick Venture         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
2288cdf964dSPatrick Venture     EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false),
2298cdf964dSPatrick Venture                  ToolException);
2308cdf964dSPatrick Venture }
2318cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,ReadVerisonReturnExpected)232328f520fSJie Yang TEST_F(UpdateHandlerTest, ReadVerisonReturnExpected)
233328f520fSJie Yang {
234328f520fSJie Yang     /* It can return as expected, when polling and readBytes succeeds. */
235328f520fSJie Yang     EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
236328f520fSJie Yang         .WillOnce(Return(session));
237328f520fSJie Yang     ipmiblob::StatResponse readVersionResponse = {};
238*42a44c28SPatrick Williams     readVersionResponse.blob_state =
239*42a44c28SPatrick Williams         blobs::StateFlags::open_read | blobs::StateFlags::committed;
240328f520fSJie Yang     readVersionResponse.size = 10;
241328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
242328f520fSJie Yang         .WillOnce(Return(readVersionResponse));
243328f520fSJie Yang     std::vector<uint8_t> resp = {0x2d, 0xfe};
244328f520fSJie Yang     EXPECT_CALL(blobMock, readBytes(session, 0, _)).WillOnce(Return(resp));
245328f520fSJie Yang 
246328f520fSJie Yang     EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
247328f520fSJie Yang     EXPECT_EQ(resp, updater.readVersion(ipmi_flash::biosVersionBlobId));
248328f520fSJie Yang }
249328f520fSJie Yang 
TEST_F(UpdateHandlerTest,ReadVersionExceptionWhenPollingSucceedsReadBytesFails)250328f520fSJie Yang TEST_F(UpdateHandlerTest, ReadVersionExceptionWhenPollingSucceedsReadBytesFails)
251328f520fSJie Yang {
252328f520fSJie Yang     /* On readBytes, it can except. */
253328f520fSJie Yang     EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
254328f520fSJie Yang         .WillOnce(Return(session));
255328f520fSJie Yang     ipmiblob::StatResponse readVersionResponse = {};
256*42a44c28SPatrick Williams     readVersionResponse.blob_state =
257*42a44c28SPatrick Williams         blobs::StateFlags::open_read | blobs::StateFlags::committed;
258328f520fSJie Yang     readVersionResponse.size = 10;
259328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
260328f520fSJie Yang         .WillOnce(Return(readVersionResponse));
261328f520fSJie Yang     EXPECT_CALL(blobMock, readBytes(session, 0, _))
262328f520fSJie Yang         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
263328f520fSJie Yang     EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
264328f520fSJie Yang     EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId),
265328f520fSJie Yang                  ToolException);
266328f520fSJie Yang }
267328f520fSJie Yang 
TEST_F(UpdateHandlerTest,ReadVersionReturnsErrorIfPollingFails)268f88bcf3bSWilliam A. Kennington III TEST_F(UpdateHandlerTest, ReadVersionReturnsErrorIfPollingFails)
269328f520fSJie Yang {
270f88bcf3bSWilliam A. Kennington III     /* It can throw an error, when polling fails. */
271328f520fSJie Yang     EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
2723f596287SWilly Tu         .Times(3)
2733f596287SWilly Tu         .WillRepeatedly(Return(session));
274328f520fSJie Yang     ipmiblob::StatResponse readVersionResponse = {};
275328f520fSJie Yang     readVersionResponse.blob_state = blobs::StateFlags::commit_error;
276328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
2773f596287SWilly Tu         .Times(3)
2783f596287SWilly Tu         .WillRepeatedly(Return(readVersionResponse));
2793f596287SWilly Tu     EXPECT_CALL(blobMock, closeBlob(session)).Times(3).WillRepeatedly(Return());
280f88bcf3bSWilliam A. Kennington III     EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId),
281f88bcf3bSWilliam A. Kennington III                  ToolException);
282328f520fSJie Yang }
283328f520fSJie Yang 
TEST_F(UpdateHandlerTest,ReadVersionReturnExpectedWithRetries)2843f596287SWilly Tu TEST_F(UpdateHandlerTest, ReadVersionReturnExpectedWithRetries)
2853f596287SWilly Tu {
2863f596287SWilly Tu     /* It can return as expected, when polling and readBytes succeeds. */
2873f596287SWilly Tu     EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
2883f596287SWilly Tu         .Times(3)
2893f596287SWilly Tu         .WillRepeatedly(Return(session));
2903f596287SWilly Tu     ipmiblob::StatResponse readVersionResponse = {};
291*42a44c28SPatrick Williams     readVersionResponse.blob_state =
292*42a44c28SPatrick Williams         blobs::StateFlags::open_read | blobs::StateFlags::committed;
2933f596287SWilly Tu     readVersionResponse.size = 10;
2943f596287SWilly Tu     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
2953f596287SWilly Tu         .Times(3)
2963f596287SWilly Tu         .WillRepeatedly(Return(readVersionResponse));
2973f596287SWilly Tu     std::vector<uint8_t> resp = {0x2d, 0xfe};
2983f596287SWilly Tu     EXPECT_CALL(blobMock, readBytes(session, 0, _))
2993f596287SWilly Tu         .WillOnce(Throw(ToolException("asdf")))
3003f596287SWilly Tu         .WillOnce(Throw(ToolException("asdf")))
3013f596287SWilly Tu         .WillOnce(Return(resp));
3023f596287SWilly Tu 
3033f596287SWilly Tu     EXPECT_CALL(blobMock, closeBlob(session)).Times(3).WillRepeatedly(Return());
3043f596287SWilly Tu     EXPECT_EQ(resp, updater.readVersion(ipmi_flash::biosVersionBlobId));
3053f596287SWilly Tu }
3063f596287SWilly Tu 
TEST_F(UpdateHandlerTest,ReadVersionCovertsOpenBlobExceptionToToolException)307328f520fSJie Yang TEST_F(UpdateHandlerTest, ReadVersionCovertsOpenBlobExceptionToToolException)
308328f520fSJie Yang {
309328f520fSJie Yang     /* On open, it can except and this is converted to a ToolException. */
310328f520fSJie Yang     EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
311328f520fSJie Yang         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
312328f520fSJie Yang     EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId),
313328f520fSJie Yang                  ToolException);
314328f520fSJie Yang }
315328f520fSJie Yang 
TEST_F(UpdateHandlerTest,CleanArtifactsSkipsCleanupIfUnableToOpen)3168cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, CleanArtifactsSkipsCleanupIfUnableToOpen)
3178cdf964dSPatrick Venture {
3188cdf964dSPatrick Venture     /* It only tries to commit if it's able to open the blob.  However, if
3198cdf964dSPatrick Venture      * committing fails, this error is ignored.
3208cdf964dSPatrick Venture      */
3218cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _))
3228cdf964dSPatrick Venture         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
3238cdf964dSPatrick Venture     EXPECT_CALL(blobMock, commit(_, _)).Times(0);
3248cdf964dSPatrick Venture     EXPECT_CALL(blobMock, closeBlob(_)).Times(0);
3258cdf964dSPatrick Venture 
3268cdf964dSPatrick Venture     updater.cleanArtifacts();
3278cdf964dSPatrick Venture }
3288cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,CleanArtifactsIfOpenDoesClose)3298cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, CleanArtifactsIfOpenDoesClose)
3308cdf964dSPatrick Venture {
3318cdf964dSPatrick Venture     /* The closeBlob call is called even if commit excepts. */
3328cdf964dSPatrick Venture     std::uint16_t session = 0xa5eb;
3338cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _))
3348cdf964dSPatrick Venture         .WillOnce(Return(session));
3358cdf964dSPatrick Venture     EXPECT_CALL(blobMock, commit(session, _))
3368cdf964dSPatrick Venture         .WillOnce(Throw(ipmiblob::BlobException("asdf")));
3378cdf964dSPatrick Venture     EXPECT_CALL(blobMock, closeBlob(session));
3388cdf964dSPatrick Venture 
3398cdf964dSPatrick Venture     updater.cleanArtifacts();
3408cdf964dSPatrick Venture }
3418cdf964dSPatrick Venture 
TEST_F(UpdateHandlerTest,CleanArtifactsSuccessPath)3428cdf964dSPatrick Venture TEST_F(UpdateHandlerTest, CleanArtifactsSuccessPath)
3438cdf964dSPatrick Venture {
3448cdf964dSPatrick Venture     std::uint16_t session = 0xa5eb;
3458cdf964dSPatrick Venture     EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _))
3468cdf964dSPatrick Venture         .WillOnce(Return(session));
3478cdf964dSPatrick Venture     EXPECT_CALL(blobMock, commit(session, _));
3488cdf964dSPatrick Venture     EXPECT_CALL(blobMock, closeBlob(session));
3498cdf964dSPatrick Venture 
3508cdf964dSPatrick Venture     updater.cleanArtifacts();
3518cdf964dSPatrick Venture }
3528cdf964dSPatrick Venture 
3531f09d414SPatrick Venture class UpdaterTest : public ::testing::Test
3541f09d414SPatrick Venture {
3551f09d414SPatrick Venture   protected:
3568cdf964dSPatrick Venture     static constexpr char image[] = "image.bin";
3578cdf964dSPatrick Venture     static constexpr char signature[] = "signature.bin";
3588cdf964dSPatrick Venture     static constexpr char layout[] = "static";
3598cdf964dSPatrick Venture     static constexpr char path[] = "/flash/static";
3608cdf964dSPatrick Venture 
3611f09d414SPatrick Venture     ipmiblob::BlobInterfaceMock blobMock;
3621f09d414SPatrick Venture     std::uint16_t session = 0xbeef;
3636749ba1cSBrandon Kim     bool defaultIgnore = false;
3641f09d414SPatrick Venture };
3651f09d414SPatrick Venture 
TEST_F(UpdaterTest,UpdateMainReturnsSuccessIfAllSuccess)3661f09d414SPatrick Venture TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
3671f09d414SPatrick Venture {
3681f09d414SPatrick Venture     UpdateHandlerMock handler;
3691f09d414SPatrick Venture 
3708cdf964dSPatrick Venture     EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
3718cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
3728cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
3738cdf964dSPatrick Venture         .WillOnce(Return());
3746749ba1cSBrandon Kim     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
3751f09d414SPatrick Venture         .WillOnce(Return(true));
3766749ba1cSBrandon Kim     EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
3771f09d414SPatrick Venture         .WillOnce(Return(true));
378203ad804SWilly Tu     EXPECT_CALL(blobMock, getBlobList())
379203ad804SWilly Tu         .WillOnce(Return(std::vector<std::string>({})));
3801f09d414SPatrick Venture 
381203ad804SWilly Tu     updaterMain(&handler, &blobMock, image, signature, layout, defaultIgnore);
382203ad804SWilly Tu }
383203ad804SWilly Tu 
TEST_F(UpdaterTest,UpdateMainReturnsSuccessIfAllSuccessWithDeleteActiveBlob)384203ad804SWilly Tu TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccessWithDeleteActiveBlob)
385203ad804SWilly Tu {
386203ad804SWilly Tu     UpdateHandlerMock handler;
387203ad804SWilly Tu 
388203ad804SWilly Tu     EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
389203ad804SWilly Tu     EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
390203ad804SWilly Tu     EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
391203ad804SWilly Tu         .WillOnce(Return());
392203ad804SWilly Tu     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
393203ad804SWilly Tu         .WillOnce(Return(true));
394203ad804SWilly Tu     EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
395203ad804SWilly Tu         .WillOnce(Return(true));
396203ad804SWilly Tu     EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
397203ad804SWilly Tu     EXPECT_CALL(blobMock, deleteBlob(ipmi_flash::activeImageBlobId))
3982862421cSPatrick Venture         .WillOnce(Return(true));
399203ad804SWilly Tu     EXPECT_CALL(blobMock, getBlobList())
400203ad804SWilly Tu         .WillOnce(Return(std::vector<std::string>(
401203ad804SWilly Tu             {ipmi_flash::staticLayoutBlobId, ipmi_flash::activeImageBlobId})));
402203ad804SWilly Tu 
403203ad804SWilly Tu     updaterMain(&handler, &blobMock, image, signature, layout, defaultIgnore);
4046749ba1cSBrandon Kim }
4056749ba1cSBrandon Kim 
TEST_F(UpdaterTest,UpdateMainReturnsSuccessWithIgnoreUpdate)4066749ba1cSBrandon Kim TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate)
4076749ba1cSBrandon Kim {
4086749ba1cSBrandon Kim     UpdateHandlerMock handler;
4096749ba1cSBrandon Kim     bool updateIgnore = true;
4106749ba1cSBrandon Kim 
4118cdf964dSPatrick Venture     EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
4128cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
4138cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
4148cdf964dSPatrick Venture         .WillOnce(Return());
4156749ba1cSBrandon Kim     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
4166749ba1cSBrandon Kim         .WillOnce(Return(true));
4176749ba1cSBrandon Kim     EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore))
4186749ba1cSBrandon Kim         .WillOnce(Return(true));
419203ad804SWilly Tu     EXPECT_CALL(blobMock, getBlobList())
420203ad804SWilly Tu         .WillOnce(Return(std::vector<std::string>({})));
4216749ba1cSBrandon Kim 
422203ad804SWilly Tu     updaterMain(&handler, &blobMock, image, signature, layout, updateIgnore);
4231f09d414SPatrick Venture }
424380832ccSPatrick Venture 
TEST_F(UpdaterTest,UpdateMainCleansUpOnFailure)4255f2fcc4eSPatrick Venture TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
4265f2fcc4eSPatrick Venture {
4275f2fcc4eSPatrick Venture     UpdateHandlerMock handler;
4285f2fcc4eSPatrick Venture 
4298cdf964dSPatrick Venture     EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
4308cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
4318cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
4328cdf964dSPatrick Venture         .WillOnce(Return());
4336749ba1cSBrandon Kim     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
4345f2fcc4eSPatrick Venture         .WillOnce(Return(false));
4355f2fcc4eSPatrick Venture     EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
436203ad804SWilly Tu     EXPECT_CALL(blobMock, getBlobList())
437203ad804SWilly Tu         .WillOnce(Return(std::vector<std::string>({})));
4385f2fcc4eSPatrick Venture 
439203ad804SWilly Tu     EXPECT_THROW(updaterMain(&handler, &blobMock, image, signature, layout,
440203ad804SWilly Tu                              defaultIgnore),
4418cdf964dSPatrick Venture                  ToolException);
4428cdf964dSPatrick Venture }
4438cdf964dSPatrick Venture 
TEST_F(UpdaterTest,UpdateMainExceptsOnUpdateBlobFailure)4448cdf964dSPatrick Venture TEST_F(UpdaterTest, UpdateMainExceptsOnUpdateBlobFailure)
4458cdf964dSPatrick Venture {
4468cdf964dSPatrick Venture     UpdateHandlerMock handler;
4478cdf964dSPatrick Venture 
4488cdf964dSPatrick Venture     EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
4498cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
4508cdf964dSPatrick Venture     EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
4518cdf964dSPatrick Venture         .WillOnce(Return());
4528cdf964dSPatrick Venture     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
4538cdf964dSPatrick Venture         .WillOnce(Return(true));
4548cdf964dSPatrick Venture     EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
4558cdf964dSPatrick Venture         .WillOnce(Return(false));
4568cdf964dSPatrick Venture     EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
457203ad804SWilly Tu     EXPECT_CALL(blobMock, getBlobList())
458203ad804SWilly Tu         .WillOnce(Return(std::vector<std::string>({})));
4598cdf964dSPatrick Venture 
460203ad804SWilly Tu     EXPECT_THROW(updaterMain(&handler, &blobMock, image, signature, layout,
461203ad804SWilly Tu                              defaultIgnore),
4628cdf964dSPatrick Venture                  ToolException);
4638cdf964dSPatrick Venture }
4648cdf964dSPatrick Venture 
TEST_F(UpdaterTest,UpdateMainExceptsIfAvailableNotFound)4658cdf964dSPatrick Venture TEST_F(UpdaterTest, UpdateMainExceptsIfAvailableNotFound)
4668cdf964dSPatrick Venture {
4678cdf964dSPatrick Venture     UpdateHandlerMock handler;
4688cdf964dSPatrick Venture 
4698cdf964dSPatrick Venture     EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(false));
4708cdf964dSPatrick Venture 
471203ad804SWilly Tu     EXPECT_THROW(updaterMain(&handler, &blobMock, image, signature, layout,
472203ad804SWilly Tu                              defaultIgnore),
4739f937c45SPatrick Venture                  ToolException);
4745f2fcc4eSPatrick Venture }
4755f2fcc4eSPatrick Venture 
476380832ccSPatrick Venture } // namespace host_tool
477