blob: ff839af6b8f9d9656aee07499843dc5b18129f3c [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 Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Venture68bb1432018-11-09 20:08:48 -08006
7#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 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070036 blobs, data, CreateVerifyMock());
Patrick Venture68bb1432018-11-09 20:08:48 -080037
38 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -070039 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture68bb1432018-11-09 20:08:48 -080040
41 EXPECT_TRUE(handler->open(
42 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070043 hashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080044
45 /* The active hash blob_id was added. */
46 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080047 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture68bb1432018-11-09 20:08:48 -080048 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070049 activeHashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080050
51 /* Set up close() expectations. */
52 EXPECT_CALL(dataMock, close());
53 EXPECT_CALL(imageMock, close());
54 EXPECT_TRUE(handler->close(0));
55
56 /* Close does not delete the active blob id. This indicates that there is
57 * data queued.
58 */
59}
60
61TEST(FirmwareHandlerCloseTest, CloseSuceedsWithoutDataHandler)
62{
63 /* Boring test where you open a blob_id using ipmi, so there's no data
64 * handler, and it's closed and everything looks right.
65 */
66 DataHandlerMock dataMock;
67 ImageHandlerMock imageMock;
68
69 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070070 {hashBlobId, &imageMock},
Patrick Venture68bb1432018-11-09 20:08:48 -080071 {"asdf", &imageMock},
72 };
73 std::vector<DataHandlerPack> data = {
74 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
75 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
76 };
77
Patrick Venture4eb55952018-11-16 15:36:24 -080078 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070079 blobs, data, CreateVerifyMock());
Patrick Venture68bb1432018-11-09 20:08:48 -080080
Patrick Venture7dad86f2019-05-17 08:52:20 -070081 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture68bb1432018-11-09 20:08:48 -080082
83 EXPECT_TRUE(handler->open(
84 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070085 hashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080086
87 /* The active hash blob_id was added. */
88 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080089 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture68bb1432018-11-09 20:08:48 -080090 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070091 activeHashBlobId));
Patrick Venture68bb1432018-11-09 20:08:48 -080092
93 /* Set up close() expectations. */
94 EXPECT_CALL(imageMock, close());
95 EXPECT_TRUE(handler->close(0));
96}
97
98} // namespace blobs