blob: 5bede335a4970ba25c5c94f770a5ed318a977f0b [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"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture68bb1432018-11-09 20:08:48 -08005
Patrick Venture4eb55952018-11-16 15:36:24 -08006#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venture68bb1432018-11-09 20:08:48 -08007#include <vector>
8
9#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
12namespace blobs
13{
14using ::testing::Eq;
15using ::testing::Return;
16using ::testing::StrEq;
17
18TEST(FirmwareHandlerCloseTest, CloseSuceedsWithDataHandler)
19{
20 /* Boring test where you open a blob_id, then verify that when it's closed
21 * everything looks right.
22 */
23 DataHandlerMock dataMock;
24 ImageHandlerMock imageMock;
25
26 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070027 {hashBlobId, &imageMock},
Patrick Venture68bb1432018-11-09 20:08:48 -080028 {"asdf", &imageMock},
29 };
30 std::vector<DataHandlerPack> data = {
31 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
32 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
33 };
34
Patrick Venture4eb55952018-11-16 15:36:24 -080035 sdbusplus::SdBusMock sdbus_mock;
36 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
37
38 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
39 std::move(bus_mock), blobs, data);
Patrick Venture68bb1432018-11-09 20:08:48 -080040
41 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -070042 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture68bb1432018-11-09 20:08:48 -080043
44 EXPECT_TRUE(handler->open(
45 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070046 hashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080047
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(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070052 activeHashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080053
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 = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070073 {hashBlobId, &imageMock},
Patrick Venture68bb1432018-11-09 20:08:48 -080074 {"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
Patrick Venture7dad86f2019-05-17 08:52:20 -070087 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture68bb1432018-11-09 20:08:48 -080088
89 EXPECT_TRUE(handler->open(
90 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070091 hashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080092
93 /* The active hash blob_id was added. */
94 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080095 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture68bb1432018-11-09 20:08:48 -080096 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070097 activeHashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080098
99 /* Set up close() expectations. */
100 EXPECT_CALL(imageMock, close());
101 EXPECT_TRUE(handler->close(0));
102}
103
104} // namespace blobs