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