blob: c5b89023db03d83a9634e008f14e6780d4e14d94 [file] [log] [blame]
Patrick Venture68bb1432018-11-09 20:08:48 -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 Venture68bb1432018-11-09 20:08:48 -08006#include <vector>
7
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11namespace blobs
12{
13using ::testing::Eq;
14using ::testing::Return;
15using ::testing::StrEq;
16
17TEST(FirmwareHandlerCloseTest, CloseSuceedsWithDataHandler)
18{
19 /* Boring test where you open a blob_id, then verify that when it's closed
20 * everything looks right.
21 */
22 DataHandlerMock dataMock;
23 ImageHandlerMock imageMock;
24
25 std::vector<HandlerPack> blobs = {
26 {FirmwareBlobHandler::hashBlobID, &imageMock},
27 {"asdf", &imageMock},
28 };
29 std::vector<DataHandlerPack> data = {
30 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
31 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
32 };
33
Patrick Venture4eb55952018-11-16 15:36:24 -080034 sdbusplus::SdBusMock sdbus_mock;
35 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
36
37 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
38 std::move(bus_mock), blobs, data);
Patrick Venture68bb1432018-11-09 20:08:48 -080039
40 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
41 EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
42 .WillOnce(Return(true));
43
44 EXPECT_TRUE(handler->open(
45 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
46 FirmwareBlobHandler::hashBlobID));
47
48 /* The active hash blob_id was added. */
49 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080050 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture68bb1432018-11-09 20:08:48 -080051 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
52 FirmwareBlobHandler::activeHashBlobID));
53
54 /* Set up close() expectations. */
55 EXPECT_CALL(dataMock, close());
56 EXPECT_CALL(imageMock, close());
57 EXPECT_TRUE(handler->close(0));
58
59 /* Close does not delete the active blob id. This indicates that there is
60 * data queued.
61 */
62}
63
64TEST(FirmwareHandlerCloseTest, CloseSuceedsWithoutDataHandler)
65{
66 /* Boring test where you open a blob_id using ipmi, so there's no data
67 * handler, and it's closed and everything looks right.
68 */
69 DataHandlerMock dataMock;
70 ImageHandlerMock imageMock;
71
72 std::vector<HandlerPack> blobs = {
73 {FirmwareBlobHandler::hashBlobID, &imageMock},
74 {"asdf", &imageMock},
75 };
76 std::vector<DataHandlerPack> data = {
77 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
78 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
79 };
80
Patrick Venture4eb55952018-11-16 15:36:24 -080081 sdbusplus::SdBusMock sdbus_mock;
82 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
83
84 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
85 std::move(bus_mock), blobs, data);
Patrick Venture68bb1432018-11-09 20:08:48 -080086
87 EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
88 .WillOnce(Return(true));
89
90 EXPECT_TRUE(handler->open(
91 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
92 FirmwareBlobHandler::hashBlobID));
93
94 /* The active hash blob_id was added. */
95 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080096 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture68bb1432018-11-09 20:08:48 -080097 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
98 FirmwareBlobHandler::activeHashBlobID));
99
100 /* Set up close() expectations. */
101 EXPECT_CALL(imageMock, close());
102 EXPECT_TRUE(handler->close(0));
103}
104
105} // namespace blobs