blob: d74f3c807ba2f151cac14b760fbd487cfcb6815e [file] [log] [blame]
Patrick Venture18235e62018-11-08 10:21:09 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07003#include "firmware_unittest.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08004#include "image_mock.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -07005#include "triggerable_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07006#include "util.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08007
8#include <cstdint>
9#include <cstring>
10#include <vector>
11
12#include <gtest/gtest.h>
13
Patrick Venture1d5a31c2019-05-20 11:38:22 -070014namespace ipmi_flash
Patrick Venture18235e62018-11-08 10:21:09 -080015{
Patrick Venture18235e62018-11-08 10:21:09 -080016using ::testing::Eq;
17using ::testing::Return;
18
Patrick Venture1361a122019-05-20 07:36:05 -070019class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest
Patrick Ventureeb5df142019-05-17 18:43:36 -070020{
Patrick Ventureeb5df142019-05-17 18:43:36 -070021};
22
Patrick Venture1361a122019-05-20 07:36:05 -070023class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
24{
25};
26
27TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080028{
29 /* Verify if data type ipmi, it calls write with the bytes. */
Patrick Venture1361a122019-05-20 07:36:05 -070030 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080031
32 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070033 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
34 "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080035
36 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
37
Patrick Venture1361a122019-05-20 07:36:05 -070038 EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080039 EXPECT_TRUE(handler->write(0, 0, bytes));
40}
41
Patrick Venture1361a122019-05-20 07:36:05 -070042TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080043{
44 /* Verify if data type non-ipmi, it calls write with the length. */
Patrick Venture6e307a72018-11-09 18:21:17 -080045 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture1361a122019-05-20 07:36:05 -070046 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080047
48 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070049 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
50 "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
60 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
Patrick Venture1361a122019-05-20 07:36: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 Venture6e307a72018-11-09 18:21:17 -080069 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture1361a122019-05-20 07:36:05 -070070 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080071
72 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070073 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
74 "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080075
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 Venture1d5a31c2019-05-20 11:38:22 -070088} // namespace ipmi_flash