Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 3 | #include "firmware_unittest.hpp" |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 4 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 5 | #include "triggerable_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 6 | #include "util.hpp" |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 7 | |
| 8 | #include <cstdint> |
| 9 | #include <cstring> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <gtest/gtest.h> |
| 13 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 14 | namespace ipmi_flash |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 15 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 16 | using ::testing::Eq; |
| 17 | using ::testing::Return; |
| 18 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 19 | class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest |
Patrick Venture | eb5df14 | 2019-05-17 18:43:36 -0700 | [diff] [blame] | 20 | { |
Patrick Venture | eb5df14 | 2019-05-17 18:43:36 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 23 | class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest |
| 24 | { |
| 25 | }; |
| 26 | |
| 27 | TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 28 | { |
| 29 | /* Verify if data type ipmi, it calls write with the bytes. */ |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 30 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 31 | |
| 32 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 33 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 34 | "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 | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 49 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
| 50 | "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 | |
| 60 | EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes)); |
Patrick Venture | 1361a12 | 2019-05-20 07:36: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 | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 69 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | 1361a12 | 2019-05-20 07:36: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 | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 73 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
| 74 | "asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 75 | |
| 76 | struct ExtChunkHdr request; |
| 77 | request.length = 4; /* number of bytes to read. */ |
| 78 | |
| 79 | std::vector<std::uint8_t> ipmiRequest; |
| 80 | ipmiRequest.resize(sizeof(request)); |
| 81 | std::memcpy(ipmiRequest.data(), &request, sizeof(request)); |
| 82 | ipmiRequest.push_back(1); |
| 83 | |
| 84 | /* ipmiRequest is too large by one byte. */ |
| 85 | EXPECT_FALSE(handler->write(0, 0, ipmiRequest)); |
| 86 | } |
| 87 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 88 | } // namespace ipmi_flash |