Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 1 | /* The goal of these tests is to verify the behavior of all blob commands given |
| 2 | * the current state is updatePending. This state is achieved as an exit from |
| 3 | * verificationCompleted. |
| 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 | ebe456f | 2019-05-30 16:40:39 -0700 | [diff] [blame] | 21 | using ::testing::IsEmpty; |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 22 | using ::testing::Return; |
Patrick Venture | ad93383 | 2019-05-30 14:13:29 -0700 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -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 FirmwareHandlerUpdatePendingTest : public IpmiOnlyFirmwareStaticTest |
| 44 | { |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * There are the following calls (parameters may vary): |
| 49 | * canHandleBlob(blob) |
| 50 | * getBlobIds |
| 51 | */ |
Patrick Venture | ad93383 | 2019-05-30 14:13:29 -0700 | [diff] [blame] | 52 | TEST_F(FirmwareHandlerUpdatePendingTest, GetBlobsListHasExpectedValues) |
| 53 | { |
| 54 | getToUpdatePending(); |
| 55 | |
| 56 | std::vector<std::string> expected = {updateBlobId, activeImageBlobId, |
| 57 | hashBlobId, staticLayoutBlobId}; |
| 58 | EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected)); |
| 59 | } |
| 60 | |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 61 | /* |
Patrick Venture | 19f5d88 | 2019-05-30 14:34:55 -0700 | [diff] [blame] | 62 | * open(blob) - because updatePending is in a fileOpen==false state, one can |
| 63 | * then open blobs. However, because we're in a special state, we will restrict |
| 64 | * them s.t. they can only open the updateBlobId. |
| 65 | */ |
| 66 | TEST_F(FirmwareHandlerUpdatePendingTest, |
| 67 | OpenUpdateBlobIdIsSuccessfulAndDoesNotChangeState) |
| 68 | { |
| 69 | getToUpdatePending(); |
| 70 | |
| 71 | /* Opening the update blob isn't interesting, except it's required for |
| 72 | * commit() which triggers the update process. |
| 73 | */ |
| 74 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
Patrick Venture | 6b0aa18 | 2019-05-30 14:47:32 -0700 | [diff] [blame] | 75 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
Patrick Venture | 19f5d88 | 2019-05-30 14:34:55 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST_F(FirmwareHandlerUpdatePendingTest, OpenAnyBlobOtherThanUpdateFails) |
| 79 | { |
| 80 | getToUpdatePending(); |
| 81 | |
| 82 | auto blobs = handler->getBlobIds(); |
| 83 | for (const auto& blob : blobs) |
| 84 | { |
| 85 | if (blob == updateBlobId) |
| 86 | { |
| 87 | continue; |
| 88 | } |
| 89 | EXPECT_FALSE(handler->open(session, flags, blob)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
Patrick Venture | 6b0aa18 | 2019-05-30 14:47:32 -0700 | [diff] [blame] | 94 | * close(session) - close from this state is uninteresting. |
| 95 | */ |
| 96 | TEST_F(FirmwareHandlerUpdatePendingTest, CloseUpdateBlobDoesNotChangeState) |
| 97 | { |
| 98 | /* Verify nothing changes when one just opens, then closes the updateBlobId. |
| 99 | */ |
| 100 | getToUpdatePending(); |
| 101 | |
| 102 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 103 | |
| 104 | handler->close(session); |
| 105 | |
| 106 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 107 | EXPECT_TRUE(handler->canHandleBlob(updateBlobId)); |
| 108 | } |
| 109 | |
| 110 | /* |
Patrick Venture | 94bb970 | 2019-05-30 14:53:22 -0700 | [diff] [blame] | 111 | * writemeta(session) - this will return failure. |
| 112 | */ |
| 113 | TEST_F(FirmwareHandlerUpdatePendingTest, WriteMetaToUpdateBlobReturnsFailure) |
| 114 | { |
| 115 | getToUpdatePending(); |
| 116 | |
| 117 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 118 | EXPECT_FALSE(handler->writeMeta(session, 0, {0x01})); |
| 119 | } |
| 120 | |
| 121 | /* |
Patrick Venture | 4e2fdcd | 2019-05-30 14:58:57 -0700 | [diff] [blame] | 122 | * write(session) |
| 123 | */ |
| 124 | TEST_F(FirmwareHandlerUpdatePendingTest, WriteToUpdateBlobReturnsFailure) |
| 125 | { |
| 126 | getToUpdatePending(); |
| 127 | |
| 128 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 129 | EXPECT_FALSE(handler->write(session, 0, {0x01})); |
| 130 | } |
| 131 | |
| 132 | /* |
Patrick Venture | ebe456f | 2019-05-30 16:40:39 -0700 | [diff] [blame] | 133 | * read(session) |
| 134 | */ |
| 135 | TEST_F(FirmwareHandlerUpdatePendingTest, ReadFromUpdateBlobIdReturnsEmpty) |
| 136 | { |
| 137 | getToUpdatePending(); |
| 138 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 139 | } |
| 140 | |
| 141 | /* |
Patrick Venture | 5f56269 | 2019-05-30 16:49:46 -0700 | [diff] [blame] | 142 | * stat(blob) |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 143 | */ |
Patrick Venture | 5f56269 | 2019-05-30 16:49:46 -0700 | [diff] [blame] | 144 | TEST_F(FirmwareHandlerUpdatePendingTest, StatOnActiveImageReturnsFailure) |
| 145 | { |
| 146 | getToUpdatePending(); |
| 147 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
| 148 | |
| 149 | blobs::BlobMeta meta; |
| 150 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 151 | } |
| 152 | |
| 153 | TEST_F(FirmwareHandlerUpdatePendingTest, StatOnUpdateBlobReturnsFailure) |
| 154 | { |
| 155 | getToUpdatePending(); |
| 156 | ASSERT_TRUE(handler->canHandleBlob(updateBlobId)); |
| 157 | |
| 158 | blobs::BlobMeta meta; |
| 159 | EXPECT_FALSE(handler->stat(updateBlobId, &meta)); |
| 160 | } |
| 161 | |
| 162 | TEST_F(FirmwareHandlerUpdatePendingTest, StatOnNormalBlobsReturnsSuccess) |
| 163 | { |
| 164 | getToUpdatePending(); |
| 165 | |
| 166 | blobs::BlobMeta expected; |
| 167 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 168 | expected.size = 0; |
| 169 | |
| 170 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 171 | for (const auto& blob : testBlobs) |
| 172 | { |
| 173 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 174 | |
| 175 | blobs::BlobMeta meta = {}; |
| 176 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 177 | EXPECT_EQ(expected, meta); |
| 178 | } |
| 179 | } |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
Patrick Venture | 40d7ffc | 2019-05-30 17:12:06 -0700 | [diff] [blame] | 182 | * stat(session) |
| 183 | * In this case, you can open updateBlobId without changing state, therefore, |
| 184 | * let's call stat() against a session against this file. This done, ahead of |
| 185 | * commit() should report the state as "other." |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 186 | */ |
Patrick Venture | 40d7ffc | 2019-05-30 17:12:06 -0700 | [diff] [blame] | 187 | TEST_F(FirmwareHandlerUpdatePendingTest, |
| 188 | SessionStatOnUpdateBlobIdReturnsFailure) |
| 189 | { |
| 190 | getToUpdatePending(); |
| 191 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 192 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 193 | |
| 194 | blobs::BlobMeta meta, expectedMeta = {}; |
| 195 | expectedMeta.size = 0; |
| 196 | expectedMeta.blobState = flags; |
| 197 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 198 | static_cast<std::uint8_t>(ActionStatus::unknown)); |
Patrick Venture | 40d7ffc | 2019-05-30 17:12:06 -0700 | [diff] [blame] | 199 | |
| 200 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 201 | EXPECT_EQ(expectedMeta, meta); |
| 202 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 203 | } |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 204 | |
| 205 | /* |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 206 | * commit(session) |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 207 | */ |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 208 | TEST_F(FirmwareHandlerUpdatePendingTest, |
| 209 | CommitOnUpdateBlobTriggersUpdateAndChangesState) |
| 210 | { |
| 211 | /* Commit triggers the update mechanism (similarly for the verifyBlobId) and |
| 212 | * changes state to updateStarted. |
| 213 | */ |
| 214 | getToUpdatePending(); |
| 215 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 216 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 217 | |
| 218 | EXPECT_CALL(*updateMockPtr, triggerUpdate()).WillOnce(Return(true)); |
| 219 | |
| 220 | EXPECT_TRUE(handler->commit(session, {})); |
| 221 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 222 | } |
| 223 | |
| 224 | TEST_F(FirmwareHandlerUpdatePendingTest, |
| 225 | CommitOnUpdateBlobTriggersUpdateAndReturnsFailureDoesNotChangeState) |
| 226 | { |
| 227 | getToUpdatePending(); |
| 228 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 229 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 230 | |
| 231 | EXPECT_CALL(*updateMockPtr, triggerUpdate()).WillOnce(Return(false)); |
| 232 | |
| 233 | EXPECT_FALSE(handler->commit(session, {})); |
| 234 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 235 | } |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 236 | |
| 237 | /* |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 238 | * TODO: deleteBlob(blob) |
Patrick Venture | 0cd945c | 2019-05-30 13:36:53 -0700 | [diff] [blame] | 239 | */ |
| 240 | |
| 241 | } // namespace |
| 242 | } // namespace ipmi_flash |