Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -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 | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | using ::testing::Eq; |
| 13 | using ::testing::Return; |
| 14 | |
| 15 | TEST(FirmwareSessionStateTest, DataTypeIpmiNoMetadata) |
| 16 | { |
| 17 | /* Verifying running stat if the type of data session is IPMI returns no |
| 18 | * metadata. |
| 19 | */ |
| 20 | ImageHandlerMock imageMock1, imageMock2; |
| 21 | std::vector<HandlerPack> blobs = { |
| 22 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 23 | {"asdf", &imageMock2}, |
| 24 | }; |
| 25 | |
| 26 | std::vector<DataHandlerPack> data = { |
| 27 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 28 | }; |
| 29 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 30 | sdbusplus::SdBusMock sdbus_mock; |
| 31 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 32 | |
| 33 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 34 | std::move(bus_mock), blobs, data); |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 35 | |
| 36 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 37 | |
| 38 | EXPECT_TRUE(handler->open( |
| 39 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 40 | |
| 41 | int size = 512; |
| 42 | EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size)); |
| 43 | |
| 44 | struct BlobMeta meta; |
| 45 | EXPECT_TRUE(handler->stat(0, &meta)); |
| 46 | EXPECT_EQ(meta.blobState, |
| 47 | OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi); |
| 48 | EXPECT_EQ(meta.size, size); |
| 49 | EXPECT_EQ(meta.metadata.size(), 0); |
| 50 | } |
| 51 | |
| 52 | TEST(FirmwareSessionStateTest, DataTypeP2AReturnsMetadata) |
| 53 | { |
| 54 | /* Really any type that isn't IPMI can return metadata, but we only expect |
| 55 | * P2A to for now. Later, LPC may have reason to provide data, and can by |
| 56 | * simply implementing read(). |
| 57 | */ |
| 58 | ImageHandlerMock imageMock1, imageMock2; |
| 59 | std::vector<HandlerPack> blobs = { |
| 60 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 61 | {"asdf", &imageMock2}, |
| 62 | }; |
| 63 | |
| 64 | DataHandlerMock dataMock; |
| 65 | |
| 66 | std::vector<DataHandlerPack> data = { |
| 67 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 68 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 69 | }; |
| 70 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 71 | sdbusplus::SdBusMock sdbus_mock; |
| 72 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 73 | |
| 74 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 75 | std::move(bus_mock), blobs, data); |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 76 | |
| 77 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
| 78 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 79 | |
| 80 | EXPECT_TRUE(handler->open( |
| 81 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf")); |
| 82 | |
| 83 | int size = 512; |
| 84 | EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size)); |
| 85 | std::vector<std::uint8_t> mBytes = {0x01, 0x02}; |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 86 | EXPECT_CALL(dataMock, readMeta()).WillOnce(Return(mBytes)); |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 87 | |
| 88 | struct BlobMeta meta; |
| 89 | EXPECT_TRUE(handler->stat(0, &meta)); |
| 90 | EXPECT_EQ(meta.blobState, |
| 91 | OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc); |
| 92 | EXPECT_EQ(meta.size, size); |
| 93 | EXPECT_EQ(meta.metadata.size(), mBytes.size()); |
| 94 | EXPECT_EQ(meta.metadata[0], mBytes[0]); |
| 95 | EXPECT_EQ(meta.metadata[1], mBytes[1]); |
| 96 | } |
| 97 | |
| 98 | } // namespace blobs |