blob: 20c63a954563532af30afd5a3b2914643e06c2f9 [file] [log] [blame]
Patrick Venturefa06a5f2019-07-01 09:22:38 -07001/* The goal of these tests is to verify that once a host-client has started the
2 * process with one blob bundle, they cannot pivot to upload data to another.
3 *
4 * This prevent someone from starting to upload a BMC firmware, and then midway
5 * through start uploading a BIOS image.
6 */
7#include "firmware_handler.hpp"
8#include "flags.hpp"
9#include "image_mock.hpp"
10#include "status.hpp"
11#include "triggerable_mock.hpp"
12#include "util.hpp"
13
14#include <memory>
15#include <string>
16#include <unordered_map>
17#include <vector>
18
19#include <gtest/gtest.h>
20
21namespace ipmi_flash
22{
23namespace
24{
25
26using ::testing::Return;
27
28class IpmiOnlyTwoFirmwaresTest : public ::testing::Test
29{
30 protected:
31 void SetUp() override
32 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070033 std::unique_ptr<ImageHandlerInterface> image =
34 std::make_unique<ImageHandlerMock>();
35 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070036 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -070037
38 image = std::make_unique<ImageHandlerMock>();
39 staticImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070040 blobs.emplace_back(staticLayoutBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -070041
42 image = std::make_unique<ImageHandlerMock>();
43 biosImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070044 blobs.emplace_back(biosBlobId, std::move(image));
Patrick Venturefa06a5f2019-07-01 09:22:38 -070045
46 std::unique_ptr<TriggerableActionInterface> bmcPrepareMock =
47 std::make_unique<TriggerMock>();
48 bmcPrepareMockPtr =
49 reinterpret_cast<TriggerMock*>(bmcPrepareMock.get());
50
51 std::unique_ptr<TriggerableActionInterface> bmcVerifyMock =
52 std::make_unique<TriggerMock>();
53 bmcVerifyMockPtr = reinterpret_cast<TriggerMock*>(bmcVerifyMock.get());
54
55 std::unique_ptr<TriggerableActionInterface> bmcUpdateMock =
56 std::make_unique<TriggerMock>();
57 bmcUpdateMockPtr = reinterpret_cast<TriggerMock*>(bmcUpdateMock.get());
58
59 std::unique_ptr<TriggerableActionInterface> biosPrepareMock =
60 std::make_unique<TriggerMock>();
61 biosPrepareMockPtr =
62 reinterpret_cast<TriggerMock*>(biosPrepareMock.get());
63
64 std::unique_ptr<TriggerableActionInterface> biosVerifyMock =
65 std::make_unique<TriggerMock>();
66 biosVerifyMockPtr =
67 reinterpret_cast<TriggerMock*>(biosVerifyMock.get());
68
69 std::unique_ptr<TriggerableActionInterface> biosUpdateMock =
70 std::make_unique<TriggerMock>();
71 biosUpdateMockPtr =
72 reinterpret_cast<TriggerMock*>(biosUpdateMock.get());
73
74 ActionMap packs;
75
76 std::unique_ptr<ActionPack> bmcPack = std::make_unique<ActionPack>();
77 bmcPack->preparation = std::move(bmcPrepareMock);
78 bmcPack->verification = std::move(bmcVerifyMock);
79 bmcPack->update = std::move(bmcUpdateMock);
80
81 std::unique_ptr<ActionPack> biosPack = std::make_unique<ActionPack>();
82 biosPack->preparation = std::move(biosPrepareMock);
83 biosPack->verification = std::move(biosVerifyMock);
84 biosPack->update = std::move(biosUpdateMock);
85
86 packs[staticLayoutBlobId] = std::move(bmcPack);
Patrick Venture7c2a00e2019-07-01 17:33:03 -070087 packs[biosBlobId] = std::move(biosPack);
Patrick Venturefa06a5f2019-07-01 09:22:38 -070088
Patrick Venture4934daa2020-09-22 16:37:44 -070089 std::vector<DataHandlerPack> data;
90 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
91
Patrick Venturefa06a5f2019-07-01 09:22:38 -070092 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070093 std::move(blobs), std::move(data), std::move(packs));
Patrick Venturefa06a5f2019-07-01 09:22:38 -070094 }
95
96 void expectedState(FirmwareBlobHandler::UpdateState state)
97 {
98 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
99 EXPECT_EQ(state, realHandler->getCurrentState());
100 }
101
Patrick Ventured4e20de2019-07-18 12:48:05 -0700102 ImageHandlerMock *hashImageMock, *staticImageMock, *biosImageMock;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700103
104 std::vector<HandlerPack> blobs;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700105
106 std::unique_ptr<blobs::GenericBlobInterface> handler;
107
108 TriggerMock *bmcPrepareMockPtr, *bmcVerifyMockPtr, *bmcUpdateMockPtr;
109 TriggerMock *biosPrepareMockPtr, *biosVerifyMockPtr, *biosUpdateMockPtr;
110
111 std::uint16_t session = 1;
112 std::uint16_t flags =
113 blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700114};
115
116TEST_F(IpmiOnlyTwoFirmwaresTest, OpeningBiosAfterBlobFails)
117{
118 /* You can only have one file open at a time, and the first firmware file
119 * you open locks it down
120 */
Patrick Ventured4e20de2019-07-18 12:48:05 -0700121 EXPECT_CALL(*staticImageMock, open(staticLayoutBlobId))
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700122 .WillOnce(Return(true));
123 EXPECT_CALL(*bmcPrepareMockPtr, trigger()).WillOnce(Return(true));
124
125 EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
126 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
127
Patrick Ventured4e20de2019-07-18 12:48:05 -0700128 EXPECT_CALL(*staticImageMock, close()).WillOnce(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700129 handler->close(session);
130
131 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
132
Patrick Ventured4e20de2019-07-18 12:48:05 -0700133 EXPECT_CALL(*biosImageMock, open(biosBlobId)).Times(0);
Patrick Venture7c2a00e2019-07-01 17:33:03 -0700134 EXPECT_FALSE(handler->open(session, flags, biosBlobId));
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700135}
136
137TEST_F(IpmiOnlyTwoFirmwaresTest, OpeningHashBeforeBiosSucceeds)
138{
139 /* Opening the hash blob does nothing special in this regard. */
Patrick Ventured4e20de2019-07-18 12:48:05 -0700140 EXPECT_CALL(*hashImageMock, open(hashBlobId)).WillOnce(Return(true));
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700141 EXPECT_TRUE(handler->open(session, flags, hashBlobId));
142
143 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
144
Patrick Ventured4e20de2019-07-18 12:48:05 -0700145 EXPECT_CALL(*hashImageMock, close()).WillOnce(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700146 handler->close(session);
147
148 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
149 ASSERT_FALSE(handler->canHandleBlob(verifyBlobId));
150
Patrick Ventured4e20de2019-07-18 12:48:05 -0700151 EXPECT_CALL(*biosImageMock, open(biosBlobId)).WillOnce(Return(true));
Patrick Venture7c2a00e2019-07-01 17:33:03 -0700152 EXPECT_TRUE(handler->open(session, flags, biosBlobId));
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700153
154 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
155
Patrick Ventured4e20de2019-07-18 12:48:05 -0700156 EXPECT_CALL(*biosImageMock, close()).WillOnce(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700157 handler->close(session);
158}
159
160} // namespace
161} // namespace ipmi_flash