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