xref: /openbmc/phosphor-ipmi-flash/tools/test/tools_updater_unittest.cpp (revision 9b37b09541d468a616d586bf533b684a3751c0f7)
1 #include "data_interface_mock.hpp"
2 #include "flags.hpp"
3 #include "status.hpp"
4 #include "tool_errors.hpp"
5 #include "updater.hpp"
6 #include "updater_mock.hpp"
7 #include "util.hpp"
8 
9 #include <ipmiblob/test/blob_interface_mock.hpp>
10 
11 #include <string>
12 
13 #include <gtest/gtest.h>
14 
15 namespace host_tool
16 {
17 
18 using ::testing::_;
19 using ::testing::Eq;
20 using ::testing::Return;
21 using ::testing::TypedEq;
22 
23 class UpdateHandlerTest : public ::testing::Test
24 {
25   protected:
26     const std::uint16_t session = 0xbeef;
27 
28     DataInterfaceMock handlerMock;
29     ipmiblob::BlobInterfaceMock blobMock;
30     UpdateHandler updater{&blobMock, &handlerMock};
31 };
32 
33 TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
34 {
35     EXPECT_CALL(blobMock, getBlobList())
36         .WillOnce(
37             Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
38 
39     EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
40 }
41 
42 TEST_F(UpdateHandlerTest, SendFileSuccess)
43 {
44     /* Call sendFile to verify it does what we expect. */
45     std::string firmwareImage = "image.bin";
46 
47     std::uint16_t supported =
48         static_cast<std::uint16_t>(
49             ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
50         static_cast<std::uint16_t>(
51             ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
52 
53     EXPECT_CALL(handlerMock, supportedType())
54         .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
55 
56     EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
57         .WillOnce(Return(session));
58 
59     EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
60         .WillOnce(Return(true));
61 
62     EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
63 
64     updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
65 }
66 
67 TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
68 {
69     EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
70         .WillOnce(Return(session));
71     EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
72     ipmiblob::StatResponse verificationResponse = {};
73     /* the other details of the response are ignored, and should be. */
74     verificationResponse.metadata.push_back(
75         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
76 
77     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
78         .WillOnce(Return(verificationResponse));
79     EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
80 
81     EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
82 }
83 
84 class UpdaterTest : public ::testing::Test
85 {
86   protected:
87     ipmiblob::BlobInterfaceMock blobMock;
88     std::uint16_t session = 0xbeef;
89     bool defaultIgnore = false;
90 };
91 
92 TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
93 {
94     const std::string image = "image.bin";
95     const std::string signature = "signature.bin";
96     UpdateHandlerMock handler;
97 
98     EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
99     EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
100     EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
101     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
102         .WillOnce(Return(true));
103     EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
104         .WillOnce(Return(true));
105 
106     updaterMain(&handler, image, signature, "static", defaultIgnore);
107 }
108 
109 TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate)
110 {
111     const std::string image = "image.bin";
112     const std::string signature = "signature.bin";
113     UpdateHandlerMock handler;
114     bool updateIgnore = true;
115 
116     EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
117     EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
118     EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
119     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
120         .WillOnce(Return(true));
121     EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore))
122         .WillOnce(Return(true));
123 
124     updaterMain(&handler, image, signature, "static", updateIgnore);
125 }
126 
127 TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
128 {
129     const std::string image = "image.bin";
130     const std::string signature = "signature.bin";
131     UpdateHandlerMock handler;
132 
133     EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
134     EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
135     EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
136     EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
137         .WillOnce(Return(false));
138     EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
139 
140     EXPECT_THROW(
141         updaterMain(&handler, image, signature, "static", defaultIgnore),
142         ToolException);
143 }
144 
145 } // namespace host_tool
146