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