Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
| 3 | #include "image_mock.hpp" |
| 4 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 5 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | namespace blobs |
| 12 | { |
| 13 | using ::testing::Eq; |
| 14 | using ::testing::Return; |
| 15 | using ::testing::StrEq; |
| 16 | |
| 17 | TEST(FirmwareHandlerCloseTest, CloseSuceedsWithDataHandler) |
| 18 | { |
| 19 | /* Boring test where you open a blob_id, then verify that when it's closed |
| 20 | * everything looks right. |
| 21 | */ |
| 22 | DataHandlerMock dataMock; |
| 23 | ImageHandlerMock imageMock; |
| 24 | |
| 25 | std::vector<HandlerPack> blobs = { |
| 26 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 27 | {"asdf", &imageMock}, |
| 28 | }; |
| 29 | std::vector<DataHandlerPack> data = { |
| 30 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 31 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 32 | }; |
| 33 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 34 | sdbusplus::SdBusMock sdbus_mock; |
| 35 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 36 | |
| 37 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 38 | std::move(bus_mock), blobs, data); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 39 | |
| 40 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
| 41 | EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID))) |
| 42 | .WillOnce(Return(true)); |
| 43 | |
| 44 | EXPECT_TRUE(handler->open( |
| 45 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
| 46 | FirmwareBlobHandler::hashBlobID)); |
| 47 | |
| 48 | /* The active hash blob_id was added. */ |
| 49 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 50 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 51 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 52 | FirmwareBlobHandler::activeHashBlobID)); |
| 53 | |
| 54 | /* Set up close() expectations. */ |
| 55 | EXPECT_CALL(dataMock, close()); |
| 56 | EXPECT_CALL(imageMock, close()); |
| 57 | EXPECT_TRUE(handler->close(0)); |
| 58 | |
| 59 | /* Close does not delete the active blob id. This indicates that there is |
| 60 | * data queued. |
| 61 | */ |
| 62 | } |
| 63 | |
| 64 | TEST(FirmwareHandlerCloseTest, CloseSuceedsWithoutDataHandler) |
| 65 | { |
| 66 | /* Boring test where you open a blob_id using ipmi, so there's no data |
| 67 | * handler, and it's closed and everything looks right. |
| 68 | */ |
| 69 | DataHandlerMock dataMock; |
| 70 | ImageHandlerMock imageMock; |
| 71 | |
| 72 | std::vector<HandlerPack> blobs = { |
| 73 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 74 | {"asdf", &imageMock}, |
| 75 | }; |
| 76 | std::vector<DataHandlerPack> data = { |
| 77 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 78 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 79 | }; |
| 80 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 81 | sdbusplus::SdBusMock sdbus_mock; |
| 82 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 83 | |
| 84 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 85 | std::move(bus_mock), blobs, data); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 86 | |
| 87 | EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID))) |
| 88 | .WillOnce(Return(true)); |
| 89 | |
| 90 | EXPECT_TRUE(handler->open( |
| 91 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 92 | FirmwareBlobHandler::hashBlobID)); |
| 93 | |
| 94 | /* The active hash blob_id was added. */ |
| 95 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 96 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 97 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 98 | FirmwareBlobHandler::activeHashBlobID)); |
| 99 | |
| 100 | /* Set up close() expectations. */ |
| 101 | EXPECT_CALL(imageMock, close()); |
| 102 | EXPECT_TRUE(handler->close(0)); |
| 103 | } |
| 104 | |
| 105 | } // namespace blobs |