blob: d9fcae4501ac799cbc14839864c68eabde83b468 [file] [log] [blame]
Patrick Venture18235e62018-11-08 10:21:09 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
4
5#include <cstdint>
6#include <cstring>
Patrick Venture4eb55952018-11-16 15:36:24 -08007#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venture18235e62018-11-08 10:21:09 -08008#include <vector>
9
10#include <gtest/gtest.h>
11
12namespace blobs
13{
Patrick Venture18235e62018-11-08 10:21:09 -080014using ::testing::Eq;
15using ::testing::Return;
16
17TEST(FirmwareHandlerWriteTest, DataTypeIpmiWriteSuccess)
18{
19 /* Verify if data type ipmi, it calls write with the bytes. */
20
21 ImageHandlerMock imageMock1, imageMock2;
22 std::vector<HandlerPack> blobs = {
23 {FirmwareBlobHandler::hashBlobID, &imageMock1},
24 {"asdf", &imageMock2},
25 };
26
27 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080028 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture18235e62018-11-08 10:21:09 -080029 };
30
Patrick Venture4eb55952018-11-16 15:36:24 -080031 sdbusplus::SdBusMock sdbus_mock;
32 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
33
34 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
35 std::move(bus_mock), blobs, data);
Patrick Venture18235e62018-11-08 10:21:09 -080036
37 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
38
39 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080040 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080041
42 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
43
44 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
45 EXPECT_TRUE(handler->write(0, 0, bytes));
46}
47
48TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteSuccess)
49{
50 /* Verify if data type non-ipmi, it calls write with the length. */
51
52 ImageHandlerMock imageMock1, imageMock2;
53 std::vector<HandlerPack> blobs = {
54 {FirmwareBlobHandler::hashBlobID, &imageMock1},
55 {"asdf", &imageMock2},
56 };
57
58 DataHandlerMock dataMock;
59
60 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080061 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
62 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080063 };
64
Patrick Venture4eb55952018-11-16 15:36:24 -080065 sdbusplus::SdBusMock sdbus_mock;
66 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
67
68 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
69 std::move(bus_mock), blobs, data);
Patrick Venture18235e62018-11-08 10:21:09 -080070
Patrick Venture6e307a72018-11-09 18:21:17 -080071 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080072 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
73
74 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080075 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080076
77 struct ExtChunkHdr request;
78 request.length = 4; /* number of bytes to read. */
79 std::vector<std::uint8_t> ipmiRequest;
80 ipmiRequest.resize(sizeof(request));
81 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
82
83 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
84
85 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
86 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
87 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
88}
89
90TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest)
91{
92 /* Verify the data type non-ipmi, if the request's structure doesn't match,
93 * return failure. */
94
95 ImageHandlerMock imageMock1, imageMock2;
96 std::vector<HandlerPack> blobs = {
97 {FirmwareBlobHandler::hashBlobID, &imageMock1},
98 {"asdf", &imageMock2},
99 };
100
101 DataHandlerMock dataMock;
102
103 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800104 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
105 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -0800106 };
107
Patrick Venture4eb55952018-11-16 15:36:24 -0800108 sdbusplus::SdBusMock sdbus_mock;
109 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
110
111 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
112 std::move(bus_mock), blobs, data);
Patrick Venture18235e62018-11-08 10:21:09 -0800113
Patrick Venture6e307a72018-11-09 18:21:17 -0800114 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -0800115 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
116
117 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800118 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -0800119
120 struct ExtChunkHdr request;
121 request.length = 4; /* number of bytes to read. */
122
123 std::vector<std::uint8_t> ipmiRequest;
124 ipmiRequest.resize(sizeof(request));
125 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
126 ipmiRequest.push_back(1);
127
128 /* ipmiRequest is too large by one byte. */
129 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
130}
131
132} // namespace blobs