blob: 7df07433a685e6ebec3ab09c086aa7f8a5920e96 [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 Venture1d66fe62019-06-03 14:57:27 -07005#include "triggerable_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07006#include "util.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 Venture930c7b72019-05-24 11:11:08 -070016namespace
17{
18
Patrick Venture21c62c12018-11-09 17:46:58 -080019using ::testing::Eq;
Patrick Venture72388d72018-11-07 15:06:36 -080020using ::testing::Return;
Patrick Venture991910a2018-11-09 19:43:48 -080021using ::testing::StrEq;
Patrick Venture72388d72018-11-07 15:06:36 -080022
Patrick Venture1361a122019-05-20 07:36:05 -070023class FirmwareHandlerOpenTestIpmiOnly : public IpmiOnlyFirmwareTest
Patrick Venture6f09ff52019-05-17 18:09:37 -070024{
Patrick Venture6f09ff52019-05-17 18:09:37 -070025};
26
Patrick Venture1361a122019-05-20 07:36:05 -070027class FirmwareHandlerOpenTestLpc : public FakeLpcFirmwareTest
Patrick Venture6f09ff52019-05-17 18:09:37 -070028{
Patrick Venture6f09ff52019-05-17 18:09:37 -070029};
30
31TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValid)
Patrick Venture72388d72018-11-07 15:06:36 -080032{
33 /* The client passes write set, and a transport that's supported, and a
34 * firmware image blob_id that's supported. */
Patrick Venture72388d72018-11-07 15:06:36 -080035 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
36
37 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070038 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
39 "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080040
41 /* The active image blob_id was added. */
42 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -070043 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080044 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070045 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080046}
47
Patrick Venture6f09ff52019-05-17 18:09:37 -070048TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile)
Patrick Venture21c62c12018-11-09 17:46:58 -080049{
50 /* Open the hash file by blob_id. */
Patrick Venture7dad86f2019-05-17 08:52:20 -070051 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture21c62c12018-11-09 17:46:58 -080052
53 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070054 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070055 hashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080056
57 /* The active hash blob_id was added. */
58 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -070059 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080060 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070061 activeHashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080062}
63
Patrick Venture6f09ff52019-05-17 18:09:37 -070064TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds)
Patrick Venture6e307a72018-11-09 18:21:17 -080065{
66 /* Attempting to open a file that has an active handler, and use that active
67 * handler method.
68 */
Patrick Venture6e307a72018-11-09 18:21:17 -080069 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -070070 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture6e307a72018-11-09 18:21:17 -080071
72 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070073 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070074 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080075
76 /* The active hash blob_id was added. */
77 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -070078 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -080079 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070080 activeHashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080081}
82
Patrick Venture6f09ff52019-05-17 18:09:37 -070083TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure)
Patrick Venture6e307a72018-11-09 18:21:17 -080084{
85 /* The data handler call returns failure on open, therefore open fails. */
Patrick Venture6e307a72018-11-09 18:21:17 -080086 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
87
88 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070089 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -070090 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -080091
Patrick Venture930c7b72019-05-24 11:11:08 -070092 /* The active hash blob_id was not added. */
Patrick Venture6e307a72018-11-09 18:21:17 -080093 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -070094 EXPECT_EQ(2, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -080095}
96
Patrick Venture6f09ff52019-05-17 18:09:37 -070097TEST_F(FirmwareHandlerOpenTestIpmiOnly,
98 OpenEverythingSucceedsVerifyOpenFileCheck)
Patrick Venture991910a2018-11-09 19:43:48 -080099{
100 /* Verify only one file can be open at a time by opening a file, trying
101 * again, then closing, and trying again.
102 */
Patrick Venture6f09ff52019-05-17 18:09:37 -0700103 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800104
105 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700106 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
107 "asdf"));
Patrick Venture991910a2018-11-09 19:43:48 -0800108
109 /* The active image blob_id was added. */
110 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -0700111 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800112 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700113 activeImageBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800114
115 /* Open the hash file (since we opened an image file). */
116 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700117 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700118 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800119
Patrick Venture68bb1432018-11-09 20:08:48 -0800120 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800121
Patrick Venture6f09ff52019-05-17 18:09:37 -0700122 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800123
124 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700125 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700126 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800127}
128
Patrick Venture6f09ff52019-05-17 18:09:37 -0700129TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails)
Patrick Venture21c62c12018-11-09 17:46:58 -0800130{
131 /* Attempting to open the active image blob, when it's present will fail.
132 *
133 * TODO: We'll need another test that closes first because you can only have
134 * one file open at a time.
135 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800136 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
137
138 EXPECT_TRUE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700139 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
140 "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -0800141
142 /* The active image blob_id was added. */
143 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -0700144 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800145 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700146 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -0800147
Patrick Venture991910a2018-11-09 19:43:48 -0800148 /* Close only active session, to verify it's failing on attempt to open a
149 * specific blob_id.
150 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800151 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800152
Patrick Venture21c62c12018-11-09 17:46:58 -0800153 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700154 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700155 activeImageBlobId));
Patrick Venture72388d72018-11-07 15:06:36 -0800156}
157
Patrick Venture6f09ff52019-05-17 18:09:37 -0700158TEST_F(FirmwareHandlerOpenTestIpmiOnly,
159 OpenWithEverythingValidImageHandlerFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800160{
161 /* The image handler for a specific type of image is allowed to return
162 * failure on open. let's simulate that. */
Patrick Venture72388d72018-11-07 15:06:36 -0800163 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
164
165 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700166 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
167 "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800168
169 /* Verify blob_id list doesn't grow. */
170 auto currentBlobs = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -0700171 EXPECT_EQ(2, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800172}
173
Patrick Venture6f09ff52019-05-17 18:09:37 -0700174TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800175{
176 /* The client must set the file write bit. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800177 EXPECT_FALSE(
178 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800179}
180
Patrick Venture6f09ff52019-05-17 18:09:37 -0700181TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit)
Patrick Venture72388d72018-11-07 15:06:36 -0800182{
183 /* The client sends a request with a transport mechanism not supported. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800184 EXPECT_FALSE(
185 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800186}
187
Patrick Venture6f09ff52019-05-17 18:09:37 -0700188TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId)
Patrick Venture72388d72018-11-07 15:06:36 -0800189{
190 /* The client sends a request with an invalid image blob_id. */
Patrick Venture72388d72018-11-07 15:06:36 -0800191 EXPECT_FALSE(handler->open(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700192 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
193 "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800194}
195
196/* TODO: The client sends a request during verification. */
197/* TODO: The client sends a second request to open when there is already an open
198 * file.
199 */
200/* TODO: The client sends a request to open active image. */
201/* TODO: The client sends a request to open active hash. */
202
Patrick Venture930c7b72019-05-24 11:11:08 -0700203} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700204} // namespace ipmi_flash