blob: c8c93c0420474aa1267aa0e01db87217b61d4f7f [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
14namespace blobs
15{
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 Venture05abf7e2018-11-09 11:02:56 -080035 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080036
37 /* The active image blob_id was added. */
38 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080039 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080040 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070041 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080042}
43
Patrick Venture6f09ff52019-05-17 18:09:37 -070044TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile)
Patrick Venture21c62c12018-11-09 17:46:58 -080045{
46 /* Open the hash file by blob_id. */
Patrick Venture7dad86f2019-05-17 08:52:20 -070047 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture21c62c12018-11-09 17:46:58 -080048
49 EXPECT_TRUE(handler->open(
50 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070051 hashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080052
53 /* The active hash blob_id was added. */
54 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080055 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080056 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070057 activeHashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080058}
59
Patrick Venture6f09ff52019-05-17 18:09:37 -070060TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds)
Patrick Venture6e307a72018-11-09 18:21:17 -080061{
62 /* Attempting to open a file that has an active handler, and use that active
63 * handler method.
64 */
Patrick Venture6e307a72018-11-09 18:21:17 -080065 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -070066 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture6e307a72018-11-09 18:21:17 -080067
68 EXPECT_TRUE(handler->open(
69 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070070 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080071
72 /* The active hash blob_id was added. */
73 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080074 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -080075 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070076 activeHashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080077}
78
Patrick Venture6f09ff52019-05-17 18:09:37 -070079TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure)
Patrick Venture6e307a72018-11-09 18:21:17 -080080{
81 /* The data handler call returns failure on open, therefore open fails. */
Patrick Venture6e307a72018-11-09 18:21:17 -080082 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
83
84 EXPECT_FALSE(handler->open(
85 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070086 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080087
88 /* The active hash blob_id was added. */
89 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080090 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -080091}
92
Patrick Venture6f09ff52019-05-17 18:09:37 -070093TEST_F(FirmwareHandlerOpenTestIpmiOnly,
94 OpenEverythingSucceedsVerifyOpenFileCheck)
Patrick Venture991910a2018-11-09 19:43:48 -080095{
96 /* Verify only one file can be open at a time by opening a file, trying
97 * again, then closing, and trying again.
98 */
Patrick Venture6f09ff52019-05-17 18:09:37 -070099 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800100
101 EXPECT_TRUE(handler->open(
102 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
103
104 /* The active image blob_id was added. */
105 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800106 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800107 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700108 activeImageBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800109
110 /* Open the hash file (since we opened an image file). */
111 EXPECT_FALSE(handler->open(
112 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700113 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800114
Patrick Venture68bb1432018-11-09 20:08:48 -0800115 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800116
Patrick Venture6f09ff52019-05-17 18:09:37 -0700117 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800118
119 EXPECT_TRUE(handler->open(
120 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700121 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800122}
123
Patrick Venture6f09ff52019-05-17 18:09:37 -0700124TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails)
Patrick Venture21c62c12018-11-09 17:46:58 -0800125{
126 /* Attempting to open the active image blob, when it's present will fail.
127 *
128 * TODO: We'll need another test that closes first because you can only have
129 * one file open at a time.
130 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800131 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
132
133 EXPECT_TRUE(handler->open(
134 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
135
136 /* The active image blob_id was added. */
137 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800138 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800139 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700140 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -0800141
Patrick Venture991910a2018-11-09 19:43:48 -0800142 /* Close only active session, to verify it's failing on attempt to open a
143 * specific blob_id.
144 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800145 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800146
Patrick Venture21c62c12018-11-09 17:46:58 -0800147 EXPECT_FALSE(handler->open(
148 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700149 activeImageBlobId));
Patrick Venture72388d72018-11-07 15:06:36 -0800150}
151
Patrick Venture6f09ff52019-05-17 18:09:37 -0700152TEST_F(FirmwareHandlerOpenTestIpmiOnly,
153 OpenWithEverythingValidImageHandlerFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800154{
155 /* The image handler for a specific type of image is allowed to return
156 * failure on open. let's simulate that. */
Patrick Venture72388d72018-11-07 15:06:36 -0800157 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
158
159 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800160 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800161
162 /* Verify blob_id list doesn't grow. */
163 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800164 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800165}
166
Patrick Venture6f09ff52019-05-17 18:09:37 -0700167TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800168{
169 /* The client must set the file write bit. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800170 EXPECT_FALSE(
171 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800172}
173
Patrick Venture6f09ff52019-05-17 18:09:37 -0700174TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit)
Patrick Venture72388d72018-11-07 15:06:36 -0800175{
176 /* The client sends a request with a transport mechanism not supported. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800177 EXPECT_FALSE(
178 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800179}
180
Patrick Venture6f09ff52019-05-17 18:09:37 -0700181TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId)
Patrick Venture72388d72018-11-07 15:06:36 -0800182{
183 /* The client sends a request with an invalid image blob_id. */
Patrick Venture72388d72018-11-07 15:06:36 -0800184 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800185 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800186}
187
188/* TODO: The client sends a request during verification. */
189/* TODO: The client sends a second request to open when there is already an open
190 * file.
191 */
192/* TODO: The client sends a request to open active image. */
193/* TODO: The client sends a request to open active hash. */
194
195} // namespace blobs