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