Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
| 3 | #include "image_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 4 | #include "util.hpp" |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 5 | #include "verification_mock.hpp" |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 6 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 7 | #include <memory> |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | namespace blobs |
| 14 | { |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 15 | using ::testing::Eq; |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 16 | using ::testing::Return; |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 17 | using ::testing::StrEq; |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 18 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 19 | class FirmwareHandlerOpenTestIpmiOnly : public ::testing::Test |
| 20 | { |
| 21 | protected: |
| 22 | ImageHandlerMock imageMock; |
| 23 | std::vector<HandlerPack> blobs; |
| 24 | std::vector<DataHandlerPack> data; |
| 25 | std::unique_ptr<GenericBlobInterface> handler; |
| 26 | |
| 27 | void SetUp() override |
| 28 | { |
| 29 | blobs = { |
| 30 | {hashBlobId, &imageMock}, |
| 31 | {"asdf", &imageMock}, |
| 32 | }; |
| 33 | data = { |
| 34 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 35 | }; |
| 36 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 37 | blobs, data, CreateVerifyMock()); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | class FirmwareHandlerOpenTestLpc : public ::testing::Test |
| 42 | { |
| 43 | protected: |
| 44 | DataHandlerMock dataMock; |
| 45 | ImageHandlerMock imageMock; |
| 46 | std::vector<HandlerPack> blobs; |
| 47 | std::vector<DataHandlerPack> data; |
| 48 | std::unique_ptr<GenericBlobInterface> handler; |
| 49 | |
| 50 | void SetUp() override |
| 51 | { |
| 52 | blobs = { |
| 53 | {hashBlobId, &imageMock}, |
| 54 | {"asdf", &imageMock}, |
| 55 | }; |
| 56 | data = { |
| 57 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 58 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 59 | }; |
| 60 | |
| 61 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 62 | blobs, data, CreateVerifyMock()); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValid) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 67 | { |
| 68 | /* The client passes write set, and a transport that's supported, and a |
| 69 | * firmware image blob_id that's supported. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 70 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 71 | |
| 72 | EXPECT_TRUE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 73 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 74 | |
| 75 | /* The active image blob_id was added. */ |
| 76 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 77 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 78 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 79 | activeImageBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 82 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile) |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 83 | { |
| 84 | /* Open the hash file by blob_id. */ |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 85 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 86 | |
| 87 | EXPECT_TRUE(handler->open( |
| 88 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 89 | hashBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 90 | |
| 91 | /* The active hash blob_id was added. */ |
| 92 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 93 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 94 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 95 | activeHashBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 98 | TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds) |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 99 | { |
| 100 | /* Attempting to open a file that has an active handler, and use that active |
| 101 | * handler method. |
| 102 | */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 103 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 104 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 105 | |
| 106 | EXPECT_TRUE(handler->open( |
| 107 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 108 | hashBlobId)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 109 | |
| 110 | /* The active hash blob_id was added. */ |
| 111 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 112 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 113 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 114 | activeHashBlobId)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 117 | TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure) |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 118 | { |
| 119 | /* The data handler call returns failure on open, therefore open fails. */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 120 | EXPECT_CALL(dataMock, open()).WillOnce(Return(false)); |
| 121 | |
| 122 | EXPECT_FALSE(handler->open( |
| 123 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 124 | hashBlobId)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 125 | |
| 126 | /* The active hash blob_id was added. */ |
| 127 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 128 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 131 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, |
| 132 | OpenEverythingSucceedsVerifyOpenFileCheck) |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 133 | { |
| 134 | /* Verify only one file can be open at a time by opening a file, trying |
| 135 | * again, then closing, and trying again. |
| 136 | */ |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 137 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 138 | |
| 139 | EXPECT_TRUE(handler->open( |
| 140 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 141 | |
| 142 | /* The active image blob_id was added. */ |
| 143 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 144 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 145 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 146 | activeImageBlobId)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 147 | |
| 148 | /* Open the hash file (since we opened an image file). */ |
| 149 | EXPECT_FALSE(handler->open( |
| 150 | 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 151 | hashBlobId)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 152 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 153 | EXPECT_TRUE(handler->close(0)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 154 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 155 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 156 | |
| 157 | EXPECT_TRUE(handler->open( |
| 158 | 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 159 | hashBlobId)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 162 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails) |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 163 | { |
| 164 | /* Attempting to open the active image blob, when it's present will fail. |
| 165 | * |
| 166 | * TODO: We'll need another test that closes first because you can only have |
| 167 | * one file open at a time. |
| 168 | */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 169 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 170 | |
| 171 | EXPECT_TRUE(handler->open( |
| 172 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 173 | |
| 174 | /* The active image blob_id was added. */ |
| 175 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 176 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 177 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 178 | activeImageBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 179 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 180 | /* Close only active session, to verify it's failing on attempt to open a |
| 181 | * specific blob_id. |
| 182 | */ |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 183 | EXPECT_TRUE(handler->close(0)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 184 | |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 185 | EXPECT_FALSE(handler->open( |
| 186 | 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 187 | activeImageBlobId)); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 190 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, |
| 191 | OpenWithEverythingValidImageHandlerFails) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 192 | { |
| 193 | /* The image handler for a specific type of image is allowed to return |
| 194 | * failure on open. let's simulate that. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 195 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false)); |
| 196 | |
| 197 | EXPECT_FALSE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 198 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 199 | |
| 200 | /* Verify blob_id list doesn't grow. */ |
| 201 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 202 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 205 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 206 | { |
| 207 | /* The client must set the file write bit. */ |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 208 | EXPECT_FALSE( |
| 209 | handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 212 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 213 | { |
| 214 | /* The client sends a request with a transport mechanism not supported. */ |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 215 | EXPECT_FALSE( |
| 216 | handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 219 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 220 | { |
| 221 | /* The client sends a request with an invalid image blob_id. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 222 | EXPECT_FALSE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 223 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* TODO: The client sends a request during verification. */ |
| 227 | /* TODO: The client sends a second request to open when there is already an open |
| 228 | * file. |
| 229 | */ |
| 230 | /* TODO: The client sends a request to open active image. */ |
| 231 | /* TODO: The client sends a request to open active hash. */ |
| 232 | |
| 233 | } // namespace blobs |