blob: 891ade281ab7493b291d1cc7a649a7aceae26e8a [file] [log] [blame]
Patrick Venturecc7d1602018-11-15 13:58:33 -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 Venturecc7d1602018-11-15 13:58:33 -08005
Patrick Venture4eb55952018-11-16 15:36:24 -08006#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturecc7d1602018-11-15 13:58:33 -08007#include <vector>
8
9#include <gtest/gtest.h>
10
11namespace blobs
12{
13using ::testing::Eq;
14using ::testing::Return;
15
16TEST(FirmwareSessionStateTest, DataTypeIpmiNoMetadata)
17{
18 /* Verifying running stat if the type of data session is IPMI returns no
19 * metadata.
20 */
21 ImageHandlerMock imageMock1, imageMock2;
22 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070023 {hashBlobId, &imageMock1},
Patrick Venturecc7d1602018-11-15 13:58:33 -080024 {"asdf", &imageMock2},
25 };
26
27 std::vector<DataHandlerPack> data = {
28 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
29 };
30
Patrick Venture4eb55952018-11-16 15:36:24 -080031 sdbusplus::SdBusMock sdbus_mock;
32 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
33
34 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -070035 std::move(bus_mock), blobs, data, "");
Patrick Venturecc7d1602018-11-15 13:58:33 -080036
37 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
38
39 EXPECT_TRUE(handler->open(
40 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
41
42 int size = 512;
43 EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
44
45 struct BlobMeta meta;
46 EXPECT_TRUE(handler->stat(0, &meta));
47 EXPECT_EQ(meta.blobState,
48 OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi);
49 EXPECT_EQ(meta.size, size);
50 EXPECT_EQ(meta.metadata.size(), 0);
51}
52
53TEST(FirmwareSessionStateTest, DataTypeP2AReturnsMetadata)
54{
55 /* Really any type that isn't IPMI can return metadata, but we only expect
56 * P2A to for now. Later, LPC may have reason to provide data, and can by
57 * simply implementing read().
58 */
59 ImageHandlerMock imageMock1, imageMock2;
60 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070061 {hashBlobId, &imageMock1},
Patrick Venturecc7d1602018-11-15 13:58:33 -080062 {"asdf", &imageMock2},
63 };
64
65 DataHandlerMock dataMock;
66
67 std::vector<DataHandlerPack> data = {
68 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
69 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
70 };
71
Patrick Venture4eb55952018-11-16 15:36:24 -080072 sdbusplus::SdBusMock sdbus_mock;
73 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
74
75 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -070076 std::move(bus_mock), blobs, data, "");
Patrick Venturecc7d1602018-11-15 13:58:33 -080077
78 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
79 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
80
81 EXPECT_TRUE(handler->open(
82 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
83
84 int size = 512;
85 EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
86 std::vector<std::uint8_t> mBytes = {0x01, 0x02};
Patrick Venture74304642019-01-17 09:31:04 -080087 EXPECT_CALL(dataMock, readMeta()).WillOnce(Return(mBytes));
Patrick Venturecc7d1602018-11-15 13:58:33 -080088
89 struct BlobMeta meta;
90 EXPECT_TRUE(handler->stat(0, &meta));
91 EXPECT_EQ(meta.blobState,
92 OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc);
93 EXPECT_EQ(meta.size, size);
94 EXPECT_EQ(meta.metadata.size(), mBytes.size());
95 EXPECT_EQ(meta.metadata[0], mBytes[0]);
96 EXPECT_EQ(meta.metadata[1], mBytes[1]);
97}
98
99} // namespace blobs