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