Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 1 | /* The goal of these tests is to verify the behavior of all blob commands given |
| 2 | * the current state is updateStarted. This state is achieved as an exit from |
| 3 | * updatePending. |
| 4 | */ |
| 5 | #include "firmware_handler.hpp" |
| 6 | #include "firmware_unittest.hpp" |
| 7 | #include "status.hpp" |
| 8 | #include "util.hpp" |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | namespace ipmi_flash |
| 17 | { |
| 18 | namespace |
| 19 | { |
| 20 | |
Patrick Venture | 73e2bdf | 2019-05-31 11:21:52 -0700 | [diff] [blame] | 21 | using ::testing::IsEmpty; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 22 | using ::testing::Return; |
Patrick Venture | d9a9578 | 2019-05-31 10:46:56 -0700 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * There are the following calls (parameters may vary): |
| 27 | * canHandleBlob(blob) |
| 28 | * getBlobIds |
| 29 | * deleteBlob(blob) |
| 30 | * stat(blob) |
| 31 | * stat(session) |
| 32 | * open(blob) |
| 33 | * close(session) |
| 34 | * writemeta(session) |
| 35 | * write(session) |
| 36 | * read(session) |
| 37 | * commit(session) |
| 38 | */ |
| 39 | |
| 40 | class FirmwareHandlerUpdateStartedTest : public IpmiOnlyFirmwareStaticTest |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame^] | 41 | {}; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 42 | |
Patrick Venture | f515b9c | 2019-05-31 10:38:16 -0700 | [diff] [blame] | 43 | /* |
| 44 | * open(blob) |
| 45 | */ |
| 46 | TEST_F(FirmwareHandlerUpdateStartedTest, AttemptToOpenFilesReturnsFailure) |
| 47 | { |
| 48 | /* In state updateStarted a file is open, which means no others can be. */ |
| 49 | getToUpdateStarted(); |
| 50 | |
| 51 | auto blobsToOpen = handler->getBlobIds(); |
| 52 | for (const auto& blob : blobsToOpen) |
| 53 | { |
| 54 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 55 | } |
| 56 | } |
| 57 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 58 | /* canHandleBlob(blob) |
| 59 | * getBlobIds |
| 60 | */ |
Patrick Venture | d9a9578 | 2019-05-31 10:46:56 -0700 | [diff] [blame] | 61 | TEST_F(FirmwareHandlerUpdateStartedTest, VerifyListOfBlobs) |
| 62 | { |
| 63 | getToUpdateStarted(); |
| 64 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 65 | EXPECT_THAT( |
| 66 | handler->getBlobIds(), |
| 67 | UnorderedElementsAreArray( |
| 68 | {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId})); |
Patrick Venture | d9a9578 | 2019-05-31 10:46:56 -0700 | [diff] [blame] | 69 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 72 | * deleteBlob(blob) |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 73 | */ |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 74 | TEST_F(FirmwareHandlerUpdateStartedTest, DeleteBlobReturnsFalse) |
| 75 | { |
| 76 | /* Try deleting all blobs, it doesn't really matter which though because you |
| 77 | * cannot close out an open session, therefore you must fail to delete |
| 78 | * anything unless everything is closed. |
| 79 | */ |
| 80 | getToUpdateStarted(); |
| 81 | auto blobs = handler->getBlobIds(); |
| 82 | for (const auto& b : blobs) |
| 83 | { |
| 84 | EXPECT_FALSE(handler->deleteBlob(b)); |
| 85 | } |
| 86 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * stat(blob) |
| 90 | */ |
Patrick Venture | fc34cfa | 2019-05-31 11:02:51 -0700 | [diff] [blame] | 91 | TEST_F(FirmwareHandlerUpdateStartedTest, StatOnActiveImageReturnsFailure) |
| 92 | { |
| 93 | getToUpdateStarted(); |
| 94 | |
| 95 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
| 96 | |
| 97 | blobs::BlobMeta meta; |
| 98 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 99 | } |
| 100 | |
| 101 | TEST_F(FirmwareHandlerUpdateStartedTest, StatOnUpdateBlobReturnsFailure) |
| 102 | { |
| 103 | getToUpdateStarted(); |
| 104 | |
| 105 | ASSERT_TRUE(handler->canHandleBlob(updateBlobId)); |
| 106 | |
| 107 | blobs::BlobMeta meta; |
| 108 | EXPECT_FALSE(handler->stat(updateBlobId, &meta)); |
| 109 | } |
| 110 | |
| 111 | TEST_F(FirmwareHandlerUpdateStartedTest, StatOnNormalBlobsReturnsSuccess) |
| 112 | { |
| 113 | getToUpdateStarted(); |
| 114 | |
Patrick Venture | fc34cfa | 2019-05-31 11:02:51 -0700 | [diff] [blame] | 115 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 116 | for (const auto& blob : testBlobs) |
| 117 | { |
| 118 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 119 | |
| 120 | blobs::BlobMeta meta = {}; |
| 121 | EXPECT_TRUE(handler->stat(blob, &meta)); |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 122 | EXPECT_EQ(expectedIdleMeta, meta); |
Patrick Venture | fc34cfa | 2019-05-31 11:02:51 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 127 | * writemeta(session) |
| 128 | */ |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 129 | TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure) |
| 130 | { |
| 131 | getToUpdateStarted(); |
| 132 | EXPECT_FALSE(handler->writeMeta(session, 0, {0x01})); |
| 133 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * write(session) |
| 137 | */ |
Patrick Venture | 575118d | 2019-05-31 11:20:35 -0700 | [diff] [blame] | 138 | TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure) |
| 139 | { |
| 140 | getToUpdateStarted(); |
| 141 | EXPECT_FALSE(handler->write(session, 0, {0x01})); |
| 142 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * read(session) |
| 146 | */ |
Patrick Venture | 73e2bdf | 2019-05-31 11:21:52 -0700 | [diff] [blame] | 147 | TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty) |
| 148 | { |
| 149 | getToUpdateStarted(); |
| 150 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 151 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 154 | * commit(session) |
| 155 | */ |
| 156 | TEST_F(FirmwareHandlerUpdateStartedTest, |
| 157 | CallingCommitShouldReturnTrueAndHaveNoEffect) |
| 158 | { |
| 159 | getToUpdateStarted(); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 160 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 161 | |
| 162 | EXPECT_TRUE(handler->commit(session, {})); |
| 163 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 164 | } |
| 165 | |
| 166 | /* |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 167 | * stat(session) - this will trigger a check, and the state may change. |
| 168 | */ |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 169 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 170 | CallStatChecksActionStatusReturnsRunningDoesNotChangeState) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 171 | { |
| 172 | getToUpdateStarted(); |
| 173 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 174 | .WillOnce(Return(ActionStatus::running)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 175 | |
| 176 | blobs::BlobMeta meta, expectedMeta = {}; |
| 177 | expectedMeta.size = 0; |
| 178 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 179 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 180 | static_cast<std::uint8_t>(ActionStatus::running)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 181 | |
| 182 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 183 | EXPECT_EQ(expectedMeta, meta); |
| 184 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 185 | } |
| 186 | |
| 187 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 188 | CallStatChecksActionStatusReturnsFailedChangesStateToCompleted) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 189 | { |
| 190 | getToUpdateStarted(); |
| 191 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 192 | .WillOnce(Return(ActionStatus::failed)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 193 | |
| 194 | blobs::BlobMeta meta, expectedMeta = {}; |
| 195 | expectedMeta.size = 0; |
| 196 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 197 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 198 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 199 | |
| 200 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 201 | EXPECT_EQ(expectedMeta, meta); |
| 202 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 203 | } |
| 204 | |
| 205 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 206 | CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 207 | { |
| 208 | getToUpdateStarted(); |
| 209 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 210 | .WillOnce(Return(ActionStatus::success)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 211 | |
| 212 | blobs::BlobMeta meta, expectedMeta = {}; |
| 213 | expectedMeta.size = 0; |
| 214 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 215 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 216 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 217 | |
| 218 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 219 | EXPECT_EQ(expectedMeta, meta); |
| 220 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 221 | } |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 222 | |
| 223 | /* |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 224 | * close(session) - this will abort. |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 225 | */ |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 226 | TEST_F(FirmwareHandlerUpdateStartedTest, CloseOnUpdateDuringUpdateAbortsProcess) |
| 227 | { |
| 228 | getToUpdateStarted(); |
| 229 | EXPECT_CALL(*updateMockPtr, abort()).Times(1); |
| 230 | |
| 231 | EXPECT_TRUE(handler->close(session)); |
| 232 | |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 233 | EXPECT_THAT(handler->getBlobIds(), |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 234 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 235 | |
| 236 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 237 | } |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 238 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 239 | } // namespace |
| 240 | } // namespace ipmi_flash |