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