blob: 48c32175cfd1abf7133ffcfd832cf55a87d802f8 [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
Patrick Ventureeb5df142019-05-17 18:43:36 -070018class FirmwareHandlerWriteTest : public ::testing::Test
19{
20 protected:
21 ImageHandlerMock imageMock1, imageMock2;
22 std::vector<HandlerPack> blobs;
23
24 void SetUp() override
25 {
26 blobs = {
27 {hashBlobId, &imageMock1},
28 {"asdf", &imageMock2},
29 };
30 }
31};
32
33TEST_F(FirmwareHandlerWriteTest, DataTypeIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080034{
35 /* Verify if data type ipmi, it calls write with the bytes. */
Patrick Venture18235e62018-11-08 10:21:09 -080036 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080037 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture18235e62018-11-08 10:21:09 -080038 };
39
Patrick Venture4eb55952018-11-16 15:36:24 -080040 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070041 blobs, data, CreateVerifyMock());
Patrick Venture18235e62018-11-08 10:21:09 -080042
43 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
44
45 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080046 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080047
48 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
49
50 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
51 EXPECT_TRUE(handler->write(0, 0, bytes));
52}
53
Patrick Ventureeb5df142019-05-17 18:43:36 -070054TEST_F(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080055{
56 /* Verify if data type non-ipmi, it calls write with the length. */
Patrick Venture18235e62018-11-08 10:21:09 -080057 DataHandlerMock dataMock;
58
59 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080060 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
61 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080062 };
63
Patrick Venture4eb55952018-11-16 15:36:24 -080064 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070065 blobs, data, CreateVerifyMock());
Patrick Venture18235e62018-11-08 10:21:09 -080066
Patrick Venture6e307a72018-11-09 18:21:17 -080067 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080068 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
69
70 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080071 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080072
73 struct ExtChunkHdr request;
74 request.length = 4; /* number of bytes to read. */
75 std::vector<std::uint8_t> ipmiRequest;
76 ipmiRequest.resize(sizeof(request));
77 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
78
79 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
80
81 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
82 EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
83 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
84}
85
Patrick Ventureeb5df142019-05-17 18:43:36 -070086TEST_F(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest)
Patrick Venture18235e62018-11-08 10:21:09 -080087{
88 /* Verify the data type non-ipmi, if the request's structure doesn't match,
89 * return failure. */
Patrick Venture18235e62018-11-08 10:21:09 -080090 DataHandlerMock dataMock;
91
92 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080093 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
94 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
Patrick Venture18235e62018-11-08 10:21:09 -080095 };
96
Patrick Venture4eb55952018-11-16 15:36:24 -080097 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070098 blobs, data, CreateVerifyMock());
Patrick Venture18235e62018-11-08 10:21:09 -080099
Patrick Venture6e307a72018-11-09 18:21:17 -0800100 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -0800101 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
102
103 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800104 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -0800105
106 struct ExtChunkHdr request;
107 request.length = 4; /* number of bytes to read. */
108
109 std::vector<std::uint8_t> ipmiRequest;
110 ipmiRequest.resize(sizeof(request));
111 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
112 ipmiRequest.push_back(1);
113
114 /* ipmiRequest is too large by one byte. */
115 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
116}
117
118} // namespace blobs