xref: /openbmc/phosphor-ipmi-flash/tools/test/tools_updater_unittest.cpp (revision 9f937c45938ea4d0ec596a4edf9182f11519fae3)
1 #include "data_interface_mock.hpp"
2 #include "status.hpp"
3 #include "tool_errors.hpp"
4 #include "updater.hpp"
5 #include "updater_mock.hpp"
6 #include "util.hpp"
7 
8 #include <blobs-ipmid/blobs.hpp>
9 #include <ipmiblob/test/blob_interface_mock.hpp>
10 #include <string>
11 
12 #include <gtest/gtest.h>
13 
14 namespace host_tool
15 {
16 
17 using ::testing::_;
18 using ::testing::Eq;
19 using ::testing::Return;
20 using ::testing::TypedEq;
21 
22 class UpdateHandlerTest : public ::testing::Test
23 {
24   protected:
25     const std::uint16_t session = 0xbeef;
26 
27     DataInterfaceMock handlerMock;
28     ipmiblob::BlobInterfaceMock blobMock;
29     UpdateHandler updater{&blobMock, &handlerMock};
30 };
31 
32 TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
33 {
34     ipmiblob::StatResponse statObj = {};
35     statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi |
36                          ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc;
37 
38     EXPECT_CALL(blobMock, getBlobList())
39         .WillOnce(
40             Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
41     EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(
42                               ipmi_flash::staticLayoutBlobId)))
43         .WillOnce(Return(statObj));
44 
45     EXPECT_CALL(handlerMock, supportedType())
46         .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
47 
48     EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
49 }
50 
51 TEST_F(UpdateHandlerTest, SendFileSuccess)
52 {
53     /* Call sendFile to verify it does what we expect. */
54     std::string firmwareImage = "image.bin";
55 
56     std::uint16_t supported =
57         static_cast<std::uint16_t>(
58             ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) |
59         static_cast<std::uint16_t>(blobs::OpenFlags::write);
60 
61     EXPECT_CALL(handlerMock, supportedType())
62         .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
63 
64     EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
65         .WillOnce(Return(session));
66 
67     EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
68         .WillOnce(Return(true));
69 
70     EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
71 
72     updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
73 }
74 
75 TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
76 {
77     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
78         .WillOnce(Return(session));
79     EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
80     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));
84 
85     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 
92 class UpdaterTest : public ::testing::Test
93 {
94   protected:
95     ipmiblob::BlobInterfaceMock blobMock;
96     std::uint16_t session = 0xbeef;
97 };
98 
99 TEST_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, "static");
114 }
115 
116 TEST_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, "static"),
130                  ToolException);
131 }
132 
133 } // namespace host_tool
134