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