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 | |
| 66 | std::vector<std::string> expected = {updateBlobId, hashBlobId, |
| 67 | activeImageBlobId, staticLayoutBlobId}; |
| 68 | EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected)); |
| 69 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * TODO: deleteBlob(blob) |
| 73 | */ |
| 74 | |
| 75 | /* |
| 76 | * stat(blob) |
| 77 | */ |
Patrick Venture | fc34cfa | 2019-05-31 11:02:51 -0700 | [diff] [blame] | 78 | TEST_F(FirmwareHandlerUpdateStartedTest, StatOnActiveImageReturnsFailure) |
| 79 | { |
| 80 | getToUpdateStarted(); |
| 81 | |
| 82 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
| 83 | |
| 84 | blobs::BlobMeta meta; |
| 85 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 86 | } |
| 87 | |
| 88 | TEST_F(FirmwareHandlerUpdateStartedTest, StatOnUpdateBlobReturnsFailure) |
| 89 | { |
| 90 | getToUpdateStarted(); |
| 91 | |
| 92 | ASSERT_TRUE(handler->canHandleBlob(updateBlobId)); |
| 93 | |
| 94 | blobs::BlobMeta meta; |
| 95 | EXPECT_FALSE(handler->stat(updateBlobId, &meta)); |
| 96 | } |
| 97 | |
| 98 | TEST_F(FirmwareHandlerUpdateStartedTest, StatOnNormalBlobsReturnsSuccess) |
| 99 | { |
| 100 | getToUpdateStarted(); |
| 101 | |
| 102 | blobs::BlobMeta expected; |
| 103 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 104 | expected.size = 0; |
| 105 | |
| 106 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 107 | for (const auto& blob : testBlobs) |
| 108 | { |
| 109 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 110 | |
| 111 | blobs::BlobMeta meta = {}; |
| 112 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 113 | EXPECT_EQ(expected, meta); |
| 114 | } |
| 115 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 116 | |
| 117 | /* |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 118 | * writemeta(session) |
| 119 | */ |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 120 | TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure) |
| 121 | { |
| 122 | getToUpdateStarted(); |
| 123 | EXPECT_FALSE(handler->writeMeta(session, 0, {0x01})); |
| 124 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * write(session) |
| 128 | */ |
Patrick Venture | 575118d | 2019-05-31 11:20:35 -0700 | [diff] [blame] | 129 | TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure) |
| 130 | { |
| 131 | getToUpdateStarted(); |
| 132 | EXPECT_FALSE(handler->write(session, 0, {0x01})); |
| 133 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * read(session) |
| 137 | */ |
Patrick Venture | 73e2bdf | 2019-05-31 11:21:52 -0700 | [diff] [blame] | 138 | TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty) |
| 139 | { |
| 140 | getToUpdateStarted(); |
| 141 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 142 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 145 | * commit(session) |
| 146 | */ |
| 147 | TEST_F(FirmwareHandlerUpdateStartedTest, |
| 148 | CallingCommitShouldReturnTrueAndHaveNoEffect) |
| 149 | { |
| 150 | getToUpdateStarted(); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 151 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 152 | |
| 153 | EXPECT_TRUE(handler->commit(session, {})); |
| 154 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 155 | } |
| 156 | |
| 157 | /* |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 158 | * stat(session) - this will trigger a check, and the state may change. |
| 159 | */ |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 160 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 161 | CallStatChecksActionStatusReturnsRunningDoesNotChangeState) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 162 | { |
| 163 | getToUpdateStarted(); |
| 164 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 165 | .WillOnce(Return(ActionStatus::running)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 166 | |
| 167 | blobs::BlobMeta meta, expectedMeta = {}; |
| 168 | expectedMeta.size = 0; |
| 169 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 170 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 171 | static_cast<std::uint8_t>(ActionStatus::running)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 172 | |
| 173 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 174 | EXPECT_EQ(expectedMeta, meta); |
| 175 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 176 | } |
| 177 | |
| 178 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 179 | CallStatChecksActionStatusReturnsFailedChangesStateToCompleted) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 180 | { |
| 181 | getToUpdateStarted(); |
| 182 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 183 | .WillOnce(Return(ActionStatus::failed)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 184 | |
| 185 | blobs::BlobMeta meta, expectedMeta = {}; |
| 186 | expectedMeta.size = 0; |
| 187 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 188 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 189 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 190 | |
| 191 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 192 | EXPECT_EQ(expectedMeta, meta); |
| 193 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 194 | } |
| 195 | |
| 196 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 197 | CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 198 | { |
| 199 | getToUpdateStarted(); |
| 200 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 201 | .WillOnce(Return(ActionStatus::success)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 202 | |
| 203 | blobs::BlobMeta meta, expectedMeta = {}; |
| 204 | expectedMeta.size = 0; |
| 205 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 206 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 207 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 208 | |
| 209 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 210 | EXPECT_EQ(expectedMeta, meta); |
| 211 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 212 | } |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * TODO: close(session) - this will abort. |
| 216 | */ |
| 217 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 218 | } // namespace |
| 219 | } // namespace ipmi_flash |