blob: 481f3ec8113969b47bd74aece95a0b5acb343c37 [file] [log] [blame]
Patrick Venture84778b82019-06-26 20:11:09 -07001#include "data.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08002#include "data_mock.hpp"
3#include "firmware_handler.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07004#include "firmware_unittest.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08005#include "image_mock.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -07006#include "triggerable_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07007#include "util.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08008
9#include <cstdint>
10#include <cstring>
11#include <vector>
12
13#include <gtest/gtest.h>
14
Patrick Venture1d5a31c2019-05-20 11:38:22 -070015namespace ipmi_flash
Patrick Venture18235e62018-11-08 10:21:09 -080016{
Patrick Ventured4e20de2019-07-18 12:48:05 -070017namespace
18{
19
Patrick Venture18235e62018-11-08 10:21:09 -080020using ::testing::Eq;
21using ::testing::Return;
22
Patrick Venture1361a122019-05-20 07:36:05 -070023class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest
Patrick Ventureeb5df142019-05-17 18:43:36 -070024{
Patrick Ventureeb5df142019-05-17 18:43:36 -070025};
26
Patrick Venture1361a122019-05-20 07:36:05 -070027class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
28{
29};
30
31TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080032{
33 /* Verify if data type ipmi, it calls write with the bytes. */
Patrick Ventured4e20de2019-07-18 12:48:05 -070034 EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080035
36 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070037 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080038
39 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
40
Patrick Ventured4e20de2019-07-18 12:48:05 -070041 EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080042 EXPECT_TRUE(handler->write(0, 0, bytes));
43}
44
Patrick Venture1361a122019-05-20 07:36:05 -070045TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080046{
47 /* Verify if data type non-ipmi, it calls write with the length. */
Patrick Venture6e307a72018-11-09 18:21:17 -080048 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070049 EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080050
51 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070052 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080053
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 Ventured4e20de2019-07-18 12:48:05 -070063 EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080064 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
65}
66
Patrick Venture1361a122019-05-20 07:36:05 -070067TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
Patrick Venture18235e62018-11-08 10:21:09 -080068{
69 /* Verify the data type non-ipmi, if the request's structure doesn't match,
70 * return failure. */
Patrick Venture6e307a72018-11-09 18:21:17 -080071 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070072 EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080073
74 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070075 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080076
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 Ventured4e20de2019-07-18 12:48:05 -070089} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -070090} // namespace ipmi_flash