blob: 4da41a1c42849a68fa2a5f2a0fc7fa920e5141a5 [file] [log] [blame]
Patrick Venture72388d72018-11-07 15:06:36 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07003#include "firmware_unittest.hpp"
Patrick Venture72388d72018-11-07 15:06:36 -08004#include "image_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07005#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07006#include "verification_mock.hpp"
Patrick Venture72388d72018-11-07 15:06:36 -08007
Patrick Venture6f09ff52019-05-17 18:09:37 -07008#include <memory>
Patrick Venture72388d72018-11-07 15:06:36 -08009#include <vector>
10
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
Patrick Venture1d5a31c2019-05-20 11:38:22 -070014namespace ipmi_flash
Patrick Venture72388d72018-11-07 15:06:36 -080015{
Patrick Venture21c62c12018-11-09 17:46:58 -080016using ::testing::Eq;
Patrick Venture72388d72018-11-07 15:06:36 -080017using ::testing::Return;
Patrick Venture991910a2018-11-09 19:43:48 -080018using ::testing::StrEq;
Patrick Venture72388d72018-11-07 15:06:36 -080019
Patrick Venture1361a122019-05-20 07:36:05 -070020class FirmwareHandlerOpenTestIpmiOnly : public IpmiOnlyFirmwareTest
Patrick Venture6f09ff52019-05-17 18:09:37 -070021{
Patrick Venture6f09ff52019-05-17 18:09:37 -070022};
23
Patrick Venture1361a122019-05-20 07:36:05 -070024class FirmwareHandlerOpenTestLpc : public FakeLpcFirmwareTest
Patrick Venture6f09ff52019-05-17 18:09:37 -070025{
Patrick Venture6f09ff52019-05-17 18:09:37 -070026};
27
28TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValid)
Patrick Venture72388d72018-11-07 15:06:36 -080029{
30 /* The client passes write set, and a transport that's supported, and a
31 * firmware image blob_id that's supported. */
Patrick Venture72388d72018-11-07 15:06:36 -080032 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
33
34 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070035 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
36 "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080037
38 /* The active image blob_id was added. */
39 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080040 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080041 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070042 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080043}
44
Patrick Venture6f09ff52019-05-17 18:09:37 -070045TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile)
Patrick Venture21c62c12018-11-09 17:46:58 -080046{
47 /* Open the hash file by blob_id. */
Patrick Venture7dad86f2019-05-17 08:52:20 -070048 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture21c62c12018-11-09 17:46:58 -080049
50 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070051 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070052 hashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080053
54 /* The active hash blob_id was added. */
55 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080056 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080057 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070058 activeHashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080059}
60
Patrick Venture6f09ff52019-05-17 18:09:37 -070061TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds)
Patrick Venture6e307a72018-11-09 18:21:17 -080062{
63 /* Attempting to open a file that has an active handler, and use that active
64 * handler method.
65 */
Patrick Venture6e307a72018-11-09 18:21:17 -080066 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -070067 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture6e307a72018-11-09 18:21:17 -080068
69 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070070 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070071 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080072
73 /* The active hash blob_id was added. */
74 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080075 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -080076 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070077 activeHashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080078}
79
Patrick Venture6f09ff52019-05-17 18:09:37 -070080TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure)
Patrick Venture6e307a72018-11-09 18:21:17 -080081{
82 /* The data handler call returns failure on open, therefore open fails. */
Patrick Venture6e307a72018-11-09 18:21:17 -080083 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
84
85 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070086 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070087 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080088
89 /* The active hash blob_id was added. */
90 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080091 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -080092}
93
Patrick Venture6f09ff52019-05-17 18:09:37 -070094TEST_F(FirmwareHandlerOpenTestIpmiOnly,
95 OpenEverythingSucceedsVerifyOpenFileCheck)
Patrick Venture991910a2018-11-09 19:43:48 -080096{
97 /* Verify only one file can be open at a time by opening a file, trying
98 * again, then closing, and trying again.
99 */
Patrick Venture6f09ff52019-05-17 18:09:37 -0700100 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800101
102 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700103 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
104 "asdf"));
Patrick Venture991910a2018-11-09 19:43:48 -0800105
106 /* The active image blob_id was added. */
107 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800108 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800109 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700110 activeImageBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800111
112 /* Open the hash file (since we opened an image file). */
113 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700114 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700115 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800116
Patrick Venture68bb1432018-11-09 20:08:48 -0800117 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800118
Patrick Venture6f09ff52019-05-17 18:09:37 -0700119 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800120
121 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700122 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700123 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800124}
125
Patrick Venture6f09ff52019-05-17 18:09:37 -0700126TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails)
Patrick Venture21c62c12018-11-09 17:46:58 -0800127{
128 /* Attempting to open the active image blob, when it's present will fail.
129 *
130 * TODO: We'll need another test that closes first because you can only have
131 * one file open at a time.
132 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800133 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
134
135 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700136 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
137 "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -0800138
139 /* The active image blob_id was added. */
140 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800141 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800142 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700143 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -0800144
Patrick Venture991910a2018-11-09 19:43:48 -0800145 /* Close only active session, to verify it's failing on attempt to open a
146 * specific blob_id.
147 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800148 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800149
Patrick Venture21c62c12018-11-09 17:46:58 -0800150 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700151 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700152 activeImageBlobId));
Patrick Venture72388d72018-11-07 15:06:36 -0800153}
154
Patrick Venture6f09ff52019-05-17 18:09:37 -0700155TEST_F(FirmwareHandlerOpenTestIpmiOnly,
156 OpenWithEverythingValidImageHandlerFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800157{
158 /* The image handler for a specific type of image is allowed to return
159 * failure on open. let's simulate that. */
Patrick Venture72388d72018-11-07 15:06:36 -0800160 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
161
162 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700163 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
164 "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800165
166 /* Verify blob_id list doesn't grow. */
167 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800168 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800169}
170
Patrick Venture6f09ff52019-05-17 18:09:37 -0700171TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800172{
173 /* The client must set the file write bit. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800174 EXPECT_FALSE(
175 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800176}
177
Patrick Venture6f09ff52019-05-17 18:09:37 -0700178TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit)
Patrick Venture72388d72018-11-07 15:06:36 -0800179{
180 /* The client sends a request with a transport mechanism not supported. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800181 EXPECT_FALSE(
182 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800183}
184
Patrick Venture6f09ff52019-05-17 18:09:37 -0700185TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId)
Patrick Venture72388d72018-11-07 15:06:36 -0800186{
187 /* The client sends a request with an invalid image blob_id. */
Patrick Venture72388d72018-11-07 15:06:36 -0800188 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700189 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
190 "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800191}
192
193/* TODO: The client sends a request during verification. */
194/* TODO: The client sends a second request to open when there is already an open
195 * file.
196 */
197/* TODO: The client sends a request to open active image. */
198/* TODO: The client sends a request to open active hash. */
199
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700200} // namespace ipmi_flash