1 /**
2  * The goal of these tests is to verify the behavior of all blob commands given
3  * the current state is notYetStarted.  The initial state.
4  */
5 #include "firmware_handler.hpp"
6 #include "firmware_unittest.hpp"
7 
8 #include <cstdint>
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::Return;
20 using ::testing::UnorderedElementsAreArray;
21 
22 class FirmwareHandlerNotYetStartedTest : public IpmiOnlyFirmwareStaticTest
23 {};
24 
25 /*
26  * There are the following calls (parameters may vary):
27  * Note: you cannot have a session yet, so only commands that don't take a
28  * session parameter are valid. Once you open() from this state, we will vary
29  * you transition out of this state (ensuring the above is true). Technically
30  * the firmware handler receives the session number with open(), but the blob
31  * manager is providing this normally.
32  *
33  * canHandleBlob
34  * getBlobIds
35  * deleteBlob
36  * stat
37  * open
38  *
39  * canHandleBlob is just a count check (or something similar) against what is
40  * returned by getBlobIds.  It is tested in firmware_canhandle_unittest
41  */
42 
43 /*
44  * deleteBlob()
45  */
46 TEST_F(FirmwareHandlerNotYetStartedTest, DeleteBlobInStateReturnsFalse)
47 {
48     auto blobs = handler->getBlobIds();
49     for (const auto& b : blobs)
50     {
51         EXPECT_FALSE(handler->deleteBlob(b));
52     }
53 }
54 
55 /* canHandleBlob, getBlobIds */
56 TEST_F(FirmwareHandlerNotYetStartedTest, GetBlobListValidateListContents)
57 {
58     /* By only checking that the hash and static blob ids are present to start
59      * with, we're also verifying others aren't.
60      */
61     EXPECT_THAT(handler->getBlobIds(),
62                 UnorderedElementsAreArray(startingBlobs));
63 
64     /* Verify canHandleBlob is reading from the same list (basically) */
65     for (const auto& blob : startingBlobs)
66     {
67         EXPECT_TRUE(handler->canHandleBlob(blob));
68     }
69 }
70 
71 /* stat(blob_id) */
72 TEST_F(FirmwareHandlerNotYetStartedTest, StatEachBlobIdVerifyResults)
73 {
74     /* In this original state, calling stat() on the blob ids will return the
75      * idle status
76      */
77 
78     auto blobs = handler->getBlobIds();
79     for (const auto& blob : blobs)
80     {
81         blobs::BlobMeta meta = {};
82         EXPECT_TRUE(handler->stat(blob, &meta));
83         EXPECT_EQ(expectedIdleMeta, meta);
84     }
85 }
86 
87 /* open(each blob id) (verifyblobid will no longer be available at this state.
88  */
89 TEST_F(FirmwareHandlerNotYetStartedTest, OpenStaticImageFileVerifyStateChange)
90 {
91     EXPECT_CALL(*imageMock2, open(staticLayoutBlobId)).WillOnce(Return(true));
92     EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
93 
94     EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
95 
96     expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
97 
98     EXPECT_TRUE(handler->canHandleBlob(activeImageBlobId));
99 }
100 
101 TEST_F(FirmwareHandlerNotYetStartedTest, OpenHashFileVerifyStateChange)
102 {
103     EXPECT_CALL(*hashImageMock, open(hashBlobId)).WillOnce(Return(true));
104     /* Opening the hash blob id doesn't trigger a preparation, only a firmware
105      * blob.
106      */
107     EXPECT_CALL(*prepareMockPtr, trigger()).Times(0);
108 
109     EXPECT_TRUE(handler->open(session, flags, hashBlobId));
110 
111     expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
112 
113     EXPECT_TRUE(handler->canHandleBlob(activeHashBlobId));
114 }
115 
116 /*
117  * expire(session)
118  */
119 TEST_F(FirmwareHandlerNotYetStartedTest, ExpireOnNotYetStartedAbortsProcess)
120 {
121     ASSERT_TRUE(handler->expire(session));
122 
123     EXPECT_THAT(handler->getBlobIds(),
124                 UnorderedElementsAreArray(startingBlobs));
125 
126     expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
127 }
128 
129 } // namespace
130 } // namespace ipmi_flash
131