blob: 77f63268fe15b6d82ba26538338869e676cbbbac [file] [log] [blame]
Patrick Venture18235e62018-11-08 10:21:09 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07003#include "firmware_unittest.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08004#include "image_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07005#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07006#include "verification_mock.hpp"
Patrick Venture18235e62018-11-08 10:21:09 -08007
8#include <cstdint>
9#include <cstring>
10#include <vector>
11
12#include <gtest/gtest.h>
13
14namespace blobs
15{
Patrick Venture18235e62018-11-08 10:21:09 -080016using ::testing::Eq;
17using ::testing::Return;
18
Patrick Venture1361a122019-05-20 07:36:05 -070019class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest
Patrick Ventureeb5df142019-05-17 18:43:36 -070020{
Patrick Ventureeb5df142019-05-17 18:43:36 -070021};
22
Patrick Venture1361a122019-05-20 07:36:05 -070023class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
24{
25};
26
27TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080028{
29 /* Verify if data type ipmi, it calls write with the bytes. */
Patrick Venture1361a122019-05-20 07:36:05 -070030 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080031
32 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080033 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080034
35 std::vector<std::uint8_t> bytes = {0xaa, 0x55};
36
Patrick Venture1361a122019-05-20 07:36:05 -070037 EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080038 EXPECT_TRUE(handler->write(0, 0, bytes));
39}
40
Patrick Venture1361a122019-05-20 07:36:05 -070041TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
Patrick Venture18235e62018-11-08 10:21:09 -080042{
43 /* Verify if data type non-ipmi, it calls write with the length. */
Patrick Venture6e307a72018-11-09 18:21:17 -080044 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture1361a122019-05-20 07:36:05 -070045 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080046
47 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080048 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -080049
50 struct ExtChunkHdr request;
51 request.length = 4; /* number of bytes to read. */
52 std::vector<std::uint8_t> ipmiRequest;
53 ipmiRequest.resize(sizeof(request));
54 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
55
56 std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
57
58 EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
Patrick Venture1361a122019-05-20 07:36:05 -070059 EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080060 EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
61}
62
Patrick Venture1361a122019-05-20 07:36:05 -070063TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
Patrick Venture18235e62018-11-08 10:21:09 -080064{
65 /* Verify the data type non-ipmi, if the request's structure doesn't match,
66 * return failure. */
Patrick Venture6e307a72018-11-09 18:21:17 -080067 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture1361a122019-05-20 07:36:05 -070068 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture18235e62018-11-08 10:21:09 -080069
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
76 std::vector<std::uint8_t> ipmiRequest;
77 ipmiRequest.resize(sizeof(request));
78 std::memcpy(ipmiRequest.data(), &request, sizeof(request));
79 ipmiRequest.push_back(1);
80
81 /* ipmiRequest is too large by one byte. */
82 EXPECT_FALSE(handler->write(0, 0, ipmiRequest));
83}
84
85} // namespace blobs