Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 1 | #include "data_interface_mock.hpp" |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 2 | #include "flags.hpp" |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 3 | #include "status.hpp" |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 4 | #include "tool_errors.hpp" |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 5 | #include "updater.hpp" |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 6 | #include "updater_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 7 | #include "util.hpp" |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 8 | |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 9 | #include <blobs-ipmid/blobs.hpp> |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 10 | #include <ipmiblob/blob_errors.hpp> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 11 | #include <ipmiblob/test/blob_interface_mock.hpp> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 12 | |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 13 | #include <string> |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 14 | #include <vector> |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 15 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 16 | #include <gmock/gmock.h> |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
| 18 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 19 | namespace host_tool |
| 20 | { |
| 21 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 22 | using ::testing::_; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 23 | using ::testing::Return; |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 24 | using ::testing::Throw; |
Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 25 | using ::testing::TypedEq; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 26 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 27 | class UpdateHandlerTest : public ::testing::Test |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 28 | { |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 29 | protected: |
| 30 | const std::uint16_t session = 0xbeef; |
| 31 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 32 | DataInterfaceMock handlerMock; |
| 33 | ipmiblob::BlobInterfaceMock blobMock; |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 34 | UpdateHandler updater{&blobMock, &handlerMock}; |
| 35 | }; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 36 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 37 | TEST_F(UpdateHandlerTest, CheckAvailableSuccess) |
| 38 | { |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 39 | EXPECT_CALL(blobMock, getBlobList()) |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 40 | .WillOnce( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 41 | Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId}))); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 42 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 43 | EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 46 | TEST_F(UpdateHandlerTest, CheckAvailableFailure) |
| 47 | { |
| 48 | EXPECT_CALL(blobMock, getBlobList()) |
| 49 | .WillOnce(Return(std::vector<std::string>())); |
| 50 | |
| 51 | EXPECT_FALSE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId)); |
| 52 | } |
| 53 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 54 | TEST_F(UpdateHandlerTest, SendFileSuccess) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 55 | { |
| 56 | /* Call sendFile to verify it does what we expect. */ |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 57 | std::string firmwareImage = "image.bin"; |
| 58 | |
| 59 | std::uint16_t supported = |
| 60 | static_cast<std::uint16_t>( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 61 | ipmi_flash::FirmwareFlags::UpdateFlags::lpc) | |
| 62 | static_cast<std::uint16_t>( |
| 63 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 64 | |
| 65 | EXPECT_CALL(handlerMock, supportedType()) |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 66 | .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 67 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 68 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 69 | .WillOnce(Return(session)); |
| 70 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 71 | EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 72 | .WillOnce(Return(true)); |
| 73 | |
| 74 | EXPECT_CALL(blobMock, closeBlob(session)).Times(1); |
| 75 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 76 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 79 | TEST_F(UpdateHandlerTest, SendFileExceptsOnBlobOpening) |
| 80 | { |
| 81 | std::string firmwareImage = "image.bin"; |
| 82 | |
| 83 | std::uint16_t supported = |
| 84 | static_cast<std::uint16_t>( |
| 85 | ipmi_flash::FirmwareFlags::UpdateFlags::lpc) | |
| 86 | static_cast<std::uint16_t>( |
| 87 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite); |
| 88 | |
| 89 | EXPECT_CALL(handlerMock, supportedType()) |
| 90 | .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc)); |
| 91 | |
| 92 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) |
| 93 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 94 | |
| 95 | EXPECT_THROW( |
| 96 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage), |
| 97 | ToolException); |
| 98 | } |
| 99 | |
| 100 | TEST_F(UpdateHandlerTest, SendFileHandlerFailureCausesException) |
| 101 | { |
| 102 | std::string firmwareImage = "image.bin"; |
| 103 | |
| 104 | std::uint16_t supported = |
| 105 | static_cast<std::uint16_t>( |
| 106 | ipmi_flash::FirmwareFlags::UpdateFlags::lpc) | |
| 107 | static_cast<std::uint16_t>( |
| 108 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite); |
| 109 | |
| 110 | EXPECT_CALL(handlerMock, supportedType()) |
Willy Tu | 1b23b77 | 2023-03-15 01:33:03 -0700 | [diff] [blame] | 111 | .WillRepeatedly(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc)); |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 112 | |
| 113 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) |
Willy Tu | 1b23b77 | 2023-03-15 01:33:03 -0700 | [diff] [blame] | 114 | .WillRepeatedly(Return(session)); |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 115 | |
| 116 | EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) |
Willy Tu | 1b23b77 | 2023-03-15 01:33:03 -0700 | [diff] [blame] | 117 | .WillRepeatedly(Return(false)); |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 118 | |
Willy Tu | 1b23b77 | 2023-03-15 01:33:03 -0700 | [diff] [blame] | 119 | EXPECT_CALL(blobMock, closeBlob(session)).Times(3); |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 120 | |
| 121 | EXPECT_THROW( |
| 122 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage), |
| 123 | ToolException); |
| 124 | } |
| 125 | |
Willy Tu | 1b23b77 | 2023-03-15 01:33:03 -0700 | [diff] [blame] | 126 | TEST_F(UpdateHandlerTest, SendFileHandlerPassWithRetries) |
| 127 | { |
| 128 | std::string firmwareImage = "image.bin"; |
| 129 | |
| 130 | std::uint16_t supported = |
| 131 | static_cast<std::uint16_t>( |
| 132 | ipmi_flash::FirmwareFlags::UpdateFlags::lpc) | |
| 133 | static_cast<std::uint16_t>( |
| 134 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite); |
| 135 | |
| 136 | EXPECT_CALL(handlerMock, supportedType()) |
| 137 | .WillRepeatedly(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc)); |
| 138 | |
| 139 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) |
| 140 | .WillRepeatedly(Return(session)); |
| 141 | |
| 142 | EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) |
| 143 | .WillOnce(Return(false)) |
| 144 | .WillOnce(Return(false)) |
| 145 | .WillOnce(Return(true)); |
| 146 | |
| 147 | EXPECT_CALL(blobMock, closeBlob(session)).Times(3); |
| 148 | |
| 149 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); |
| 150 | } |
| 151 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 152 | TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 153 | { |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 154 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 155 | .WillOnce(Return(session)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 156 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 157 | ipmiblob::StatResponse verificationResponse = {}; |
| 158 | /* the other details of the response are ignored, and should be. */ |
| 159 | verificationResponse.metadata.push_back( |
| 160 | static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success)); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 161 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 162 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 163 | .WillOnce(Return(verificationResponse)); |
| 164 | EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); |
| 165 | |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 166 | EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false)); |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 169 | TEST_F(UpdateHandlerTest, VerifyFileHandleSkipsPollingIfIgnoreStatus) |
| 170 | { |
| 171 | /* if ignoreStatus, it'll skip polling for a verification result. */ |
| 172 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
| 173 | .WillOnce(Return(session)); |
| 174 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); |
| 175 | |
| 176 | EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); |
| 177 | |
| 178 | EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, true)); |
| 179 | } |
| 180 | |
| 181 | TEST_F(UpdateHandlerTest, VerifyFileConvertsOpenBlobExceptionToToolException) |
| 182 | { |
| 183 | /* On open, it can except and this is converted to a ToolException. */ |
| 184 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
| 185 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 186 | EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false), |
| 187 | ToolException); |
| 188 | } |
| 189 | |
Willy Tu | 3f59628 | 2024-04-12 16:27:17 +0000 | [diff] [blame] | 190 | TEST_F(UpdateHandlerTest, VerifyFileToolException) |
| 191 | { |
| 192 | /* On open, it can except and this is converted to a ToolException. */ |
| 193 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
| 194 | .Times(3) |
| 195 | .WillRepeatedly(Throw(ToolException("asdf"))); |
| 196 | EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false), |
| 197 | ToolException); |
| 198 | } |
| 199 | |
| 200 | TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccessWithRetries) |
| 201 | { |
| 202 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
| 203 | .Times(3) |
| 204 | .WillRepeatedly(Return(session)); |
| 205 | EXPECT_CALL(blobMock, commit(session, _)) |
| 206 | .WillOnce(Throw(ToolException("asdf"))) |
| 207 | .WillOnce(Throw(ToolException("asdf"))) |
| 208 | .WillOnce(Return()); |
| 209 | ipmiblob::StatResponse verificationResponse = {}; |
| 210 | /* the other details of the response are ignored, and should be. */ |
| 211 | verificationResponse.metadata.push_back( |
| 212 | static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success)); |
| 213 | |
| 214 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 215 | .WillOnce(Return(verificationResponse)); |
| 216 | EXPECT_CALL(blobMock, closeBlob(session)).Times(3).WillRepeatedly(Return()); |
| 217 | |
| 218 | EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false)); |
| 219 | } |
| 220 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 221 | TEST_F(UpdateHandlerTest, VerifyFileCommitExceptionForwards) |
| 222 | { |
| 223 | /* On commit, it can except. */ |
| 224 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
| 225 | .WillOnce(Return(session)); |
| 226 | EXPECT_CALL(blobMock, commit(session, _)) |
| 227 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 228 | EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false), |
| 229 | ToolException); |
| 230 | } |
| 231 | |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 232 | TEST_F(UpdateHandlerTest, ReadVerisonReturnExpected) |
| 233 | { |
| 234 | /* It can return as expected, when polling and readBytes succeeds. */ |
| 235 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _)) |
| 236 | .WillOnce(Return(session)); |
| 237 | ipmiblob::StatResponse readVersionResponse = {}; |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 238 | readVersionResponse.blob_state = |
| 239 | blobs::StateFlags::open_read | blobs::StateFlags::committed; |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 240 | readVersionResponse.size = 10; |
| 241 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 242 | .WillOnce(Return(readVersionResponse)); |
| 243 | std::vector<uint8_t> resp = {0x2d, 0xfe}; |
| 244 | EXPECT_CALL(blobMock, readBytes(session, 0, _)).WillOnce(Return(resp)); |
| 245 | |
| 246 | EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); |
| 247 | EXPECT_EQ(resp, updater.readVersion(ipmi_flash::biosVersionBlobId)); |
| 248 | } |
| 249 | |
| 250 | TEST_F(UpdateHandlerTest, ReadVersionExceptionWhenPollingSucceedsReadBytesFails) |
| 251 | { |
| 252 | /* On readBytes, it can except. */ |
| 253 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _)) |
| 254 | .WillOnce(Return(session)); |
| 255 | ipmiblob::StatResponse readVersionResponse = {}; |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 256 | readVersionResponse.blob_state = |
| 257 | blobs::StateFlags::open_read | blobs::StateFlags::committed; |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 258 | readVersionResponse.size = 10; |
| 259 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 260 | .WillOnce(Return(readVersionResponse)); |
| 261 | EXPECT_CALL(blobMock, readBytes(session, 0, _)) |
| 262 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 263 | EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); |
| 264 | EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId), |
| 265 | ToolException); |
| 266 | } |
| 267 | |
William A. Kennington III | f88bcf3 | 2021-10-14 02:15:10 -0700 | [diff] [blame] | 268 | TEST_F(UpdateHandlerTest, ReadVersionReturnsErrorIfPollingFails) |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 269 | { |
William A. Kennington III | f88bcf3 | 2021-10-14 02:15:10 -0700 | [diff] [blame] | 270 | /* It can throw an error, when polling fails. */ |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 271 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _)) |
Willy Tu | 3f59628 | 2024-04-12 16:27:17 +0000 | [diff] [blame] | 272 | .Times(3) |
| 273 | .WillRepeatedly(Return(session)); |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 274 | ipmiblob::StatResponse readVersionResponse = {}; |
| 275 | readVersionResponse.blob_state = blobs::StateFlags::commit_error; |
| 276 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
Willy Tu | 3f59628 | 2024-04-12 16:27:17 +0000 | [diff] [blame] | 277 | .Times(3) |
| 278 | .WillRepeatedly(Return(readVersionResponse)); |
| 279 | EXPECT_CALL(blobMock, closeBlob(session)).Times(3).WillRepeatedly(Return()); |
William A. Kennington III | f88bcf3 | 2021-10-14 02:15:10 -0700 | [diff] [blame] | 280 | EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId), |
| 281 | ToolException); |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Willy Tu | 3f59628 | 2024-04-12 16:27:17 +0000 | [diff] [blame] | 284 | TEST_F(UpdateHandlerTest, ReadVersionReturnExpectedWithRetries) |
| 285 | { |
| 286 | /* It can return as expected, when polling and readBytes succeeds. */ |
| 287 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _)) |
| 288 | .Times(3) |
| 289 | .WillRepeatedly(Return(session)); |
| 290 | ipmiblob::StatResponse readVersionResponse = {}; |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 291 | readVersionResponse.blob_state = |
| 292 | blobs::StateFlags::open_read | blobs::StateFlags::committed; |
Willy Tu | 3f59628 | 2024-04-12 16:27:17 +0000 | [diff] [blame] | 293 | readVersionResponse.size = 10; |
| 294 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 295 | .Times(3) |
| 296 | .WillRepeatedly(Return(readVersionResponse)); |
| 297 | std::vector<uint8_t> resp = {0x2d, 0xfe}; |
| 298 | EXPECT_CALL(blobMock, readBytes(session, 0, _)) |
| 299 | .WillOnce(Throw(ToolException("asdf"))) |
| 300 | .WillOnce(Throw(ToolException("asdf"))) |
| 301 | .WillOnce(Return(resp)); |
| 302 | |
| 303 | EXPECT_CALL(blobMock, closeBlob(session)).Times(3).WillRepeatedly(Return()); |
| 304 | EXPECT_EQ(resp, updater.readVersion(ipmi_flash::biosVersionBlobId)); |
| 305 | } |
| 306 | |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 307 | TEST_F(UpdateHandlerTest, ReadVersionCovertsOpenBlobExceptionToToolException) |
| 308 | { |
| 309 | /* On open, it can except and this is converted to a ToolException. */ |
| 310 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _)) |
| 311 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 312 | EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId), |
| 313 | ToolException); |
| 314 | } |
| 315 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 316 | TEST_F(UpdateHandlerTest, CleanArtifactsSkipsCleanupIfUnableToOpen) |
| 317 | { |
| 318 | /* It only tries to commit if it's able to open the blob. However, if |
| 319 | * committing fails, this error is ignored. |
| 320 | */ |
| 321 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _)) |
| 322 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 323 | EXPECT_CALL(blobMock, commit(_, _)).Times(0); |
| 324 | EXPECT_CALL(blobMock, closeBlob(_)).Times(0); |
| 325 | |
| 326 | updater.cleanArtifacts(); |
| 327 | } |
| 328 | |
| 329 | TEST_F(UpdateHandlerTest, CleanArtifactsIfOpenDoesClose) |
| 330 | { |
| 331 | /* The closeBlob call is called even if commit excepts. */ |
| 332 | std::uint16_t session = 0xa5eb; |
| 333 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _)) |
| 334 | .WillOnce(Return(session)); |
| 335 | EXPECT_CALL(blobMock, commit(session, _)) |
| 336 | .WillOnce(Throw(ipmiblob::BlobException("asdf"))); |
| 337 | EXPECT_CALL(blobMock, closeBlob(session)); |
| 338 | |
| 339 | updater.cleanArtifacts(); |
| 340 | } |
| 341 | |
| 342 | TEST_F(UpdateHandlerTest, CleanArtifactsSuccessPath) |
| 343 | { |
| 344 | std::uint16_t session = 0xa5eb; |
| 345 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _)) |
| 346 | .WillOnce(Return(session)); |
| 347 | EXPECT_CALL(blobMock, commit(session, _)); |
| 348 | EXPECT_CALL(blobMock, closeBlob(session)); |
| 349 | |
| 350 | updater.cleanArtifacts(); |
| 351 | } |
| 352 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 353 | class UpdaterTest : public ::testing::Test |
| 354 | { |
| 355 | protected: |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 356 | static constexpr char image[] = "image.bin"; |
| 357 | static constexpr char signature[] = "signature.bin"; |
| 358 | static constexpr char layout[] = "static"; |
| 359 | static constexpr char path[] = "/flash/static"; |
| 360 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 361 | ipmiblob::BlobInterfaceMock blobMock; |
| 362 | std::uint16_t session = 0xbeef; |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 363 | bool defaultIgnore = false; |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 366 | TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess) |
| 367 | { |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 368 | UpdateHandlerMock handler; |
| 369 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 370 | EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true)); |
| 371 | EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return()); |
| 372 | EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature)) |
| 373 | .WillOnce(Return()); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 374 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 375 | .WillOnce(Return(true)); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 376 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore)) |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 377 | .WillOnce(Return(true)); |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 378 | EXPECT_CALL(blobMock, getBlobList()) |
| 379 | .WillOnce(Return(std::vector<std::string>({}))); |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 380 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 381 | updaterMain(&handler, &blobMock, image, signature, layout, defaultIgnore); |
| 382 | } |
| 383 | |
| 384 | TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccessWithDeleteActiveBlob) |
| 385 | { |
| 386 | UpdateHandlerMock handler; |
| 387 | |
| 388 | EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true)); |
| 389 | EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return()); |
| 390 | EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature)) |
| 391 | .WillOnce(Return()); |
| 392 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) |
| 393 | .WillOnce(Return(true)); |
| 394 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore)) |
| 395 | .WillOnce(Return(true)); |
| 396 | EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return()); |
| 397 | EXPECT_CALL(blobMock, deleteBlob(ipmi_flash::activeImageBlobId)) |
Patrick Venture | 2862421 | 2021-11-09 09:04:03 -0800 | [diff] [blame] | 398 | .WillOnce(Return(true)); |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 399 | EXPECT_CALL(blobMock, getBlobList()) |
| 400 | .WillOnce(Return(std::vector<std::string>( |
| 401 | {ipmi_flash::staticLayoutBlobId, ipmi_flash::activeImageBlobId}))); |
| 402 | |
| 403 | updaterMain(&handler, &blobMock, image, signature, layout, defaultIgnore); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate) |
| 407 | { |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 408 | UpdateHandlerMock handler; |
| 409 | bool updateIgnore = true; |
| 410 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 411 | EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true)); |
| 412 | EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return()); |
| 413 | EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature)) |
| 414 | .WillOnce(Return()); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 415 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) |
| 416 | .WillOnce(Return(true)); |
| 417 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore)) |
| 418 | .WillOnce(Return(true)); |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 419 | EXPECT_CALL(blobMock, getBlobList()) |
| 420 | .WillOnce(Return(std::vector<std::string>({}))); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 421 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 422 | updaterMain(&handler, &blobMock, image, signature, layout, updateIgnore); |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 423 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 424 | |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 425 | TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure) |
| 426 | { |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 427 | UpdateHandlerMock handler; |
| 428 | |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 429 | EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true)); |
| 430 | EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return()); |
| 431 | EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature)) |
| 432 | .WillOnce(Return()); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 433 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 434 | .WillOnce(Return(false)); |
| 435 | EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return()); |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 436 | EXPECT_CALL(blobMock, getBlobList()) |
| 437 | .WillOnce(Return(std::vector<std::string>({}))); |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 438 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 439 | EXPECT_THROW(updaterMain(&handler, &blobMock, image, signature, layout, |
| 440 | defaultIgnore), |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 441 | ToolException); |
| 442 | } |
| 443 | |
| 444 | TEST_F(UpdaterTest, UpdateMainExceptsOnUpdateBlobFailure) |
| 445 | { |
| 446 | UpdateHandlerMock handler; |
| 447 | |
| 448 | EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true)); |
| 449 | EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return()); |
| 450 | EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature)) |
| 451 | .WillOnce(Return()); |
| 452 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) |
| 453 | .WillOnce(Return(true)); |
| 454 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore)) |
| 455 | .WillOnce(Return(false)); |
| 456 | EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return()); |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 457 | EXPECT_CALL(blobMock, getBlobList()) |
| 458 | .WillOnce(Return(std::vector<std::string>({}))); |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 459 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 460 | EXPECT_THROW(updaterMain(&handler, &blobMock, image, signature, layout, |
| 461 | defaultIgnore), |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 462 | ToolException); |
| 463 | } |
| 464 | |
| 465 | TEST_F(UpdaterTest, UpdateMainExceptsIfAvailableNotFound) |
| 466 | { |
| 467 | UpdateHandlerMock handler; |
| 468 | |
| 469 | EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(false)); |
| 470 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 471 | EXPECT_THROW(updaterMain(&handler, &blobMock, image, signature, layout, |
| 472 | defaultIgnore), |
Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 473 | ToolException); |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 476 | } // namespace host_tool |