blob: da1a371c7b5433f9defc09584d92376b689d75fa [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 Venture9b37b092020-05-28 20:58:57 -070024{};
Patrick Ventureeb5df142019-05-17 18:43:36 -070025
Patrick Venture1361a122019-05-20 07:36:05 -070026class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
Patrick Venture9b37b092020-05-28 20:58:57 -070027{};
Patrick Venture1361a122019-05-20 07:36:05 -070028
29TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080030{
31 /* Verify if data type ipmi, it calls write with the bytes. */
Jason Ling56a22732020-10-23 19:53:17 -070032 EXPECT_CALL(*imageMock, open("asdf", std::ios::out)).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080033
Willy Tub487eb42021-09-16 21:44:43 -070034 EXPECT_TRUE(
35 handler->open(0,
36 static_cast<std::uint16_t>(blobs::OpenFlags::write) |
37 FirmwareFlags::UpdateFlags::ipmi,
38 "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080039
40 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
41
Patrick Ventured4e20de2019-07-18 12:48:05 -070042 EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080043 EXPECT_TRUE(handler->write(0, 0, bytes));
44}
45
Patrick Venture1361a122019-05-20 07:36:05 -070046TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080047{
48 /* Verify if data type non-ipmi, it calls write with the length. */
Patrick Venture4934daa2020-09-22 16:37:44 -070049 EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
Jason Ling56a22732020-10-23 19:53:17 -070050 EXPECT_CALL(*imageMock, open("asdf", std::ios::out)).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080051
Willy Tub487eb42021-09-16 21:44:43 -070052 EXPECT_TRUE(
53 handler->open(0,
54 static_cast<std::uint16_t>(blobs::OpenFlags::write) |
55 FirmwareFlags::UpdateFlags::lpc,
56 "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080057
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 Venture4934daa2020-09-22 16:37:44 -070066 EXPECT_CALL(*dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
Patrick Ventured4e20de2019-07-18 12:48:05 -070067 EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080068 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
69}
70
Patrick Venture1361a122019-05-20 07:36:05 -070071TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
Patrick Venture18235e62018-11-08 10:21:09 -080072{
73 /* Verify the data type non-ipmi, if the request's structure doesn't match,
74 * return failure. */
Patrick Venture4934daa2020-09-22 16:37:44 -070075 EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
Jason Ling56a22732020-10-23 19:53:17 -070076 EXPECT_CALL(*imageMock, open("asdf", std::ios::out)).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080077
Willy Tub487eb42021-09-16 21:44:43 -070078 EXPECT_TRUE(
79 handler->open(0,
80 static_cast<std::uint16_t>(blobs::OpenFlags::write) |
81 FirmwareFlags::UpdateFlags::lpc,
82 "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080083
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 Ventured4e20de2019-07-18 12:48:05 -070096} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -070097} // namespace ipmi_flash