blob: e9b70ba85ea2cbc798a32f60104dfd40c2db13e6 [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. */
Patrick Ventured4e20de2019-07-18 12:48:05 -070032 EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080033
34 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070035 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080036
37 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
38
Patrick Ventured4e20de2019-07-18 12:48:05 -070039 EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080040 EXPECT_TRUE(handler->write(0, 0, bytes));
41}
42
Patrick Venture1361a122019-05-20 07:36:05 -070043TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080044{
45 /* Verify if data type non-ipmi, it calls write with the length. */
Patrick Venture4934daa2020-09-22 16:37:44 -070046 EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070047 EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080048
49 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070050 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080051
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
Patrick Venture4934daa2020-09-22 16:37:44 -070060 EXPECT_CALL(*dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
Patrick Ventured4e20de2019-07-18 12:48:05 -070061 EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080062 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
63}
64
Patrick Venture1361a122019-05-20 07:36:05 -070065TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
Patrick Venture18235e62018-11-08 10:21:09 -080066{
67 /* Verify the data type non-ipmi, if the request's structure doesn't match,
68 * return failure. */
Patrick Venture4934daa2020-09-22 16:37:44 -070069 EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070070 EXPECT_CALL(*imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080071
72 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070073 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080074
75 struct ExtChunkHdr request;
76 request.length = 4; /* number of bytes to read. */
77
78 std::vector<std::uint8_t> ipmiRequest;
79 ipmiRequest.resize(sizeof(request));
80 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
81 ipmiRequest.push_back(1);
82
83 /* ipmiRequest is too large by one byte. */
84 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
85}
86
Patrick Ventured4e20de2019-07-18 12:48:05 -070087} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -070088} // namespace ipmi_flash