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