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