blob: 72ffcb3d798711a07154f9dceec669b1e566d5aa [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"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08006
7#include <cstdint>
8#include <cstring>
9#include <vector>
10
11#include <gtest/gtest.h>
12
13namespace blobs
14{
Patrick Venture18235e62018-11-08 10:21:09 -080015using ::testing::Eq;
16using ::testing::Return;
17
18TEST(FirmwareHandlerWriteTest, DataTypeIpmiWriteSuccess)
19{
20 /* Verify if data type ipmi, it calls write with the bytes. */
21
22 ImageHandlerMock imageMock1, imageMock2;
23 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070024 {hashBlobId, &imageMock1},
Patrick Venture18235e62018-11-08 10:21:09 -080025 {"asdf", &imageMock2},
26 };
27
28 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080029 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture18235e62018-11-08 10:21:09 -080030 };
31
Patrick Venture4eb55952018-11-16 15:36:24 -080032 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070033 blobs, data, CreateVerifyMock());
Patrick Venture18235e62018-11-08 10:21:09 -080034
35 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
36
37 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080038 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080039
40 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
41
42 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
43 EXPECT_TRUE(handler->write(0, 0, bytes));
44}
45
46TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteSuccess)
47{
48 /* Verify if data type non-ipmi, it calls write with the length. */
49
50 ImageHandlerMock imageMock1, imageMock2;
51 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070052 {hashBlobId, &imageMock1},
Patrick Venture18235e62018-11-08 10:21:09 -080053 {"asdf", &imageMock2},
54 };
55
56 DataHandlerMock dataMock;
57
58 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080059 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
60 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080061 };
62
Patrick Venture4eb55952018-11-16 15:36:24 -080063 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070064 blobs, data, CreateVerifyMock());
Patrick Venture18235e62018-11-08 10:21:09 -080065
Patrick Venture6e307a72018-11-09 18:21:17 -080066 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080067 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
68
69 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080070 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080071
72 struct ExtChunkHdr request;
73 request.length = 4; /* number of bytes to read. */
74 std::vector<std::uint8_t> ipmiRequest;
75 ipmiRequest.resize(sizeof(request));
76 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
77
78 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
79
80 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
81 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
82 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
83}
84
85TEST(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest)
86{
87 /* Verify the data type non-ipmi, if the request's structure doesn't match,
88 * return failure. */
89
90 ImageHandlerMock imageMock1, imageMock2;
91 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070092 {hashBlobId, &imageMock1},
Patrick Venture18235e62018-11-08 10:21:09 -080093 {"asdf", &imageMock2},
94 };
95
96 DataHandlerMock dataMock;
97
98 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080099 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
100 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -0800101 };
102
Patrick Venture4eb55952018-11-16 15:36:24 -0800103 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700104 blobs, data, CreateVerifyMock());
Patrick Venture18235e62018-11-08 10:21:09 -0800105
Patrick Venture6e307a72018-11-09 18:21:17 -0800106 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -0800107 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
108
109 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800110 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -0800111
112 struct ExtChunkHdr request;
113 request.length = 4; /* number of bytes to read. */
114
115 std::vector<std::uint8_t> ipmiRequest;
116 ipmiRequest.resize(sizeof(request));
117 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
118 ipmiRequest.push_back(1);
119
120 /* ipmiRequest is too large by one byte. */
121 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
122}
123
124} // namespace blobs