blob: 58cb5b21fe03f96afba249ecf3edfa5bc21c9ed2 [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
62 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
63
64 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080065 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080066
67 struct ExtChunkHdr request;
68 request.length = 4; /* number of bytes to read. */
69 std::vector<std::uint8_t> ipmiRequest;
70 ipmiRequest.resize(sizeof(request));
71 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
72
73 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
74
75 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
76 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
77 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
78}
79
80TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest)
81{
82 /* Verify the data type non-ipmi, if the request's structure doesn't match,
83 * return failure. */
84
85 ImageHandlerMock imageMock1, imageMock2;
86 std::vector<HandlerPack> blobs = {
87 {FirmwareBlobHandler::hashBlobID, &imageMock1},
88 {"asdf", &imageMock2},
89 };
90
91 DataHandlerMock dataMock;
92
93 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080094 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
95 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080096 };
97
98 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
99
100 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
101
102 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800103 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -0800104
105 struct ExtChunkHdr request;
106 request.length = 4; /* number of bytes to read. */
107
108 std::vector<std::uint8_t> ipmiRequest;
109 ipmiRequest.resize(sizeof(request));
110 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
111 ipmiRequest.push_back(1);
112
113 /* ipmiRequest is too large by one byte. */
114 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
115}
116
117} // namespace blobs