blob: 0ba20f5277b559771a16e430064ade8aa0f0200a [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>
7#include <vector>
8
9#include <gtest/gtest.h>
10
11namespace blobs
12{
Patrick Venture18235e62018-11-08 10:21:09 -080013using ::testing::Eq;
14using ::testing::Return;
15
16TEST(FirmwareHandlerWriteTest, DataTypeIpmiWriteSuccess)
17{
18 /* Verify if data type ipmi, it calls write with the bytes. */
19
20 ImageHandlerMock imageMock1, imageMock2;
21 std::vector<HandlerPack> blobs = {
22 {FirmwareBlobHandler::hashBlobID, &imageMock1},
23 {"asdf", &imageMock2},
24 };
25
26 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080027 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture18235e62018-11-08 10:21:09 -080028 };
29
30 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
31
32 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
33
34 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080035 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080036
37 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
38
39 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
40 EXPECT_TRUE(handler->write(0, 0, bytes));
41}
42
43TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteSuccess)
44{
45 /* Verify if data type non-ipmi, it calls write with the length. */
46
47 ImageHandlerMock imageMock1, imageMock2;
48 std::vector<HandlerPack> blobs = {
49 {FirmwareBlobHandler::hashBlobID, &imageMock1},
50 {"asdf", &imageMock2},
51 };
52
53 DataHandlerMock dataMock;
54
55 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080056 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
57 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080058 };
59
60 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
61
Patrick Venture6e307a72018-11-09 18:21:17 -080062 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080063 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
64
65 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080066 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080067
68 struct ExtChunkHdr request;
69 request.length = 4; /* number of bytes to read. */
70 std::vector<std::uint8_t> ipmiRequest;
71 ipmiRequest.resize(sizeof(request));
72 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
73
74 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
75
76 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
77 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
78 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
79}
80
81TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest)
82{
83 /* Verify the data type non-ipmi, if the request's structure doesn't match,
84 * return failure. */
85
86 ImageHandlerMock imageMock1, imageMock2;
87 std::vector<HandlerPack> blobs = {
88 {FirmwareBlobHandler::hashBlobID, &imageMock1},
89 {"asdf", &imageMock2},
90 };
91
92 DataHandlerMock dataMock;
93
94 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080095 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
96 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080097 };
98
99 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
100
Patrick Venture6e307a72018-11-09 18:21:17 -0800101 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -0800102 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
103
104 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800105 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -0800106
107 struct ExtChunkHdr request;
108 request.length = 4; /* number of bytes to read. */
109
110 std::vector<std::uint8_t> ipmiRequest;
111 ipmiRequest.resize(sizeof(request));
112 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
113 ipmiRequest.push_back(1);
114
115 /* ipmiRequest is too large by one byte. */
116 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
117}
118
119} // namespace blobs