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" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 4 | #include "util.hpp" |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 5 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 6 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace blobs |
| 13 | { |
| 14 | using ::testing::Eq; |
| 15 | using ::testing::Return; |
| 16 | using ::testing::StrEq; |
| 17 | |
| 18 | TEST(FirmwareHandlerCloseTest, CloseSuceedsWithDataHandler) |
| 19 | { |
| 20 | /* Boring test where you open a blob_id, then verify that when it's closed |
| 21 | * everything looks right. |
| 22 | */ |
| 23 | DataHandlerMock dataMock; |
| 24 | ImageHandlerMock imageMock; |
| 25 | |
| 26 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 27 | {hashBlobId, &imageMock}, |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 28 | {"asdf", &imageMock}, |
| 29 | }; |
| 30 | std::vector<DataHandlerPack> data = { |
| 31 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 32 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 33 | }; |
| 34 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 35 | sdbusplus::SdBusMock sdbus_mock; |
| 36 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 37 | |
| 38 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 39 | std::move(bus_mock), blobs, data); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 40 | |
| 41 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 42 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 43 | |
| 44 | EXPECT_TRUE(handler->open( |
| 45 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 46 | hashBlobId)); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 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(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 52 | activeHashBlobId)); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 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 = { |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 73 | {hashBlobId, &imageMock}, |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 87 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 88 | |
| 89 | EXPECT_TRUE(handler->open( |
| 90 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 91 | hashBlobId)); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 92 | |
| 93 | /* The active hash blob_id was added. */ |
| 94 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 95 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 96 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame^] | 97 | activeHashBlobId)); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 98 | |
| 99 | /* Set up close() expectations. */ |
| 100 | EXPECT_CALL(imageMock, close()); |
| 101 | EXPECT_TRUE(handler->close(0)); |
| 102 | } |
| 103 | |
| 104 | } // namespace blobs |