blob: 20bf8bf89ea9bb00c05078b2252826b974b13c4d [file] [log] [blame]
Patrick Ventureebcc5222019-05-23 10:36:40 -07001/**
2 * The goal of these tests is to verify the behavior of all blob commands given
3 * the current state is uploadInProgress. This state is achieved when an image
4 * or hash blob is opened and the handler is expected to receive bytes.
5 */
6#include "firmware_handler.hpp"
7#include "firmware_unittest.hpp"
8
9#include <gtest/gtest.h>
10
11namespace ipmi_flash
12{
13namespace
14{
15
16using ::testing::Return;
17using ::testing::UnorderedElementsAreArray;
18
19/*
20 * There are the following calls (parameters may vary):
21 * canHandleBlob(blob)
22 * getBlobIds
23 * deleteBlob(blob)
24 * stat(blob)
25 * stat(session)
26 * open(blob)
27 * close(session)
28 * writemeta(session)
29 * write(session)
30 * read(session)
31 *
32 * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs
33 * will inform what canHandleBlob will return.
34 */
35class FirmwareHandlerUploadInProgressTest : public IpmiOnlyFirmwareStaticTest
36{
37};
38
39TEST_F(FirmwareHandlerUploadInProgressTest, GetBlobIdsVerifyOutputActiveImage)
40{
41 /* Opening the image file will add the active image blob id */
42 std::uint16_t flags =
43 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
44 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
45
46 EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
47
48 EXPECT_TRUE(handler->open(1, flags, staticLayoutBlobId));
49 EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
50 realHandler->getCurrentState());
51
52 std::vector<std::string> expectedAfterImage = {
53 staticLayoutBlobId, hashBlobId, verifyBlobId, activeImageBlobId};
54 EXPECT_THAT(handler->getBlobIds(),
55 UnorderedElementsAreArray(expectedAfterImage));
56}
57
58TEST_F(FirmwareHandlerUploadInProgressTest, GetBlobIdsVerifyOutputActiveHash)
59{
60 /* Opening the image file will add the active image blob id */
61 std::uint16_t flags =
62 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
63 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
64
65 EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true));
66
67 EXPECT_TRUE(handler->open(1, flags, hashBlobId));
68 EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
69 realHandler->getCurrentState());
70
71 std::vector<std::string> expectedAfterImage = {
72 staticLayoutBlobId, hashBlobId, verifyBlobId, activeHashBlobId};
73 EXPECT_THAT(handler->getBlobIds(),
74 UnorderedElementsAreArray(expectedAfterImage));
75}
76
Patrick Venture6f729782019-05-23 11:16:13 -070077/* TODO: Try deleting some blobs -- in this state it will depend on what the
78 * blob id is, but it's not yet implemented
79 */
80
Patrick Venture41205cc2019-05-23 11:43:43 -070081/*
82 * stat(blob)
83 */
84TEST_F(FirmwareHandlerUploadInProgressTest, StatOnActiveImageReturnsFailure)
85{
86 /* you cannot call stat() on the active image or the active hash or the
87 * verify blob.
88 */
89 std::uint16_t flags =
90 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
91 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
92
93 EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
94
95 EXPECT_TRUE(handler->open(1, flags, staticLayoutBlobId));
96 EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
97 realHandler->getCurrentState());
98
99 std::vector<std::string> expectedAfterImage = {
100 staticLayoutBlobId, hashBlobId, verifyBlobId, activeImageBlobId};
101 EXPECT_THAT(handler->getBlobIds(),
102 UnorderedElementsAreArray(expectedAfterImage));
103
104 blobs::BlobMeta meta;
105 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
106}
107
108TEST_F(FirmwareHandlerUploadInProgressTest, StatOnActiveHashReturnsFailure)
109{
110 /* this test is separate from the active image one so that the state doesn't
111 * change from close.
112 */
113 std::uint16_t flags =
114 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
115 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
116
117 EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true));
118
119 EXPECT_TRUE(handler->open(1, flags, hashBlobId));
120 EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
121 realHandler->getCurrentState());
122
123 std::vector<std::string> expectedAfterImage = {
124 staticLayoutBlobId, hashBlobId, verifyBlobId, activeHashBlobId};
125 EXPECT_THAT(handler->getBlobIds(),
126 UnorderedElementsAreArray(expectedAfterImage));
127
128 blobs::BlobMeta meta;
129 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
130}
131
132TEST_F(FirmwareHandlerUploadInProgressTest, StatOnNormalBlobsReturnsSuccess)
133{
134 /* Calling stat() on the normal blobs (not the active) ones will work and
135 * return the same information as in the notYetStarted state.
136 */
137 blobs::BlobMeta expected;
138 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
139 expected.size = 0;
140
141 std::uint16_t flags =
142 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
143 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
144
145 EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
146
147 EXPECT_TRUE(handler->open(1, flags, staticLayoutBlobId));
148 EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
149 realHandler->getCurrentState());
150
151 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
152 for (const auto& blob : testBlobs)
153 {
154 blobs::BlobMeta meta = {};
155 EXPECT_TRUE(handler->stat(blob, &meta));
156 EXPECT_EQ(expected, meta);
157 }
158}
159
160/* TODO: stat(verifyblobid) only should exist once both /image and /hash have
161 * closed, so add this test when this is the case. NOTE: /image or /tarball
162 * should have the same effect.
163 */
164
165/*
166 * stat(session)
167 * open(blob)
168 * close(session)
169 * writemeta(session)
170 * write(session)
171 * read(session)
172 */
173
Patrick Ventureebcc5222019-05-23 10:36:40 -0700174} // namespace
175} // namespace ipmi_flash