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