xref: /openbmc/phosphor-ipmi-flash/bmc/firmware-handler/test/firmware_write_unittest.cpp (revision 56a2273fc7e609ddac670a57d55089d24448025a)
1 #include "data.hpp"
2 #include "data_mock.hpp"
3 #include "firmware_handler.hpp"
4 #include "firmware_unittest.hpp"
5 #include "image_mock.hpp"
6 #include "triggerable_mock.hpp"
7 #include "util.hpp"
8 
9 #include <cstdint>
10 #include <cstring>
11 #include <vector>
12 
13 #include <gtest/gtest.h>
14 
15 namespace ipmi_flash
16 {
17 namespace
18 {
19 
20 using ::testing::Eq;
21 using ::testing::Return;
22 
23 class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest
24 {};
25 
26 class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
27 {};
28 
29 TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
30 {
31     /* Verify if data type ipmi, it calls write with the bytes. */
32     EXPECT_CALL(*imageMock, open("asdf", std::ios::out)).WillOnce(Return(true));
33 
34     EXPECT_TRUE(handler->open(
35         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf"));
36 
37     std::vector<std::uint8_t> bytes = {0xaa, 0x55};
38 
39     EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
40     EXPECT_TRUE(handler->write(0, 0, bytes));
41 }
42 
43 TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
44 {
45     /* Verify if data type non-ipmi, it calls write with the length. */
46     EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
47     EXPECT_CALL(*imageMock, open("asdf", std::ios::out)).WillOnce(Return(true));
48 
49     EXPECT_TRUE(handler->open(
50         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
51 
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));
61     EXPECT_CALL(*imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
62     EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
63 }
64 
65 TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
66 {
67     /* Verify the data type non-ipmi, if the request's structure doesn't match,
68      * return failure. */
69     EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
70     EXPECT_CALL(*imageMock, open("asdf", std::ios::out)).WillOnce(Return(true));
71 
72     EXPECT_TRUE(handler->open(
73         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc, "asdf"));
74 
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 
87 } // namespace
88 } // namespace ipmi_flash
89