1 /**
2  * The goal of these tests is to verify opening the ubi tarball changes state
3  * as expected and does not regress.
4  */
5 #include "firmware_handler.hpp"
6 #include "firmware_unittest.hpp"
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <gtest/gtest.h>
13 
14 namespace ipmi_flash
15 {
16 namespace
17 {
18 
19 using ::testing::UnorderedElementsAreArray;
20 
21 class FirmwareHandlerNotYetStartedUbitTest : public ::testing::Test
22 {
23   protected:
SetUp()24     void SetUp() override
25     {
26         std::unique_ptr<ImageHandlerInterface> image =
27             std::make_unique<ImageHandlerMock>();
28         hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
29         blobs.emplace_back(hashBlobId, std::move(image));
30 
31         image = std::make_unique<ImageHandlerMock>();
32         imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
33         blobs.emplace_back(ubiTarballBlobId, std::move(image));
34 
35         std::unique_ptr<TriggerableActionInterface> verifyMock =
36             std::make_unique<TriggerMock>();
37         verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
38 
39         std::unique_ptr<TriggerableActionInterface> updateMock =
40             std::make_unique<TriggerMock>();
41         updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
42 
43         std::unique_ptr<ActionPack> actionPack = std::make_unique<ActionPack>();
44         actionPack->preparation = CreateTriggerMock();
45         actionPack->verification = std::move(verifyMock);
46         actionPack->update = std::move(updateMock);
47 
48         ActionMap packs;
49         packs[ubiTarballBlobId] = std::move(actionPack);
50 
51         std::vector<DataHandlerPack> data;
52         data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
53 
54         handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
55             std::move(blobs), std::move(data), std::move(packs));
56     }
57 
expectedState(FirmwareBlobHandler::UpdateState state)58     void expectedState(FirmwareBlobHandler::UpdateState state)
59     {
60         auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
61         EXPECT_EQ(state, realHandler->getCurrentState());
62     }
63 
openToInProgress(const std::string & blobId)64     void openToInProgress(const std::string& blobId)
65     {
66         if (blobId == hashBlobId)
67         {
68             EXPECT_CALL(*hashImageMock, open(blobId, std::ios::out))
69                 .WillOnce(Return(true));
70         }
71         else
72         {
73             EXPECT_CALL(*imageMock, open(blobId, std::ios::out))
74                 .WillOnce(Return(true));
75         }
76         EXPECT_TRUE(handler->open(session, flags, blobId));
77         expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
78     }
79 
80     ImageHandlerMock *hashImageMock, *imageMock;
81     std::vector<HandlerPack> blobs;
82     std::unique_ptr<blobs::GenericBlobInterface> handler;
83     TriggerMock* verifyMockPtr;
84     TriggerMock* updateMockPtr;
85 
86     std::uint16_t session = 1;
87     std::uint16_t flags = static_cast<std::uint16_t>(blobs::OpenFlags::write) |
88                           FirmwareFlags::UpdateFlags::ipmi;
89 };
90 
TEST_F(FirmwareHandlerNotYetStartedUbitTest,OpeningTarballMovesToUploadInProgress)91 TEST_F(FirmwareHandlerNotYetStartedUbitTest,
92        OpeningTarballMovesToUploadInProgress)
93 {
94     expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
95 
96     EXPECT_THAT(handler->getBlobIds(),
97                 UnorderedElementsAreArray({hashBlobId, ubiTarballBlobId}));
98 
99     openToInProgress(ubiTarballBlobId);
100     EXPECT_THAT(handler->getBlobIds(),
101                 UnorderedElementsAreArray(
102                     {hashBlobId, ubiTarballBlobId, activeImageBlobId}));
103 }
104 
105 } // namespace
106 } // namespace ipmi_flash
107