Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 1 | #include "data.hpp" |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 2 | #include "data_mock.hpp" |
| 3 | #include "firmware_handler.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 4 | #include "firmware_unittest.hpp" |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 5 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 6 | #include "triggerable_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 7 | #include "util.hpp" |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 8 | |
| 9 | #include <cstdint> |
| 10 | #include <cstring> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 15 | namespace ipmi_flash |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 16 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 17 | namespace |
| 18 | { |
| 19 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 20 | using ::testing::Eq; |
| 21 | using ::testing::Return; |
| 22 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 23 | class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest |
Patrick Venture | eb5df14 | 2019-05-17 18:43:36 -0700 | [diff] [blame] | 24 | { |
Patrick Venture | eb5df14 | 2019-05-17 18:43:36 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 27 | class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest |
| 28 | { |
| 29 | }; |
| 30 | |
| 31 | TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 32 | { |
| 33 | /* Verify if data type ipmi, it calls write with the bytes. */ |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 34 | EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 35 | |
| 36 | EXPECT_TRUE(handler->open( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 37 | 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 38 | |
| 39 | std::vector<std::uint8_t> bytes = {0xaa, 0x55}; |
| 40 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 41 | EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 42 | EXPECT_TRUE(handler->write(0, 0, bytes)); |
| 43 | } |
| 44 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 45 | TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 46 | { |
| 47 | /* Verify if data type non-ipmi, it calls write with the length. */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 48 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 49 | EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 50 | |
| 51 | EXPECT_TRUE(handler->open( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 52 | 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 53 | |
| 54 | struct ExtChunkHdr request; |
| 55 | request.length = 4; /* number of bytes to read. */ |
| 56 | std::vector<std::uint8_t> ipmiRequest; |
| 57 | ipmiRequest.resize(sizeof(request)); |
| 58 | std::memcpy(ipmiRequest.data(), &request, sizeof(request)); |
| 59 | |
| 60 | std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04}; |
| 61 | |
| 62 | EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 63 | EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 64 | EXPECT_TRUE(handler->write(0, 0, ipmiRequest)); |
| 65 | } |
| 66 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 67 | TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 68 | { |
| 69 | /* Verify the data type non-ipmi, if the request's structure doesn't match, |
| 70 | * return failure. */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 71 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 72 | EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 73 | |
| 74 | EXPECT_TRUE(handler->open( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 75 | 0, blobs::OpenFlags::write | FirmwareFlags::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 | |
| 80 | std::vector<std::uint8_t> ipmiRequest; |
| 81 | ipmiRequest.resize(sizeof(request)); |
| 82 | std::memcpy(ipmiRequest.data(), &request, sizeof(request)); |
| 83 | ipmiRequest.push_back(1); |
| 84 | |
| 85 | /* ipmiRequest is too large by one byte. */ |
| 86 | EXPECT_FALSE(handler->write(0, 0, ipmiRequest)); |
| 87 | } |
| 88 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame^] | 89 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 90 | } // namespace ipmi_flash |