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