blob: 13f802bea998ea24d1f699136b12a826976b73c4 [file] [log] [blame]
Patrick Venture72388d72018-11-07 15:06:36 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Venture72388d72018-11-07 15:06:36 -08006
Patrick Venture6f09ff52019-05-17 18:09:37 -07007#include <memory>
Patrick Venture72388d72018-11-07 15:06:36 -08008#include <vector>
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13namespace blobs
14{
Patrick Venture21c62c12018-11-09 17:46:58 -080015using ::testing::Eq;
Patrick Venture72388d72018-11-07 15:06:36 -080016using ::testing::Return;
Patrick Venture991910a2018-11-09 19:43:48 -080017using ::testing::StrEq;
Patrick Venture72388d72018-11-07 15:06:36 -080018
Patrick Venture6f09ff52019-05-17 18:09:37 -070019class FirmwareHandlerOpenTestIpmiOnly : public ::testing::Test
20{
21 protected:
22 ImageHandlerMock imageMock;
23 std::vector<HandlerPack> blobs;
24 std::vector<DataHandlerPack> data;
25 std::unique_ptr<GenericBlobInterface> handler;
26
27 void SetUp() override
28 {
29 blobs = {
30 {hashBlobId, &imageMock},
31 {"asdf", &imageMock},
32 };
33 data = {
34 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
35 };
36 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
37 blobs, data, CreateVerifyMock());
38 }
39};
40
41class FirmwareHandlerOpenTestLpc : public ::testing::Test
42{
43 protected:
44 DataHandlerMock dataMock;
45 ImageHandlerMock imageMock;
46 std::vector<HandlerPack> blobs;
47 std::vector<DataHandlerPack> data;
48 std::unique_ptr<GenericBlobInterface> handler;
49
50 void SetUp() override
51 {
52 blobs = {
53 {hashBlobId, &imageMock},
54 {"asdf", &imageMock},
55 };
56 data = {
57 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
58 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
59 };
60
61 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
62 blobs, data, CreateVerifyMock());
63 }
64};
65
66TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValid)
Patrick Venture72388d72018-11-07 15:06:36 -080067{
68 /* The client passes write set, and a transport that's supported, and a
69 * firmware image blob_id that's supported. */
Patrick Venture72388d72018-11-07 15:06:36 -080070 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
71
72 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080073 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080074
75 /* The active image blob_id was added. */
76 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080077 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080078 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070079 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080080}
81
Patrick Venture6f09ff52019-05-17 18:09:37 -070082TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile)
Patrick Venture21c62c12018-11-09 17:46:58 -080083{
84 /* Open the hash file by blob_id. */
Patrick Venture7dad86f2019-05-17 08:52:20 -070085 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture21c62c12018-11-09 17:46:58 -080086
87 EXPECT_TRUE(handler->open(
88 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070089 hashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080090
91 /* The active hash blob_id was added. */
92 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080093 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080094 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070095 activeHashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080096}
97
Patrick Venture6f09ff52019-05-17 18:09:37 -070098TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds)
Patrick Venture6e307a72018-11-09 18:21:17 -080099{
100 /* Attempting to open a file that has an active handler, and use that active
101 * handler method.
102 */
Patrick Venture6e307a72018-11-09 18:21:17 -0800103 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -0700104 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture6e307a72018-11-09 18:21:17 -0800105
106 EXPECT_TRUE(handler->open(
107 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700108 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800109
110 /* The active hash blob_id was added. */
111 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800112 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800113 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700114 activeHashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800115}
116
Patrick Venture6f09ff52019-05-17 18:09:37 -0700117TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure)
Patrick Venture6e307a72018-11-09 18:21:17 -0800118{
119 /* The data handler call returns failure on open, therefore open fails. */
Patrick Venture6e307a72018-11-09 18:21:17 -0800120 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
121
122 EXPECT_FALSE(handler->open(
123 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700124 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800125
126 /* The active hash blob_id was added. */
127 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800128 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800129}
130
Patrick Venture6f09ff52019-05-17 18:09:37 -0700131TEST_F(FirmwareHandlerOpenTestIpmiOnly,
132 OpenEverythingSucceedsVerifyOpenFileCheck)
Patrick Venture991910a2018-11-09 19:43:48 -0800133{
134 /* Verify only one file can be open at a time by opening a file, trying
135 * again, then closing, and trying again.
136 */
Patrick Venture6f09ff52019-05-17 18:09:37 -0700137 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800138
139 EXPECT_TRUE(handler->open(
140 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
141
142 /* The active image blob_id was added. */
143 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800144 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800145 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700146 activeImageBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800147
148 /* Open the hash file (since we opened an image file). */
149 EXPECT_FALSE(handler->open(
150 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700151 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800152
Patrick Venture68bb1432018-11-09 20:08:48 -0800153 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800154
Patrick Venture6f09ff52019-05-17 18:09:37 -0700155 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800156
157 EXPECT_TRUE(handler->open(
158 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700159 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800160}
161
Patrick Venture6f09ff52019-05-17 18:09:37 -0700162TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails)
Patrick Venture21c62c12018-11-09 17:46:58 -0800163{
164 /* Attempting to open the active image blob, when it's present will fail.
165 *
166 * TODO: We'll need another test that closes first because you can only have
167 * one file open at a time.
168 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800169 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
170
171 EXPECT_TRUE(handler->open(
172 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
173
174 /* The active image blob_id was added. */
175 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800176 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800177 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700178 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -0800179
Patrick Venture991910a2018-11-09 19:43:48 -0800180 /* Close only active session, to verify it's failing on attempt to open a
181 * specific blob_id.
182 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800183 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800184
Patrick Venture21c62c12018-11-09 17:46:58 -0800185 EXPECT_FALSE(handler->open(
186 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700187 activeImageBlobId));
Patrick Venture72388d72018-11-07 15:06:36 -0800188}
189
Patrick Venture6f09ff52019-05-17 18:09:37 -0700190TEST_F(FirmwareHandlerOpenTestIpmiOnly,
191 OpenWithEverythingValidImageHandlerFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800192{
193 /* The image handler for a specific type of image is allowed to return
194 * failure on open. let's simulate that. */
Patrick Venture72388d72018-11-07 15:06:36 -0800195 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
196
197 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800198 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800199
200 /* Verify blob_id list doesn't grow. */
201 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800202 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800203}
204
Patrick Venture6f09ff52019-05-17 18:09:37 -0700205TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails)
Patrick Venture72388d72018-11-07 15:06:36 -0800206{
207 /* The client must set the file write bit. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800208 EXPECT_FALSE(
209 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800210}
211
Patrick Venture6f09ff52019-05-17 18:09:37 -0700212TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit)
Patrick Venture72388d72018-11-07 15:06:36 -0800213{
214 /* The client sends a request with a transport mechanism not supported. */
Patrick Venture05abf7e2018-11-09 11:02:56 -0800215 EXPECT_FALSE(
216 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800217}
218
Patrick Venture6f09ff52019-05-17 18:09:37 -0700219TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId)
Patrick Venture72388d72018-11-07 15:06:36 -0800220{
221 /* The client sends a request with an invalid image blob_id. */
Patrick Venture72388d72018-11-07 15:06:36 -0800222 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800223 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800224}
225
226/* TODO: The client sends a request during verification. */
227/* TODO: The client sends a second request to open when there is already an open
228 * file.
229 */
230/* TODO: The client sends a request to open active image. */
231/* TODO: The client sends a request to open active hash. */
232
233} // namespace blobs