Patrick Venture | 9e5aab2 | 2018-11-09 20:49:28 -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 | 9e5aab2 | 2018-11-09 20:49:28 -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(FirmwareHandlerDeleteTest, DeleteActiveHashSucceeds) |
| 18 | { |
| 19 | /* Delete active image succeeds. */ |
| 20 | DataHandlerMock dataMock; |
| 21 | ImageHandlerMock imageMock; |
| 22 | |
| 23 | std::vector<HandlerPack> blobs = { |
| 24 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
| 25 | {"asdf", &imageMock}, |
| 26 | }; |
| 27 | std::vector<DataHandlerPack> data = { |
| 28 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 29 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 30 | }; |
| 31 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 32 | sdbusplus::SdBusMock sdbus_mock; |
| 33 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 34 | |
| 35 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 36 | std::move(bus_mock), blobs, data); |
Patrick Venture | 9e5aab2 | 2018-11-09 20:49:28 -0800 | [diff] [blame] | 37 | |
| 38 | EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID))) |
| 39 | .WillOnce(Return(true)); |
| 40 | |
| 41 | EXPECT_TRUE(handler->open( |
| 42 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 43 | FirmwareBlobHandler::hashBlobID)); |
| 44 | |
| 45 | /* The active hash blob_id was added. */ |
| 46 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 47 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 9e5aab2 | 2018-11-09 20:49:28 -0800 | [diff] [blame] | 48 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 49 | FirmwareBlobHandler::activeHashBlobID)); |
| 50 | |
| 51 | /* Set up close() expectations. */ |
| 52 | EXPECT_CALL(imageMock, close()); |
| 53 | EXPECT_TRUE(handler->close(0)); |
| 54 | |
| 55 | currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 56 | EXPECT_EQ(4, currentBlobs.size()); |
Patrick Venture | 9e5aab2 | 2018-11-09 20:49:28 -0800 | [diff] [blame] | 57 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 58 | FirmwareBlobHandler::activeHashBlobID)); |
| 59 | |
| 60 | /* Delete the blob. */ |
| 61 | EXPECT_TRUE(handler->deleteBlob(FirmwareBlobHandler::activeHashBlobID)); |
| 62 | |
| 63 | currentBlobs = handler->getBlobIds(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 64 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 9e5aab2 | 2018-11-09 20:49:28 -0800 | [diff] [blame] | 65 | EXPECT_EQ(0, std::count(currentBlobs.begin(), currentBlobs.end(), |
| 66 | FirmwareBlobHandler::activeHashBlobID)); |
| 67 | } |
| 68 | |
| 69 | } // namespace blobs |