Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
| 3 | #include "image_mock.hpp" |
| 4 | |
| 5 | #include <cstdint> |
| 6 | #include <cstring> |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 7 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace blobs |
| 13 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 14 | using ::testing::Eq; |
| 15 | using ::testing::Return; |
| 16 | |
| 17 | TEST(FirmwareHandlerWriteTest, DataTypeIpmiWriteSuccess) |
| 18 | { |
| 19 | /* Verify if data type ipmi, it calls write with the bytes. */ |
| 20 | |
| 21 | ImageHandlerMock imageMock1, imageMock2; |
| 22 | std::vector<HandlerPack> blobs = { |
| 23 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 24 | {"asdf", &imageMock2}, |
| 25 | }; |
| 26 | |
| 27 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 28 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 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( |
| 35 | std::move(bus_mock), blobs, data); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 36 | |
| 37 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 38 | |
| 39 | EXPECT_TRUE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 40 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 41 | |
| 42 | std::vector<std::uint8_t> bytes = {0xaa, 0x55}; |
| 43 | |
| 44 | EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true)); |
| 45 | EXPECT_TRUE(handler->write(0, 0, bytes)); |
| 46 | } |
| 47 | |
| 48 | TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteSuccess) |
| 49 | { |
| 50 | /* Verify if data type non-ipmi, it calls write with the length. */ |
| 51 | |
| 52 | ImageHandlerMock imageMock1, imageMock2; |
| 53 | std::vector<HandlerPack> blobs = { |
| 54 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 55 | {"asdf", &imageMock2}, |
| 56 | }; |
| 57 | |
| 58 | DataHandlerMock dataMock; |
| 59 | |
| 60 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 61 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 62 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 65 | sdbusplus::SdBusMock sdbus_mock; |
| 66 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 67 | |
| 68 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 69 | std::move(bus_mock), blobs, data); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 70 | |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 71 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 72 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 73 | |
| 74 | EXPECT_TRUE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 75 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 76 | |
| 77 | struct ExtChunkHdr request; |
| 78 | request.length = 4; /* number of bytes to read. */ |
| 79 | std::vector<std::uint8_t> ipmiRequest; |
| 80 | ipmiRequest.resize(sizeof(request)); |
| 81 | std::memcpy(ipmiRequest.data(), &request, sizeof(request)); |
| 82 | |
| 83 | std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04}; |
| 84 | |
| 85 | EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes)); |
| 86 | EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true)); |
| 87 | EXPECT_TRUE(handler->write(0, 0, ipmiRequest)); |
| 88 | } |
| 89 | |
| 90 | TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest) |
| 91 | { |
| 92 | /* Verify the data type non-ipmi, if the request's structure doesn't match, |
| 93 | * return failure. */ |
| 94 | |
| 95 | ImageHandlerMock imageMock1, imageMock2; |
| 96 | std::vector<HandlerPack> blobs = { |
| 97 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 98 | {"asdf", &imageMock2}, |
| 99 | }; |
| 100 | |
| 101 | DataHandlerMock dataMock; |
| 102 | |
| 103 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 104 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 105 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 108 | sdbusplus::SdBusMock sdbus_mock; |
| 109 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 110 | |
| 111 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 112 | std::move(bus_mock), blobs, data); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 113 | |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 114 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 115 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 116 | |
| 117 | EXPECT_TRUE(handler->open( |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 118 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 119 | |
| 120 | struct ExtChunkHdr request; |
| 121 | request.length = 4; /* number of bytes to read. */ |
| 122 | |
| 123 | std::vector<std::uint8_t> ipmiRequest; |
| 124 | ipmiRequest.resize(sizeof(request)); |
| 125 | std::memcpy(ipmiRequest.data(), &request, sizeof(request)); |
| 126 | ipmiRequest.push_back(1); |
| 127 | |
| 128 | /* ipmiRequest is too large by one byte. */ |
| 129 | EXPECT_FALSE(handler->write(0, 0, ipmiRequest)); |
| 130 | } |
| 131 | |
| 132 | } // namespace blobs |