Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 1 | /* The goal of these tests is to verify the behavior of all blob commands given |
| 2 | * the current state is verificationStarted. This state is achieved as a out of |
| 3 | * verificationPending. |
| 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 | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 21 | using ::testing::IsEmpty; |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 22 | using ::testing::Return; |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -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 | * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs |
| 40 | * will inform what canHandleBlob will return. |
| 41 | */ |
| 42 | |
| 43 | class FirmwareHandlerVerificationStartedTest : public IpmiOnlyFirmwareStaticTest |
| 44 | { |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 48 | * canHandleBlob(blob) |
| 49 | * getBlobIds() |
| 50 | */ |
| 51 | TEST_F(FirmwareHandlerVerificationStartedTest, GetBlobIdsReturnsExpectedList) |
| 52 | { |
| 53 | getToVerificationStarted(staticLayoutBlobId); |
| 54 | |
| 55 | std::vector<std::string> expectedList = { |
| 56 | activeImageBlobId, staticLayoutBlobId, hashBlobId, verifyBlobId}; |
| 57 | |
| 58 | EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expectedList)); |
| 59 | |
| 60 | for (const auto& blob : expectedList) |
| 61 | { |
| 62 | EXPECT_TRUE(handler->canHandleBlob(blob)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 67 | * stat(session) |
| 68 | */ |
| 69 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 70 | StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning) |
| 71 | { |
| 72 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 73 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 74 | .WillOnce(Return(ActionStatus::running)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 75 | |
| 76 | blobs::BlobMeta meta, expectedMeta = {}; |
| 77 | expectedMeta.size = 0; |
| 78 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 79 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 80 | static_cast<std::uint8_t>(ActionStatus::running)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 81 | |
| 82 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 83 | EXPECT_EQ(expectedMeta, meta); |
| 84 | } |
| 85 | |
| 86 | TEST_F(FirmwareHandlerVerificationStartedTest, |
Patrick Venture | d52c2c3 | 2019-05-30 09:54:35 -0700 | [diff] [blame] | 87 | StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsOther) |
| 88 | { |
| 89 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 90 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 91 | .WillOnce(Return(ActionStatus::unknown)); |
Patrick Venture | d52c2c3 | 2019-05-30 09:54:35 -0700 | [diff] [blame] | 92 | |
| 93 | blobs::BlobMeta meta, expectedMeta = {}; |
| 94 | expectedMeta.size = 0; |
| 95 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 96 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 97 | static_cast<std::uint8_t>(ActionStatus::unknown)); |
Patrick Venture | d52c2c3 | 2019-05-30 09:54:35 -0700 | [diff] [blame] | 98 | |
| 99 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 100 | EXPECT_EQ(expectedMeta, meta); |
| 101 | } |
| 102 | |
| 103 | TEST_F(FirmwareHandlerVerificationStartedTest, |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 104 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed) |
| 105 | { |
| 106 | /* If the returned state from the verification handler is failed, it sets |
| 107 | * commit_error and transitions to verificationCompleted. |
| 108 | */ |
| 109 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 110 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 111 | .WillOnce(Return(ActionStatus::failed)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 112 | |
| 113 | blobs::BlobMeta meta, expectedMeta = {}; |
| 114 | expectedMeta.size = 0; |
| 115 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 116 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 117 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 118 | |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 119 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 120 | EXPECT_EQ(expectedMeta, meta); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 121 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 125 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess) |
| 126 | { |
| 127 | /* If the returned state from the verification handler is success, it sets |
| 128 | * committed and transitions to verificationCompleted. |
| 129 | */ |
| 130 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 131 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 132 | .WillOnce(Return(ActionStatus::success)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 133 | |
| 134 | blobs::BlobMeta meta, expectedMeta = {}; |
| 135 | expectedMeta.size = 0; |
| 136 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 137 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 138 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 139 | |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 140 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 141 | EXPECT_EQ(expectedMeta, meta); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 142 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* TODO: Once verificationCompleted is the state, canHandleBlob should accept |
| 146 | * updateBlobId. |
| 147 | */ |
| 148 | |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 149 | /* TODO: deleteBlob(blob) */ |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 150 | |
| 151 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 152 | * stat(blob) |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 153 | */ |
| 154 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure) |
| 155 | { |
| 156 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 157 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 158 | |
| 159 | blobs::BlobMeta meta; |
| 160 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 161 | } |
| 162 | |
| 163 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure) |
| 164 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 165 | getToVerificationStarted(hashBlobId); |
| 166 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 167 | |
| 168 | blobs::BlobMeta meta; |
| 169 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 170 | } |
| 171 | |
| 172 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure) |
| 173 | { |
| 174 | /* the verifyBlobId is available starting at verificationPending. */ |
| 175 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 176 | ASSERT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 177 | |
| 178 | blobs::BlobMeta meta; |
| 179 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 180 | } |
| 181 | |
| 182 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess) |
| 183 | { |
| 184 | getToVerificationStarted(staticLayoutBlobId); |
| 185 | |
| 186 | blobs::BlobMeta expected; |
| 187 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 188 | expected.size = 0; |
| 189 | |
| 190 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 191 | for (const auto& blob : testBlobs) |
| 192 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 193 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 194 | |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 195 | blobs::BlobMeta meta = {}; |
| 196 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 197 | EXPECT_EQ(expected, meta); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 202 | * writemeta(session) |
| 203 | */ |
| 204 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 205 | WriteMetaOnVerifySessionReturnsFailure) |
| 206 | { |
| 207 | getToVerificationStarted(staticLayoutBlobId); |
| 208 | |
| 209 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 210 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * write(session) |
| 215 | */ |
Patrick Venture | 9c6de5a | 2019-05-24 15:12:26 -0700 | [diff] [blame] | 216 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 217 | WriteOnVerifySessionReturnsFailure) |
| 218 | { |
| 219 | getToVerificationStarted(staticLayoutBlobId); |
| 220 | |
| 221 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 222 | EXPECT_FALSE(handler->write(session, 0, bytes)); |
| 223 | } |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 226 | * open(blob) - there is nothing you can open, this state has an open file. |
Patrick Venture | d450910 | 2019-05-24 15:22:04 -0700 | [diff] [blame] | 227 | */ |
| 228 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 229 | AttemptToOpenImageFileReturnsFailure) |
| 230 | { |
| 231 | /* Attempt to open a file one normally can open, however, as there is |
| 232 | * already a file open, this will fail. |
| 233 | */ |
| 234 | getToVerificationStarted(staticLayoutBlobId); |
| 235 | |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 236 | auto blobsToOpen = handler->getBlobIds(); |
| 237 | for (const auto& blob : blobsToOpen) |
| 238 | { |
| 239 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 240 | } |
Patrick Venture | d450910 | 2019-05-24 15:22:04 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 244 | * read(session) |
| 245 | */ |
| 246 | TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty) |
| 247 | { |
| 248 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 249 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 253 | * commit(session) |
| 254 | */ |
| 255 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 256 | CommitOnVerifyDuringVerificationHasNoImpact) |
| 257 | { |
| 258 | getToVerificationStarted(staticLayoutBlobId); |
| 259 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 260 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 264 | * close(session) - close while state if verificationStarted without calling |
| 265 | * stat first will abort. |
Patrick Venture | 35b3fc9 | 2019-05-24 15:36:01 -0700 | [diff] [blame] | 266 | * TODO: implement this test when we implement abort. |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 267 | */ |
| 268 | |
| 269 | } // namespace |
| 270 | } // namespace ipmi_flash |