blob: b3353c958f256114003c70f0302b86b405e92f40 [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 Venture18235e62018-11-08 10:21:09 -080017using ::testing::Eq;
18using ::testing::Return;
19
Patrick Venture1361a122019-05-20 07:36:05 -070020class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest
Patrick Ventureeb5df142019-05-17 18:43:36 -070021{
Patrick Ventureeb5df142019-05-17 18:43:36 -070022};
23
Patrick Venture1361a122019-05-20 07:36:05 -070024class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
25{
26};
27
28TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080029{
30 /* Verify if data type ipmi, it calls write with the bytes. */
Patrick Venture1361a122019-05-20 07:36:05 -070031 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080032
33 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070034 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "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 Venture84778b82019-06-26 20:11:09 -070049 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080050
51 struct ExtChunkHdr request;
52 request.length = 4; /* number of bytes to read. */
53 std::vector<std::uint8_t> ipmiRequest;
54 ipmiRequest.resize(sizeof(request));
55 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
56
57 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
58
59 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
Patrick Venture1361a122019-05-20 07:36:05 -070060 EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080061 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
62}
63
Patrick Venture1361a122019-05-20 07:36:05 -070064TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
Patrick Venture18235e62018-11-08 10:21:09 -080065{
66 /* Verify the data type non-ipmi, if the request's structure doesn't match,
67 * return failure. */
Patrick Venture6e307a72018-11-09 18:21:17 -080068 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture1361a122019-05-20 07:36:05 -070069 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080070
71 EXPECT_TRUE(handler->open(
Patrick Venture84778b82019-06-26 20:11:09 -070072 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080073
74 struct ExtChunkHdr request;
75 request.length = 4; /* number of bytes to read. */
76
77 std::vector<std::uint8_t> ipmiRequest;
78 ipmiRequest.resize(sizeof(request));
79 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
80 ipmiRequest.push_back(1);
81
82 /* ipmiRequest is too large by one byte. */
83 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
84}
85
Patrick Venture1d5a31c2019-05-20 11:38:22 -070086} // namespace ipmi_flash