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" |
| 4 | |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 12 | using ::testing::Eq; |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 13 | using ::testing::Return; |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 14 | using ::testing::StrEq; |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 15 | |
| 16 | TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid) |
| 17 | { |
| 18 | /* The client passes write set, and a transport that's supported, and a |
| 19 | * firmware image blob_id that's supported. */ |
| 20 | |
| 21 | ImageHandlerMock imageMock; |
| 22 | |
| 23 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 24 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 25 | {"asdf", &imageMock}, |
| 26 | }; |
| 27 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 28 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 32 | |
| 33 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 34 | |
| 35 | EXPECT_TRUE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 36 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 37 | |
| 38 | /* The active image blob_id was added. */ |
| 39 | auto currentBlobs = handler->getBlobIds(); |
| 40 | EXPECT_EQ(3, currentBlobs.size()); |
| 41 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 42 | FirmwareBlobHandler::activeImageBlobID)); |
| 43 | } |
| 44 | |
| 45 | TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile) |
| 46 | { |
| 47 | /* Open the hash file by blob_id. */ |
| 48 | ImageHandlerMock imageMock; |
| 49 | |
| 50 | std::vector<HandlerPack> blobs = { |
| 51 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 52 | {"asdf", &imageMock}, |
| 53 | }; |
| 54 | std::vector<DataHandlerPack> data = { |
| 55 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 56 | }; |
| 57 | |
| 58 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 59 | |
| 60 | EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID))) |
| 61 | .WillOnce(Return(true)); |
| 62 | |
| 63 | EXPECT_TRUE(handler->open( |
| 64 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 65 | FirmwareBlobHandler::hashBlobID)); |
| 66 | |
| 67 | /* The active hash blob_id was added. */ |
| 68 | auto currentBlobs = handler->getBlobIds(); |
| 69 | EXPECT_EQ(3, currentBlobs.size()); |
| 70 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 71 | FirmwareBlobHandler::activeHashBlobID)); |
| 72 | } |
| 73 | |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 74 | TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerAllSucceeds) |
| 75 | { |
| 76 | /* Attempting to open a file that has an active handler, and use that active |
| 77 | * handler method. |
| 78 | */ |
| 79 | DataHandlerMock dataMock; |
| 80 | ImageHandlerMock imageMock; |
| 81 | |
| 82 | std::vector<HandlerPack> blobs = { |
| 83 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 84 | {"asdf", &imageMock}, |
| 85 | }; |
| 86 | std::vector<DataHandlerPack> data = { |
| 87 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 88 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 89 | }; |
| 90 | |
| 91 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 92 | |
| 93 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
| 94 | EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID))) |
| 95 | .WillOnce(Return(true)); |
| 96 | |
| 97 | EXPECT_TRUE(handler->open( |
| 98 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
| 99 | FirmwareBlobHandler::hashBlobID)); |
| 100 | |
| 101 | /* The active hash blob_id was added. */ |
| 102 | auto currentBlobs = handler->getBlobIds(); |
| 103 | EXPECT_EQ(3, currentBlobs.size()); |
| 104 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 105 | FirmwareBlobHandler::activeHashBlobID)); |
| 106 | } |
| 107 | |
| 108 | TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerReturnsFailure) |
| 109 | { |
| 110 | /* The data handler call returns failure on open, therefore open fails. */ |
| 111 | DataHandlerMock dataMock; |
| 112 | ImageHandlerMock imageMock; |
| 113 | |
| 114 | std::vector<HandlerPack> blobs = { |
| 115 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 116 | {"asdf", &imageMock}, |
| 117 | }; |
| 118 | std::vector<DataHandlerPack> data = { |
| 119 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 120 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 121 | }; |
| 122 | |
| 123 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 124 | |
| 125 | EXPECT_CALL(dataMock, open()).WillOnce(Return(false)); |
| 126 | |
| 127 | EXPECT_FALSE(handler->open( |
| 128 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
| 129 | FirmwareBlobHandler::hashBlobID)); |
| 130 | |
| 131 | /* The active hash blob_id was added. */ |
| 132 | auto currentBlobs = handler->getBlobIds(); |
| 133 | EXPECT_EQ(2, currentBlobs.size()); |
| 134 | } |
| 135 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 136 | TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsVerifyOpenFileCheck) |
| 137 | { |
| 138 | /* Verify only one file can be open at a time by opening a file, trying |
| 139 | * again, then closing, and trying again. |
| 140 | */ |
| 141 | ImageHandlerMock imageMock1, imageMock2; |
| 142 | |
| 143 | std::vector<HandlerPack> blobs = { |
| 144 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 145 | {"asdf", &imageMock2}, |
| 146 | }; |
| 147 | std::vector<DataHandlerPack> data = { |
| 148 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 149 | }; |
| 150 | |
| 151 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 152 | |
| 153 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 154 | |
| 155 | EXPECT_TRUE(handler->open( |
| 156 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 157 | |
| 158 | /* The active image blob_id was added. */ |
| 159 | auto currentBlobs = handler->getBlobIds(); |
| 160 | EXPECT_EQ(3, currentBlobs.size()); |
| 161 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 162 | FirmwareBlobHandler::activeImageBlobID)); |
| 163 | |
| 164 | /* Open the hash file (since we opened an image file). */ |
| 165 | EXPECT_FALSE(handler->open( |
| 166 | 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 167 | FirmwareBlobHandler::hashBlobID)); |
| 168 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame^] | 169 | EXPECT_TRUE(handler->close(0)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 170 | |
| 171 | EXPECT_CALL(imageMock1, open(StrEq(FirmwareBlobHandler::hashBlobID))) |
| 172 | .WillOnce(Return(true)); |
| 173 | |
| 174 | EXPECT_TRUE(handler->open( |
| 175 | 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 176 | FirmwareBlobHandler::hashBlobID)); |
| 177 | } |
| 178 | |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 179 | TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails) |
| 180 | { |
| 181 | /* Attempting to open the active image blob, when it's present will fail. |
| 182 | * |
| 183 | * TODO: We'll need another test that closes first because you can only have |
| 184 | * one file open at a time. |
| 185 | */ |
| 186 | ImageHandlerMock imageMock; |
| 187 | |
| 188 | std::vector<HandlerPack> blobs = { |
| 189 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 190 | {"asdf", &imageMock}, |
| 191 | }; |
| 192 | std::vector<DataHandlerPack> data = { |
| 193 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 194 | }; |
| 195 | |
| 196 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 197 | |
| 198 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 199 | |
| 200 | EXPECT_TRUE(handler->open( |
| 201 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 202 | |
| 203 | /* The active image blob_id was added. */ |
| 204 | auto currentBlobs = handler->getBlobIds(); |
| 205 | EXPECT_EQ(3, currentBlobs.size()); |
| 206 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 207 | FirmwareBlobHandler::activeImageBlobID)); |
| 208 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 209 | /* Close only active session, to verify it's failing on attempt to open a |
| 210 | * specific blob_id. |
| 211 | */ |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame^] | 212 | EXPECT_TRUE(handler->close(0)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 213 | |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 214 | EXPECT_FALSE(handler->open( |
| 215 | 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 216 | FirmwareBlobHandler::activeImageBlobID)); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails) |
| 220 | { |
| 221 | /* The image handler for a specific type of image is allowed to return |
| 222 | * failure on open. let's simulate that. */ |
| 223 | |
| 224 | ImageHandlerMock imageMock; |
| 225 | |
| 226 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 227 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 228 | {"asdf", &imageMock}, |
| 229 | }; |
| 230 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 231 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 235 | |
| 236 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false)); |
| 237 | |
| 238 | EXPECT_FALSE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 239 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 240 | |
| 241 | /* Verify blob_id list doesn't grow. */ |
| 242 | auto currentBlobs = handler->getBlobIds(); |
| 243 | EXPECT_EQ(2, currentBlobs.size()); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails) |
| 247 | { |
| 248 | /* The client must set the file write bit. */ |
| 249 | |
| 250 | ImageHandlerMock imageMock; |
| 251 | |
| 252 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 253 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 254 | {"asdf", &imageMock}, |
| 255 | }; |
| 256 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 257 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 261 | |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 262 | EXPECT_FALSE( |
| 263 | handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit) |
| 267 | { |
| 268 | /* The client sends a request with a transport mechanism not supported. */ |
| 269 | |
| 270 | ImageHandlerMock imageMock; |
| 271 | |
| 272 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 273 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 274 | {"asdf", &imageMock}, |
| 275 | }; |
| 276 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 277 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 281 | |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 282 | EXPECT_FALSE( |
| 283 | handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId) |
| 287 | { |
| 288 | /* The client sends a request with an invalid image blob_id. */ |
| 289 | |
| 290 | ImageHandlerMock imageMock; |
| 291 | |
| 292 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 293 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 294 | {"asdf", &imageMock}, |
| 295 | }; |
| 296 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 297 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 301 | |
| 302 | EXPECT_FALSE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 303 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* TODO: The client sends a request during verification. */ |
| 307 | /* TODO: The client sends a second request to open when there is already an open |
| 308 | * file. |
| 309 | */ |
| 310 | /* TODO: The client sends a request to open active image. */ |
| 311 | /* TODO: The client sends a request to open active hash. */ |
| 312 | |
| 313 | } // namespace blobs |