1 #include "data_mock.hpp"
2 #include "firmware_handler.hpp"
3 #include "firmware_unittest.hpp"
4 #include "image_mock.hpp"
5 #include "triggerable_mock.hpp"
6 #include "util.hpp"
7 
8 #include <memory>
9 #include <vector>
10 
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13 
14 namespace ipmi_flash
15 {
16 namespace
17 {
18 
19 using ::testing::Return;
20 using ::testing::StrEq;
21 
22 class FirmwareHandlerCloseTest : public FakeLpcFirmwareTest
23 {};
24 
25 TEST_F(FirmwareHandlerCloseTest, CloseSucceedsWithDataHandler)
26 {
27     /* Boring test where you open a blob_id, then verify that when it's closed
28      * everything looks right.
29      */
30     EXPECT_CALL(*dataMock, open()).WillOnce(Return(true));
31     EXPECT_CALL(*hashImageMock, open(StrEq(hashBlobId), std::ios::out))
32         .WillOnce(Return(true));
33 
34     EXPECT_TRUE(handler->open(
35         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::lpc,
36         hashBlobId));
37 
38     /* The active hash blob_id was added. */
39     auto currentBlobs = handler->getBlobIds();
40     EXPECT_EQ(3, currentBlobs.size());
41     EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
42                             activeHashBlobId));
43 
44     /* Set up close() expectations. */
45     EXPECT_CALL(*dataMock, close());
46     EXPECT_CALL(*hashImageMock, close());
47     EXPECT_TRUE(handler->close(0));
48 
49     /* Close does not delete the active blob id.  This indicates that there is
50      * data queued.
51      */
52 }
53 
54 TEST_F(FirmwareHandlerCloseTest, CloseSucceedsWithoutDataHandler)
55 {
56     /* Boring test where you open a blob_id using ipmi, so there's no data
57      * handler, and it's closed and everything looks right.
58      */
59     EXPECT_CALL(*hashImageMock, open(StrEq(hashBlobId), std::ios::out))
60         .WillOnce(Return(true));
61 
62     EXPECT_TRUE(handler->open(
63         0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi,
64         hashBlobId));
65 
66     /* The active hash blob_id was added. */
67     auto currentBlobs = handler->getBlobIds();
68     EXPECT_EQ(3, currentBlobs.size());
69     EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
70                             activeHashBlobId));
71 
72     /* Set up close() expectations. */
73     EXPECT_CALL(*hashImageMock, close());
74     EXPECT_TRUE(handler->close(0));
75 }
76 
77 } // namespace
78 } // namespace ipmi_flash
79