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